Interactive Graph Theory Learning
Interactive Graph Theory Learning
Guest User
Using app without sign in
All-pairs shortest path calculator
Finds shortest paths between all pairs of vertices
Select an algorithm and generate steps to begin visualization
The Floyd-Warshall algorithm computes shortest paths between every pair of vertices in a weighted graph in a single run. It is a classic example of dynamic programming on graphs and handles negative edge weights as long as there are no negative cycles.
The algorithm iterates over every vertex k and asks, for every pair (i, j), whether the path from i to j improves by passing through k. The update dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]) is applied for all pairs, growing the set of allowed intermediate vertices one at a time. Three nested loops over the vertices give O(V cubed) time and O(V squared) space, which is practical for dense graphs of up to a few thousand nodes.
Floyd-Warshall answers all-pairs distance queries in route planning, computes the transitive closure of relations, finds graph diameters, and supports arbitrage detection across all currency pairs at once. It is a favorite interview topic for testing dynamic programming intuition on graphs.
Three nested loops and one line of update. The subtlety is entirely in the loop order: k must be the outermost loop, and getting that wrong is the classic bug.
FloydWarshall(graph):
dist = V by V matrix, all infinity
for each vertex v: dist[v][v] = 0
for each edge (u,v,w): dist[u][v] = w
for k in vertices: // intermediate
for i in vertices: // source
for j in vertices: // target
if dist[i][k] + dist[k][j] < dist[i][j]:
dist[i][j] = dist[i][k] + dist[k][j]
next[i][j] = next[i][k] // for path reconstruction
// Negative cycle iff dist[v][v] < 0 for some vThe invariant is that after the iteration for k, dist[i][j] is the shortest path from i to j using only vertices from the first k as intermediates. k has to be outermost for that to hold: it is what expands the set of permitted waypoints one at a time. Putting k innermost still terminates and still produces plausible numbers, which is precisely what makes the bug so hard to catch.
Run Floyd-Warshall on a small directed graph and watch one entry improve twice as the set of allowed intermediates grows.
Example graph: Directed edges A to B (3), A to C (8), B to C (2), B to D (7), C to D (1).
Final distances from A are B 3, C 5, D 6. The A to D entry improved twice, from infinity to 10 to 6, which shows the layering directly: the k = C pass could only find the better route because the k = B pass had already improved A to C. That dependency is why k must be the outer loop.
Time: O(V^3) · Space: O(V^2)
Three nested loops over all vertices give exactly V cubed iterations, each doing constant work. There is no early exit and no dependence on the number of edges, so the algorithm costs the same on a sparse graph as on a dense one. Space is the V by V distance matrix, plus a second matrix if you want to reconstruct paths rather than just their lengths. In practice V cubed is fine up to a few thousand vertices; at 5,000 it is 125 billion operations and running Dijkstra from every vertex, at O(V·E·log V), becomes the better choice on sparse graphs.
Floyd-Warshall wins on density and simplicity, and loses badly on sparse graphs at scale.
| Alternative | Prefer it when | Cost |
|---|---|---|
| Dijkstra from every vertex | Sparse graph, no negative weights. Much faster when E is far below V squared. | O(V·E·log V) |
| Johnson’s algorithm | Sparse graph with negative weights. Reweights with Bellman-Ford, then runs Dijkstra from each vertex. | O(V·E + V^2·log V) |
| BFS from every vertex | The graph is unweighted, so all-pairs hop counts are all you need. | O(V·(V + E)) |
| Transitive closure | You only need reachability, not distance. The same triple loop with boolean OR, which is Warshall’s original algorithm. | O(V^3) |
Read the full article: Shortest Path Algorithms Explained
Related algorithms: Dijkstra's Algorithm, Bellman-Ford Algorithm