Lesson 67 of 86AdvancedPro Interactive Lesson

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

MetricComplexity
Time ComplexityO(V³)
Space ComplexityO(V²)
Topic ChapterChapter 12: Shortest Paths & Spanning Trees
Difficulty LevelAdvanced

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.

Unlock the full course

All 86 lessons across 15 chapters

View pricing

Delete account

This permanently deletes your account and ends any Pro access.