Measuring Distance from Several Sources
An unweighted graph can assign each vertex the distance to its nearest source by starting all source vertices in the same zero-distance layer.
- Source
- Anbloq
- Language
- English
- Created
- Updated
- Chapters
- 4
- Reading time
- 1 min
In this article4 chapters
Trace a line
In A–B–C–D–E, mark A and E with distance zero. Their next neighbors B and D get one. C then gets two from either side. A breadth-first search preserves the layer ordering for both starting points.
Record nearest-source ties
A single source label at C can use whichever discovered it first. Reporting both equally close sources requires additional state and a declared tie rule. Function postconditions distinguish the two outputs.[1]
Initialize all source distances at zero
On a line of vertices A through F, let B and F be sources. A and C sit one step from B, E one step from F, and D two from either. Adding all sources to the initial queue makes both waves advance together. See breadth-first search.
Distance 0: B,F
Distance 1: A,C,E
Distance 2: D
Ties: D can retain either source under a documented rule
Preserve source identity only if the result needs it
Nearest-source distance is well defined even where no single nearest source is unique. If a caller needs deterministic attribution, choose a tie rule and check it when waves meet. With weighted edges, use an algorithm compatible with those weights.