Algorithm Search⌘K

Proofs: Goldberg's scaling algorithm

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

Goldberg's scaling algorithm is correct full

This proof shows: correctness, termination.

Claim. Let GG have integer weights and source ss. GOLDBERG-SCALING ends. It returns NEGATIVE-CYCLE if and only if a negative cycle is reachable from ss. Otherwise it returns δ(s,v)\delta(s, v) for every vv (+∞+\infty if ss does not reach vv) and parents that form a shortest-path tree.

Let RR be the set of vertices reachable from ss and GRG_R the subgraph on RR kept by line L1. Every walk from ss stays in RR, so distances from ss are the same in GG and GRG_R, and a cycle in GRG_R is reachable from ss. So it is enough to prove the claim for GRG_R with "a negative cycle in GRG_R" in place of "a reachable negative cycle". Write wi(e)=⌈w(e)/2b−i⌉w_i(e) = \lceil w(e) / 2^{b - i} \rceil and c(u,v)=wi(u,v)+p(u)−p(v)c(u, v) = w_i(u, v) + p(u) - p(v) during scale ii.

Step 1: rounding. For every integer ww and q=2b−iq = 2^{b-i}, ⌈w/q⌉≥2⌈w/(2q)⌉−1\lceil w/q \rceil \ge 2 \lceil w / (2q) \rceil - 1: write x=w/qx = w/q; then ⌈x/2⌉<x/2+1\lceil x/2 \rceil < x/2 + 1, so 2⌈x/2⌉−1<x+12 \lceil x/2 \rceil - 1 < x + 1, and since the left side is an integer, it is at most ⌈x⌉\lceil x \rceil. Also w0(e)=⌈w(e)/2b⌉≥0w_0(e) = \lceil w(e) / 2^b \rceil \ge 0, because w(e)≥−N>−2bw(e) \ge -N > -2^b. And wi(e)≥w(e)/2b−iw_i(e) \ge w(e) / 2^{b-i}, so for every cycle CC, wi(C)<0w_i(C) < 0 implies w(C)<0w(C) < 0.

Step 2: start of a scale. Suppose that at the end of scale i−1i - 1 (or at the start, for i=1i = 1, with p=0p = 0 and the weights w0≥0w_0 \ge 0), wi−1(u,v)+p(u)−p(v)≥0w_{i-1}(u, v) + p(u) - p(v) \ge 0 for every edge. After line L3, with Step 1, c(u,v)=wi(u,v)+2p(u)−2p(v)≥2(wi−1(u,v)+p(u)−p(v))−1≥−1c(u, v) = w_i(u, v) + 2p(u) - 2p(v) \ge 2(w_{i-1}(u, v) + p(u) - p(v)) - 1 \ge -1.

Invariant. During scale ii, every reduced cost is an integer at least −1-1, and no step turns an edge with c≥0c \ge 0 into one with c=−1c = -1. So the set of improvable vertices never grows. Step 2 gives it at the start; Steps 4 and 5 show that each step keeps it.

Step 3: components and levels. If a strongly connected component of the admissible graph contains an edge (u,v)(u, v) with c=−1c = -1, a path of admissible edges from vv back to uu closes a cycle CC with reduced cost at most −1-1. Reduced costs telescope around a cycle, so wi(C)<0w_i(C) < 0, and by Step 1, w(C)<0w(C) < 0: the report of line L5 is correct. Otherwise all admissible edges inside components cost 0, the contracted admissible graph is a DAG, and d(v)d(v), the least cost of an admissible path ending at vv (the empty path costs 0), is well defined, at most 0, constant on each component, and satisfies d(v)≤d(u)+c(u,v)d(v) \le d(u) + c(u, v) for every admissible edge. An improvable vertex vv with a −1-1 edge (u,v)(u, v) has d(v)≤d(u)−1≤−1d(v) \le d(u) - 1 \le -1; the edge joins two components.

Step 4: the cut (line L7). Let X={v:d(v)≤j}X = \{v : d(v) \le j\} with j≤−1j \le -1. If u∈Xu \in X and (u,v)(u, v) is admissible, d(v)≤d(u)≤jd(v) \le d(u) \le j, so v∈Xv \in X: no admissible edge leaves XX. Lowering pp by 1 on XX changes cc by +1+1 on edges entering XX, by −1-1 on edges leaving XX, and not at all otherwise. Edges leaving XX had c≥1c \ge 1, so they end with c≥0c \ge 0. So the invariant holds. Let vv be improvable with d(v)=jd(v) = j and (u,v)(u, v) any edge with c=−1c = -1. Then d(u)≥d(v)+1=j+1d(u) \ge d(v) + 1 = j + 1, so u∉Xu \notin X and the edge now costs 0. So every improvable vertex of level jj stops being improvable.

Step 5: the path step (lines L8 to L10). Let v∗v^* have the least level −L-L, and let PP be an admissible path in the contracted DAG achieving it; its edges cost 0 or −1-1, and exactly LL of them cost −1-1. Let x1,…,xLx_1, \ldots, x_L be their heads in order along PP. Inside each component, admissible edges cost 0, so PP extends to a walk in GRG_R through the same −1-1 edges; for j≤ij \le i, the part of this walk from xjx_j to xix_i has reduced cost −(i−j)-(i - j).

