Proofs: D'Esopo-Pape algorithm
These are the human proofs of D'Esopo-Pape algorithm, at full length.
The D'Esopo-Pape algorithm with edge counts is correct and ends full
This proof shows: correctness, termination.
Claim. Let be a directed graph with real weights and a source . DESOPO-PAPE ends on every input. It returns NEGATIVE-CYCLE if and only if a cycle of negative weight is reachable from . Otherwise it returns for every (with for vertices not reachable from ), and for each with , .
None of the steps uses the position at which a vertex enters the deque; they hold for any rule that puts a vertex into the deque when its label drops and it is not already there.
Step 1: labels are walk weights. Number the label changes in time order. To each finite label assign a walk: for the initial , and when line L7 sets , let be the current walk of followed by the edge . By induction, at all times and is the number of edges of . In particular whenever no negative cycle is reachable from , since then every walk from to weighs at least .
Step 2: a repeated vertex means a negative cycle. Every prefix of that ends at a vertex is the walk as it was at some earlier label setting of , and the settings along are in increasing time from the start to the end. Suppose occurs twice in , and the two prefixes ending there correspond to settings at times . Then for a closed walk from , and . Each setting strictly lowers the label, so , and . A closed walk of negative weight splits into simple cycles, one of which is negative, and it is reachable from along .
Step 3: soundness of the report. Line L7 returns NEGATIVE-CYCLE only when , so has at least vertex occurrences and repeats a vertex. By Step 2 a negative cycle is reachable from .
Step 4: termination. As long as no report happens, every has at most edges, so every label value is the weight of one of the finitely many walks from with at most edges. Labels only decrease, strictly, so each vertex has finitely many label changes. Each entry into the deque, apart from the first entry of , follows a label change, and each pass of line L3 removes one entry. So the loop ends.
Step 5: an empty deque means no tense edge. Call tense if and . Invariant: every tense edge has in the deque. At the start only has a finite label and is in the deque. Scanning in lines L5 to L9 relaxes each edge out of , after which none of them is tense, as long as is unchanged; if a self-loop lowers during the scan, is put back in the deque by line L9. An edge can become tense only when drops (a drop of cannot make it tense), and line L7 to L9 then puts into the deque unless it is already there. So the invariant holds, and when the loop ends with an empty deque, no edge is tense.
Step 6: no negative cycle gives the distances. Suppose no negative cycle is reachable from . By Step 3 there is no report, and by Step 4 the loop ends. : the label of is a walk weight from to , at least by Step 1, and it starts at 0. For reachable from , take a shortest path . By Step 5, for each , once , which holds by induction from . Adding gives , and with Step 1, . A vertex that is not reachable never gets a finite label, since a finite label is the weight of a walk from . For the predecessors: when was last set, , and the edge is not tense at the end, so equality holds.
Step 7: a reachable negative cycle is reported. Suppose a negative cycle is reachable from , and suppose the algorithm returns distances. By Step 4 it ended with an empty deque, so by Step 5 no edge is tense. Every vertex on a path from then has a finite label, by induction along the path as in Step 6, so all have finite labels and for each (indices modulo ). Adding these inequalities, the labels cancel and , a contradiction. So the algorithm returns NEGATIVE-CYCLE.
Steps 3, 4, 6 and 7 give the claim.