Dijkstra's Algorithm: Correctness Proof
The Induction Strategy
We prove Dijkstra’s algorithm terminates with for all vertices . The proof uses induction on the size of set (vertices whose distances have been finalised by extraction from the priority queue).
Loop invariant : At the start of each while-loop iteration, for every vertex , we have . That is, all vertices extracted so far have their correct shortest-path distances.
Initialisation
Before the first iteration, , so holds vacuously. The distance estimates are initialised: , and all other . The upper-bound property (analogous to BFS Lemma 2) holds: never underestimates the true distance.
The Convergence Property of RELAX
A supporting lemma used inside the proof:
Convergence Property: If is a shortest path from to , and before the edge is relaxed, then after the relaxation.
Proof: After relaxing : But we also have the upper-bound property at all times. Therefore exactly.
In words: if we already know the correct distance to , and is the last edge on a true shortest path to , then relaxing that edge pins down ‘s correct distance.
Maintenance: Proof by Contradiction
Assume the invariant fails at some point. Let be the first vertex for which when it is added to .
- , since (correct).
- There is a shortest path from to , because if were unreachable, , which would be correct.
- at the moment is selected (it contains at least ).
Consider any shortest path . Let be the first vertex along (starting from ) that is not yet in at the moment is selected. Let be ‘s immediate predecessor on . Decompose:
Since was added to before , and is the first vertex where the invariant fails, we know . When was added to , the edge was relaxed. By the convergence property:
Now, because all edge weights are non-negative, and appears before on a shortest path:
Because was the vertex with minimum value selected from the priority queue, and as well:
Chaining these inequalities:
But the upper-bound property gives . Together:
This contradicts the assumption that was incorrect. Therefore the invariant is maintained.
Why Non-Negative Weights Are Essential
The inequality relies on the fact that all edges on (from to ) have non-negative weight. If any edge on were negative, the path could have negative total weight, making false (it’s possible that is reachable from with a negative total, making ). This is why Dijkstra fails on graphs with negative edges: the greedy choice of minimum is no longer guaranteed to be optimal.
Termination
When the while loop exits, the priority queue is empty. Since , all vertices are now in . The invariant holds for all , so for every vertex. The algorithm is correct.
The Predecessor Subgraph Is a Tree
Because RELAX sets whenever a shorter path through is found, and the final values equal the true distances, the predecessor subgraph (edges for ) is a shortest-path tree rooted at : a directed tree where the unique path from to any vertex is a shortest path in .
Summary of Proof Structure
| Step | Method |
|---|---|
| Invariant | for all |
| Base | , true vacuously |
| Assumption for failure | Let be first vertex added to with |
| Construct counter-path | where , |
| Key facts used | Convergence property; non-negative weights; |
| Contradiction | , so |
| Conclusion | No first failure → invariant holds for all |
Past paper questions: y2023p1q9 (bidirectional variant correctness), y2024p1q9 (relaxed costs and optimality).