Proofs: Cycle-canceling algorithm
These are the human proofs of Cycle-canceling algorithm, at full length.
Cycle canceling returns a minimum-cost maximum flow full
This proof shows: correctness, termination.
Let be a directed graph with integer capacities , integer costs and . Write for the residual capacities, , for the residual graph of arcs with , with and , and .
Claim 1 (canceling keeps a maximum flow and lowers the cost). Let be a cycle of and . Pushing around as in line L8 gives residual capacities that are nonnegative and satisfy , so stays within its bounds. At each vertex of one arc of enters and one leaves, so the net flow at every vertex, including and , is unchanged: is still a maximum flow. Pushing on raises by and changes the cost by ; on it lowers by and changes the cost by . So the cost changes by .
Claim 2 (negative cycle optimality). A maximum flow has least cost among maximum flows if and only if has no cycle of negative cost.
Proof. If has a negative cycle, Claim 1 gives a cheaper maximum flow. Conversely, let be any maximum flow and set and . Then arc by arc, is positive only on arcs of , and since and have the same net flow at every vertex (0 inside, and the common maximum value at and ), is a circulation. Its cost is . By flow decomposition, a circulation is a sum of positive multiples of cycles in its support, so is a nonnegative combination of costs of cycles of . If none is negative, .
Claim 3 (line L4 to L7 find a negative cycle exactly when one exists). Line L4 is Bellman-Ford on from a virtual source joined to every vertex by an arc of cost 0. If has no negative cycle, a shortest path from the virtual source has at most arcs, the first of which is a virtual arc of cost 0, so after rounds over the real arcs all values are final and round changes nothing. If has a negative cycle, the values along it cannot all be final, since summing around the cycle would give ; so some round changes a value, and every round up to does. By the standard lemma for Bellman-Ford (see negative cycle detection), when a value changes in round , the predecessor graph contains a cycle reachable backward from the changed vertex within steps, and every cycle of the predecessor graph has negative cost. Line L6 walks predecessor steps, which is enough to enter that cycle, and line L7 reads it. Each predecessor arc was set while it had , and Bellman-Ford does not change , so the cycle lies in .
Claim 4 (termination). All data are integers, so each is an integer, , and a negative cycle has . By Claim 1 each cancellation lowers by at least 1. Every with has , so the loop ends.
Claim 5 (optimality). Line L2 gives a maximum flow, Claim 1 keeps it maximum, and the loop ends (Claim 4) when, by Claim 3, has no negative cycle. By Claim 2 the flow returned has least cost among maximum flows.