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 graphedges are one-way arrows: xy goes from x to y, and yx is a different edge. Think one-way streets.
weight wevery edge has a length w(xy) ≥ 0 (w : E → ℕ). "Weighted" means edge-weighted.
path v0, v1, …, vkwritten 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 pathany 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

SSSPP. Given a weighted directed graph G = (V, E) and a source vertex s, find a shortest path from s to every vertex v.

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

The idea: keep a set T of vertices whose distance is locked in (green on her board). Every other vertex has a label L(v), the best distance found so far. Each round: lock the unlocked vertex with the smallest label, then update the labels of its out-neighbours: is going via the newly locked vertex shorter?
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)
Lock closest town. Its number now TRUE.
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?"

The trick: read the subscript of dist as the map you're allowed to drive on. distG uses the whole map. distG[T] may only use the locked vertices. distG[T ∪ {v}] may use the locked vertices plus v itself, so the route wanders inside T and then steps out to v at the very end.
Tthe set of locked vertices (green). It starts as {s}, grows by one each round, and stops when it's all of V.
va 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.
T = done towns. Done town = we know its real distance.
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:

Negative road = magic road that gives you distance back.
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.

Water start at s. Water flow on roads.
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

  1. 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.
  2. Start: only s is locked, at distance 0, and the labels are the direct edges from s. Both promises hold. ✓
  3. Dijkstra locks v′ (smallest label). Pretend its label is too big: there's a sneakier, shorter route P.
  4. P must leave the locked area somewhere. Call the first unlocked vertex on it v″.
  5. 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′).
  6. 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. ✓
  7. Line 7 updates the other labels using v′, so (b) holds again. ✓
  8. 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
0What the whole proof is about: the two promises
1Base case: the promises hold at the start
2Part (a): the newly locked vertex v′ has the right number (2.1–2.6)
3Part (b): the other labels are updated correctly (3.1–3.4)
4Finish: why the promises give the theorem
0Goal: state exactly what we prove: two promises that hold at every round. ✓ done in lecture

Notation in this section

Tthe 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
Two promises.
(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.
  1. (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).
  2. (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).
  3. 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.
1Goal: show both promises are true at the very start, when only s is locked. ✓ done in lecture

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)
Start. Only s done.
s to s = 0. TRUE. (a) ✓
Other towns: only walk allowed = one hop from s. Number = that hop. (b) ✓
  1. (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).
  2. (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

2.1Goal: set up the contradiction. Assume v′'s number is wrong. ✓ done in lecture

Notation in this section

v′the vertex Dijkstra locks this round (smallest label, line 5)
Pa shortest route from s to v′ on the whole map
w(P)its weight, which equals distG(s,v′)
Dijkstra lock v′.
Pretend number of v′ WRONG.
Then real best walk P is shorter than the number.
  1. 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.
  2. 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′).
2.2Goal: rule out the easy case: P never leaves the locked area (apart from v′ itself). ✓ done in lecture

Notation in this section

G[T ∪ {v′}]the map with locked vertices plus v′
If walk P only use done towns (then step to v′),
that is exactly what number of v′ measures.
So number ≤ P. But we said P shorter. Liar. ✓
  1. 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.
  2. So P must have some other vertex outside T. That's the interesting case.So P − v′ contains a vertex not in T.
2.3Goal: name the place where P first leaves the locked area. ✓ done in lecture

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)
Walk along P from s.
First town that is NOT done = v″.
Walk up to v″ = P″.
  1. 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.
2.4Goal: show P″ is a shortest route to v″ on the whole map. (A student asked about this in the lecture.) ✓ done in lecture

Notation in this section

distG(s,v″)the true shortest distance to v″
subpatha piece of a path
Best walk to v′ go through v″.
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. ✓
  1. 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″).
2.5Goal: show v″'s label equals the length of P″. ✓ done in lecture

Notation in this section

