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 have integer weights and source . For a potential , . We use two facts about potentials.
Fact 1. For every walk from to , ; in particular every cycle has the same weight under and . (The terms telescope.)
Fact 2. If , then has a negative cycle if and only if has one. If on every arc, a negative cycle under is a negative cycle under .
ELIMNEG
Let be a graph with vertices and weights . Add a virtual source with an arc of weight 0 to every vertex; is the distance from , and is the least number of negative arcs on a shortest path from to , when is finite.
Lemma 3 (labels are walk weights). At all times, each is the weight of some walk from to . Hence when .
Proof. Initially , the weight of the arc . A relaxation sets , the weight of the walk to followed by the arc .
Lemma 4 (pending vertices). Call an arc violated if . At the start of each round, and when the loop ends, every violated arc has . During the Bellman-Ford phase of a round, every violated nonnegative arc has , and every violated negative arc has or marked.
Proof. Initially every vertex is in . An arc can become violated only when decreases, and every decrease puts into . In the Dijkstra phase, a vertex leaves 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 put back into . So at the end of the Dijkstra phase 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 drops again, which puts into . Unmarking then leaves only violated arcs with .
Lemma 5 (rounds). Assume has no negative cycle. At the end of the Dijkstra phase of round , for every with .
Proof. By induction on . Let have and let be a shortest path with negative arcs. Every prefix of is a shortest path.
If has no negative arc (), then from the start, as always and by Lemma 3. At the end of the Dijkstra phase of round , no nonnegative arc is violated (proof of Lemma 4), so along , , and summing gives . With Lemma 3, .
Otherwise let be the last negative arc of . The prefix to is a shortest path with at most negative arcs, so , and by induction at the end of the Dijkstra phase of round . Labels never increase and never go below , so reached its final value at its last decrease, which happened no later than that Dijkstra phase. A decrease puts into , and every Dijkstra phase empties , so was extracted with the label and marked in some round . The Bellman-Ford phase of round relaxed with , so afterward , hence . The rest of from to uses only nonnegative arcs; at the end of the Dijkstra phase of round none of them is violated, so summing gives .
Lemma 6 (ELIMNEG). If has no negative cycle, ELIMNEG returns, after at most rounds, labels with on every arc. If has a negative cycle, it returns "negative cycle".
Proof. With no negative cycle, every has a shortest path from that is simple, with at most arcs, the first of which is the nonnegative arc from ; so . By Lemma 5, at the end of the Dijkstra phase of round (if the loop gets that far) every label is exact, so the Bellman-Ford phase lowers no label (Lemma 3) and stays empty. So the loop ends after at most rounds and line L17 never fires. When the loop ends, is empty, so by Lemma 4 no arc is violated: , that is .
If has a negative cycle , the loop cannot end normally: then no arc would be violated, and summing around would give 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 grows until line L17 returns "negative cycle".
SCALEDOWN
Lemma 7. Let on , with integer . SCALEDOWN terminates. If it returns "negative cycle", then has a negative cycle under . If has no negative cycle under , it returns with for every arc .
Proof. Termination: the recursive calls in line L11 have halved, so the depth is at most ; the LDD, the SCC computation and FIXDAGEDGES are finite; and ELIMNEG ends by Lemma 6.
A recursive call runs on an induced subgraph with the same weights , so by induction on the depth it returns "negative cycle" only if , and so , has a negative cycle. Line L13 runs ELIMNEG on . If it reports a negative cycle, then has one by Fact 1, and so has one by Fact 2, since .
Now assume has no negative cycle under . Then has none (Fact 2), and has none (Fact 1), so the recursive calls do not report a cycle, and by Lemma 6 line L13 returns with . Let . For every arc, , so
The lemma does not use any property of , of the components, or of ; these affect only the running time.
SPMAIN
Let be the set of vertices reachable from , and . Line L1 keeps . Every walk from stays in , so is the same in and in for , it is for , and a negative cycle is reachable from in exactly when has a negative cycle.
Invariant. If has no negative cycle, then before the call in line L4 with the value , every arc has .
Proof. For , and . The call returns with by Lemma 7, since has no negative cycle (Facts 1 and 2).
Theorem. SPMAIN returns "negative cycle" exactly when a negative cycle is reachable from , and otherwise returns for every .
Proof. Suppose has no negative cycle. No call in line L4 reports a cycle (Lemma 7). After the last call, with , every , so and line L5 passes. Dijkstra in line L6 is correct for nonnegative weights and gives a tree whose path from to each is simple and shortest under . Let be a simple shortest path from to under , which exists as there is no negative cycle. By Fact 1, for every path from to , , where is its number of arcs. From ,
so , and since both are integers, . Hence , which is the value returned.
Suppose has a negative cycle , so . If some call in line L4 reports a cycle, the answer is right. Otherwise, after the loop, by Facts 1 and 2, , so some arc of has 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.
Termination. Line L3 runs 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.