Proofs: Generic shortest path algorithm
These are the human proofs of Generic shortest path algorithm, at full length.
The generic relaxation method is correct for every order full
This proof shows: correctness, termination.
Claim. Let be a directed graph with real weights and a source , and fix any rule for line L4. GENERIC-SSSP ends. It returns NEGATIVE-CYCLE if and only if a cycle of negative weight is reachable from . Otherwise it returns for every , with for the vertices that does not reach, and for every with .
Step 1: labels are walk weights. Assign to each finite label a walk: for , and when line L5 sets , let be the current followed by the edge . By induction on the steps, and is the number of edges of at all times. Hence, if no negative cycle is reachable from , at all times.
Step 2: a repeated vertex gives a negative cycle. Every prefix of ending at a vertex is as it was at an earlier setting of , and these settings occur in increasing time along . If occurs twice, at settings , then for a closed walk , and . Each setting strictly lowers the label (line L3 requires a tense edge), so . A closed walk of negative weight contains a simple cycle of negative weight, and it is reachable from along .
Step 3: the report is sound. Line L6 fires only when has at least edges, hence at least vertex occurrences, and so repeats a vertex. By Step 2, a negative cycle is reachable from .
Step 4: termination. As long as line L6 has not fired, every has at most edges, so every label is the weight of one of the finitely many walks from with at most edges. Each step strictly lowers one label, so the number of steps is finite. Each step either fires line L6 or loops back to line L3, which ends the loop when no tense edge is left. So the algorithm ends.
Step 5: no negative cycle gives the distances. Suppose no negative cycle is reachable from . By Step 3, line L6 never fires, so by Step 4 the loop ends with no tense edge: for every edge with . The label is the weight of a closed walk from , at least by Step 1, and it starts at 0, so . For a reachable with a shortest path , induction along the path gives and , and adding up, . With Step 1, . An unreachable has no walk from , so its label stays . For the predecessor of : when was last set, since labels only fall, and the edge is not tense at the end, so equality holds.
Step 6: a reachable negative cycle is reported. Suppose a negative cycle is reachable from and the algorithm returns labels. By Step 4 the loop ended with no tense edge. As in Step 5, every vertex on a path from has a finite label, so for each (indices modulo ); adding these, the labels cancel and , a contradiction. So the algorithm returns NEGATIVE-CYCLE.
Steps 3 to 6 give the claim.