Why does this BFS solution work for this question about euclidean distance and what’s its complexity?

Given a matrix of 1s and 0s where 0 represents houses and 1 represents stores, find the square of minimum Euclidean distance of every house to nearest store. Return it as a vector of vector.

[[1, 1, 0],  [0, 0, 0],    [0, 0, 0]] 

should return

[[0, 0, 1],  [1, 1, 2],     [4, 4, 5]]  

Basic BFS: ( approach-1)

If there are N elements in the matrix, one could do BFS starting every house to solve this. The worst case complexity of this approach is: Worst number of BFSes * O(E+V), where E is the number of edges in the matrix and V number of nodes = N * O(N + 4N) as number of edges is a constant factor of 4N. Time complexity: O(N*N)

multi-source BFS (approach-2)

  • Initialize the queue with all nodes (i, j) where i is the row number and j is the column for all stores. For each node (i, j) in the queue also store its corresponding (si, sj) node for the store that gives it the minimum square of euclidean distance (call it sqDist). For each store node (i, j), (si, sj) would be same as (i, j)

  • Initialize the distance answer that we are looking for each house with -1

  • During multi-source BFS, for a node (i, j) and it’s best store (si, sj) in the queue, look at each of 8 adjacent nodes (since it’s euclidean distance, we need to consider all 8 nodes not only 4 of left, right, top, down). If the current distance value for any of those nodes (nexti, nextj) is > the sqDist if (si, sj) is chosen as the best store for (nexti, nextj), then

    • update distance of (nexti, nextj) with the lower value
    • add (nexti,nextj) along with its best store as (si, sj) to the queue
  • Keep doing this until queue is empty

My question:

1) why does this work? Since we’re looking at euclidean distance it’s possible that (nexti, nextj) may not be added to the queue (or have their distance updated) for a certain (si, sj) but that (si, sj) node may still update the distance for nodes that are farther to it than (nexti, nextj) due to nature of euclidean distance. So (si, sj) may still need to be in the queue. How does this solution work inspite of this issue or what’s the intuitive conceptual idea of why this works despite this issue as it’s not obvious?

2) What’s the time complexity?

it seems in the worst case you could add a node in the queue N times. So it becomes N * O(N *4N) where 4N is the number of edges in the matrix. So is it not any better than doing BFS individually for each house?

Can someone in Cyber Security or IT help answer this basic question on the change of today’s malware? [closed]

1.) Before the most common types of malware were usually trojan horses and various other types of viruses derived from one’s own e-mail on a desktop. Given the timespan since those days, the game has changed. Today ways of breaching a user’s data have changed drastically. What are the most prevalent methods that an average person should be aware of today?

A question regarding the stacking of temporary HP

Right now I am playing a Mystic (UA), and I have chosen the Order of the Immortal as my discipline. One of the abilities I have acquired gives me 5 temporary hit points at the start of my turn, as my intelligence is a 20.

If I have gained temporary HP in one turn and I haven’t been hit, do I continue gaining the HP or do I have to expend the Temporary HP before gaining more?

Question about unity paint game

I want to make a paint game using unity 2D, such like “mix & paint” mobile game. Basically player needs to brush colours into shape, then the game will complete when it is fully-coloured.

I’ve tried to achieve it using particle system for the brush & sprite mask for the shape that works quite well. Then for checking it is fully-coloured i am using sampling method, it is also works but i does not feel comfortable with it.

So my question is: what is the best way to achieve this? or how will you guys approach this problem? Also, any idea or material is appreciated as answer! thanks

[ Politics ] Open Question : Is the death rate from Coronavirus over-estimated?

https://www.wsj.com/articles/is-the-coronavirus-as-deadly-as-they-say-11585088464 We actually KNOW that these death rates of 1-3% are over-estimated due to the fact that we know that not everyone or even close to everyone with the disease has been tested. The REAL question is how much.  This virus MAY be less deadly than the flu or 10 times more deadly.