Lesson 5: Weighted Routes — Dijkstra's Shortest-Path Algorithm

Learning objective

Students will explain why fewest hops (BFS) can fail on a weighted graph, execute Dijkstra's shortest-path algorithm step by step using a tentative distance table (dist[], prev[], settled), and explain why negative edge weights violate Dijkstra's greedy settlement rule.

Materials

Vocabulary

Lesson flow

1. Opening: When Fewest Hops Is the Slowest Route (7 min)

  1. Draw a 3-node triangle on the board representing travel times in minutes between A, B, and C:
  2. Direct highway edge A — C has weight 10 minutes (congested construction zone, 1 hop).
  3. Local road A — B has weight 2 minutes, and B — C has weight 3 minutes (2 hops total).
  4. Ask:
  5. "Which route from A to C does BFS (Lesson 4) pick?" → A → C (1 hop, 10 minutes), because BFS ignores edge weights and only counts hops!
  6. "Which route is actually faster?" → A → B → C (2 + 3 = 5 minutes!).
  7. Introduce Edsger W. Dijkstra's 1959 insight: When all edge weights are non-negative (w >= 0), if we always pick the unsettled vertex u with the smallest tentative dist[u], no detour through any other unsettled vertex can ever make dist[u] smaller!

2. Direct instruction: Complete Step-by-Step Trace of Dijkstra's Algorithm (16 min)

Consider this 5-vertex weighted network (weights in milliseconds of network latency): - A — B (weight 4), A — C (weight 1) - C — B (weight 2), C — D (weight 5) - B — D (weight 1), B — E (weight 7) - D — E (weight 3)

        (4)       (7)
    A ------- B ------- E
    |       / |       /
 (1)|  (2)/   |(1)   /(3)
    |   /     |     /
    C ------- D ---/
        (5)

Dijkstra's Algorithm Rules: 1. Set dist[A] = 0 and dist[v] = ∞ for all other vertices. Set settled = {}. 2. While unsettled vertices remain: - Pick u: Choose the unsettled vertex with the minimum dist[u] and mark u as settled. - Relax edges out of u: For each unsettled neighbor v of u, compute new_cost = dist[u] + w(u, v). If new_cost < dist[v], update dist[v] = new_cost and prev[v] = u.

Trace every iteration on the board:

Round Settled u (dist[u]) Edge Relaxations Tested from u dist[A] dist[B] (prev) dist[C] (prev) dist[D] (prev) dist[E] (prev) Settled Set
Init — — 0 ∞ ∞ ∞ ∞ {}
1 A (0) A→B: 0+4=4 < ∞ ✓
A→C: 0+1=1 < ∞ ✓
0 4 (A) 1 (A) ∞ ∞ {A}
2 C (1) C→B: 1+2=3 < 4 ✓ (improved!)
C→D: 1+5=6 < ∞ ✓
0 3 (C) 1 (A) 6 (C) ∞ {A, C}
3 B (3) B→D: 3+1=4 < 6 ✓ (improved!)
B→E: 3+7=10 < ∞ ✓
0 3 (C) 1 (A) 4 (B) 10 (B) {A, C, B}
4 D (4) D→E: 4+3=7 < 10 ✓ (improved!) 0 3 (C) 1 (A) 4 (B) 7 (D) {A, C, B, D}
5 E (7) (all neighbors settled) 0 3 (C) 1 (A) 4 (B) 7 (D) {A, C, B, D, E}

Reconstruct the minimum-weight path from A to E: Follow prev backward from E: E ← D ← B ← C ← A. Reverse it: A → C → B → D → E with total cost 1 + 2 + 1 + 3 = 7 ms! Notice that A → B → E (2 hops) costs 4 + 7 = 11 ms, and A → C → D → E (3 hops) costs 1 + 5 + 3 = 9 ms, while the 4-hop path A → C → B → D → E costs only 7 ms!

3. Main activity: Pair Dijkstra Trace & The Negative-Weight Counterexample (17 min)

  1. Part A — Pair Dijkstra Trace (12 min): Pairs trace Dijkstra's algorithm from S to T on this 5-vertex graph:
  2. S — A (6), S — B (2)
  3. B — A (3), B — C (8)
  4. A — C (1), A — T (9)
  5. C — T (4) Students fill out the 5-round table and verify:
  6. Round 1 settles S (0) → dist[A]=6 (S), dist[B]=2 (S)
  7. Round 2 settles B (2) → B→A relaxes dist[A] from 6 down to 2+3 = 5 (B); B→C sets dist[C] = 2+8 = 10 (B)
  8. Round 3 settles A (5) → A→C relaxes dist[C] from 10 down to 5+1 = 6 (A); A→T sets dist[T] = 5+9 = 14 (A)
  9. Round 4 settles C (6) → C→T relaxes dist[T] from 14 down to 6+4 = 10 (C)
  10. Round 5 settles T (10) → Shortest path is S → B → A → C → T with cost 10.
  11. Part B — Why Negative Edges Break Dijkstra (5 min): Draw a 3-vertex directed graph:
  12. X → Y has weight 5
  13. X → Z has weight 2
  14. Y → Z has weight -6 (a negative edge!)
  15. Trace what Dijkstra does from X:
    • Round 1 settles X (0) → dist[Z] = 2, dist[Y] = 5.
    • Round 2 greedily settles Z (2) because 2 < 5!
    • But going X → Y → Z actually costs 5 + (-6) = -1 < 2! Because w(Y, Z) = -6 was negative, Dijkstra's assumption that "adding more edges can never reduce cost" failed.

4. Discussion questions (5 min)

5. End-of-unit synthesis & Closing check (5 min)

  1. In the Part A trace (S, A, B, C, T), why did Dijkstra settle vertex B (dist = 2) before vertex A (dist = 6) in Round 2?
  2. Give a concrete 3-vertex example (P, Q, R with positive edge weights) where the 1-hop path P → R is longer than the 2-hop path P → Q → R.
  3. Which data structure or algorithm from this 5-lesson unit (Stack, Queue, Hash Table, BFS, or Dijkstra) should an engineer choose for each task below?
  4. (a) Looking up a user's profile by their unique username "alex_42" in O(1) average time.
  5. (b) Finding the fastest driving route across a highway network where each road segment is labeled with its travel time in minutes.
  6. (c) Implementing the Undo button in a drawing program.
  7. (d) Finding the minimum number of friend-of-a-friend introductions connecting two people in an unweighted social graph.

Worked answers for closing check

  1. Because at the start of Round 2, the unsettled vertices had tentative distances dist[B] = 2, dist[A] = 6, dist[C] = ∞, and dist[T] = ∞. Dijkstra always selects the unsettled vertex with the smallest tentative distance (2 < 6).
  2. Any triangle where w(P, Q) + w(Q, R) < w(P, R), such as w(P, R) = 15, w(P, Q) = 4, and w(Q, R) = 5 (4 + 5 = 9 < 15).
  3. (a) Hash Table; (b) Dijkstra's Algorithm; (c) Stack (LIFO); (d) Breadth-First Search (BFS) (using a FIFO Queue).

Differentiation

Teacher notes