Proofs: Bellman-Ford algorithm
These are the human proofs of Bellman-Ford algorithm, at full length.
Bellman-Ford returns the distances or a negative cycle full
This proof shows: correctness, termination.
Claim. Let be a directed graph with vertices, real weights , and a source . BELLMAN-FORD ends. If no cycle of negative weight is reachable from , it returns nil for the cycle, for each , and for each with . If such a cycle is reachable, it returns a cycle of of negative weight.
Termination. Each loop has a fixed bound: passes of edges, then at most edges, steps of line L11 and at most steps of line L12 (justified in Step 4). So the algorithm ends.
Notation. is the least weight of a walk from to with at most edges ( if there is none). is the value of at the end of pass , with the values after line L2.
Step 1: every finite estimate is the weight of a walk, so . This is the upper-bound property: at the start only is finite, and line L7 sets with finite, the weight of a walk to plus one edge. The same holds for the relaxation in line L11.
Step 2: for (as long as the passes run). By induction on . For , and for . For , take a walk to with at most edges and weight (if there is nothing to show). If has fewer than edges, by the hypothesis, because estimates never increase. Else is a walk to some with edges, then the edge . By the hypothesis, , so at the moment pass relaxes , is finite. After line L6 and L7, . Later steps of the pass only lower . So . If line L8 stops the loop early after pass , then no edge was tense in pass , so the values do not change in any later pass, and the bound holds for all ; Step 3 uses this.
Step 3: no reachable negative cycle. Then for each reachable , a shortest walk can be chosen without a repeated vertex: removing a cycle from a walk does not increase its weight, because each cycle has weight at least . So . By Steps 1 and 2, at the end of the passes, also when line L8 stops early. Now each edge with has , so the check of line L10 fails for every edge, and line L13 returns nil. When line L8 returns, the same values are returned. For with : when line L7 last set , it set with at that time; afterwards can only decrease, so , and if it were strictly less, the edge would be tense at the end, which we excluded. So . Following from then gives a walk whose weight telescopes; since and all values are finite, the walk ends at (a predecessor cycle would have weight by telescoping, but Step 5 shows that every predecessor cycle has negative weight, so there is none). Thus the predecessor edges form a shortest-path tree.
Step 4: a reachable negative cycle is always found. Let be a negative cycle reachable from . Then pass cannot have stopped early by line L8: that would mean no edge is tense, and the page on relaxation proves that some edge is tense after any finite sequence of relaxations when a negative cycle is reachable. For the same reason the check of lines L9 and L10 finds a tense edge . Line L11 relaxes it, so now by Step 2.
We show that following from never reaches nil. Throughout the run, each vertex with nil satisfies
because the assignment of makes it an equality, later only can fall while stays, and a change of also sets again. Also $\pi[s] = $ nil implies , because changes only together with . Now assume the chain reaches nil without repeating a vertex, at with $\pi[x_r] = $ nil. All have finite estimates, so (every other vertex with a finite estimate has a predecessor), and . Adding (A) along the chain gives , where is the path with edges. So , a contradiction. Hence the chain repeats a vertex. A chain with at most distinct vertices enters its cycle after at most steps, so after the steps of line L11 the vertex lies on a cycle of predecessor edges, and line L12 follows from back to in at most steps.
Step 5: every cycle of predecessor edges has negative weight. Consider the moment a cycle of predecessor edges () first appears. It is created by a relaxation that sets for one . Just before it, the edge was tense: . For the other edges of , (A) gives . All these values are finite. Adding the inequalities, the sum of the values is the same on both sides, so . The weight of does not depend on , so stays negative while it exists. With Step 4, the cycle that line L12 returns has negative weight.