Skip to content
Part IA Lent Term

Dijkstra's Algorithm: Correctness Proof

The Induction Strategy

We prove Dijkstra’s algorithm terminates with v.d=δ(s,v)v.d = \delta(s, v) for all vertices vv. The proof uses induction on the size of set SS (vertices whose distances have been finalised by extraction from the priority queue).

Loop invariant Φ\Phi: At the start of each while-loop iteration, for every vertex vSv \in S, we have v.d=δ(s,v)v.d = \delta(s, v). That is, all vertices extracted so far have their correct shortest-path distances.

Initialisation

Before the first iteration, S=S = \emptyset, so Φ\Phi holds vacuously. The distance estimates are initialised: s.d=0=δ(s,s)s.d = 0 = \delta(s,s), and all other v.d=δ(s,v)v.d = \infty \ge \delta(s,v). The upper-bound property (analogous to BFS Lemma 2) holds: v.dv.d never underestimates the true distance.

The Convergence Property of RELAX

A supporting lemma used inside the proof:

Convergence Property: If suvs \leadsto u \to v is a shortest path from ss to vv, and u.d=δ(s,u)u.d = \delta(s, u) before the edge (u,v)(u, v) is relaxed, then v.d=δ(s,v)v.d = \delta(s, v) after the relaxation.

Proof: After relaxing (u,v)(u, v): v.du.d+w(u,v)=δ(s,u)+w(u,v)=δ(s,v)v.d \le u.d + w(u, v) = \delta(s, u) + w(u, v) = \delta(s, v) But we also have the upper-bound property v.dδ(s,v)v.d \ge \delta(s, v) at all times. Therefore v.d=δ(s,v)v.d = \delta(s, v) exactly.

In words: if we already know the correct distance to uu, and (u,v)(u, v) is the last edge on a true shortest path to vv, then relaxing that edge pins down vv‘s correct distance.

Maintenance: Proof by Contradiction

Assume the invariant fails at some point. Let uu be the first vertex for which u.dδ(s,u)u.d \neq \delta(s, u) when it is added to SS.

  • usu \neq s, since s.d=0=δ(s,s)s.d = 0 = \delta(s, s) (correct).
  • There is a shortest path from ss to uu, because if uu were unreachable, u.d==δ(s,u)u.d = \infty = \delta(s, u), which would be correct.
  • SS \neq \emptyset at the moment uu is selected (it contains at least ss).

Consider any shortest path p=sup = s \leadsto u. Let yy be the first vertex along pp (starting from ss) that is not yet in SS at the moment uu is selected. Let xSx \in S be yy‘s immediate predecessor on pp. Decompose:

p=sp1xyp2up = s \underset{p_1}{\leadsto} x \to y \underset{p_2}{\leadsto} u

Since xx was added to SS before uu, and uu is the first vertex where the invariant fails, we know x.d=δ(s,x)x.d = \delta(s, x). When xx was added to SS, the edge (x,y)(x, y) was relaxed. By the convergence property:

y.d=δ(s,y)y.d = \delta(s, y)

Now, because all edge weights are non-negative, and yy appears before uu on a shortest path: δ(s,y)δ(s,u)\delta(s, y) \le \delta(s, u)

Because uu was the vertex with minimum dd value selected from the priority queue, and ySy \notin S as well: u.dy.du.d \le y.d

Chaining these inequalities: δ(s,u)δ(s,y)=y.du.d\delta(s, u) \ge \delta(s, y) = y.d \ge u.d

But the upper-bound property gives u.dδ(s,u)u.d \ge \delta(s, u). Together: u.d=δ(s,u)u.d = \delta(s, u)

This contradicts the assumption that u.du.d was incorrect. Therefore the invariant Φ\Phi is maintained.

Why Non-Negative Weights Are Essential

The inequality δ(s,y)δ(s,u)\delta(s, y) \le \delta(s, u) relies on the fact that all edges on p2p_2 (from yy to uu) have non-negative weight. If any edge on p2p_2 were negative, the path yuy \leadsto u could have negative total weight, making δ(s,y)δ(s,u)\delta(s, y) \le \delta(s, u) false (it’s possible that uu is reachable from yy with a negative total, making δ(s,y)>δ(s,u)\delta(s, y) > \delta(s, u)). This is why Dijkstra fails on graphs with negative edges: the greedy choice of minimum dd is no longer guaranteed to be optimal.

Termination

When the while loop exits, the priority queue QQ is empty. Since Q=VSQ = V \setminus S, all vertices are now in SS. The invariant Φ\Phi holds for all vVv \in V, so v.d=δ(s,v)v.d = \delta(s, v) for every vertex. The algorithm is correct.

The Predecessor Subgraph Is a Tree

Because RELAX sets π[v]=u\pi[v] = u whenever a shorter path through uu is found, and the final dd values equal the true distances, the predecessor subgraph GπG_\pi (edges (π[v],v)(\pi[v], v) for vsv \neq s) is a shortest-path tree rooted at ss: a directed tree where the unique path from ss to any vertex is a shortest path in GG.

Summary of Proof Structure

StepMethod
Invariantv.d=δ(s,v)v.d = \delta(s,v) for all vSv \in S
BaseS=S = \emptyset, true vacuously
Assumption for failureLet uu be first vertex added to SS with u.dδ(s,u)u.d \neq \delta(s,u)
Construct counter-pathsxyus \leadsto x \to y \leadsto u where xSx \in S, ySy \notin S
Key facts usedConvergence property; non-negative weights; u.dy.du.d \le y.d
Contradictionδ(s,u)δ(s,y)=y.du.d\delta(s,u) \ge \delta(s,y) = y.d \ge u.d, so u.d=δ(s,u)u.d = \delta(s,u)
ConclusionNo first failure → invariant holds for all

Past paper questions: y2023p1q9 (bidirectional variant correctness), y2024p1q9 (relaxed costs and optimality).