Floyd–Warshall Ready
vertices 0
edges 0
checks 0
  • Current
  • Looking at
  • Done
  • On the route

Ready

Comparisons 0 Shifts 0
Ready

Edges are written A-B:4 (letters A–F, weights 1–99, up to 12 edges) · Keyboard: ← → step · Space play / pause · R reset

How it works

Every operation, in plain steps

These are exactly the steps the visualizer animates.

One table with every distance

Floyd–Warshall keeps a V × V table d[i][j] = the shortest distance found so far from i to j. It starts with the direct edges (∞ when there is none, 0 on the diagonal). Then it lets each vertex k in turn be a possible STOP-OVER: if i → k → j is shorter than the best i → j so far, the table is updated. After every vertex has had its turn, the table holds the shortest distance between EVERY pair. A second table, next[i][j], remembers the first step of each best route so the routes themselves can be read back.

#define INF 1000000000
int d[V][V];       // d[i][j]: shortest distance from i to j found so far
int next[V][V];    // next[i][j]: the first vertex after i on that route

// start: the direct edges
for (i...) for (j...)
    d[i][j] = (i == j) ? 0 : weight(i, j);    // INF if there is no edge

// then try every vertex k as a stop-over
for (k...) for (i...) for (j...)
    if (d[i][k] + d[k][j] < d[i][j])
        d[i][j] = d[i][k] + d[k][j];

The idea

  1. d[i][j] starts as the direct edge i → j (∞ if none, 0 if i = j).
  2. Allow only vertex A as a stop-over, then A and B, then A, B and C, …
  3. After step k, d[i][j] is the best route using only the first k vertices as stop-overs.
  4. After the last vertex, nothing is left to improve: all pairs are done.

One step: stop-over k

  1. For every pair (i, j) look at three cells.
  2. Compare d[i][k] + d[k][j] with d[i][j].
  3. Shorter through k: update d[i][j] (and next[i][j]).
  4. If d[i][k] or d[k][j] is ∞ there is no route through k: skip.

Reading a route

  1. next[i][j] = the first vertex after i on the best route to j.
  2. Start at i, jump to next[i][j], then next[that][j], …
  3. Stop when you reach j: that is the whole path.
  4. d[i][j] = ∞ means there is no route at all.

Directed and undirected

  1. Undirected: every edge fills two cells (d[u][v] and d[v][u]).
  2. Directed: only d[u][v]; the table need not be symmetric.
  3. The same three loops work for both.
  4. Weights here are positive (no negative-cost edges).

All pairs vs one source

  1. Floyd–Warshall: every pair at once, O(V³).
  2. Dijkstra from every vertex: V runs of O(V²) — also O(V³).
  3. Floyd–Warshall is shorter to write: three loops.
  4. On sparse graphs, V runs of Dijkstra with a heap can be faster.

Where it is used

  1. Tables of distances between all cities.
  2. Finding the best meeting point for several people.
  3. Transitive closure: who can reach whom.
  4. Small graphs where every answer is needed.
Algorithm

Pseudocode

Language-independent steps. The visualizer shows the same logic in C, Python and Java.

1 FLOYD-WARSHALL(G)
2 for i ← 1 to V: for j ← 1 to V
3 d[i][j] ← 0 if i = j, the edge weight if there is an edge, ∞ otherwise
4 for k ← 1 to V (the stop-over)
5 for i ← 1 to V
6 for j ← 1 to V
7 if d[i][k] + d[k][j] < d[i][j] then
8 d[i][j] ← d[i][k] + d[k][j]
Complexity

How fast is each operation?

V = number of vertices. The three nested loops always run V³ times, whatever the number of edges. The table needs V² cells.

Operation Best Average Worst Speed
Floyd–Warshall (all pairs) O(V³) O(V³) O(V³) Grows with n
Memory for the distance table O(V²) O(V²) O(V²) Grows with n
Read one path from the result O(1) O(V) O(V) Grows with n

All pairs vs single source

Floyd–Warshall Dijkstra (once) Dijkstra from every vertex
Answers All pairs One source to all All pairs
Time O(V³) O(V²) (array) O(V³) (array)
Code Three nested loops A priority queue / min search Dijkstra in a loop
Negative weights Fine (no negative cycle) Not allowed Not allowed
Best for Small graphs, every answer needed One start vertex Large sparse graphs (with a heap)

The distance table for an unreachable pair

Value Meaning
0 From a vertex to itself
A number The shortest distance found so far (final after the last stop-over)
∞ No route found (yet) — it stays ∞ if the pair is not connected
Practice

Check your understanding

Answer each question — you will see the explanation straight away.

0 / 8 correct
Question 1

What is the time complexity of Floyd–Warshall?

Question 2

What does the outer loop variable k stand for?

Question 3

Before the loops start, what is d[i][j] when there is no edge from i to j (and i ≠ j)?

Question 4

Floyd–Warshall finishes with d[A][C] = ∞. What does that mean?

Question 5

How does Floyd–Warshall let you read the actual path, not just its length?

Question 6

A graph has 6 vertices. How many times does the innermost line of Floyd–Warshall run?

Question 7

When is Floyd–Warshall a good choice?

Question 8

In an UNDIRECTED graph, what is true about the finished table?

Assessment

Graphs quiz

Test yourself: 5 questions, 70% to pass. Create a free account to take the quiz, save your progress and work towards the certificate.