Let c+=max⁡(c,0)c^+ = \max(c, 0) and D(v)=min⁡(0,min⁡j(dist+(xj,v)−j))D(v) = \min(0, \min_j (\mathrm{dist}^+(x_j, v) - j)), where dist+\mathrm{dist}^+ is the distance with lengths c+c^+. These lengths are nonnegative integers and −L≤D≤0-L \le D \le 0, so Dial's buckets −L,…,0-L, \ldots, 0 compute DD exactly. DD is the distance from a virtual source with edges of length −j-j to xjx_j and 0 to every vertex, so D(v)≤D(u)+c+(u,v)D(v) \le D(u) + c^+(u, v) for every edge. After p←p+Dp \leftarrow p + D, the new reduced cost is c′(u,v)=c(u,v)+D(u)−D(v)≥c(u,v)−c+(u,v)c'(u, v) = c(u, v) + D(u) - D(v) \ge c(u, v) - c^+(u, v), which is ≥0\ge 0 if c(u,v)≥0c(u, v) \ge 0 and ≥−1\ge -1 if c(u,v)=−1c(u, v) = -1. So the invariant holds.

Now assume GRG_R has no negative cycle, and let (y,xj)(y, x_j) be an edge with c=−1c = -1. We show c′(y,xj)≥0c'(y, x_j) \ge 0, that is, D(y)≥D(xj)+1D(y) \ge D(x_j) + 1. Since D(xj)≤−jD(x_j) \le -j, it is enough to show D(y)≥1−jD(y) \ge 1 - j. The term 0 is at least 1−j1 - j as j≥1j \ge 1. For i<ji < j, dist+(xi,y)−i≥−i≥1−j\mathrm{dist}^+(x_i, y) - i \ge -i \ge 1 - j. For i≥ji \ge j, take a path QQ from xix_i to yy with c+(Q)=dist+(xi,y)c^+(Q) = \mathrm{dist}^+(x_i, y) (if there is none, the term is ∞\infty). The walk from xjx_j to xix_i, then QQ, then the edge (y,xj)(y, x_j) is a closed walk with reduced cost at most −(i−j)+c+(Q)−1-(i - j) + c^+(Q) - 1; it splits into cycles, each with wi≥0w_i \ge 0 (by Step 1, a cycle negative for wiw_i is negative for ww), and reduced costs telescope, so this is at least 0. Hence dist+(xi,y)≥i−j+1\mathrm{dist}^+(x_i, y) \ge i - j + 1, and the term is at least 1−j1 - j. So, without a negative cycle, every xjx_j stops being improvable, and line L10 never fires. Contrapositively, when line L10 fires, GRG_R has a negative cycle, and the report is correct.

Step 6: progress. Let kk be the number of improvable vertices. If some level holds at least k\sqrt k of them, Step 4 fixes at least k\sqrt k. Otherwise each of the at most LL nonempty levels holds fewer than k\sqrt k, so L>k/k=kL > k / \sqrt k = \sqrt k; the xjx_j are LL different improvable vertices (each is the head of a −1-1 edge, and they lie in different components because levels strictly decrease along PP at each −1-1 edge), and Step 5 fixes all of them. With the invariant, kk strictly decreases in every iteration that does not return, so the loop of line L4 ends, after at most nn iterations.

Step 7: no negative cycle. Assume GRG_R has no negative cycle. By Steps 3 and 5 no report is made. Each scale ends with c≥0c \ge 0 on every edge, which is the hypothesis of Step 2 for the next scale. After scale bb, wb=ww_b = w (and if b=0b = 0, no weight is negative and p=0p = 0), so w(u,v)+p(u)−p(v)≥0w(u, v) + p(u) - p(v) \ge 0 on every edge. For any path QQ from ss to vv, its reduced length is w(Q)+p(s)−p(v)w(Q) + p(s) - p(v), the same shift for all paths to vv. Dijkstra's algorithm with these nonnegative lengths gives the reduced distance δ(s,v)+p(s)−p(v)\delta(s, v) + p(s) - p(v) and a tree of reduced-shortest paths, which are shortest paths for ww. Line L11 undoes the shift. Vertices outside RR keep ∞\infty.

Step 8: a negative cycle. Assume GRG_R has a negative cycle CC. If the algorithm did not report, then by Step 7's argument it would end with w(u,v)+p(u)−p(v)≥0w(u, v) + p(u) - p(v) \ge 0 on every edge, and summing around CC would give w(C)≥0w(C) \ge 0, a contradiction. (When b=0b = 0 there is no negative weight, so no negative cycle.) So it reports NEGATIVE-CYCLE, and by Steps 3 and 5 it reports only when a negative cycle exists.

Termination. There are bb scales, each loop ends by Step 6, and every line inside is a finite computation. ■\blacksquare

Back to Goldberg's scaling algorithm