The algorithm
- Set the source's label to 0 and every other to infinity.
- Repeat V - 1 times: relax every arc, once, in whatever order they happen to be stored.
- Stop early if a full round changes nothing, because nothing later will change either.
- Perform one more round. If anything improves, report a negative cycle and recover it from the predecessor pointers.
Theorem · The round invariantAfter i complete rounds, for every vertex v whose shortest path from the source uses at most i arcs, d[v] equals that shortest distance.
By induction on i. For i = 0 the only such vertex is the source, whose label is 0.
Assume it after i rounds and let v have a shortest path s, ..., u, v with i + 1 arcs. The prefix to u is itself a shortest path with i arcs, so d[u] is correct after round i, by hypothesis. Round i + 1 relaxes the arc from u to v at some point, and by then d[u] is at most its correct value, so afterwards d[v] is at most d[u] + w, the true shortest distance. The upper-bound invariant of the relaxation step says it is not less, so it is equal.
□
Note what the proof does not need: the arcs may be relaxed in any order within a round, and d[u] may have been improved earlier in the same round, which only helps. That is why the algorithm is indifferent to how the arcs are stored, and also why the number of rounds it actually needs is not.
Example · Five vertices, four negative arcs, and no negative cycleThe lab opens on 1>2 6, 1>3 7, 2>3 8, 2>4 5, 2>5 -4, 3>4 -3, 3>5 9, 4>2 -2, 5>1 2, 5>4 7 from vertex 1. The distances come out 0, 2, 7, 4, -2. The first round reaches 0, 2, 7, 4, 2 and the second improves only vertex 5, to -2; the third changes nothing and the run stops after 3 rounds and 30 relaxations. Floyd's all-pairs matrix, computed separately, gives the same row from vertex 1. The nearest-first schedule gives 0, 2, 7, 4, 2.
The route to vertex 2 is 1, 3, 4, 2, three arcs, at 7 - 3 - 2 = 2. Its label was already final after the first round even though its path has three arcs, because the arcs happened to be swept in an order that carried the improvement all the way along in one pass. The invariant promises correctness by round three; it does not forbid getting there sooner.
The arc order is worth a measurement of its own
Take a chain of five arcs each of weight 1, and type it backwards: 5>6 1, 4>5 1, 3>4 1, 2>3 1, 1>2 1. Each round pushes the frontier forward by exactly one arc, so the run takes 6 rounds and 20 relaxations, and the round each label became final is 0, 1, 2, 3, 4, 5. Type the same five arcs forwards and one sweep carries the improvement the whole way: 2 rounds, 10 relaxations, and every label final in round 1. Both give the distances 0, 1, 2, 3, 4, 5.
That is the cleanest example on this course of a count moving while an answer does not. Same vertices, same arcs, same weights, same distances; two thirds less work. And it is also why the bound is V - 1 rounds rather than something smaller: the backwards chain needs every one of them, so no smaller number is correct for all inputs.
The certificate
Theorem · Negative cycle detectionIf a round after the V - 1 improves any label, then some cycle reachable from the source has negative total weight, and no shortest path exists to the vertices beyond it. If no such round improves anything, every label is a true shortest distance.
The forward direction is the round invariant read backwards: if no negative cycle is reachable then every shortest path has at most V - 1 arcs, so round V - 1 finished the job and round V finds nothing. If something does improve, the improving path uses V arcs, hence repeats a vertex, hence contains a cycle; and it could only be an improvement if that cycle is negative.
Example · A negative cycle, returned as the cycleOn 1>2 1, 2>3 -3, 3>4 1, 4>2 1 the lab runs all four rounds, something changes in the last, and 16 relaxations are performed. The labels it prints, 0, -3, -5, -4, are not distances and the panel does not present them as any: they are one lap further round the loop than the previous values. What it returns instead is the cycle 2, 3, 4, 2, whose weight is -1, so every lap subtracts one more.
A flag saying “negative cycle” would leave the reader with nowhere to go. The cycle itself is actionable: in a scheduling or currency model it names the loop that must be broken, and in a debugging session it names the arc whose weight was typed with the wrong sign.
Measured against proved. On the opening graph: 3 rounds, 30 relaxations, five distances. On the backwards chain: 6 rounds, 20 relaxations. The bound is O(V E), from V - 1 rounds of E relaxations each, and it is attained — the backwards chain needs every round. What the measurements cannot tell you is that no ordering forces more than V - 1, and that is what the invariant is for.