Algorithm Search⌘K

Proofs: Johnson's algorithm

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

Johnson's algorithm returns all distances or reports a negative cycle full

This proof shows: correctness, termination.

Let G=(V,E)G = (V, E) be a directed graph with nn vertices, mm arcs and real weights ww. Line numbers refer to the pseudocode of JOHNSON on the main page.

Fact 1 (cycles of G′G'). The graph G′G' of line L1 has a negative cycle if and only if GG has one.

Proof. The new vertex qq has no incoming arc, so no cycle of G′G' passes through qq. Hence the cycles of G′G' are exactly the cycles of GG, with the same weights. □\square

Fact 2 (line L3). Line L3 returns "negative cycle" if and only if GG has a negative cycle.

Proof. Every vertex of G′G' is reachable from qq by its arc q→vq \to v. Bellman-Ford from qq reports a negative cycle exactly when a negative cycle is reachable from qq (see Bellman-Ford). Since every cycle is reachable from qq, this happens exactly when G′G' has a negative cycle, which by Fact 1 is when GG has one. □\square

From now on, assume GG has no negative cycle.

Fact 3 (hh is real). For every v∈Vv \in V, h(v)=δ(q,v)h(v) = \delta(q, v) is a real number with h(v)≤0h(v) \le 0.

Proof. vv is reachable from qq, so δ(q,v)<∞\delta(q, v) < \infty. No negative cycle exists in G′G', so δ(q,v)>−∞\delta(q, v) > -\infty. The arc q→vq \to v of weight 0 gives δ(q,v)≤0\delta(q, v) \le 0. □\square

Fact 4 (w^≥0\hat w \ge 0). For every arc (u,v)∈E(u, v) \in E, w^(u,v)=w(u,v)+h(u)−h(v)≥0\hat w(u, v) = w(u, v) + h(u) - h(v) \ge 0.

Proof. A shortest path from qq to uu followed by (u,v)(u, v) is a walk from qq to vv of weight h(u)+w(u,v)h(u) + w(u, v), so h(v)≤h(u)+w(u,v)h(v) \le h(u) + w(u, v). This is the triangle inequality of the optimality conditions. Rearranged, it is w^(u,v)≥0\hat w(u, v) \ge 0. □\square

Lemma (reweighting). For every walk p=⟨v0,v1,…,vk⟩p = \langle v_0, v_1, \ldots, v_k \rangle in GG, w^(p)=w(p)+h(v0)−h(vk)\hat w(p) = w(p) + h(v_0) - h(v_k). Hence for all u,v∈Vu, v \in V, vv is reachable from uu under ww if and only if under w^\hat w, and when it is, δ^(u,v)=δ(u,v)+h(u)−h(v)\hat\delta(u, v) = \delta(u, v) + h(u) - h(v).

Proof. Sum the reduced costs along pp:

w^(p)=∑i=1k(w(vi−1,vi)+h(vi−1)−h(vi))=w(p)+h(v0)−h(vk),\hat w(p) = \sum_{i=1}^{k} \big(w(v_{i-1}, v_i) + h(v_{i-1}) - h(v_i)\big) = w(p) + h(v_0) - h(v_k),

because the sum of h(vi−1)−h(vi)h(v_{i-1}) - h(v_i) telescopes. The two weight functions are on the same arc set, so the walks from uu to vv are the same set in both cases, and reachability is the same. Each walk from uu to vv changes by the same real constant h(u)−h(v)h(u) - h(v), so the infimum over these walks changes by that constant too. Both infima are real: δ(u,v)>−∞\delta(u, v) > -\infty because GG has no negative cycle, and δ^(u,v)≥0\hat\delta(u, v) \ge 0 by Fact 4. □\square

Fact 5 (Dijkstra on w^\hat w). For each uu, line L7 returns δ^(u,v)\hat\delta(u, v) for every vv, with ∞\infty for the vertices not reachable from uu.

Proof. By Fact 4 all weights w^\hat w are nonnegative, and Dijkstra's algorithm is correct on graphs with nonnegative weights. □\square

Theorem. If GG has a negative cycle, JOHNSON returns "negative cycle". Otherwise it returns a matrix DD with D[u][v]=δ(u,v)D[u][v] = \delta(u, v) for all u,v∈Vu, v \in V, where D[u][v]=∞D[u][v] = \infty exactly when vv is not reachable from uu.

Proof. The first part is Fact 2. Otherwise, fix u,vu, v. If vv is not reachable from uu, Fact 5 gives δ^(u,v)=∞\hat\delta(u, v) = \infty, the lemma says this is the case exactly when δ(u,v)=∞\delta(u, v) = \infty, and line L8 keeps ∞\infty. If vv is reachable, Fact 5 and the lemma give

D[u][v]=δ^(u,v)−h(u)+h(v)=δ(u,v)+h(u)−h(v)−h(u)+h(v)=δ(u,v),D[u][v] = \hat\delta(u, v) - h(u) + h(v) = \delta(u, v) + h(u) - h(v) - h(u) + h(v) = \delta(u, v),

where h(u)h(u) and h(v)h(v) are real by Fact 3. □\square

Termination. Bellman-Ford on G′G' does at most nn passes over its m+nm + n arcs. Line L5 does mm steps. Lines L6 to L8 run Dijkstra nn times, and each run ends after it extracts each vertex at most once, followed by nn steps of line L8. All loops are bounded, so JOHNSON terminates on every finite input. ■\blacksquare

Back to Johnson's algorithm