Proofs: Shortest Path Faster Algorithm
These are the human proofs of Shortest Path Faster Algorithm, at full length.
SPFA returns the distances or reports a reachable negative cycle full
This proof shows: correctness, termination.
Let have vertices, real weights and source . Line numbers refer to the pseudocode of SPFA on the main page. A label change is one execution of line L7.
Walks behind the labels. Attach to each vertex with a walk from to : at the start , and at each label change of through the arc , set to the current followed by . These walks are only for the proof.
Fact 1. At all times, for every with , and is the number of arcs of .
Proof. True at the start, since and . A label change of sets and , which by induction are the weight and the arc count of followed by . Other labels do not change.
Fact 2. Labels only decrease, and each label change of makes strictly smaller.
Proof. Line L7 runs only when the test of line L6 holds.
Lemma 1 (repeated vertices). If at some time has with , then the closed walk has negative weight, and it is reachable from .
Proof. By construction, the prefix of was the walk at some time , and was the label at that time, by Fact 1. Here is the time of the label change that created , or the start for , and , because was built from the walk that existed at that moment. The vertex has a label change at time (if , then is a label change). By Fact 2, the label of after time is strictly smaller than its label just before , which is at most its label at time . So . Since is followed by the closed walk , . starts at , which reaches from .
A negative closed walk contains a negative cycle (see negative cycle), so we get:
Corollary 1. If line L8 returns "negative cycle", then a negative cycle is reachable from .
Proof. At that moment , so has at least vertices by Fact 1 and repeats one. Apply Lemma 1.
Lemma 2 (queue invariant). At the test of line L3, every arc with and has in .
Proof. At the start only is finite, and is in . Consider one iteration that dequeues . Line L5 scans every arc out of , and after its relaxation each such arc satisfies ; later relaxations in the same scan only lower other labels with , or lower itself through a self-loop. In the last case line L9 puts back in . Every vertex whose label goes down is in after line L9, which covers the arcs out of that may now be violated. Lowering cannot violate an arc into . So the invariant holds again at the next test.
Lemma 3 (termination). SPFA terminates.
Proof. While line L8 has not returned, every , so by Fact 1 every finite label is the weight of a walk from with at most arcs. There are finitely many such walks, so the labels take values in a finite set. By Fact 2 each vertex has finitely many label changes, and so line L9 adds finitely many vertices to . Each iteration of line L3 removes one vertex from , so the loop ends.
Theorem. If a negative cycle is reachable from , SPFA returns "negative cycle". Otherwise it returns for every , with exactly when is not reachable from , and is a shortest-path tree.
Proof. By Lemma 3, SPFA either returns at line L8 or reaches line L10 with empty. Suppose it reaches line L10. By Fact 1 only reachable vertices have finite labels, and every reachable vertex has one: along a path from , an arc with would be violated, against Lemma 2. So, by Lemma 2, holds on every arc whose tail is reachable.
No negative cycle is reachable. Let be a cycle through a reachable vertex. All its vertices are reachable, so their labels are real, and summing over gives .
The labels are the distances. The label never changed: a change would make the weight of a closed walk from by Fact 1, a negative closed walk reachable from , which contains a negative cycle reachable from . So , each finite label is the weight of a walk from , and the triangle inequality holds on every arc with a reachable tail. Theorem B of the optimality conditions gives .
So if a negative cycle is reachable from , SPFA cannot reach line L10, and it returns "negative cycle". If no negative cycle is reachable, Corollary 1 shows that line L8 never returns, and the run ends at line L10 with the distances. For the tree: each parent arc was tight when it was set, can only have gone down since then, and by the optimality conditions it cannot go below ; so every parent arc is tight at the end, and with the distances final, the parents form a shortest-path tree by its predecessor-subgraph property.
Running time. Define passes: pass 0 is the dequeue of , and pass dequeues the vertices in at the end of pass . Claim: a label set during pass has . For , , so . For pass , the dequeued vertex entered during pass , so its current label was set during pass or later, and by induction on time; hence . So a label set during pass has and line L8 returns. There are at most passes. Each vertex is in at most once at a time, so it is dequeued at most once per pass, and a pass scans each arc at most once. The total time is , where is the number of vertices reachable from , which is .