Breadth-First Search and Distance Layers
Prove the queue invariant behind unweighted shortest paths and reconstruct a path without storing every candidate.
- Source
- Anbloq
- Language
- English
- Created
- Updated
- Chapters
- 2
- Reading time
- 1 min
In this article2 chapters
Discover vertices in order of path length
Assign a source distance zero and enqueue it. When removing a vertex of distance \(d\), each undiscovered neighbor gets distance \(d+1\). A FIFO queue places all pending vertices of distance \(d\) before the new entries. If a discovered vertex had a shorter path, its predecessor on that shorter path would have been processed earlier and would already have discovered it. Mark vertices when enqueuing to avoid several copies of the same work. On an adjacency list the total work is \(O(|V|+|E|)\).
One predecessor is enough for a witness
In a diamond with two equal routes from \(s\) to \(t\), either route can first discover \(t\). Store that predecessor and follow the predecessors back while distance decreases. Multiple sources can all start at distance zero to find distance to the nearest one. Dijkstra’s algorithm adapts the pending order to nonnegative edge costs. Distributed graph processing uses distance layers to organize communication rounds.[1][2]