Algorithm Search⌘K

Proofs: Near-linear-time negative-weight SSSP

These are the human proofs of Near-linear-time negative-weight SSSP, at full length.

SPMAIN returns the shortest-path distances from s or reports a negative cycle reachable from s full

This proof shows: correctness, termination.

Let G=(V,E)G = (V, E) have integer weights ww and source ss. For a potential ϕ\phi, wϕ(u,v)=w(u,v)+ϕ(u)−ϕ(v)w_\phi(u, v) = w(u, v) + \phi(u) - \phi(v). We use two facts about potentials.

Fact 1. For every walk PP from aa to bb, wϕ(P)=w(P)+ϕ(a)−ϕ(b)w_\phi(P) = w(P) + \phi(a) - \phi(b); in particular every cycle has the same weight under ww and wϕw_\phi. (The terms ϕ\phi telescope.)

Fact 2. If c>0c > 0, then c⋅wc \cdot w has a negative cycle if and only if ww has one. If w′≥ww' \ge w on every arc, a negative cycle under w′w' is a negative cycle under ww.

ELIMNEG

Let HH be a graph with NN vertices and weights ww. Add a virtual source σ\sigma with an arc of weight 0 to every vertex; dist(v)\mathrm{dist}(v) is the distance from σ\sigma, and η(v)\eta(v) is the least number of negative arcs on a shortest path from σ\sigma to vv, when dist(v)\mathrm{dist}(v) is finite.

Lemma 3 (labels are walk weights). At all times, each d(v)d(v) is the weight of some walk from σ\sigma to vv. Hence d(v)≥dist(v)d(v) \ge \mathrm{dist}(v) when dist(v)>−∞\mathrm{dist}(v) > -\infty.

Proof. Initially d(v)=0d(v) = 0, the weight of the arc σ→v\sigma \to v. A relaxation sets d(x)=d(v)+w(v,x)d(x) = d(v) + w(v, x), the weight of the walk to vv followed by the arc (v,x)(v, x). □\square

Lemma 4 (pending vertices). Call an arc (v,x)(v, x) violated if d(v)+w(v,x)<d(x)d(v) + w(v, x) < d(x). At the start of each round, and when the loop ends, every violated arc (v,x)(v, x) has v∈Qv \in Q. During the Bellman-Ford phase of a round, every violated nonnegative arc (v,x)(v, x) has v∈Qv \in Q, and every violated negative arc (v,x)(v, x) has v∈Qv \in Q or vv marked.

Proof. Initially every vertex is in QQ. An arc can become violated only when d(v)d(v) decreases, and every decrease puts vv into QQ. In the Dijkstra phase, a vertex leaves QQ only when it is extracted with its current label (stale heap entries carry larger keys and are skipped), and it is then marked and its nonnegative arcs are relaxed, so they are not violated after that; later decreases of d(v)d(v) put vv back into QQ. So at the end of the Dijkstra phase QQ is empty, no nonnegative arc is violated, and every violated negative arc starts at a marked vertex. The Bellman-Ford phase relaxes every negative arc out of a marked vertex, after which it is not violated unless d(v)d(v) drops again, which puts vv into QQ. Unmarking then leaves only violated arcs with v∈Qv \in Q. □\square

Lemma 5 (rounds). Assume HH has no negative cycle. At the end of the Dijkstra phase of round ii, d(v)=dist(v)d(v) = \mathrm{dist}(v) for every vv with η(v)≤i−1\eta(v) \le i - 1.

Proof. By induction on ii. Let vv have η(v)≤i−1\eta(v) \le i - 1 and let P=σ,v0,v1,…,vr=vP = \sigma, v_0, v_1, \ldots, v_r = v be a shortest path with η(v)\eta(v) negative arcs. Every prefix of PP is a shortest path.

If PP has no negative arc (η(v)=0\eta(v) = 0), then dist(v0)=0=d(v0)\mathrm{dist}(v_0) = 0 = d(v_0) from the start, as d(v0)≤0d(v_0) \le 0 always and d(v0)≥dist(v0)d(v_0) \ge \mathrm{dist}(v_0) by Lemma 3. At the end of the Dijkstra phase of round i≥1i \ge 1, no nonnegative arc is violated (proof of Lemma 4), so along PP, d(vj)≤d(vj−1)+w(vj−1,vj)d(v_j) \le d(v_{j-1}) + w(v_{j-1}, v_j), and summing gives d(v)≤w(P)=dist(v)d(v) \le w(P) = \mathrm{dist}(v). With Lemma 3, d(v)=dist(v)d(v) = \mathrm{dist}(v).

