Algorithm Search⌘K

Proofs: Dijkstra's algorithm

These are the human proofs of Dijkstra's algorithm, at full length.

Dijkstra's algorithm computes shortest-path distances when all weights are nonnegative full

This proof shows: correctness, termination.

Claim. Let G=(V,E)G = (V, E) be a directed graph with w(u,v)≥0w(u, v) \ge 0 for each edge, and let s∈Vs \in V. Then DIJKSTRA(G,w,s)(G, w, s) ends after at most m+1m + 1 passes of the loop of line L4. At the end, d[v]=δ(s,v)d[v] = \delta(s, v) for each v∈Vv \in V, and for each v≠sv \ne s with d[v]<∞d[v] < \infty, the edge (π[v],v)(\pi[v], v) exists and d[v]=d[π[v]]+w(π[v],v)d[v] = d[\pi[v]] + w(\pi[v], v), so the predecessor edges form a shortest-path tree of the vertices that ss reaches.

Setup. Let S={v:done[v]=true}S = \{v : \text{done}[v] = \text{true}\}. For v∈Vv \in V define μS(v)\mu_S(v) as the least element of the set {d[x]+w(x,v):(x,v)∈E,x∈S}\{d[x] + w(x, v) : (x, v) \in E, x \in S\}, together with the element 00 if v=sv = s; let μS(v)=∞\mu_S(v) = \infty when this set is empty. We use the upper-bound property of relaxation: every finite d[v]d[v] is the weight of a walk from ss to vv, so d[v]≥δ(s,v)d[v] \ge \delta(s, v) at all times. With w≥0w \ge 0 there is no negative cycle, so each δ(s,v)\delta(s, v) is a finite number or ∞\infty, and δ(s,v)≥0\delta(s, v) \ge 0.

Invariant. At each test of line L4:

  • (I1) d[v]=δ(s,v)d[v] = \delta(s, v) for each v∈Sv \in S, and π[v]\pi[v] satisfies the claim for each v∈S∖{s}v \in S \setminus \{s\};
  • (I2) d[v]=μS(v)d[v] = \mu_S(v) for each v∉Sv \notin S; when d[v]<∞d[v] < \infty and v≠sv \ne s, π[v]=x\pi[v] = x for an x∈Sx \in S with d[v]=d[x]+w(x,v)d[v] = d[x] + w(x, v);
  • (I3) for each v∉Sv \notin S with d[v]<∞d[v] < \infty, the heap holds the pair (d[v],v)(d[v], v); each pair (k,v)(k, v) in the heap has k≥d[v]k \ge d[v].

Initialization. After lines L1 to L3, S=∅S = \emptyset, d[s]=0=μ∅(s)d[s] = 0 = \mu_\emptyset(s), and d[v]=∞=μ∅(v)d[v] = \infty = \mu_\emptyset(v) for v≠sv \ne s. The heap holds only (0,s)(0, s). All three parts hold.

Maintenance. Let line L5 take the pair (k,u)(k, u) with the least key.

Case 1: u∈Su \in S. Nothing else changes, so the invariant holds.

Case 2: u∉Su \notin S. By (I3), k≥d[u]k \ge d[u], and the heap holds (d[u],u)(d[u], u), so the least key is at most d[u]d[u]; thus k=d[u]k = d[u], and d[u]<∞d[u] < \infty. By (I3) again, each v∉Sv \notin S with d[v]<∞d[v] < \infty has a pair with key d[v]≥kd[v] \ge k. Hence

d[u]=min⁡v∉Sd[v].(1)d[u] = \min_{v \notin S} d[v]. \tag{1}

We show d[u]≤δ(s,u)d[u] \le \delta(s, u). Since d[u]<∞d[u] < \infty, uu is reachable. Let P=⟨s=p0,p1,…,pr=u⟩P = \langle s = p_0, p_1, \ldots, p_r = u \rangle be a shortest path; it exists because there is no negative cycle. Let pjp_j be the first vertex of PP that is not in SS; it exists because u∉Su \notin S. If j=0j = 0, then pj=sp_j = s and d[s]≤μS(s)≤0≤δ(s,u)d[s] \le \mu_S(s) \le 0 \le \delta(s, u). If j>0j > 0, then pj−1∈Sp_{j-1} \in S, so by (I2) and (I1),

