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 Algorithms

Floyd–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

  1. Initialise dist[i][j] with the direct edge weight, 0 on the diagonal, ∞ where no edge exists.
  2. For each k from 0 to V−1, declare k a legal intermediate stop.
  3. For every pair (i, j): if dist[i][k] + dist[k][j] < dist[i][j], route i→j through k and update.
  4. The order of updates within a round never matters — each round only refines with one new via-node.
  5. 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.

Floyd–WarshallO(V³)
All-pairs shortest paths by allowing each node in turn as an intermediate stop.
ABCDEF
A042∞∞∞
B4015∞∞
C210810∞
D∞58026
E∞∞10203
F∞∞∞630
via—
Step 1 / 16
Default Current / Compare Swap / Write Settled Active / Path Pivot / Reject Locked (hollow) Target
Initialise with direct edge weights: dist[i][j] = weight, 0 on the diagonal, ∞ where no edge exists.
Python · Graph Algorithms in Code
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 dist

Behind 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

BestO(V³)
AverageO(V³)
WorstO(V³)
SpaceO(V²) for the distance matrix

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

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits

Keep the momentum going

Step through a related structure, pattern or course — the ideas reinforce each other and stick faster.