BFS: Correctness and Properties
What We Need to Prove
When BFS terminates, for every vertex reachable from source , the value equals the true shortest-path distance (the minimum number of edges on any path from to ). For unreachable vertices, . Additionally, following predecessor pointers backwards from any vertex reconstructs a shortest path to .
The proof proceeds through three lemmas, culminating in a contradiction argument that no vertex can be the “first” to receive a wrong distance.
Lemma 1: Upper Bound on Neighbour Distance
If , then .
Proof: If is unreachable from , then and the inequality trivially holds. If is reachable, the shortest path to is at most the shortest path to plus one more edge . It cannot be longer than that (though it could be shorter if there is a path to that bypasses ). Hence the inequality.
Lemma 2: Never Underestimates
On termination, for all , we have .
Proof by induction on the number of enqueue operations:
Base case: Immediately before the while loop begins: , so the hypothesis holds for . For all other vertices, , even if unreachable. Hypothesis holds.
Inductive step: The while/for loops only change when is white (not pending), via the assignment . By the induction hypothesis, . Then: where the last inequality is Lemma 1. Since is never changed again (it becomes grey immediately), the property persists.
This lemma says the algorithm never reports a distance too low; it may overestimate until convergence.
Lemma 3: Queue Disciplines the Order
Queue property : If the queue is (head to tail), then and the values are non-decreasing from head to tail: for all .
Proof by induction on queue operations.
- Dequeue: Removing leaves as the new head. From we have (since ). So holds after dequeue.
- Enqueue: When (with ) is appended, we need to verify and . The first holds because (from before dequeue, ). The second holds because .
Corollary: If vertex is enqueued before vertex , then on termination. This follows from the non-decreasing property when and are simultaneously in the queue and from transitivity of when they are not.
Termination: Proof by Contradiction
Assume the algorithm errs. Let be the vertex with minimum whose . By Lemma 2, we must have . Also (since is correct), and must be reachable from (otherwise ).
Let be the immediate predecessor of on a true shortest path . Then . Since and is the minimum- vertex with a wrong , we have . Hence:
Now consider the moment was dequeued from . Vertex was in exactly one of three states:
- Not yet enqueued (white): The inner loop would explore the edge and set . But we have — contradiction.
- In the queue (grey, pending): was enqueued earlier by some with . By the corollary, (since was enqueued before was dequeued, and was dequeued before , so in dequeue order). Thus , contradicting .
- Already dequeued (black): By the corollary, , contradicting .
All three cases produce contradictions. Therefore no such exists; BFS computes correct distances for all vertices.
Shortest-Path Tree
The predecessor subgraph where and forms a breadth-first tree. It is a tree because each vertex (except ) has exactly one predecessor and no cycles exist. The unique path in from to any reachable is a shortest path in .
Applications
| Application | How BFS solves it |
|---|---|
| Unweighted shortest paths | in edge count |
| Bipartite testing | Colour layers alternately; check for conflicts |
| Connected components | BFS from any unvisited vertex; repeat |
| Web crawling | Explore by link distance from seed |
| Social network analysis | Degrees of separation via BFS depth |
Summary
| Property | Statement |
|---|---|
| Lemma 1 | if |
| Lemma 2 | at all times |
| Lemma 3 | Queue has at most 2 consecutive values |
| Correctness | for all on termination |
| Predecessor tree | edges form a BFS tree rooted at |
Past paper questions: y2025p1q9(a)(i) (BFS time analysis with different representations).