A Breadth-First Search by Hand

Breadth-first search expands the earliest waiting vertex and discovers reachable vertices in layers by unweighted distance.

Source
Anbloq
Language
English
Created
Updated
Chapters
4
Reading time
1 min
In this article4 chapters
  1. Trace one queue
  2. Keep one recorded predecessor
  3. Mark vertices as soon as they enter the queue
  4. Explain what discovery order guarantees

Trace one queue

From A, discover B and C. If B discovers D, the queue after B holds C followed by D. Their distances from A are one and two. FIFO queues provide the ordering that makes each layer appear before the next.

Keep one recorded predecessor

Store B as predecessor of D on its first discovery. If C also links to D, the route through B is already a valid shortest witness. The breadth-first search invariant proves why no shorter route arrives later.[1]

Mark vertices as soon as they enter the queue

In undirected edges A–B, A–C, B–D and C–D, start at A and process alphabetical neighbors. B enqueues D at distance two. C sees D already discovered and need not enqueue it again. See a first-in, first-out queue.

Queue after A: [B,C], both at distance 1
Process B: [C,D], D at distance 2 with parent B
Process C: [D], D already known
Process D: []

Explain what discovery order guarantees

A FIFO queue exhausts depth \(k\) before processing depth \(k+1\). Each wave extends paths by one edge, which proves the first distance discovered is minimum in the unweighted graph. Changing neighbor order can change parents while preserving distances.

References

  1. Eric Lehman, F. Thomson Leighton and Albert R. Meyer, Mathematics for Computer Science ↩