Floyd-Warshall Algorithm

All-pairs shortest paths. Dynamic programming on graphs. Finding distances between every pair of nodes.

41 lessons
118 min

Lessons

1. Intro

(All-pairs shortest paths)

3m

2. The All-Pairs Problem

Finding every pair's distance

3m

3. Naive Approach

(Running Dijkstra n times)

3m

4. The Core Idea

Paths through intermediate ...

3m

5. The DP State

(Using vertices 0 to k)

3m

6. Space Improvement

(Overwriting in place)

3m

7. Quiz: Floyd-Warshall DP State

Knowledge check

2m1 problems

8. Why k is Outermost

(Correctness depends on order)

3m

9. The Algorithm

(Three nested loops)

3m

10. Time Complexity

(Cubic in vertices)

3m

11. Handling Negative Edges

(Unlike Dijkstra it works)

3m

12. Detecting Negative Cycles

(Checking the diagonal)

3m

13. Quiz: Negative Edges in Floyd-Warshall

Knowledge check

2m1 problems

14. Path Reconstruction

(Storing next hops)

3m

15. Transitive Closure

Reachability instead of dis...

3m

16. When to Use Floyd-Warshall

(vs running Dijkstra n times)

3m

17. Quiz: When to Use Floyd-Warshall

Knowledge check

2m1 problems

18. CSES 1672 Shortest Routes II - Problem Statement

(CSES 1672 walkthrough intro)

3m1 problems

19. CSES 1672 Shortest Routes II - Why Floyd-Warshall Fits

(All-pairs in one shot)

3m

20. CSES 1672 Shortest Routes II - Handling Multiple Edges

(Taking the minimum weight)

3m

21. CSES 1672 Shortest Routes II - Implementation

(Using adjacency matrix)

3m1 problems

22. CSES 1672 Shortest Routes II - Walkthrough

(Step-by-step trace)

3m

23. CSES 1672 Shortest Routes II - Lessons

(Precomputation wins)

3m

24. LeetCode 1334 Find the City - Problem Statement

LeetCode 1334 walkthrough i...

3m1 problems

25. LeetCode 1334 Find the City - Breaking Down the Problem

(Two-step approach)

3m

26. LeetCode 1334 Find the City - Counting Reachable Cities

(Looping through distances)

3m

27. LeetCode 1334 Find the City - Implementation

(Using Floyd-Warshall)

3m1 problems

28. LeetCode 1334 Find the City - Walkthrough

(LeetCode example trace)

3m

29. LeetCode 1334 Find the City - Lessons

Post-processing distance ma...

3m

30. Quiz: Floyd-Warshall Path Reconstruction

Knowledge check

2m1 problems

31. Common Mistakes

(Mistakes to avoid)

3m

32. Floyd-Warshall vs Bellman-Ford

(Two ways to handle negatives)

3m

33. Floyd-Warshall vs Johnson's Algorithm

(Sparse graphs with negatives)

3m

34. Practical Constraints

(When n³ is acceptable)

3m

35. Memory Layout

(Using 2D array efficiently)

3m

36. Infinity Handling

(Avoiding overflow)

3m

37. Undirected Graphs

(Symmetric distance matrix)

3m

38. Variants of Floyd-Warshall

(Minimax and maximin paths)

3m

39. Quiz: Floyd-Warshall Loop Order

Knowledge check

2m1 problems

40. Practice Problems

(Build your skills)

3m

41. Section Recap

(What you learned)

3m

Practice Problems

1.
Find the CityLeetCodemedium
2.
Course Schedule IVLeetCodemedium
3.
4.
Greg and GraphCodeforcesmedium
5.
ArbitragePOJhard

Ready to start learning?

Access all 41 lessons with interactive content and progress tracking.