Graphs
110 / 170GraphsLesson 3 of 8About 71 min
Breadth-first search measures distance. Seeding the queue with many starts measures distance to the nearest one.
To find each cell's distance to the nearest source, the obvious approach runs a separate search from every cell. That is one full traversal per cell.
Turn it around, as in Unit 2's container problem and Unit 11's grid search: instead of asking each cell how far the nearest source is, let all the sources spread outward together.
Run the multi-source search on a 1 by 5 row S . . . S with no blocked cells. Give the distance assigned to each position, left to right.
| position | distance |
|---|---|
| 0 | |
| 1 | |
| 2 | |
| 3 | |
| 4 |
Read the constraints first and let them tell you what complexity is expected. Derive the approach, implement it, run the tests, and submit when it passes.
Read the constraints first and let them tell you what complexity is expected. Derive the approach, implement it, run the tests, and submit when it passes.