Skip to content
Part IA Lent Term

BFS: Correctness and Properties

What We Need to Prove

When BFS terminates, for every vertex vv reachable from source ss, the value v.dv.d equals the true shortest-path distance δ(s,v)\delta(s, v) (the minimum number of edges on any path from ss to vv). For unreachable vertices, v.d=δ(s,v)=v.d = \delta(s, v) = \infty. Additionally, following predecessor π\pi pointers backwards from any vertex reconstructs a shortest path to ss.

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 (u,v)E(u, v) \in E, then δ(s,v)δ(s,u)+1\delta(s, v) \le \delta(s, u) + 1.

Proof: If uu is unreachable from ss, then δ(s,u)=\delta(s, u) = \infty and the inequality trivially holds. If uu is reachable, the shortest path to vv is at most the shortest path to uu plus one more edge (u,v)(u, v). It cannot be longer than that (though it could be shorter if there is a path to vv that bypasses uu). Hence the inequality.

Lemma 2: v.dv.d Never Underestimates

On termination, for all vVv \in V, we have v.dδ(s,v)v.d \ge \delta(s, v).

Proof by induction on the number of enqueue operations:

Base case: Immediately before the while loop begins: s.d=0=δ(s,s)s.d = 0 = \delta(s, s), so the hypothesis holds for ss. For all other vertices, v.d=δ(s,v)v.d = \infty \ge \delta(s, v), even if unreachable. Hypothesis holds.

Inductive step: The while/for loops only change v.dv.d when vv is white (not pending), via the assignment v.d=u.d+1v.d = u.d + 1. By the induction hypothesis, u.dδ(s,u)u.d \ge \delta(s, u). Then: v.d=u.d+1δ(s,u)+1δ(s,v)v.d = u.d + 1 \ge \delta(s, u) + 1 \ge \delta(s, v) where the last inequality is Lemma 1. Since v.dv.d 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 ϕ\phi: If the queue is Q=v1,v2,,vxQ = v_1, v_2, \ldots, v_x (head to tail), then vx.dv1.d+1v_x.d \le v_1.d + 1 and the v.dv.d values are non-decreasing from head to tail: vi.dvi+1.dv_i.d \le v_{i+1}.d for all i=1,,x1i = 1, \ldots, x-1.

Proof by induction on queue operations.

  • Dequeue: Removing v1v_1 leaves v2v_2 as the new head. From ϕ\phi we have vx.dv1.d+1v2.d+1v_x.d \le v_1.d + 1 \le v_2.d + 1 (since v1.dv2.dv_1.d \le v_2.d). So ϕ\phi holds after dequeue.
  • Enqueue: When vv (with v.d=u.d+1v.d = u.d + 1) is appended, we need to verify v1.dvx.dv.dv_1.d \le \cdots \le v_x.d \le v.d and v.dv1.d+1v.d \le v_1.d + 1. The first holds because vx.du.d+1v_x.d \le u.d + 1 (from ϕ\phi before dequeue, vx.dv1.d(=u.d)+1=u.d+1=v.dv_x.d \le v_1.d (= u.d) + 1 = u.d+1 = v.d). The second holds because v.d=u.d+1=v1.d+1v.d = u.d + 1 = v_1.d + 1.

Corollary: If vertex aa is enqueued before vertex bb, then a.db.da.d \le b.d on termination. This follows from the non-decreasing property when aa and bb are simultaneously in the queue and from transitivity of \le when they are not.

Termination: Proof by Contradiction

Assume the algorithm errs. Let vv be the vertex with minimum δ(s,v)\delta(s, v) whose v.dδ(s,v)v.d \neq \delta(s, v). By Lemma 2, we must have v.d>δ(s,v)v.d > \delta(s, v). Also vsv \neq s (since s.d=0s.d = 0 is correct), and vv must be reachable from ss (otherwise δ(s,v)=v.d\delta(s,v) = \infty \ge v.d).

Let uu be the immediate predecessor of vv on a true shortest path svs \leadsto v. Then δ(s,u)=δ(s,v)1\delta(s, u) = \delta(s, v) - 1. Since δ(s,u)<δ(s,v)\delta(s, u) < \delta(s, v) and vv is the minimum-δ\delta vertex with a wrong dd, we have u.d=δ(s,u)u.d = \delta(s, u). Hence: v.d>δ(s,v)=δ(s,u)+1=u.d+1v.d > \delta(s, v) = \delta(s, u) + 1 = u.d + 1

Now consider the moment uu was dequeued from QQ. Vertex vv was in exactly one of three states:

  1. Not yet enqueued (white): The inner loop would explore the edge (u,v)(u, v) and set v.d=u.d+1v.d = u.d + 1. But we have v.d>u.d+1v.d > u.d + 1 — contradiction.
  2. In the queue (grey, pending): vv was enqueued earlier by some ww with v.d=w.d+1v.d = w.d + 1. By the corollary, w.du.dw.d \le u.d (since ww was enqueued before uu was dequeued, and uu was dequeued before vv, so wuvw \le u \le v in dequeue order). Thus v.d=w.d+1u.d+1v.d = w.d + 1 \le u.d + 1, contradicting v.d>u.d+1v.d > u.d + 1.
  3. Already dequeued (black): By the corollary, v.du.dv.d \le u.d, contradicting v.d>u.d+1v.d > u.d + 1.

All three cases produce contradictions. Therefore no such vv exists; BFS computes correct distances for all vertices.

Shortest-Path Tree

The predecessor subgraph Gπ=(Vπ,Eπ)G_\pi = (V_\pi, E_\pi) where Vπ={vv.πNIL}{s}V_\pi = \{v \mid v.\pi \neq \text{NIL}\} \cup \{s\} and Eπ={(π[v],v)vV{s}}E_\pi = \{(\pi[v], v) \mid v \in V \setminus \{s\}\} forms a breadth-first tree. It is a tree because each vertex (except ss) has exactly one predecessor and no cycles exist. The unique path in GπG_\pi from ss to any reachable vv is a shortest path in GG.

Applications

ApplicationHow BFS solves it
Unweighted shortest pathsv.d=δ(s,v)v.d = \delta(s,v) in edge count
Bipartite testingColour layers alternately; check for conflicts
Connected componentsBFS from any unvisited vertex; repeat
Web crawlingExplore by link distance from seed
Social network analysisDegrees of separation via BFS depth

Summary

PropertyStatement
Lemma 1δ(s,v)δ(s,u)+1\delta(s,v) \le \delta(s,u) + 1 if (u,v)E(u,v) \in E
Lemma 2v.dδ(s,v)v.d \ge \delta(s,v) at all times
Lemma 3Queue has at most 2 consecutive dd values
Correctnessv.d=δ(s,v)v.d = \delta(s,v) for all vv on termination
Predecessor tree(π[v],v)(\pi[v], v) edges form a BFS tree rooted at ss

Past paper questions: y2025p1q9(a)(i) (BFS time analysis with different representations).