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
- Printed weighted graph trace sheets (diagrams below) and pencils
Vocabulary
- Weighted Graph: A graph where each edge
(u, v) has a numerical weight (cost) w(u, v) representing distance, travel time, cable cost, or network latency.
- Tentative Distance (
dist[v]): The shortest known total cost from the start vertex to v found so far (initialized to 0 for start and ∞ for all other vertices).
- Edge Relaxation: Checking whether going from
start through u to neighbor v (dist[u] + w(u, v)) beats the current best known cost dist[v]; if dist[u] + w(u, v) < dist[v], we update dist[v] and set prev[v] = u.
- Settled (Finalized) Vertex: A vertex whose minimum distance from
start is permanently locked in.
Lesson flow
1. Opening: When Fewest Hops Is the Slowest Route (7 min)
- Draw a 3-node triangle on the board representing travel times in minutes between
A, B, and C:
- Direct highway edge
A — C has weight 10 minutes (congested construction zone, 1 hop).
- Local road
A — B has weight 2 minutes, and B — C has weight 3 minutes (2 hops total).
- Ask:
- "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!
- "Which route is actually faster?" →
A → B → C (2 + 3 = 5 minutes!).
- 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)
- Part A — Pair Dijkstra Trace (12 min):
Pairs trace Dijkstra's algorithm from
S to T on this 5-vertex graph:
S — A (6), S — B (2)
B — A (3), B — C (8)
A — C (1), A — T (9)
C — T (4)
Students fill out the 5-round table and verify:
- Round 1 settles
S (0) → dist[A]=6 (S), dist[B]=2 (S)
- 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)
- 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)
- Round 4 settles
C (6) → C→T relaxes dist[T] from 14 down to 6+4 = 10 (C)
- Round 5 settles
T (10) → Shortest path is S → B → A → C → T with cost 10.
- Part B — Why Negative Edges Break Dijkstra (5 min):
Draw a 3-vertex directed graph:
X → Y has weight 5
X → Z has weight 2
Y → Z has weight -6 (a negative edge!)
- 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)
- Question 1: "What happens when you run Dijkstra's algorithm on a graph where every single edge has weight
w = 1?"
- Example good answer: It behaves identically to Breadth-First Search (BFS)! BFS is simply the special case of Dijkstra's algorithm when all edge weights are equal to
1.
- Question 2: "In our Lesson 5 network (
A → C → B → D → E), dist[B] was first set to 4 in Round 1, and then lowered to 3 in Round 2. Why was it legal to change dist[B] in Round 2?"
- Example good answer: Because
B was still unsettled in Round 2! A vertex's distance is only tentative until that vertex itself is chosen as the minimum unsettled vertex u.
5. End-of-unit synthesis & Closing check (5 min)
- 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?
- 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.
- 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?
- (a) Looking up a user's profile by their unique username
"alex_42" in O(1) average time.
- (b) Finding the fastest driving route across a highway network where each road segment is labeled with its travel time in minutes.
- (c) Implementing the
Undo button in a drawing program.
- (d) Finding the minimum number of friend-of-a-friend introductions connecting two people in an unweighted social graph.
Worked answers for closing check
- 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).
- 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).
- (a) Hash Table; (b) Dijkstra's Algorithm; (c) Stack (LIFO); (d) Breadth-First Search (BFS) (using a FIFO Queue).
Differentiation
- Extra support: Have students cross out settled vertices with a check mark (
✓) in the table header at the end of each round so they only compare tentative distances among the remaining unsettled columns.
- Extra challenge: Ask students how A (A-Star) Search* speeds up Dijkstra's algorithm on a physical map by adding an admissible heuristic estimate
h(v) (such as straight-line Euclidean distance to the goal) to the priority key dist[v] + h(v).
Teacher notes
- Common misconception: Students sometimes try to add the new edge weight
w(u, v) to the old dist[v] instead of computing dist[u] + w(u, v). Write the relaxation question on the board in words: "Is going through u (dist[u] + edge) shorter than the best route to v we knew so far (dist[v])?"