Home / COMP2870 / Graph algorithms / Dijkstra proof
Why Dijkstra works
Shortest paths and Theorem 3.1, built up from the ground
Lecture of 5 Oct. Plain English first, with the lecture notes' wording underneath.
A · The ideas you need
Nothing here is the proof yet. These are the pieces it's built from.
Directed weighted graphs, path weight, distance
| directed graph | edges are one-way arrows: xy goes from x to y, and yx is a different edge. Think one-way streets. |
| weight w | every edge has a length w(xy) ≥ 0 (w : E → ℕ). "Weighted" means edge-weighted. |
| path v0, v1, …, vk | written as just its vertices, because between consecutive vertices there's only one edge, so the edges are implied. Its weight is the sum of its edge weights: w(P) = Σi=1k w(vi−1vi). |
| distG(u, v) | the distance from u to v in G: the smallest weight of any u→v path. If there's no path, it's ∞ by convention. |
| a shortest path | any u→v path whose weight equals dist(u, v). There can be several, which is why it's "a shortest path" but "the distance". |
The problem: Single Source Shortest Paths
So it isn't one route, it's a whole set of them: the best route from s to each vertex. Breadth First Search does this when every edge counts as 1. Dijkstra handles real weights (as long as they're ≥ 0).
Dijkstra's algorithm
1 for v ≠ s: L(v) ← w(sv) (∞ if there's no edge sv)
2 L(s) ← 0
3 T ← {s}
4 while T ≠ V:
5 v′ ← the vertex not in T with the smallest label
6 T ← T ∪ {v′} v′ is now locked (green)
7 for v ∉ T: if L(v′) + w(v′v) < L(v): L(v) ← L(v′) + w(v′v)
Look at its neighbours. Going through it cheaper? Yes → new smaller number.
Repeat until all towns locked.
Line 7 in words (her phrase): "I compare the old label with the label of the new vertex plus the weight of the edge from it, and I keep the smaller." In the trace, record each round in a table, as she recommended. It's much easier than remembering what changed.
shortest-path tree edge giving the current label just locked / just updated · filled vertex = in T (locked)
Decoding the notation: "which map am I allowed to use?"
| T | the set of locked vertices (green). It starts as {s}, grows by one each round, and stops when it's all of V. |
| v | a placeholder: whichever vertex you're looking at. "For every v ∈ T" means: pick any locked vertex. |
| v′ | the specific vertex being locked this round (smallest label). |
| G[T] | the induced subgraph from week 1: only the vertices in T, with every edge between them. |
| T ∪ {v} | T plus one extra vertex, v. |
v = any town. Just a name.
distG = shortest walk, can walk anywhere.
distG[T] = shortest walk, can only step on done towns.
distG[T ∪ {v}] = walk only on done towns… then one last step to v.
route being described other edges · faded = not on the allowed map
Why the weights must be ≥ 0
The whole proof leans on one thing: a path can't get shorter by going further. A negative edge breaks that. This is the counterexample from the notes:
Then long walk can end up cheaper than short walk.
Dijkstra lock town too early. Number wrong. Algorithm break.
B · The proof (Theorem 3.1)
Dijkstra's algorithm finds the distance from s to every other vertex.
The whole proof in one minute
The analogy: water spreading from s. Pour water in at s. It flows along the edges, and longer edges take longer. The places it reaches first are the closest ones. Once a place is wet, no later route can reach it sooner, because water only ever moves forward and nothing flows backwards in time (no negative edges). Dijkstra "wets" vertices in exactly this order: the smallest label is the next place the water reaches.
Close town get wet first. Far town get wet later.
Town wet = done. Nothing reach it faster later.
Dijkstra = list of when each town get wet.
Every step, in one line each
- Goal: two promises hold every round. (a) Locked vertices have their true distance. (b) Unlocked labels are the best route through locked vertices, then one step.
- Start: only s is locked, at distance 0, and the labels are the direct edges from s. Both promises hold. ✓
- Dijkstra locks v′ (smallest label). Pretend its label is too big: there's a sneakier, shorter route P.
- P must leave the locked area somewhere. Call the first unlocked vertex on it v″.
- The bit of P up to v″ is a "locked area, then one step" route, so L(v″) ≤ that bit ≤ all of P (weights ≥ 0) < L(v′).
- So v″ had a smaller label than v′. But Dijkstra picked v′ as the smallest. Impossible. So v′'s label was right, and (a) holds. ✓
- Line 7 updates the other labels using v′, so (b) holds again. ✓
- At the end, T = V, so by (a) every label is the true distance. □
The proof, section by section
The proof is long because it has many small goals. Each box below is one goal. Read the Goal line first, then the notation, then the caveman version, then the steps. The tags show where the lecture has got to.
| § | Proves |
|---|---|
| 0 | What the whole proof is about: the two promises |
| 1 | Base case: the promises hold at the start |
| 2 | Part (a): the newly locked vertex v′ has the right number (2.1–2.6) |
| 3 | Part (b): the other labels are updated correctly (3.1–3.4) |
| 4 | Finish: why the promises give the theorem |
Notation in this section
| T | the locked (done) vertices |
| L(v) | v's current label (number) |
| distG(s,v) | shortest route on the whole map |
| distG[T](s,v) | shortest route using only locked vertices |
| distG[T ∪ {v}](s,v) | shortest route using locked vertices, plus v at the very end |
| induction on |T| | prove it at the start, then show each round keeps it true |
(a) Done town: number TRUE.
(b) Not-done town: number = best walk through done land, then one hop.
Show both stay true every round.
At the end every town done → every number true.
- (a) For every locked v, L(v) is the true distance, and some shortest route stays inside the locked area.For every v ∈ T, L(v) = distG(s,v) = distG[T](s,v).
- (b) For every unlocked v, L(v) is the best route that, apart from v, only uses locked vertices.For every v ∉ T, L(v) = distG[T ∪ {v}](s,v).
- We prove these hold at every stage, by induction on how many vertices are locked.We prove by induction on |T| that (a) and (b) hold at each stage of the algorithm.
Notation in this section
| T = {s} | only the source is locked (line 3) |
| G[{s}] | the map with just s on it |
| w(sv) | weight of the direct edge s→v (∞ if none) |
s to s = 0. TRUE. (a) ✓
Other towns: only walk allowed = one hop from s. Number = that hop. (b) ✓
- (a) for s: the only route from s to s is "stay put", weight 0, and line 2 set L(s) = 0.Any path from s to s consists of the single vertex s, so distG(s,s) = distG[{s}](s,s) = 0 = L(s).
- (b) for every other v: using only s and then v, the only route is the direct edge s→v. Line 1 set L(v) to exactly that.For v ≠ s, the weight of a shortest path from s to v that apart from v passes only through s is w(sv), so by line 1, (b) holds.
§2 · Part (a): the newly locked vertex has the right number
Big picture of §2: Dijkstra locks v′ (smallest label). Pretend its number is wrong. Then a sneaky shorter route exists. It must leave the locked area at some v″, and we show v″ would have had a smaller label. Impossible.
locked area T edge that gave v′ its label the pretend route P
Notation in this section
| v′ | the vertex Dijkstra locks this round (smallest label, line 5) |
| P | a shortest route from s to v′ on the whole map |
| w(P) | its weight, which equals distG(s,v′) |
Pretend number of v′ WRONG.
Then real best walk P is shorter than the number.
- Assume both promises hold just before v′ is locked (induction hypothesis).Assume (a) and (b) hold for T just before v′ is added to T in line 6.
- Pretend L(v′) is not the true distance. Take a shortest route P to v′. It's shorter than the label: w(P) < L(v′).Suppose L(v′) ≠ distG(s,v′). Let P be a shortest (s,v′)-path, so w(P) = distG(s,v′) < L(v′).
Notation in this section
| G[T ∪ {v′}] | the map with locked vertices plus v′ |
that is exactly what number of v′ measures.
So number ≤ P. But we said P shorter. Liar. ✓
- If every vertex of P except v′ is locked, then P is exactly the kind of route promise (b) talks about for v′. So L(v′) ≤ w(P). That contradicts w(P) < L(v′).If all vertices of P − v′ are in T, then P is a path in G[T ∪ {v′}], so by (b), L(v′) ≤ w(P), a contradiction.
- So P must have some other vertex outside T. That's the interesting case.So P − v′ contains a vertex not in T.
Notation in this section
| v″ | the first vertex on P (walking from s) that is not locked |
| P″ | the piece of P from s up to v″ (blue on her board) |
First town that is NOT done = v″.
Walk up to v″ = P″.
- Walk along P from s. The first vertex you meet that isn't locked, call it v″. The piece of P up to v″, call it P″.Let v″ be the first vertex not in T when tracing P from s to v′. Let P″ be the (s, v″)-subpath of P.
Notation in this section
| distG(s,v″) | the true shortest distance to v″ |
| subpath | a piece of a path |
Someone say: "shorter walk to v″ exist!"
Ok. Use it, then same walk after.
Now whole walk to v′ shorter. But P was shortest! Liar.
So piece of best walk = best walk. ✓
- A piece of a shortest route is itself a shortest route. If there were a shortcut to v″, you could take it and then follow the rest of P to v′, giving a route shorter than P. But P was a shortest route.P″ is a shortest (s, v″)-path (else there would be a shorter (s, v′)-path). So w(P″) = distG(s, v″).
Notation in this section
| distG[T ∪ {v″}](s,v″) | shortest route to v″ using locked vertices plus v″ at the end |
Promise (b) say: number of v″ = best walk like that.
P″ is best walk. So number of v″ = P″. ✓
- Every vertex of P″ before v″ is locked (v″ is the first one that isn't). So P″ is a "locked vertices, then v″" route, and it's the shortest of all routes (2.4), so in particular the shortest of those.Since all nodes of P″ − v″ belong to T, w(P″) = distG(s,v″) = distG[T ∪ {v″}](s,v″).
- Promise (b) says v″'s label is exactly that best route. So L(v″) = w(P″).By induction hypothesis (b), L(v″) = distG[T ∪ {v″}](s,v″). It follows that L(v″) = w(P″).
Notation in this section
| line 5 | Dijkstra picks the unlocked vertex with the smallest label |
P″ is piece of P. Roads never negative. So P″ ≤ P.
P < L(v′).
So L(v″) < L(v′).
But v′ had SMALLEST number! Liar.
So v′ number TRUE. Part (a) done ✓
- P″ is a piece of P, and the rest of P can't weigh a negative amount. So w(P″) ≤ w(P).w(P″) ≤ w(P) (all weights are ≥ 0).
- Chain: L(v″) = w(P″) ≤ w(P) < L(v′). So v″ had a smaller label than v′.L(v″) = w(P″) ≤ w(P) = distG(s, v′) < L(v′).
- But Dijkstra picked v′ at line 5 because it had the smallest label. Contradiction. So L(v′) is the true distance, and promise (a) holds for the new locked set. ✓This contradicts line 5. Therefore L(v′) = distG(s,v′) = distG[T ∪ {v′}](s,v′), and (a) holds.
§3 · Part (b): the other labels are updated correctly
Big picture of §3: v′ just got locked. Every other unlocked vertex v might now have a better route, through v′. Line 7 checks this. We show that line 7 gives exactly the best route.
Notation in this section
| L′(v) | v's label after this round (after line 7). L is the old label. |
| T ∪ {v′} | the new locked set |
| G[T ∪ {v, v′}] | the map with the new locked vertices plus v |
| "Let v ∈ V ∖ (T ∪ {v′})" | pick any vertex that is still unlocked. The goal is a "for every" statement, so we start with "Let" (her point in the lecture). |
Other towns: is number still right?
Goal is "for every town", so start: "Let v be any not-done town."
Old number OK by promise (b). New number = line 7.
- Goal: every still-unlocked v has the right new label for the bigger locked set.To prove: for every v ∈ V ∖ (T ∪ {v′}), L′(v) = distG[T ∪ {v, v′}](s,v).
- It's a "for every" statement, so start with: take any such v.Let v ∈ V ∖ (T ∪ {v′}).
- What we know: by promise (b), the old label is the best route through the old locked set.By the induction hypothesis (b), L(v) = distG[T ∪ {v}](s,v).
- What line 7 did: new label = the smaller of the old label and "v′'s number plus the edge v′→v".By line 7, L′(v) = min{ L(v), L(v′) + w(v′v) }.
- If there's no route at all, both sides are ∞ and we're done. So assume a route exists.If there is no (s,v)-path in G[T ∪ {v,v′}], then L′(v) = ∞, so assume distG[T ∪ {v,v′}](s,v) < ∞.
"New T" is just T ∪ {v′}. She called it "our current T" on the board. That's her shorthand, but in a proof write T ∪ {v′}.
Why start with "Let v…" and not "Suppose not…"? A student suggested contradiction: "assume it fails for some vertex". She said that works but is cumbersome. For a for every statement, the clean way is direct:
Every town = too many to check one by one.
So: pick ANY one town. Call it v. Know nothing special about it.
Prove it for that v.
v was any town → true for all towns. ✓
The "no route at all" case comes first. Before choosing a best route R, she deals with the case where there isn't one:
Then real answer = ∞.
Line 7 never lower number below ∞ (nothing to add).
So number = ∞ = right. ✓
Now only case left: a walk exists. Pick best one: R (her blue path).
Why "if possible, avoid v′"? She admitted it's a trick "you will not come up with by yourself". It's set up so §3.3 works: if a best route through v′ exists and one avoiding v′ exists, we always take the avoiding one. So if R does use v′, that's because no equally good route avoids it. That fact gives the contradiction in §3.3.
Notation in this section
| R | a shortest route to v using only the new locked set plus v. If there's a choice, pick one that avoids v′. |
If R not use v′: it is an old walk. Old number already = it. Nothing change. ✓
- Take a best route R to v (using locked vertices, v′ and v). If possible, choose one that avoids v′.Let R be a shortest (s,v)-path in G[T ∪ {v,v′}]. If possible, choose R so that it does not contain v′.
- If R avoids v′, it only used the old locked set, so the old label already equals it. Line 7 keeps the old label. ✓If R does not contain v′, then w(R) = distG[T ∪ {v}](s,v) = L(v). By line 7, L′(v) = L(v), so (b) holds for v.
Notation in this section
| v″ (new meaning here) | the vertex just before v on R. (The letter is reused, as in the notes.) |
| S | a shortest route to v″ inside the locked area (from promise (a)) |
Suppose not: last stop = some done town v″.
Done town has best walk inside done land (promise a), no v′ needed.
Use that, then hop to v. Not longer, and no v′.
But we picked R to avoid v′ if possible! Liar.
So v′ is last stop. ✓
- So R uses v′. Look at the vertex right before v on R. Call it v″. Suppose it isn't v′.Let v″ be the vertex just before v in R. Suppose v′ ≠ v″, so v″ ∈ T.
- v″ is locked, so by promise (a) it has a shortest route S that stays inside the old locked area. S doesn't use v′.By induction hypothesis (a), there is an (s,v″)-path S in G[T] with w(S) = L(v″) = distG(s,v″).
- S plus the edge v″→v is a route to v that avoids v′ and is no longer than R.Let S′ be S with the edge v″v appended. S′ is an (s,v)-path in G[T ∪ {v}] with w(S′) ≤ w(R).
- But we chose R to avoid v′ whenever that was possible. Contradiction. So the vertex before v really is v′.So S′ is a shortest path that does not contain v′, contradicting the choice of R. Therefore v′ = v″.
Notation in this section
| R′ | the part of R from s up to v′ |
Best walk to v′ = L(v′) (part a).
So R = L(v′) + hop. That is what line 7 check.
New number right. Part (b) done ✓
- R = (route to v′) + edge v′→v. The route to v′ is a shortest one, which weighs L(v′).Let R′ be the (s,v′)-subpath of R. w(R) = w(R′) + w(v′v), and w(R′) = distG[T ∪ {v′}](s,v′) = L(v′).
- We needed v′ to get this good a route, so it beats the old label: L(v′) + w(v′v) < L(v). So line 7 replaced the label with exactly this, which is the best route. ✓By the choice of R, L(v′) + w(v′v) < distG[T ∪ {v}](s,v) = L(v). So by line 7, L′(v) = L(v′) + w(v′v) = distG[T ∪ {v,v′}](s,v), and (b) holds.
Notation in this section
| T = V | every vertex locked: the loop at line 4 stops |
End: all towns done.
Promise (a): done town number TRUE.
So ALL numbers true. Dijkstra works. □
- Both promises hold after every round. Dijkstra stops when every vertex is locked. Then promise (a) says every label is the true distance. □It follows that (a) and (b) hold after v′ has been added to T, proving the theorem. □
Why this finishes the theorem
The promises hold at every round. Dijkstra stops when T = V, so every vertex is locked, and by (a) every label is the true distance from s.
From the lecture (5 Oct)
- The edges are ordered pairs (directed). A path is written as just its vertices, because the edges are implied.
- No path means distance ∞, by convention. There can be many shortest paths.
- Nothing about the strategy guarantees it works. "We would have to actually prove that our strategy works." That's Theorem 3.1.
- Trace with a table: she recommended it over relying on memory of the board.
- Lots of people found the notation "alien language". Students around the room said so too. She expects that: "it can be amazing if you follow every step… it's going to take you a long time to really sit down and read the proof, explain it to yourself with your own drawings."
- The "subtle difference" in (a): dist in G versus dist in G[T], i.e. any route at all versus a route that stays in the locked area.
Exam checklist
- Method: induction on |T|, proving both (a) and (b) at every stage.
- Base case: T = {s}. (a) from line 2, (b) from line 1.
- Step (a): suppose L(v′) ≠ distG(s, v′). Take a shortest path P. Let v″ be the first vertex not in T, and P″ the subpath.
- Use (b) for v″: L(v″) = w(P″).
- Say where w ≥ 0 is used: w(P″) ≤ w(P).
- Get L(v″) < L(v′), which contradicts line 5.
- Step (b): the new label is min{L(v), L(v′) + w(v′v)}. Handle the cases R avoids v′, and R uses v′ (show v′ is just before v).
- Finish: T = V at the end, so all labels are distances.
C · Getting fluent
From "I followed it" to "I can rebuild it".
The skeleton
| Claim | Induction on |T|: (a) v ∈ T ⇒ L(v) = distG(s,v) = distG[T](s,v); (b) v ∉ T ⇒ L(v) = distG[T∪{v}](s,v). |
| Base | T = {s}: L(s) = 0 (line 2), L(v) = w(sv) (line 1). |
| Step (a) | Suppose L(v′) is wrong → shortest P → first vertex outside T is v″ → L(v″) = w(P″) ≤ w(P) < L(v′) (w ≥ 0) → contradicts line 5. |
| Step (b) | New label = min{L(v), L(v′) + w(v′v)}. A best route avoids v′ (old label), or reaches v straight from v′ (line 7). |
| End | T = V, so by (a) every label is the distance. |
Routine: the same as for Prim. Draw the T blob with P leaving it at v″ (her picture), write the skeleton from a blank page, expand each line with its reason, then redo it cold the next day.
Compared with Prim
| Prim (Theorem 2.4) | Dijkstra (Theorem 3.1) | |
|---|---|---|
| Label means | cheapest single edge from the tree to v | cheapest route through the locked area, then one step to v |
| Update (line 8 / line 7) | L(v) ← w(v′v) if smaller | L(v) ← L(v′) + w(v′v) if smaller |
| Promise | T is inside some MST | (a) and (b) above |
| Key step | swap e in for e′ (cheapest crossing edge) | a sneaky route must leave T at v″, whose label would be smaller: contradiction |
| Needs | G connected | weights ≥ 0 |