Lesson 4: Expanding Ripples — Breadth-First Search (BFS) and Fewest-Hop Routing
Learning objective
Students will execute Breadth-First Search (BFS) step by step on an unweighted graph using a FIFO queue, a visited set, a dist (hop count) table, and parent pointers to reconstruct the shortest unweighted path between two vertices.
Materials
- Printed 6-vertex network diagram (below) and index cards to serve as the FIFO queue
Vocabulary
- Breadth-First Search (BFS): A graph traversal algorithm that explores all neighbors at distance
1 hop from the start vertex before exploring vertices at distance 2 hops, 3 hops, and so on.
- Visited Set: A record of vertices already discovered (enqueued) so the algorithm never gets trapped in an infinite loop around a cycle.
- Parent (Predecessor) Pointer: For each discovered vertex
v, the vertex u that first discovered v (parent[v] = u), allowing us to trace the shortest path backward from the goal to the start.
Lesson flow
1. Opening: Dropping a Stone in a Pond (7 min)
- Consider this 6-router network (
A through F) where every link takes 1 hop:
A --- B --- E
| | |
C --- D --- F
Edges (alphabetical neighbor order):
A: [B, C]
B: [A, D, E]
C: [A, D]
D: [B, C, F]
E: [B, F]
F: [D, E]
- Ask: "If router
A wants to send a packet to router F in the fewest hops, why can't a computer just wander down the first road it sees (A → C → D → B → E → F, 5 hops)?"
- Explain the ripple idea: If we explore in expanding rings—Ring 0 (
A), Ring 1 (all 1-hop neighbors of A), Ring 2 (all 2-hop neighbors), Ring 3 (all 3-hop neighbors)—then the very first time a ring touches F, we are guaranteed to have found a path with the minimum possible number of hops!
- Ask: "Which data structure from Lesson 1 processes items in exact First-In, First-Out order so Ring 1 finishes before Ring 2?" → A Queue (FIFO)!
2. Direct instruction: Complete Step-by-Step BFS Trace from A to F (16 min)
Show the exact BFS algorithm and trace table (breaking neighbor ties alphabetically):
from collections import deque
def bfs_shortest_path(graph, start, goal):
queue = deque([start])
visited = {start}
dist = {start: 0}
parent = {start: None}
while queue:
u = queue.popleft() # DEQUEUE front vertex
if u == goal:
break
for v in graph[u]: # Inspect each neighbor of u
if v not in visited:
visited.add(v) # Mark visited IMMEDIATELY upon enqueue!
dist[v] = dist[u] + 1
parent[v] = u
queue.append(v) # ENQUEUE v at back
return dist, parent
Trace every iteration on the board:
| Step |
Dequeued u |
Unvisited Neighbors Enqueued |
Queue (front → back) |
visited Set |
Newly Recorded dist[v] & parent[v] |
| Init |
— |
Start at A |
[A] |
{A} |
dist[A]=0, parent[A]=None |
| 1 |
A |
B, C |
[B, C] |
{A, B, C} |
dist[B]=1 (p=A), dist[C]=1 (p=A) |
| 2 |
B |
D, E (A already in visited) |
[C, D, E] |
{A, B, C, D, E} |
dist[D]=2 (p=B), dist[E]=2 (p=B) |
| 3 |
C |
(none: A & D already in visited!) |
[D, E] |
{A, B, C, D, E} |
— |
| 4 |
D |
F (B & C already in visited) |
[E, F] |
{A, B, C, D, E, F} |
dist[F]=3 (p=D) |
| 5 |
E |
(none: B & F already in visited) |
[F] |
{A, B, C, D, E, F} |
— |
| 6 |
F |
Goal reached! |
[] |
{A, B, C, D, E, F} |
Stop |
Reconstructing the path via parent pointers:
Start at F: parent[F] = D → parent[D] = B → parent[B] = A → parent[A] = None.
Reverse it: A → B → D → F (3 hops!). (Note: A → B → E → F and A → C → D → F are also tied at 3 hops; alphabetical tie-breaking discovered D via B first, and F via D first.)
Critical bug to highlight: Look at Step 2 and Step 3. Why did we mark D as visited the moment B enqueued D in Step 2, instead of waiting until D was dequeued in Step 4?
Because in Step 3 (u = C), C also has an edge to D! If D were not already in visited, C would enqueue a duplicate copy of D into the queue. Always mark vertices visited at enqueue time.
3. Main activity: Pair BFS Trace on a 7-Node Campus Graph (17 min)
Student pairs trace BFS starting from S to find the hop distance and parent tree to all vertices on this 7-vertex graph (alphabetical tie-breaking):
- S: [A, B]
- A: [S, C, D]
- B: [S, D]
- C: [A, G]
- D: [A, B, E, G]
- E: [D]
- G: [C, D]
Pairs fill in the complete dist and parent table and verify:
- Layer 0: S (dist=0)
- Layer 1: A (dist=1, p=S), B (dist=1, p=S)
- Layer 2: C (dist=2, p=A), D (dist=2, p=A)
- Layer 3: G (dist=3, p=C), E (dist=3, p=D)
- Shortest path from S to G: S → A → C → G (3 hops).
4. Discussion questions (5 min)
- Question 1: "What happens to our algorithm if we replace the FIFO Queue (
popleft()) with a LIFO Stack (pop())?"
- Example good answer: It explores in a depth-first style instead of expanding in layer rings (
1 hop, 2 hops, 3 hops), plunging deep down one branch before backtracking. Because it does not finish Layer 1 and Layer 2 before exploring deeper nodes, the first path it records is not guaranteed to be the shortest path (for example, on our 7-node campus graph, popping from the end visits S, B, D, G, C and records a 4-hop path S → B → D → G → C to C instead of the 2-hop shortest path S → A → C; on the 6-router network from A, it records a 4-hop path A → C → D → F → E to E instead of 2 hops A → B → E).
- Question 2: "How many times does BFS enqueue each vertex and inspect each edge in a connected graph
G = (V, E)?"
- Example good answer: Each vertex enters the queue at most once (
|V| dequeues), and each edge is inspected at most twice (once from each endpoint), giving a linear time cost of O(|V| + |E|).
5. Closing check (5 min)
- In the 7-vertex campus graph (
S → A, B above), both A and B have an edge to D. Why did parent[D] get set to A instead of B?
- What would happen if you ran BFS on a graph containing a triangle cycle
A — B — C — A without keeping a visited set?
- After running BFS from
Start = M, your parent table records: parent[M] = None, parent[P] = M, parent[Q] = P, parent[R] = P, parent[T] = R. What is the shortest path from M to T, and how many hops long is it?
Worked answers for closing check
- Because
A was enqueued before B (alphabetical order) and therefore dequeued first. When A's neighbors were scanned, D was unvisited, so D was marked visited with parent[D] = A. When B was dequeued next, D was already in visited.
- Without a
visited set, vertices are enqueued repeatedly around the cycle (A enqueues B, C; B enqueues A, C; C enqueues A, B, and so on). If the code stops at a reachable goal (such as goal = C from A), it still halts once C is dequeued (after 3 dequeues), but leaves duplicate entries in the queue and wastes exponential work on larger graphs; if we are exploring the whole graph (or the goal is unreachable), the queue grows forever and the algorithm never terminates.
- Tracing backward from
T: T ← R ← P ← M. Reversing gives M → P → R → T, which is 3 hops long.
Differentiation
- Extra support: Give pairs physical tokens or sticky notes labeled
"VISITED" to place directly on each vertex the moment its card enters the queue, reinforcing that marking happens at enqueue time rather than dequeue time.
- Extra challenge: Have students trace true recursive or late-marked Depth-First Search (DFS) (where a vertex is marked visited when popped rather than when pushed) alongside our enqueue-marked stack variant on the 7-vertex campus graph and compare the two visitation orders.
Teacher notes
- Common misconception: Students often forget to reverse the
parent pointer chain at the end (F → D → B → A vs. A → B → D → F). Remind them that parent[v] points backward toward the start vertex, just like breadcrumbs leading back home.