Home/Graphs/Floyd-Warshall
Search algorithmsCTRL K

Floyd-Warshall

· all pairs · the distance matrix
/graphs/floyd-warshall
PIVOT
—/ 7
node paths may pass through
IMPROVED
0
cells that got shorter
CHECKS
0
of n³ in total
REACHABLE
16 / 49
pairs with a finite distance
FINITE
16
cells of the matrix
pivot noderow being updated · cell improvedpivot row and columnpivot label
7 nodes · 9 edges · seed 7 · 58 steps
step 0 / 57
Nodes7
floyd_warshall.ts
1
function floydWarshall(graph: Graph) {
2
  const n = graph.nodes.length;
3
  const distance = Array.from({ length: n }, (_, i) => Array.from({ length: n }, (_, j) => (i === j ? 0 : Infinity)));
4
  for (const edge of graph.edges) distance[edge.from][edge.to] = edge.weight;
5
  for (let pivot = 0; pivot < n; pivot++) {
6
    for (let i = 0; i < n; i++) {
7
      for (let j = 0; j < n; j++) {
8
        if (distance[i][pivot] + distance[pivot][j] < distance[i][j]) distance[i][j] = distance[i][pivot] + distance[pivot][j];
9
      }
10
    }
11
  }
12
  return distance;
13
}
CURRENT STEP
line 4

Start from the adjacency matrix: 0 on the diagonal, the edge weight where an edge exists, ∞ elsewhere.

// how it works

Understanding Floyd-Warshall

Floyd-Warshall · three nested loops and every pair is done
01

The idea

Floyd-Warshall computes the shortest distance between every pair of nodes at once, in a matrix. It adds the nodes one at a time as allowed intermediates: after pivot k, the cell [i][j] holds the shortest path from i to j that only passes through the first k nodes. The update is one line: if going through k is shorter, take it.

That is dynamic programming over the set of allowed intermediates, and the matrix can be updated in place. n³ steps is a lot, but for a few hundred nodes it is simpler and often faster than running Dijkstra from every node, and it takes negative edges in its stride.

02

The four stages

1
initthe adjacency matrix, ∞ where there is no edge
2
pivotallow paths through node k
3
updatedist[i][j] ← min(dist[i][j], dist[i][k] + dist[k][j])
4
doneafter the last pivot every cell is final
03

Complexity

best
O(V³)no early exit exists
average
O(V³)the same, whatever the graph
worst
O(V³)the same
space
O(V²)the matrix, updated in place
04

Versus its siblings

EDGE RELAXATIONS · 1 000 NODES, 3 000 EDGES
dijkstra
6,000
bellman-ford
3M
floyd-warshall
1B
johnson
9M
for all pairs on a sparse graph, Johnson (Bellman-Ford once, then Dijkstra per node) wins
05

Pseudocode

FLOYDWARSHALL(graph)
  dist[i][j] ← weight of edge i → j, 0 on the diagonal, ∞ elsewhere
  for each pivot k
    for each i, for each j
      dist[i][j] ← min(dist[i][j], dist[i][k] + dist[k][j])
  return dist
06

When to use

Dense graphs of a few hundred nodes where every pair matters: distance tables, transitive closure, graph diameter.
Any semiring, not only min-plus: with OR and AND it computes reachability, with min-max the widest paths.
07

Pitfalls

The pivot loop must be the outermost; swapping the loops gives wrong answers on some graphs.
V² memory: a million nodes is a terabyte matrix.
A negative value on the diagonal after the run means a negative cycle through that node.
08

History

Bernard Roy published it in 1959 for transitive closure, Stephen Warshall in 1962 for Boolean matrices, and Robert Floyd the same year for shortest paths; the three-nested-loop form is Peter Ingerman's. It is the standard example of dynamic programming on graphs in every algorithms course.

Back to Graphs