Proofs: Floyd-Warshall algorithm
These are the human proofs of Floyd-Warshall algorithm, at full length.
Floyd-Warshall computes all distances or detects a negative cycle full
This proof shows: correctness, termination.
Claim. Let be a directed graph on the vertices with real weights. At the end of FLOYD-WARSHALL:
- each finite is the weight of a walk from to (the empty walk when and );
- for each path from to with , and for each cycle through with no repeated vertex.
Consequently, if has no negative cycle, then for all ; and if has a negative cycle, then for some . Also, for some only if has a negative cycle.
Termination. The loops have the fixed bounds , then , so the algorithm ends.
Notation. A path inside is a path from to whose inner vertices (all but the first and the last) are in . A cycle through inside is a cycle with no repeated vertex other than its start and end , whose other vertices are in . Let .
Invariant. At the end of pass of line L3 ( means after line L2):
- (W) each finite is the weight of a walk from to ;
- (P) for each path from to inside , and for each cycle through inside .
Base, . Line L1 sets , the empty walk, and elsewhere. Line L2 lowers to the least weight of an edge from to , a walk of one edge. A path inside from is one edge, and a cycle through inside is a self-loop; line L2 covers both, and from line L1. So (W) and (P) hold.
Maintenance, from to . Entries only decrease in pass , so (P) for paths and cycles inside stays true. Line L7 sets with both terms finite (else the test of line L6 fails, with ). By (W), these are the weights of walks and ; joined, they form a walk of that weight. So (W) holds at each step of the pass.
For (P), take a path from to inside that passes through as an inner vertex (the paths that do not are inside ). Since repeats no vertex, it splits at into a path from to and a path from to , both inside . By (P) for , and at the start of pass , and these entries can only be smaller when line L6 tests the pair . After that test, , and it only decreases later. The same split works for a cycle through inside that contains : it splits into a path from to and a path from to , both inside , so after the test of the pair , . If , a cycle through inside has its other vertices in , so it is already inside . Thus (P) holds after pass .
At the end (). Every path and every cycle is inside , so parts 1 and 2 of the claim hold.
No negative cycle. Then every closed walk has weight at least 0, because it splits into cycles. A walk from to therefore weighs at least , so (W) gives . A shortest walk can be taken to be a path, since removing a closed sub-walk does not increase the weight, so (P) gives for . For , , and . So .
A negative cycle. A closed walk of negative weight contains a cycle with no repeated vertex and negative weight, because a closed walk splits into such cycles and their weights add up to its weight. Let be such a cycle and a vertex of . By part 2, .
Conversely, if , then by part 1 it is the weight of a closed walk from (not the empty one, which weighs 0), so a negative cycle exists.
The next-hop matrix. This proof covers the matrix . The matching statement for the predecessor matrix, that it encodes shortest paths when no negative cycle exists, is proved in CLRS, Section 23.2, and the next-hop matrix is its mirror image.