Floyd–Warshall All-Pairs Shortest Paths
For each allowed intermediate vertex, compare the current distance with the route through that vertex.
Core Intuition & How It Works
Floyd–Warshall finds shortest paths between all pairs of vertices in O(V³) time using dynamic programming over intermediate vertices k = 1..V. It handles negative edges and detects negative cycles via negative diagonal entries dist[i][i] < 0.
In the Byte Sized Algorithms interactive classroom, you can step forward and backward through this algorithm with your keyboard while watching the visual diagram, memory state, and highlighted pseudocode update together.
Time & Space Complexity
| Metric | Complexity |
|---|---|
| Time Complexity | O(V³) |
| Space Complexity | O(V²) |
| Topic Chapter | Chapter 12: Shortest Paths & Spanning Trees |
| Difficulty Level | Advanced |
Key Pattern to Recognize
Place the intermediate vertex k as the outermost loop: dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]).
Common Pitfalls & Edge Cases
Putting k as the innermost loop, which breaks the DP recurrence and yields incorrect multi-hop shortest paths.
Real-World & Technical Interview Applications
Small dense network latency tables, transitive closure, and graph diameter/centrality computations.