Otherwise let (a,b)(a, b) be the last negative arc of PP. The prefix to aa is a shortest path with at most i−2i - 2 negative arcs, so η(a)≤i−2\eta(a) \le i - 2, and by induction d(a)=dist(a)d(a) = \mathrm{dist}(a) at the end of the Dijkstra phase of round i−1i - 1. Labels never increase and never go below dist\mathrm{dist}, so d(a)d(a) reached its final value at its last decrease, which happened no later than that Dijkstra phase. A decrease puts aa into QQ, and every Dijkstra phase empties QQ, so aa was extracted with the label dist(a)\mathrm{dist}(a) and marked in some round j≤i−1j \le i - 1. The Bellman-Ford phase of round jj relaxed (a,b)(a, b) with d(a)=dist(a)d(a) = \mathrm{dist}(a), so afterward d(b)≤dist(a)+w(a,b)=dist(b)d(b) \le \mathrm{dist}(a) + w(a, b) = \mathrm{dist}(b), hence d(b)=dist(b)d(b) = \mathrm{dist}(b). The rest of PP from bb to vv uses only nonnegative arcs; at the end of the Dijkstra phase of round ii none of them is violated, so summing gives d(v)≤dist(b)+w(b…v)=dist(v)d(v) \le \mathrm{dist}(b) + w(b \ldots v) = \mathrm{dist}(v). □\square

Lemma 6 (ELIMNEG). If HH has no negative cycle, ELIMNEG returns, after at most NN rounds, labels dd with wd(v,x)≥0w_d(v, x) \ge 0 on every arc. If HH has a negative cycle, it returns "negative cycle".

Proof. With no negative cycle, every vv has a shortest path from σ\sigma that is simple, with at most NN arcs, the first of which is the nonnegative arc from σ\sigma; so η(v)≤N−1\eta(v) \le N - 1. By Lemma 5, at the end of the Dijkstra phase of round NN (if the loop gets that far) every label is exact, so the Bellman-Ford phase lowers no label (Lemma 3) and QQ stays empty. So the loop ends after at most NN rounds and line L17 never fires. When the loop ends, QQ is empty, so by Lemma 4 no arc is violated: d(x)≤d(v)+w(v,x)d(x) \le d(v) + w(v, x), that is wd(v,x)≥0w_d(v, x) \ge 0.

If HH has a negative cycle CC, the loop cannot end normally: then no arc would be violated, and summing wd≥0w_d \ge 0 around CC would give w(C)=wd(C)≥0w(C) = w_d(C) \ge 0 by Fact 1. Each round is finite: in the Dijkstra phase the extracted keys never decrease, because a relaxation of a nonnegative arc gives a key at least the key just extracted, so each vertex is extracted at most once per phase; the Bellman-Ford phase relaxes each arc at most once. So the counter rr grows until line L17 returns "negative cycle". □\square

SCALEDOWN

Lemma 7. Let w≥−2Bw \ge -2B on HH, with integer B≥1B \ge 1. SCALEDOWN terminates. If it returns "negative cycle", then HH has a negative cycle under ww. If HH has no negative cycle under ww, it returns ϕ\phi with wϕ(e)≥−Bw_\phi(e) \ge -B for every arc ee.

Proof. Termination: the recursive calls in line L11 have Δ\Delta halved, so the depth is at most log⁡2Δ\log_2 \Delta; the LDD, the SCC computation and FIXDAGEDGES are finite; and ELIMNEG ends by Lemma 6.

A recursive call runs on an induced subgraph H[Vj]H[V_j] with the same weights w≥−2Bw \ge -2B, so by induction on the depth it returns "negative cycle" only if H[Vj]H[V_j], and so HH, has a negative cycle. Line L13 runs ELIMNEG on (wB)ϕ2(w_B)_{\phi_2}. If it reports a negative cycle, then wBw_B has one by Fact 1, and so ww has one by Fact 2, since wB≥ww_B \ge w.

Now assume HH has no negative cycle under ww. Then wB≥ww_B \ge w has none (Fact 2), and (wB)ϕ2(w_B)_{\phi_2} has none (Fact 1), so the recursive calls do not report a cycle, and by Lemma 6 line L13 returns ψ\psi with (wB)ϕ2+ψ(e)≥0(w_B)_{\phi_2 + \psi}(e) \ge 0. Let ϕ=ϕ2+ψ\phi = \phi_2 + \psi. For every arc, wB(e)≤w(e)+Bw_B(e) \le w(e) + B, so

