Sub topic 5
Tracing Dijkstra's Algorithm Step by Step
This chapter assumes you already know edge relaxation and the greedy rule from the previous two chapters: keep a tentative distance for each vertex, repeatedly pick the unvisited vertex with the smallest tentative distance, and relax its outgoing edges. Here you will run the algorithm by hand on one small graph and record every change. Tracing on paper is the fastest way to build intuition. Later, when you debug the heap-based code, you will compare its output against a trace like this one.
The Example Graph
The graph is directed, and every weight is non-negative. The source is A.
Two edges look attractive but lose out. The direct edge A→B (4) is beaten by A→C→B (3). The direct edge B→E (10) is beaten by B→D→E (7 from B). The trace shows the algorithm finding both better routes.
Setting Up the Bookkeeping
Keep three things on paper:
- dist: the tentative distance to each vertex. It starts at 0 for
Aand ∞ for every other vertex. - prev: the vertex that the current best route arrives from.
- visited: the set of finalized vertices. A vertex's distance never changes after it is finalized.
At each step, finalize the unvisited vertex with the smallest finite dist, then relax each of its outgoing edges. Relaxing edge u→v with weight w means: if dist[u] + w < dist[v], set dist[v] to that sum and prev[v] to u.
The Trace
Each row shows the state after the chosen vertex has been finalized and its edges relaxed. Bold values changed in that step.
| Step | Finalized | A | B | C | D | E | What happened |
|---|---|---|---|---|---|---|---|
| 0 | none | 0 | ∞ | ∞ | ∞ | ∞ | Initial state |
| 1 | A | 0 | **4** (A) | **2** (A) | ∞ | ∞ | Both edges out of A improve ∞ |
| 2 | C | 0 | **3** (C) | 2 | **10** (C) | ∞ | 2+1=3 beats 4; D gets 2+8 |
| 3 | B | 0 | 3 | 2 | **8** (B) | **13** (B) | 3+5=8 beats 10; E gets 3+10 |
| 4 | D | 0 | 3 | 2 | 8 | **10** (D) | 8+2=10 beats 13 |
| 5 | E | 0 | 3 | 2 | 8 | 10 | E has no outgoing edges |
The letter in parentheses is prev for that vertex.
Reading the Trace
The vertices are finalized in the order A, C, B, D, E, and their distances come out as 0, 2, 3, 8, 10. That sequence never decreases. With non-negative weights it always comes out this way, and it is the property the correctness chapter builds on.
Several values were overwritten before they became final. B went from 4 to 3, D from 10 to 8, and E from 13 to 10. A tentative distance is an upper bound and nothing more until its vertex is finalized. Note also that step 2 picks C (2) over B (4). If you went in alphabetical order or BFS order instead, you would finalize B at 4, which is wrong.
Final Result
Follow prev backwards from any vertex to read off its path. For E, the chain is E ← D ← B ← C ← A, so the path is A → C → B → D → E with cost 2+1+5+2 = 10. A later chapter turns this backward walk into code.
Exercises
Exercise
Change the weight of C→B from 1 to 3 and trace again from A. Give the order in which vertices are finalized and the final distances.
Show answer
The order is A, C, B, D, E. After A: B=4, C=2. After C: 2+3=5 does not beat 4, so B stays 4, and D=10. After B: D=4+5=9, E=4+10=14. After D: E=9+2=11. Final distances: A=0, B=4, C=2, D=9, E=11.
Exercise
In the original trace, how many times was a tentative distance lowered after it had first become finite, and for which vertices?
Show answer
Three times. B dropped from 4 to 3 (step 2), D from 10 to 8 (step 3), and E from 13 to 10 (step 4).