distG[T ∪ {v″}](s,v″)shortest route to v″ using locked vertices plus v″ at the end
P″ only use done towns, then step to v″.
Promise (b) say: number of v″ = best walk like that.
P″ is best walk. So number of v″ = P″. ✓
  1. 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″).
  2. 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″).
2.6Goal: chain it together and reach the contradiction. ✓ done in lecture

Notation in this section

line 5Dijkstra picks the unlocked vertex with the smallest label
L(v″) = P″.
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 ✓
  1. 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).
  2. 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′).
  3. 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.

3.1Goal: write down what we must show, and start it with "Let v…". ✓ done in lecture

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).
v′ now done.
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.
  1. 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).
  2. It's a "for every" statement, so start with: take any such v.Let v ∈ V ∖ (T ∪ {v′}).
  3. 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).
  4. 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) }.
  5. 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) < ∞.
3.1+From the lecture (7 Oct): how to start part (b), and the "no route" case ✓ done in lecture

"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:

Goal say: "for EVERY not-done town v, number right."
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. ✓
Let v ∈ V ∖ (T ∪ {v′}).
Exam marks (her words): proofs are marked with partial credit, step by step. "Very few of you are going to get a 100% correct proof", but each correct piece of reasoning earns marks. Just writing the right first line for a "for every" statement ("Let v be…") already gets a mark. So always write the opening line, even if you're unsure how the rest goes.

The "no route at all" case comes first. Before choosing a best route R, she deals with the case where there isn't one:

Maybe no walk to v at all (using done towns + v′ + v).
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).
If there is no (s,v)-path in G[T ∪ {v,v′}], then distG[T ∪ {v,v′}](s,v) = ∞ = L′(v). So assume such a path exists, and let R be a shortest one, chosen (if possible) not to contain v′.

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.

3.2Goal: handle the case where the best route doesn't use v′ at all. ⏳ lecture is here

Notation in this section

Ra shortest route to v using only the new locked set plus v. If there's a choice, pick one that avoids v′.
Take best walk R to v.
If R not use v′: it is an old walk. Old number already = it. Nothing change. ✓
  1. 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′.
  2. 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.
3.3Goal: if the best route does use v′, show that v′ is the vertex right before v. next

Notation in this section

v″ (new meaning here)the vertex just before v on R. (The letter is reused, as in the notes.)
Sa shortest route to v″ inside the locked area (from promise (a))
R use v′. Is v′ the LAST stop before v?
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. ✓
  1. 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.
  2. 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″).
  3. 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).
  4. 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″.
3.4Goal: conclude that line 7 gives exactly the right new label. next

Notation in this section

R′the part of R from s up to v′
R = best walk to v′, then one hop v′→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 ✓
  1. 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′).
  2. 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.
4Goal: explain why the two promises prove the theorem. next

Notation in this section

T = Vevery vertex locked: the loop at line 4 stops
Promises true every round.
End: all towns done.
Promise (a): done town number TRUE.
So ALL numbers true. Dijkstra works. □
  1. 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)

Exam checklist

  1. Method: induction on |T|, proving both (a) and (b) at every stage.
  2. Base case: T = {s}. (a) from line 2, (b) from line 1.
  3. 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.
  4. Use (b) for v″: L(v″) = w(P″).
  5. Say where w ≥ 0 is used: w(P″) ≤ w(P).
  6. Get L(v″) < L(v′), which contradicts line 5.
  7. 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).
  8. 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

ClaimInduction on |T|: (a) v ∈ T ⇒ L(v) = distG(s,v) = distG[T](s,v); (b) v ∉ T ⇒ L(v) = distG[T∪{v}](s,v).
BaseT = {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).
EndT = 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 meanscheapest single edge from the tree to vcheapest route through the locked area, then one step to v
Update (line 8 / line 7)L(v) ← w(v′v) if smallerL(v) ← L(v′) + w(v′v) if smaller
PromiseT is inside some MST(a) and (b) above
Key stepswap e in for e′ (cheapest crossing edge)a sneaky route must leave T at v″, whose label would be smaller: contradiction
NeedsG connectedweights ≥ 0