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

Vocabulary

Lesson flow

1. Opening: Dropping a Stone in a Pond (7 min)

  1. Consider this 6-router network (A through F) where every link takes 1 hop: A --- B --- E | | | C --- D --- F Edges (alphabetical neighbor order):
  2. A: [B, C]
  3. B: [A, D, E]
  4. C: [A, D]
  5. D: [B, C, F]
  6. E: [B, F]
  7. F: [D, E]
  8. 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)?"
  9. 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!
  10. 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)

5. Closing check (5 min)

  1. 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?
  2. What would happen if you ran BFS on a graph containing a triangle cycle A — B — C — A without keeping a visited set?
  3. 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

  1. 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.
  2. 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.
  3. Tracing backward from T: T ← R ← P ← M. Reversing gives M → P → R → T, which is 3 hops long.

Differentiation

Teacher notes