🗺️ Floyd-Warshall
All-pairs shortest paths in O(V³)
Pivot k = —
Updates this run: 0
Setup
Path
Controls
Stats
Pivot k
Updates
0
Path distance
Status
Ready
Log
Info & Theory

Floyd-Warshall finds the shortest path between every pair of vertices in a weighted graph by filling a V×V distance matrix, where dist[i][j] is the cost of the best route from i to j.

Initialisation

Set dist[i][i] = 0, set dist[i][j] to the weight of the edge i → j if it exists, and to otherwise.

The triple loop

For each pivot k (outer loop), then every source i and target j, relax:

  • dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j])
  • If improved, set next[i][j] = next[i][k].

Why it works (dynamic programming)

After pivot k, dist[i][j] is the shortest path that may use only intermediate vertices 1..k. Because k is the outermost loop, dist[i][k] and dist[k][j] are already optimal sub-paths, so the recurrence is always built on optimal sub-solutions.

Complexity

Three nested loops over V vertices give O(V³) time and O(V²) space. With tiny constants it is simple to code and competitive on small or dense graphs.

Negative edges & cycles

Unlike Dijkstra, Floyd-Warshall accepts negative edges. A negative cycle makes some paths undefined; you can detect one if any dist[i][i] turns negative. This simulation uses only non-negative weights so every distance is well defined.

Frequently asked questions

What does the Floyd-Warshall algorithm compute?

Floyd-Warshall computes the shortest path between every pair of vertices in a weighted graph at once. Instead of running a single-source algorithm from each node, it fills a V×V distance matrix where dist[i][j] is the length of the shortest path from i to j. It works on directed or undirected graphs and handles negative edge weights as long as there is no negative cycle.

How does the pivot relaxation work?

The algorithm uses a triple loop with an outer pivot k. For every pair (i, j) it asks whether going from i to k and then from k to j is shorter than the current best: dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]). After the pivot has cycled through all vertices, every shortest path that uses only intermediate vertices from the full set has been found.

Why is Floyd-Warshall O(V³)?

There are three nested loops, each running over all V vertices: the pivot k, the source i, and the target j. That gives V × V × V = V³ relaxation steps, so the time complexity is O(V³). The space complexity is O(V²) for the distance matrix, plus another O(V²) if you keep a predecessor matrix for path reconstruction.

Why is the dynamic-programming order correct?

After iteration k, dist[i][j] holds the shortest path from i to j that is allowed to pass only through intermediate vertices numbered 1..k. Because the pivot k is the outermost loop, by the time we use dist[i][k] and dist[k][j] they already account for all earlier pivots, so the recurrence is always built on optimal sub-solutions. This is the classic dynamic-programming invariant that makes the algorithm correct.

How does Floyd-Warshall handle negative edges and negative cycles?

Unlike Dijkstra, Floyd-Warshall tolerates negative edge weights and still returns correct shortest paths. A negative cycle, however, means some shortest paths are undefined because you can loop forever to lower the cost. You can detect a negative cycle after the run: if any diagonal entry dist[i][i] becomes negative, vertex i lies on a negative cycle. This simulation uses only non-negative weights to keep results well defined.

How is the shortest path reconstructed?

During initialization a next-hop matrix next[i][j] is set to j whenever a direct edge exists. Whenever a relaxation through pivot k improves dist[i][j], we copy next[i][j] = next[i][k], recording that the best route now starts by heading toward k. To rebuild the path you follow next[i][j] from the source until you reach the target, collecting the vertices along the way.

How does Floyd-Warshall compare to Dijkstra?

Dijkstra solves the single-source shortest-path problem and is fast on sparse graphs, but it requires non-negative weights and gives paths from one source only. Running Dijkstra from every vertex costs about O(V·E·log V) with a heap, which beats Floyd-Warshall on large sparse graphs. Floyd-Warshall, with its flat O(V³) cost and tiny constant factors, is simpler to code and often wins on small or dense graphs and when you genuinely need all pairs.

How does it compare to Bellman-Ford?

