The Floyd-Warshall algorithm [00RO]
- November 9, 2026
Introduction: all-pairs shortest paths and negative cycles [00RP]
- November 9, 2026
Recall that Dijkstra’s algorithm solves the single-source shortest path problem (i.e. it finds the shortest path from a single start vertex to every other vertex) for weighted, directed graphs, as long as all edge weights are positive. Today we will consider an algorithm to solve the all-pairs shortest path problem (that is, to find shortest paths between all possible pairs of vertices) on directed graphs with arbitrary (possibly negative) weights.
Of course, negative cycles can pose a problem: if there is a directed cycle whose total weight is negative, then we can keep decreasing the path weight forever by just going around the cycle. In that case “shortest” paths may not be well-defined. The algorithm we consider will also be able to detect this situation.
The All-Pairs Shortest Paths problem [00RQ]
- November 9, 2026
Input: a directed, weighted graph G = (V,E) with vertices numbered 1 \dots n. Output: an n \times n matrix s where s[u,v] records the length of the shortest path from u to v, or an indication that the graph has negative cycles.
Ideally we also want to be able to recover the actual shortest path between any two vertices u and v.
Recurrence for all-pairs shortest paths [00RR]
- November 9, 2026
We want to come up with a recurrence. As is typical, we add another parameter which we use to restrict the problem. In this case, we'll add a parameter k that means we are only allowed to use vertices 1 \dots k as intermediate notes. That is, we will let s[u,v,k] denote the length of the shortest path from u to v using only vertices 1 \dots k as intermediate nodes on the path.
As base cases, we consider k = 0, that is, we are not allowed to use any intermediate vertices. We have s[u,v,0] = \begin {cases} 0 & u = v \\ w_{uv} & (u,v) \in E \\ \infty & \text {otherwise} \end {cases} That is, if we are not allowed to use any intermediate vertices, the shortest path from a vertex to itself is 0, the shortest path from u to v along an edge is the weight of the edge, and the shortest path between vertices with no edge is \infty .
(Note that we can't say s[u,u,k] = 0 for k > 0, because there might be negative edges giving a "shorter" path from u back to itself!)
Now, what about the recursive case? Suppose for some k \geq 1 we already know s[u,v,k-1] for all u,v. Then how can we compute s[u,v,k]? There are two possibilities: either the best path from u to v goes through k, or it doesn’t.
- It could be that allowing vertex k does not help, in which case s[u,v,k] = s[u,v,k-1].
- If it does help, the best path from u to v will consist of first taking the best path from u to k (using only vertices 1 \dots k-1), followed by the best path from k to v: s[u,v,k] = s[u,k,k-1] + s[k,v,k-1]. (Note: the best path from u to v won’t go through vertex k twice, unless there is a negative cycle!)
The optimal cost will simply be the minimum of these two. That is, all together, we have \begin {align*} s[u,v,0] &= \begin {cases} 0 & u = v \\ w_{uv} & (u,v) \in E \\ \infty & \text {otherwise} \end {cases} \\ s[u,v,k] &= \min \begin {cases} s[u,v,k-1] \\ s[u,k,k-1] + s[k,v,k-1] \end {cases} \end {align*}
Reconstructing all-pairs shortest paths [00RS]
- November 9, 2026
To be able to reconstruct paths after the fact, we could do something similar to what we usually do: at each step we take a minimum over two choices, so we could keep a parallel 3D table \mathit {use}[u,v,k] that records whether we should use vertex k when trying to go from u to v using only vertices 1 \dots k. We could then use this to reconstruct the optimal path from u to v as follows: \mathit {bestpath}(u,v,k) = \begin {cases} \mathit {bestpath}(u,k,k-1) + [k] + \mathit {bestpath}(k,v,k-1) \\ \mathit {bestpath}(u,v,k-1) \end {cases} depending on whether \mathit {use}[u,v,k] is true (with some appropriate base cases as well).
However, for this problem it turns out we can do something a bit more clever using only a 2D array. In particular, let \mathit {next}[u,v] denote the next vertex after u in the shortest path from u to v that we have found so far. Then in the first case, when we set s[u,v,k] to s[u,v,k-1], we do not change \mathit {next}[u,v]; in the other case, we can update it as \mathit {next}[u,v] = \mathit {next}[u,k].
However, for this problem it turns out we can do something a bit more clever using only a 2D array. In particular, let \mathit {next}[u,v] denote the next vertex after u in the shortest path from u to v that we have found so far. Then in the first case, when we set s[u,v,k] to s[u,v,k-1], we do not change \mathit {next}[u,v]; in the other case, we can update it as \mathit {next}[u,v] = \mathit {next}[u,k]. Also, we don’t actually need to keep a 3D array for s. We can simply keep overwriting the same 2D array on each iteration of the loop. If we do that, it is no longer true that s[u,v] is exactly the shortest distance using only vertices 1 \dots k on the kth iteration—but if not it can only be shorter (if there are negative edges).
Algorithm Floyd-Warshall [00RT]
- November 9, 2026
After the algorithm is finished, to reconstruct the shortest path from u to v, we simply look up each step along the path using the \mathit {next} matrix. First we look up \mathit {next}[u,v] = u_2, then we look up \mathit {next}[u_2,v] = u_3, and so on, until reaching v.
Running time for Floyd-Warshall [00RU]
- November 9, 2026
What is the running time? It’s obviously \Theta (V^3)—just three nested loops from 1 \dots n. (Which seems kind of amazing given that there could be \Theta (V^2) edges and we need to find shortest paths between every pair of vertices, of which there are also \Theta (V^2).)
Floyd-Warshall and negative cycles [00RV]
- November 9, 2026
What about negative cycles? Well, just look at s[u,u]: u is part of a negative cycle if and only if s[u,u] (the length of the shortest path from u back to itself) is negative. Hence we can check whether there are any negative cycles in the graph in \Theta (n) time, by scanning along the diagonal of the matrix and looking for any negative numbers. It turns out with some extra work this can be extended to compute exactly which paths have some negative cycle along the way (and hence can be made as small as we want by going around the negative cycle) and which do not.
Generalizing Floyd-Warshall [00RW]
- November 9, 2026
As a fun aside, this algorithm is really cool because it can actually be generalized to work over any semiring instead of just (\min , +) (technically, any star-semiring), and it turns out that by appropriate choice of semiring, (a slight generalization of) the same algorithm can be used to do a great many things, such as find most reliable paths or largest capacity paths, count the number of shortest paths or total number of paths or even compute regular expressions for all possible shortest paths, compute transitive closures, invert matrices, solve linear systems of equations, or convert a DFA into a regular expression.