Advanced Topic 36 of 51 Algorithms
Floyd-Warshall Algorithm
All-pairs shortest paths via dynamic programming.
How it works
Floyd-Warshall computes shortest paths between all pairs of vertices with three nested loops, allowing each vertex k in turn as an intermediate point and updating dist[i][j] to min(dist[i][j], dist[i][k] + dist[k][j]). It runs in O(V cubed) time and O(V squared) space, handles negative weights and detects negative cycles via negative diagonal entries.
Where it is used
All-pairs routing tables, transitive closure of relations, finding the graph diameter, computing reachability matrices and small dense graphs in network analysis use Floyd-Warshall.
Common pitfall
The loop order matters: the intermediate vertex k must be the outermost loop. Infinity plus a value can overflow integers, so guard it. It is too slow for large sparse graphs, where running Dijkstra from every vertex is better. Reconstructing paths needs a next-hop matrix.
Where it fits in the course
Floyd-Warshall Algorithm is topic 36 of 51 in Algorithms, rated Advanced. It follows Bellman-Ford Algorithm. The next topic is A* Search.
Practice and explore
- Open the interactive lesson, with a mini quiz.
- Take the Algorithms quiz.
- Explore the Graph Traversal visualizer: BFS, DFS and Dijkstra on a weighted graph.
How to study this topic
- Read the explanation above and restate the definition in your own words.
- Open the interactive lesson and answer its mini quiz.
- Read the common pitfall, then write a short example that avoids it.
- Check your understanding in the Algorithms quiz, and revisit the topic if you miss a question.
Frequently asked questions
What is Floyd-Warshall Algorithm?
Where do people use Floyd-Warshall Algorithm?
What is the most common mistake with Floyd-Warshall Algorithm?
What should I learn before this topic?
How can I practice it?
Is Floyd-Warshall Algorithm hard to learn?
How long does it take to learn Floyd-Warshall Algorithm?
Which topics are related to Floyd-Warshall Algorithm?
What should I study after Floyd-Warshall Algorithm?
Related topics
- Bellman-Ford Algorithm (Algorithms)
- A* Search (Algorithms)
- Dijkstra's Algorithm (Algorithms)
- Minimum Spanning Tree (Algorithms)
- Shor's Algorithm (Emerging & Frontier Topics)
- Grover's Algorithm (Emerging & Frontier Topics)