wϕ(e)=(wB)ϕ(e)−(wB(e)−w(e))≥0−B=−B.□w_\phi(e) = (w_B)_\phi(e) - (w_B(e) - w(e)) \ge 0 - B = -B. \qquad \square

The lemma does not use any property of EremE_{\mathrm{rem}}, of the components, or of ϕ2\phi_2; these affect only the running time.

SPMAIN

Let RR be the set of vertices reachable from ss, and k=∣R∣k = |R|. Line L1 keeps G[R]G[R]. Every walk from ss stays in RR, so δ(s,v)\delta(s, v) is the same in GG and in G[R]G[R] for v∈Rv \in R, it is +∞+\infty for v∉Rv \notin R, and a negative cycle is reachable from ss in GG exactly when G[R]G[R] has a negative cycle.

Invariant. If G[R]G[R] has no negative cycle, then before the call in line L4 with the value b=B/2ib = B / 2^i, every arc has wˉϕ(e)≥−2b\bar{w}_\phi(e) \ge -2b.

Proof. For i=1i = 1, ϕ=0\phi = 0 and wˉ(e)=2k w(e)≥−2kW≥−B=−2b\bar{w}(e) = 2k\, w(e) \ge -2kW \ge -B = -2b. The call returns ψ\psi with (wˉϕ)ψ≥−b=−2⋅(b/2)(\bar{w}_\phi)_\psi \ge -b = -2 \cdot (b / 2) by Lemma 7, since wˉϕ\bar{w}_\phi has no negative cycle (Facts 1 and 2). □\square

Theorem. SPMAIN returns "negative cycle" exactly when a negative cycle is reachable from ss, and otherwise returns δ(s,v)\delta(s, v) for every vv.

Proof. Suppose G[R]G[R] has no negative cycle. No call in line L4 reports a cycle (Lemma 7). After the last call, with b=1b = 1, every wˉϕ(e)≥−1\bar{w}_\phi(e) \ge -1, so w∗(e)=wˉϕ(e)+1≥0w^*(e) = \bar{w}_\phi(e) + 1 \ge 0 and line L5 passes. Dijkstra in line L6 is correct for nonnegative weights and gives a tree TT whose path PP from ss to each v∈Rv \in R is simple and shortest under w∗w^*. Let QQ be a simple shortest path from ss to vv under ww, which exists as there is no negative cycle. By Fact 1, for every path XX from ss to vv, w∗(X)=2k w(X)+ϕ(s)−ϕ(v)+∣X∣w^*(X) = 2k\, w(X) + \phi(s) - \phi(v) + |X|, where ∣X∣|X| is its number of arcs. From w∗(P)≤w∗(Q)w^*(P) \le w^*(Q),

2k(w(P)−w(Q))≤∣Q∣−∣P∣≤k−1<2k,2k \bigl( w(P) - w(Q) \bigr) \le |Q| - |P| \le k - 1 < 2k,

so w(P)−w(Q)<1w(P) - w(Q) < 1, and since both are integers, w(P)≤w(Q)=δ(s,v)w(P) \le w(Q) = \delta(s, v). Hence w(P)=δ(s,v)w(P) = \delta(s, v), which is the value returned.

Suppose G[R]G[R] has a negative cycle CC, so w(C)≤−1w(C) \le -1. If some call in line L4 reports a cycle, the answer is right. Otherwise, after the loop, by Facts 1 and 2, w∗(C)=2k w(C)+∣C∣≤−2k+k<0w^*(C) = 2k\, w(C) + |C| \le -2k + k < 0, so some arc of CC has w∗(e)<0w^*(e) < 0 and line L5 returns "negative cycle". In both cases the answer is correct, and line L6 is not reached.

Conversely, "negative cycle" is returned only by line L4, which needs a negative cycle by Lemma 7 and Facts 1 and 2, or by line L5, which by the invariant cannot fire when there is no negative cycle. □\square

Termination. Line L3 runs log⁡2B\log_2 B iterations, each a call of SCALEDOWN that terminates by Lemma 7; lines L1, L2, L5 and L6 are finite. So SPMAIN terminates on every input, with or without negative cycles. ■\blacksquare

Back to Near-linear-time negative-weight SSSP