Bellman-Ford is also single-source but, like Floyd-Warshall, accepts negative edges and can detect negative cycles. It runs in O(V·E) per source. If you need shortest paths from one origin in a graph with negative edges, Bellman-Ford is the natural choice; if you need every pair at once, Floyd-Warshall's three compact loops are usually easier and competitive on dense graphs.

What does ∞ (INF) mean in the distance matrix?

An entry of ∞ means there is currently no known path between those two vertices. The diagonal dist[i][i] starts at 0 because the distance from a vertex to itself is zero. As the pivot advances, ∞ entries can become finite when an intermediate vertex links two previously disconnected nodes, which is exactly the matrix 'tightening' you see in the animation.

Where is Floyd-Warshall used in practice?

It is used for routing tables in small networks, computing the transitive closure of a relation, finding graph diameter and centrality, and solving all-pairs distance queries in maps, games, and operations research. A boolean variant computes reachability, and a max-min variant solves widest-path or bottleneck problems by swapping the min/plus operations for max/min.

About this simulation

Written by MySimulator Team · Reviewed by MySimulator Editorial Review

Last updated: 5 July 2026

This simulation runs the Floyd-Warshall algorithm live on a small random directed graph with 5 to 7 vertices. It fills a V×V distance matrix by relaxing every pair (i, j) through a pivot vertex k, one triple-loop step at a time, and flashes each cell the moment it improves. Once the pivot has swept through every vertex, the shortest path between the source and target you pick is highlighted directly on the graph, reconstructed from a next-hop matrix built alongside the distances.

🔬 What it shows

A weighted directed graph (a connected ring plus a handful of extra random edges) rendered as arrows with their weights, next to a live V×V distance matrix. As the pivot k advances, the row and column for k highlight, any cell that gets relaxed flashes, and once the run finishes the edges belonging to the shortest source→target path turn purple while the distance readout shows the final total.

🎮 How to use

Set the number of Vertices (5-7) and Speed, then press Play to animate the triple loop automatically or Step to advance exactly one pivot k at a time. Pick a Source and Target from the dropdowns to choose which pair's shortest path gets traced once the algorithm finishes, and use Regenerate for a fresh random graph. The log panel lists every relaxation as it happens, in the form k=X: A→B = new distance.

💡 Did you know?

Floyd-Warshall needs no priority queue and no per-source restart — three plain nested loops over every vertex solve all pairs at once. That flat O(V³) cost with a tiny constant factor is why it is often the practical choice on small or dense graphs, even though asymptotically smarter approaches exist for large sparse ones.

Frequently asked questions

What do the Vertices and Speed sliders actually control?

The Vertices slider (5 to 7) sets how many nodes the random graph has, which also sets the size of the distance matrix and how many pivots the algorithm sweeps through. Speed (0.2 to 4) controls how fast Play advances through relaxation steps automatically; it has no effect when you use Step, which always advances exactly one pivot's worth of relaxations per click.

What happens when I press Play versus Step?

Play runs the algorithm continuously at the chosen Speed, advancing i, j and eventually the pivot k until every pivot has been processed and the run is marked Done. Step instead advances through one whole pivot k in a single click, applying all of its (i, j) relaxations at once, which is useful for pausing and inspecting the matrix between pivots.

How is the highlighted shortest path chosen and drawn?

You choose the Source and Target vertices from the dropdown selectors. Once the run finishes, the simulation reconstructs the path by following the next-hop matrix from the source until it reaches the target, and draws every edge on that route in purple over the graph, while the Path distance stat shows the total weight, or the infinity symbol if no path exists.

Why do some distance matrix cells show the infinity symbol?

An infinity symbol means no path between that row and column vertex has been found yet. The diagonal always starts at 0 because every vertex reaches itself at zero cost. As the pivot k advances through the log panel's relaxations, infinity entries turn into finite numbers whenever k turns out to be a usable stepping stone between two vertices that were not directly connected.

Why does the graph regenerate with different edges each time?

Regenerate builds a fresh random directed graph: a connected ring so every vertex can reach the next one, plus a handful of extra random edges with weights from 1 to 9, so the shape of the problem changes each run. Changing the Vertices slider also triggers a full regeneration since the matrix size and ring length depend on the vertex count.