All-Pairs Shortest Paths
Floyd–Warshall, min-plus products and the APSP conjecture.
Floyd–Warshall's three nested loops, from 1962, are still essentially the best way to compute all-pairs shortest paths on dense weighted graphs: every algorithm found since shaves off less than any polynomial factor. That stubbornness hardened into a hypothesis, the APSP conjecture, and many other problems turned out to be exactly as hard.
What you’ll learn
- Prove Floyd–Warshall correct by dynamic programming over intermediate vertices, explain why one matrix updated in place suffices, and detect negative cycles.
- Express all-pairs shortest paths through the min-plus matrix product, compute it by repeated squaring, and explain why fast matrix multiplication does not apply directly.
- Derive Seidel's algorithm for unweighted undirected graphs.
- State the APSP conjecture, Williams's algorithm, and the subcubic equivalences of Vassilevska Williams and Williams.
Floyd–Warshall, proved
Without negative cycles, a best path from to through can be taken simple. If it avoids , its intermediate vertices lie in :
If it passes through , it does so once; splitting it there leaves two paths with intermediate vertices in :
Both candidates are weights of real walks from to through , and without negative cycles no walk beats the best simple path. So the minimum of the two is exact:
One matrix suffices. During round , row and column keep their values, because . So overwriting entries in place reads exactly what the two-matrix version reads:
Negative cycles. If a negative cycle passes through , the recurrence eventually finds it as a path from back to , and ends negative; pairs connected through such a cycle have no shortest path. The in-place argument then fails as well, since is possible, and entries can grow very negative, so with fixed-width integers stop as soon as a diagonal entry turns negative. Replacing by (or, and) gives Warshall's transitive closure, which runs in with -bit words using bitsets.
In Floyd–Warshall, what does hold?
- The weight of a shortest path from to with at most 2 edges
- The weight of a shortest path from to that passes through vertex 2
- The weight of the second-shortest path from to
- The weight of a shortest path from to whose intermediate vertices are among vertices 1 and 2
Show the answer
D. The weight of a shortest path from to whose intermediate vertices are among vertices 1 and 2
Floyd–Warshall's index restricts which vertices may be used in between, not how many edges a path has. A path counted in may use vertex 1, vertex 2, both or neither, and it has at most 3 edges.
Min-plus products
By induction on , the -th min-plus power of holds the best paths with at most edges (the zeros on the diagonal let a path "wait"):
Without negative cycles, shortest paths have at most edges, and is associative, so repeated squaring reaches them:
Each product costs naively, and squarings suffice:
How many min-plus squarings does a graph with vertices need, at least, to be sure of having every shortest path?
Show the answer
10
Shortest paths have at most edges, and squarings cover paths of up to edges: is too few and is enough. Ten products of steps each is worse than Floyd–Warshall's single , but it reduces APSP to matrix products, which is the point.
Conversely, one min-plus product is an all-pairs problem: put the rows , the middle indices and the columns in three layers, with edges of weight and of weight . The distance from to is then . So APSP and the min-plus product have the same complexity up to a logarithmic factor: an algorithm for either one is an algorithm for both.
Seidel: unweighted graphs at matrix-multiplication speed
For connected, undirected graphs without weights, Seidel (1995) computes all distances with integer matrix products, in time. The idea: square the graph, solve the squared graph recursively, and recover each distance from half of it.
Square the graph. joins whenever their distance in is at most 2, so its adjacency matrix comes from one product . Distances halve, rounded up:
Recurse. The diameter halves at every level, so after at most levels the graph is complete and every distance is 1:
Recover the parity. is or . Every neighbor of has , and the neighbor before on a shortest path has . If is even, every neighbor has ; if it is odd, every neighbor has and that one has . So
The sums for all pairs form one integer product , so each level costs :
def seidel(A):
"""Distances of a connected, undirected, unweighted graph; A is its 0/1 adjacency matrix."""
n = len(A)
if all(A[i][j] or i == j for i in range(n) for j in range(n)):
return [[0 if i == j else 1 for j in range(n)] for i in range(n)] # complete graph
A2 = matmul(A, A)
B = [[1 if i != j and (A[i][j] or A2[i][j]) else 0 for j in range(n)] for i in range(n)]
T = seidel(B) # distances in G^2: T = ceil(D / 2)
X = matmul(T, A) # X[i][j] = sum of T[i][k] over the neighbors k of j
deg = [sum(row) for row in A]
return [[2 * T[i][j] - (X[i][j] < T[i][j] * deg[j]) for j in range(n)] for i in range(n)]
def matmul(P, Q): # stand-in for a fast O(n^omega) multiplication
n = len(P)
return [[sum(P[i][k] * Q[k][j] for k in range(n)) for j in range(n)] for i in range(n)]With this naive matmul the cost is ; the point is that any fast multiplication can be plugged in. Seidel's algorithm returns distances only: recovering the paths needs a "witness" for each entry of the products, which randomized techniques find with only polylogarithmic overhead.
The APSP conjecture
Fredman (1976) showed that APSP has decision trees of depth only , so cubic lower bounds cannot be proved by counting comparisons, and he turned this into an algorithm slightly faster than , by a factor of about . A long series of papers shaved further logarithmic factors. Williams (STOC 2014) then found a randomized algorithm running in time, faster than for every constant , by representing the min-plus product with low-degree polynomials borrowed from circuit complexity; Chan and Williams (SODA 2016) made it deterministic.
A min-plus product is an APSP instance on the three-layer graph, so a fast APSP algorithm multiplies fast:
One min-plus product finds a negative triangle:
The surprise is the converse: a truly subcubic algorithm for negative triangle, which answers a single yes-or-no question, would give truly subcubic min-plus products and APSP.
This is how fine-grained complexity works: it cannot prove that APSP needs cubic time, but it ties many problems to a few conjectures, so that a lower bound for one is a lower bound for all, and an algorithm for one is an algorithm for all. The lesson Fine-Grained Complexity develops the framework, alongside SETH and 3SUM.
Key takeaways
- Floyd–Warshall: holds the best paths with intermediate vertices in ; the loop is outermost, one matrix suffices because row and column stay fixed in round , and a negative diagonal entry reveals a negative cycle.
- APSP is the min-plus power , reached by squarings, and one min-plus product is an APSP instance; fast matrix multiplication does not apply directly because has no inverse, though small integer weights can be encoded.
- Seidel's algorithm solves unweighted undirected APSP in : recurse on , whose distances are , then fix each parity with one integer product.
- The APSP conjecture says no algorithm exists; the best known is (Williams, 2014), and negative triangle, min-plus product and metricity checking are subcubic-equivalent to APSP (Vassilevska Williams & Williams).
Check yourself
In the interactive lesson these are scored with your confidence, and what you miss comes back for review.
In Seidel's algorithm, the distance between and in the squared graph is . What can their distance in be?
- 5 or 6
- 6 only
- 3 or 4
- 1 or 2
Show the answer
A. 5 or 6
, so . The algorithm decides between them with the neighbors of : if , the distance is odd, so ; otherwise .
Repeated min-plus squaring costs , more than Floyd–Warshall. Why does it matter anyway?
- It is faster in practice because of better cache behavior
- It can handle negative cycles, which Floyd–Warshall cannot
- It reduces APSP to min-plus products, so any faster product algorithm, such as Williams's, immediately speeds up APSP
- It needs asymptotically less memory
Show the answer
C. It reduces APSP to min-plus products, so any faster product algorithm, such as Williams's, immediately speeds up APSP
Repeated squaring makes the min-plus product the core of APSP, and the three-layer construction shows the converse. Faster products, such as Williams's algorithm, therefore transfer directly to all-pairs shortest paths.
Why can't Strassen-style fast matrix multiplication compute min-plus products directly?
- Because the min-plus product is not associative
- Because has no subtraction, and fast matrix multiplication relies on cancelling terms
- Because min-plus matrices are never square
- Because fast matrix multiplication only works modulo 2
Show the answer
B. Because has no subtraction, and fast matrix multiplication relies on cancelling terms
Strassen's seven products combine terms that are later subtracted away; with there is no way to undo a combination. For small integer weights, encoding entries as powers of restores a ring and gives , but for general weights no such trick is known.
Which statement about the APSP conjecture is correct, as of 2025?
- It has been proved, in the decision-tree model
- Williams's 2014 algorithm refuted it
- It states that APSP requires time on the word RAM
- It says no algorithm runs in for any , which is consistent with Williams's algorithm
Show the answer
D. It says no algorithm runs in for any , which is consistent with Williams's algorithm
Williams's algorithm beats for every constant , but grows more slowly than any , so the conjecture survives. It remains unproven.
Further reading
- R. W. Floyd, "Algorithm 97: Shortest path," Communications of the ACM 5(6) (1962), 345.
- M. L. Fredman, "New bounds on the complexity of the shortest path problem," SIAM Journal on Computing 5(1) (1976), 83–89.
- R. Seidel, "On the all-pairs-shortest-path problem in unweighted undirected graphs," Journal of Computer and System Sciences 51(3) (1995), 400–403.
- R. Williams, "Faster all-pairs shortest paths via circuit complexity," STOC (2014), 664–673.
- V. Vassilevska Williams & R. Williams, "Subcubic equivalences between path, matrix, and triangle problems," Journal of the ACM 65(5) (2018), article 27; preliminary version at FOCS 2010.