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?

Will learning about integrated circuits, help me be a better computer architect(long-term)?

I do not know if this is the right place to ask this type of question, but here I go, im thinking about learning integrated circuits as part of learning more about computer hardware in general (but more focused in its architecture/hardware), will I be losing my time learning about integrated circuits?(Im planning to read the following book: “Analysis and Design of Analog Integrated Circuits 5th edition”). thanks for the answers.

What is the action-economy cost of drawing a weapon? What about two?

What does it take to draw a weapon if you start combat with it away? Is it an Interact? If so, you trigger Reactions including Attack of Opportunity. Part of a Stride (as it was in P1e)? Free? What if you have two weapons; can you draw both or are they separate actions?

While Interact seems like the most likely, I can’t seem to find the by-the-book answer to this.

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

Why attacker do not care about masking the IP of the infected device of botnet?

I get a sentence:

If an attack is created using a botnet the likelihood of tracking the attack back to its source is low. For an added level of obfuscation, an attacker may have each distributed device also spoof the IP addresses from which it sends packets. If the attacker is using a botnet such as the Mirai botnet, they generally won’t care about masking the IP of the infected device.

Why attacker do not care about masking the IP of the infected device of botnet?

Does anyone know how to make the extra data, about the number of jobs on page, show in google search results?

I have noticed that when I google for jobs for example ‘plumber jobs in Melbourne’ some results have a prepended piece of data ‘407 jobs’ before the normal meta description is shown.

example of search result

Anyone know what seek has done to get this data shown in google search results?

Do I have to be careful about DNS I’m using? [duplicate]

There’s some website which I can’t have access to and they return 403 error to me. On the other hand, there are some DNS servers, where if I use them, I can have access to those websites!

Does using these unknown DNS servers cause any security risks for me? Is there any way for the owner of DNS to use my computer or my IP address… or any malicious act or cyber attack?

Why did this happen about change of desktop background? [closed]

Today when I turned on my macbook I could see that the Desktop Background has been changed to Yosemite.

Then right after clicking on Change Desktop background… the background changed to the one I had, which was a green background with a dog in the center, except for the dog picture, it was only the green background.

After this I clicked again on Change Desktop background… and it changed again to the Yosemite picture.

Finally I went to finder file to see where the picture of the dog was, and when I clicked to see it, it was opened but at the same time 2 more pictures were opened.

After all this, I clicked again on Change Desktop background… but nothing happened. The Yosemite background didn’t change this time.

All the time when this happened, my mac didn’t have connection to internet.

Why? Is my mac hacked? Does it have a virus? What do I do?


  • Every once in a while I see a message on the upper LHS telling me that I should update something related to the system but I always click on later and then the message vanishes.

[ Politics ] Open Question : Why is Trump suddenly acting like he cares about the coronavirus crises?

He said “no” to early testing and free test kits from the WHO. He was briefed weeks ago about this coming threat and sat on his tiny hands and went golfing. He’s got blood on his hands, and the scared markets are in free fall because of his failures. F Donald J. Trump!