Sub topic 11
Why the Greedy Choice Is Correct
This chapter assumes you know the heap-based implementation from earlier chapters, the meaning of dist and visited, and the idea of edge relaxation. It also helps to have seen the negative-weight counterexample, because the proof below shows exactly where that example fails.
Dijkstra's algorithm never revisits a vertex once it has been popped and marked visited. That is only safe if the distance recorded at that moment is already the true shortest distance. This chapter proves that it is.
The Greedy Invariant
Write δ(v) for the true shortest distance from the source s to v. The algorithm keeps one invariant:
- When a vertex
uis popped from the heap and marked visited,dist[u] = δ(u).
Two facts make this plausible before we prove it:
dist[v]is never smaller thanδ(v). Every value stored indistis the length of some real path, built one relaxation at a time, and no real path can be shorter than the shortest one.- Once a vertex
xis visited, all of its outgoing edges have been relaxed.
So the danger is only that dist[u] might be too large when u is popped. The proof rules this out.
Proof by Contradiction
Suppose the invariant fails. Let u be the first vertex popped with dist[u] > δ(u). Clearly u ≠ s, because dist[s] = 0 = δ(s).
Take a true shortest path P from s to u. At the moment u is popped, s is visited and u is not yet visited. So somewhere along P there is a first vertex y that is unvisited, and its predecessor x on P is visited.
Now chain the facts together:
xwas visited beforeu, anduis the first failure, sodist[x] = δ(x).- When
xwas visited, the edge(x, y)was relaxed, sodist[y] ≤ δ(x) + w(x, y) = δ(y). The equality holds because subpaths of shortest paths are shortest paths. - All edge weights are non-negative, so the remaining part of
Pfromytouhas length at least 0. That givesδ(y) ≤ δ(u). - By assumption,
δ(u) < dist[u].
Combining them gives dist[y] ≤ δ(y) ≤ δ(u) < dist[u].
Relaxation pushed a heap entry (dist[y], y), and y is still unvisited, so that entry is still in the heap. A min-heap pops the smallest key first, so it would have popped y before u. This contradicts the choice of u. So no such u exists, and the invariant holds.
Where Each Assumption Is Used
| Step | Assumption it needs |
|---|---|
| `dist[x] = δ(x)` | `u` is the *first* failure |
| `dist[y] ≤ δ(y)` | `x`'s edges were relaxed when it was visited |
| `δ(y) ≤ δ(u)` | Non-negative edge weights |
| `y` popped before `u` | The heap always returns the minimum key |
Step 3 is the one negative weights break. In the counterexample from the earlier chapter, the edge C → B has weight -2. Going further along a path made it shorter, so a vertex popped early, like B, could still improve later.
Common Mistakes When Reasoning About Correctness
- Proving it for neighbours only. Saying "the closest neighbour of the source is correct" covers the first step. The proof needs the full invariant, applied to every vertex at the moment it is popped.
- Forgetting the case
y = u. Ifuis itself the first unvisited vertex onP, step 2 givesdist[u] ≤ δ(u)directly, which is again a contradiction. The general argument covers this case without changes. - Assuming zero-weight edges are a problem. The inequality in step 3 allows equality, so weight-0 edges are fine.
Exercise
In the proof, which single inequality fails when the graph contains a negative edge, and why does that let a popped vertex end up with the wrong distance?
Show answer
The inequality δ(y) ≤ δ(u) fails. With a negative edge on the rest of the path from y to u, the full path to u can be shorter than the prefix that ends at y. Then dist[u] can be the smallest key in the heap even though it is not final, so u is popped too early and never updated again.
Exercise
The early-termination version returns as soon as the target is popped. Which part of this chapter justifies that?
Show answer
The greedy invariant. When the target is popped, dist[target] equals δ(target), and the target is never updated after that, so the search can stop and return that value.