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 have integer weights and source . GOLDBERG-SCALING ends. It returns NEGATIVE-CYCLE if and only if a negative cycle is reachable from . Otherwise it returns for every ( if does not reach ) and parents that form a shortest-path tree.
Let be the set of vertices reachable from and the subgraph on kept by line L1. Every walk from stays in , so distances from are the same in and , and a cycle in is reachable from . So it is enough to prove the claim for with "a negative cycle in " in place of "a reachable negative cycle". Write and during scale .
Step 1: rounding. For every integer and , : write ; then , so , and since the left side is an integer, it is at most . Also , because . And , so for every cycle , implies .
Step 2: start of a scale. Suppose that at the end of scale (or at the start, for , with and the weights ), for every edge. After line L3, with Step 1, .
Invariant. During scale , every reduced cost is an integer at least , and no step turns an edge with into one with . 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 with , a path of admissible edges from back to closes a cycle with reduced cost at most . Reduced costs telescope around a cycle, so , and by Step 1, : the report of line L5 is correct. Otherwise all admissible edges inside components cost 0, the contracted admissible graph is a DAG, and , the least cost of an admissible path ending at (the empty path costs 0), is well defined, at most 0, constant on each component, and satisfies for every admissible edge. An improvable vertex with a edge has ; the edge joins two components.
Step 4: the cut (line L7). Let with . If and is admissible, , so : no admissible edge leaves . Lowering by 1 on changes by on edges entering , by on edges leaving , and not at all otherwise. Edges leaving had , so they end with . So the invariant holds. Let be improvable with and any edge with . Then , so and the edge now costs 0. So every improvable vertex of level stops being improvable.
Step 5: the path step (lines L8 to L10). Let have the least level , and let be an admissible path in the contracted DAG achieving it; its edges cost 0 or , and exactly of them cost . Let be their heads in order along . Inside each component, admissible edges cost 0, so extends to a walk in through the same edges; for , the part of this walk from to has reduced cost .
Let and , where is the distance with lengths . These lengths are nonnegative integers and , so Dial's buckets compute exactly. is the distance from a virtual source with edges of length to and 0 to every vertex, so for every edge. After , the new reduced cost is , which is if and if . So the invariant holds.
Now assume has no negative cycle, and let be an edge with . We show , that is, . Since , it is enough to show . The term 0 is at least as . For , . For , take a path from to with (if there is none, the term is ). The walk from to , then , then the edge is a closed walk with reduced cost at most ; it splits into cycles, each with (by Step 1, a cycle negative for is negative for ), and reduced costs telescope, so this is at least 0. Hence , and the term is at least . So, without a negative cycle, every stops being improvable, and line L10 never fires. Contrapositively, when line L10 fires, has a negative cycle, and the report is correct.
Step 6: progress. Let be the number of improvable vertices. If some level holds at least of them, Step 4 fixes at least . Otherwise each of the at most nonempty levels holds fewer than , so ; the are different improvable vertices (each is the head of a edge, and they lie in different components because levels strictly decrease along at each edge), and Step 5 fixes all of them. With the invariant, strictly decreases in every iteration that does not return, so the loop of line L4 ends, after at most iterations.
Step 7: no negative cycle. Assume has no negative cycle. By Steps 3 and 5 no report is made. Each scale ends with on every edge, which is the hypothesis of Step 2 for the next scale. After scale , (and if , no weight is negative and ), so on every edge. For any path from to , its reduced length is , the same shift for all paths to . Dijkstra's algorithm with these nonnegative lengths gives the reduced distance and a tree of reduced-shortest paths, which are shortest paths for . Line L11 undoes the shift. Vertices outside keep .
Step 8: a negative cycle. Assume has a negative cycle . If the algorithm did not report, then by Step 7's argument it would end with on every edge, and summing around would give , a contradiction. (When 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 scales, each loop ends by Step 6, and every line inside is a finite computation.