d[pj]=μS(pj)≤d[pj−1]+w(pj−1,pj)=δ(s,pj−1)+w(pj−1,pj)=δ(s,pj),d[p_j] = \mu_S(p_j) \le d[p_{j-1}] + w(p_{j-1}, p_j) = \delta(s, p_{j-1}) + w(p_{j-1}, p_j) = \delta(s, p_j),

where the last equality holds because a prefix of a shortest path is a shortest path. In both cases d[pj]≤δ(s,pj)d[p_j] \le \delta(s, p_j). The rest of PP, from pjp_j to uu, has weight at least 00 because each weight is nonnegative, so δ(s,pj)≤δ(s,u)\delta(s, p_j) \le \delta(s, u). With (1), d[u]≤d[pj]≤δ(s,u)d[u] \le d[p_j] \le \delta(s, u). The upper-bound property gives d[u]≥δ(s,u)d[u] \ge \delta(s, u), so d[u]=δ(s,u)d[u] = \delta(s, u). Line L7 adds uu to SS; (I2) for uu gives the predecessor part of (I1) for uu, so (I1) holds for S′=S∪{u}S' = S \cup \{u\}.

Lines L8 to L11 relax each edge (u,v)(u, v). For v∉S′v \notin S', μS′(v)=min⁡(μS(v),d[u]+w(u,v))\mu_{S'}(v) = \min(\mu_S(v), d[u] + w(u, v)) over the edges from uu to vv, and the relaxations set d[v]d[v] to exactly this value, with π[v]=u\pi[v] = u when the new value wins. So (I2) holds. For v∈S′v \in S', d[v]=δ(s,v)≤δ(s,u)+w(u,v)=d[u]+w(u,v)d[v] = \delta(s, v) \le \delta(s, u) + w(u, v) = d[u] + w(u, v), so line L9 fails and nothing changes: (I1) stays true. Each time line L10 lowers d[v]d[v], line L11 inserts (d[v],v)(d[v], v), and older pairs of vv have keys at least the old, larger d[v]d[v]. So (I3) holds.

Termination. Each pass removes one pair from the heap. Pairs are inserted once by line L3 and once by each successful test of line L9. Line L9 runs once for each edge (u,v)(u, v), at the one pass where uu enters SS, because a vertex enters SS only once. So at most m+1m + 1 pairs are ever inserted, and the loop makes at most m+1m + 1 passes.

At the end. The heap is empty. By (I3), d[v]=∞d[v] = \infty for each v∉Sv \notin S. Assume that some v∉Sv \notin S is reachable, and take a path from ss to vv with its first vertex y∉Sy \notin S. If y=sy = s, then d[s]≤0d[s] \le 0, and if y≠sy \ne s, its predecessor xx on the path is in SS, so d[y]≤d[x]+w(x,y)<∞d[y] \le d[x] + w(x, y) < \infty by (I2). Both contradict d[y]=∞d[y] = \infty. So each vertex outside SS is unreachable, and d[v]=∞=δ(s,v)d[v] = \infty = \delta(s, v) for it. For v∈Sv \in S, (I1) gives d[v]=δ(s,v)d[v] = \delta(s, v) and the predecessor condition. For v∈S∖{s}v \in S \setminus \{s\}, the value π[v]=x\pi[v] = x was set at the pass where xx entered SS, and at that time v∉Sv \notin S. So xx entered SS before vv. Following predecessors from vv therefore visits vertices that entered SS earlier and earlier, so it stops, and it can only stop at ss, the one vertex of SS with no predecessor. The predecessor edges thus form a tree rooted at ss, and along each tree path the weights add up to d[v]=δ(s,v)d[v] = \delta(s, v). ■\blacksquare

Where the hypothesis is used. Only the step δ(s,pj)≤δ(s,u)\delta(s, p_j) \le \delta(s, u) uses w≥0w \ge 0. The example on the page, with the edge b→ab \to a of weight −3-3, breaks exactly this step: there the first vertex of the shortest path to cc outside SS is bb, and δ(s,b)=4>δ(s,c)=2\delta(s, b) = 4 > \delta(s, c) = 2.

Back to Dijkstra's algorithm