Floyd-Warshall
Compute all-pairs shortest paths by gradually allowing more intermediate vertices. This is the clean dynamic-programming answer to dense shortest-path queries.
Interactive visualization
Use the controls to connect the idea to concrete operations before diving into the full write-up.
initial matrix
| from \\ to | 0 | 1 | 2 | 3 |
|---|---|---|---|---|
| 0 | 0 | 3 | 10 | inf |
| 1 | inf | 0 | 1 | 7 |
Dynamic programming over intermediates
Initial matrix: direct edges only.
Family
Graphs
Nodes, edges, traversal, connectivity, and shortest paths.
Builds on
2 topics
Read these first if you want the surrounding context.
Unlocks
0 next topics
Use these follow-ups when you want to apply the current idea in a richer setting.
Learning paths
2
This topic appears in curated progressions so you can keep moving without guessing the next step.
Problem
You need shortest paths between every pair of vertices, not just from one source.
Running Dijkstra from every vertex can work, but for dense graphs or when you want a conceptually direct all-pairs method, Floyd-Warshall is the classic choice.
Core idea
Let dist[i][j] be the best known path from i to j.
Then process vertices one by one as allowed intermediates:
At stage k, you ask:
is the best path from
itojbetter if I am allowed to route through vertexk?
That is a pure dynamic-programming recurrence.
Why it matters
Floyd-Warshall is the cleanest demonstration that shortest paths are not only about greedy algorithms like Dijkstra. Sometimes the right model is a DP over allowed intermediates.
It also handles negative edge weights, as long as there is no negative cycle.
Complexity
| Operation | Time | Space |
|---|---|---|
| All-pairs shortest paths |
This is expensive for large sparse graphs, but excellent for dense graphs or small n.
Key takeaways
- Floyd-Warshall is the standard all-pairs shortest-path DP
- It is cubic, so it shines when
nis modest or the graph is dense - Unlike Dijkstra, it can handle negative edges if there is no negative cycle
- This fills the gap between single-source shortest paths and full reachability analysis
Practice problems
| Problem | Difficulty | Key idea |
|---|---|---|
| Find the City With the Smallest Number of Neighbors at a Threshold Distance | 🟡 Medium | All-pairs distances on modest n |
| Shortest Routes II | 🟡 Medium | Multiple pair queries after preprocessing |
| Arbitrage / transitive closure variants | 🔴 Hard | DP over intermediates |
Relation to other topics
- Dijkstra is better for single-source nonnegative shortest paths
- Dynamic Programming explains the recurrence structure directly
- Graph Fundamentals provides the weighted-path model Floyd-Warshall operates on
Build on these first
These topics supply the mental model or underlying structure that this page assumes.
Graph Fundamentals
Learn the core language of nodes, edges, paths, and cycles. Graphs are the broad container that trees, BFS, DFS, Dijkstra, and MST all live inside.
Dynamic Programming
Break a problem into overlapping subproblems, solve each once, reuse the results. DP turns exponential brute force into polynomial time.
Related directions
These topics live nearby conceptually, even if they are not direct prerequisites.
Dijkstra's Algorithm
Find the shortest path in weighted graphs with non-negative edges. Dijkstra is BFS with a priority queue — it processes nodes in order of cumulative distance.
Tarjan SCC
Find strongly connected components in one DFS by tracking discovery times, low-link values, and the active stack.
More from Graphs
Stay in the same family when you want parallel variations of the same mental model.
BFS — Breadth-First Search
Explore a graph level by level using a queue. BFS finds the shortest path in unweighted graphs and is fundamental to many graph algorithms.
DFS — Depth-First Search
Dive as deep as possible before backtracking. DFS is essential for detecting cycles, topological sorting, and exploring all paths in a graph.
Graph Fundamentals
Learn the core language of nodes, edges, paths, and cycles. Graphs are the broad container that trees, BFS, DFS, Dijkstra, and MST all live inside.
0-1 BFS
Find shortest paths in graphs whose edge weights are only 0 or 1 using a deque instead of a heap.
Paths that include this topic
Follow one of these sequences if you want a guided next step instead of open-ended browsing.
Graph toolkit
Start with graph shape, then layer traversal, connectivity structure, and both single-source and all-pairs path algorithms.
Weighted shortest paths
See the spectrum from plain BFS to deque-based 0-1 BFS to priority-queue shortest paths and dense all-pairs DP.
From the blog
Pair the atlas with recent writing from the rest of the site when you want a broader engineering perspective alongside the topic graph.