Algorithms · Visualized
Floyd–Warshall
Graph Algorithms
Traverse, order, and find shortest paths and spanning trees on graphs — the machinery behind maps, networks, schedulers and dependency resolvers.
Floyd–Warshall computes the shortest distance between every ordered pair of nodes in one triple loop. Time: O(V³) average, O(V³) worst; space: O(V²) for the distance matrix.
Big-O: Traversals O(V + E); Dijkstra O(E log V) with a heap; Bellman–Ford O(V·E); Floyd–Warshall O(V³); MST O(E log E).
Floyd–Warshall (All-Pairs Shortest Paths)
Graph AlgorithmsFloyd–Warshall computes the shortest distance between every ordered pair of nodes in one triple loop. The idea: process nodes as allowed intermediate stops one at a time — after allowing node k, dist[i][j] is the shortest path using only {0..k} as intermediates. Dynamic programming over the 'via' node does all the work in O(V³), which beats running Dijkstra V times on dense graphs.
How Floyd–Warshall Works
- Initialise dist[i][j] with the direct edge weight, 0 on the diagonal, ∞ where no edge exists.
- For each k from 0 to V−1, declare k a legal intermediate stop.
- For every pair (i, j): if dist[i][k] + dist[k][j] < dist[i][j], route i→j through k and update.
- The order of updates within a round never matters — each round only refines with one new via-node.
- After V rounds dist[i][j] holds the true shortest distance for every pair; negative cycles show up as dist[i][i] < 0.
Floyd–Warshall Worked Example
Input: {graph: A—B(4), A—C(2), B—C(1), B—D(5), C—D(8), C—E(10), D—E(2), D—F(6), E—F(3), start: —}
After allowing A, B and C as intermediates, dist[A][D] becomes 2+1+5 = 8 (A→C→B→D) instead of the direct 8... then via D: dist[A][E] = 8+2 = 10, dist[A][F] = 14. Allowing E next: dist[A][F] improves to 10+3 = 13. Six rounds later the whole 6×6 matrix is exact.
| A | B | C | D | E | F | |
|---|---|---|---|---|---|---|
| A | 0 | 4 | 2 | ∞ | ∞ | ∞ |
| B | 4 | 0 | 1 | 5 | ∞ | ∞ |
| C | 2 | 1 | 0 | 8 | 10 | ∞ |
| D | ∞ | 5 | 8 | 0 | 2 | 6 |
| E | ∞ | ∞ | 10 | 2 | 0 | 3 |
| F | ∞ | ∞ | ∞ | 6 | 3 | 0 |
def floyd_warshall(n, edges):
dist = [[float('inf')] * n for _ in range(n)]
for i in range(n):
dist[i][i] = 0
for u, v, w in edges:
dist[u][v] = w
dist[v][u] = w
for k in range(n): # allow k as an intermediate stop
for i in range(n):
for j in range(n):
if dist[i][k] + dist[k][j] < dist[i][j]:
dist[i][j] = dist[i][k] + dist[k][j]
return distBehind the Scenes Execution
Press ▶ Run in the visualizer — the call stack, registers and memory will replay here in sync with the code, line by line.
Floyd–Warshall Time & Space Complexity
When to Use Floyd–Warshall
- All-pairs queries on small dense graphs — routing tables, flight matrices, game maps under ~500 nodes.
- Transitive closure and detecting negative cycles via the diagonal in the same pass.
Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Keep the momentum going
Step through a related structure, pattern or course — the ideas reinforce each other and stick faster.