Dijkstra's algorithm computes shortest-path distances when all weights are nonnegative full
This proof shows: correctness, termination.
Claim. Let G=(V,E) be a directed graph with w(u,v)≥0 for each edge, and let s∈V. Then DIJKSTRA(G,w,s) ends after at most m+1 passes of the loop of line L4. At the end, d[v]=δ(s,v) for each v∈V, and for each v=s with d[v]<∞, the edge (π[v],v) exists and d[v]=d[π[v]]+w(π[v],v), so the predecessor edges form a shortest-path tree of the vertices that s reaches.
Setup. Let S={v:done[v]=true}. For v∈V define μS(v) as the least element of the set {d[x]+w(x,v):(x,v)∈E,x∈S}, together with the element 0 if v=s; let μS(v)=∞ when this set is empty. We use the upper-bound property of relaxation: every finite d[v] is the weight of a walk from s to v, so d[v]≥δ(s,v) at all times. With w≥0 there is no negative cycle, so each δ(s,v) is a finite number or ∞, and δ(s,v)≥0.
Invariant. At each test of line L4:
- (I1) d[v]=δ(s,v) for each v∈S, and π[v] satisfies the claim for each v∈S∖{s};
- (I2) d[v]=μS(v) for each v∈/S; when d[v]<∞ and v=s, π[v]=x for an x∈S with d[v]=d[x]+w(x,v);
- (I3) for each v∈/S with d[v]<∞, the heap holds the pair (d[v],v); each pair (k,v) in the heap has k≥d[v].
Initialization. After lines L1 to L3, S=∅, d[s]=0=μ∅(s), and d[v]=∞=μ∅(v) for v=s. The heap holds only (0,s). All three parts hold.
Maintenance. Let line L5 take the pair (k,u) with the least key.
Case 1: u∈S. Nothing else changes, so the invariant holds.
Case 2: u∈/S. By (I3), k≥d[u], and the heap holds (d[u],u), so the least key is at most d[u]; thus k=d[u], and d[u]<∞. By (I3) again, each v∈/S with d[v]<∞ has a pair with key d[v]≥k. Hence
d[u]=v∈/Smind[v].(1) We show d[u]≤δ(s,u). Since d[u]<∞, u is reachable. Let P=⟨s=p0,p1,…,pr=u⟩ be a shortest path; it exists because there is no negative cycle. Let pj be the first vertex of P that is not in S; it exists because u∈/S. If j=0, then pj=s and d[s]≤μS(s)≤0≤δ(s,u). If j>0, then pj−1∈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), where the last equality holds because a prefix of a shortest path is a shortest path. In both cases d[pj]≤δ(s,pj). The rest of P, from pj to u, has weight at least 0 because each weight is nonnegative, so δ(s,pj)≤δ(s,u). With (1), d[u]≤d[pj]≤δ(s,u). The upper-bound property gives d[u]≥δ(s,u), so d[u]=δ(s,u). Line L7 adds u to S; (I2) for u gives the predecessor part of (I1) for u, so (I1) holds for S′=S∪{u}.
Lines L8 to L11 relax each edge (u,v). For v∈/S′, μS′(v)=min(μS(v),d[u]+w(u,v)) over the edges from u to v, and the relaxations set d[v] to exactly this value, with π[v]=u when the new value wins. So (I2) holds. For v∈S′, d[v]=δ(s,v)≤δ(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], line L11 inserts (d[v],v), and older pairs of v have keys at least the old, larger 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), at the one pass where u enters S, because a vertex enters S only once. So at most m+1 pairs are ever inserted, and the loop makes at most m+1 passes.
At the end. The heap is empty. By (I3), d[v]=∞ for each v∈/S. Assume that some v∈/S is reachable, and take a path from s to v with its first vertex y∈/S. If y=s, then d[s]≤0, and if y=s, its predecessor x on the path is in S, so d[y]≤d[x]+w(x,y)<∞ by (I2). Both contradict d[y]=∞. So each vertex outside S is unreachable, and d[v]=∞=δ(s,v) for it. For v∈S, (I1) gives d[v]=δ(s,v) and the predecessor condition. For v∈S∖{s}, the value π[v]=x was set at the pass where x entered S, and at that time v∈/S. So x entered S before v. Following predecessors from v therefore visits vertices that entered S earlier and earlier, so it stops, and it can only stop at s, the one vertex of S with no predecessor. The predecessor edges thus form a tree rooted at s, and along each tree path the weights add up to d[v]=δ(s,v). ■
Where the hypothesis is used. Only the step δ(s,pj)≤δ(s,u) uses w≥0. The example on the page, with the edge b→a of weight −3, breaks exactly this step: there the first vertex of the shortest path to c outside S is b, and δ(s,b)=4>δ(s,c)=2.