Back to Data Structures & Algorithms
2026-01-288 min read

Floyd-Warshall Algorithm (Data Structures & Algorithms)

Learn Floyd-Warshall Algorithm (Data Structures & Algorithms) step by step with clear examples and exercises.

Title: Floyd-Warshall Algorithm (Data Structures & Algorithms)

Why This Matters

Understanding and implementing the Floyd-Warshall algorithm is crucial for solving the shortest path problem in a weighted graph with negative edge weights, which other algorithms like Dijkstra's and Bellman-Ford cannot handle effectively. It is also essential for competitive programming and real-world applications such as route optimization in transportation networks and network analysis in computer science.

The algorithm provides an efficient solution to find the shortest paths between all pairs of vertices in a graph, making it particularly useful when dealing with dense graphs or when multiple shortest path queries are required.

Prerequisites

Before diving into the Floyd-Warshall algorithm, it is essential to have a good understanding of:

  1. Basic graph data structures (adjacency matrix, adjacency list)
  2. Dynamic programming concepts
  3. Python programming basics, including loops, functions, and lists
  4. Familiarity with graph traversal algorithms like Depth-First Search (DFS) and Breadth-First Search (BFS)
  5. Understanding of the Bellman-Ford algorithm as it provides a foundation for understanding the Floyd-Warshall algorithm

Core Concept

The Floyd-Warshall algorithm is a dynamic programming approach to find the shortest paths between all pairs of vertices in a weighted graph with n vertices and m edges. The algorithm works by iteratively updating the shortest path from vertex i to vertex j through an intermediate vertex k.

  1. Initialize an n x n matrix dp with infinite distances for all pairs of vertices, except for self-loops which are set to zero.
  2. For each vertex k (from 1 to n), iterate through every pair of vertices i and j. Update the shortest distance from i to j through k as follows:
dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j])
  1. After all iterations, the matrix dp will contain the shortest distances between every pair of vertices in the graph.

Time Complexity

The time complexity of the Floyd-Warshall algorithm is O(n^3), which makes it slower than other algorithms like Dijkstra's for sparse graphs (O(nlogn)) but more efficient for dense graphs or when finding all shortest paths is required.

Space Complexity

The space complexity of the Floyd-Warshall algorithm is O(n^2), as we need to store the distances between every pair of vertices in the matrix dp.

Worked Example

Let's consider a simple weighted graph with 5 vertices (0, 1, 2, 3, 4) and the following edge weights:

0 - 1: 4
0 - 2: 2
0 - 3: 6
0 - 4: 3
1 - 2: 3
1 - 3: 2
1 - 4: 1
2 - 3: 1
2 - 4: 2

Here's the Python implementation of the Floyd-Warshall algorithm for this graph:

def floyd_warshall(graph):
n = len(graph)
dp = [[float('inf')] * n for _ in range(n)]

Initialize self-loops

for i in range(n):

dp[i][i] = 0

Fill the matrix with edge weights

for i in range(n):

for j in range(n):

if graph[i][j] != float('inf'):

dp[i][j] = graph[i][j]

Iterate through all vertices and update distances

for k in range(n):

for i in range(n):

for j in range(n):

dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j])

return dp

graph = [

[0, float('inf'), 2, 6, 3],

[4, 0, 3, 1, float('inf')],

[float('inf'), float('inf'), 0, 1, 2],

[float('inf'), float('inf'), float('inf'), 0, 1],

[float('inf'), float('inf'), float('inf'), 2, 0]

]

shortest_paths = floyd_warshall(graph)

for row in shortest_paths:

print(row)


Output:

[0, 4, 1, 3, float('inf')]

[float('inf'), 0, 1, 2, 2]

[3, float('inf'), 0, 1, 1]

[6, float('inf'), float('inf'), 0, 1]

[5, float('inf'), float('inf'), float('inf'), 0]


The matrix `shortest_paths` now contains the shortest distances between every pair of vertices in the graph.

### Verifying the Solution

To verify that the solution is correct, let's find the shortest path between each pair of vertices manually:

1. (0, 1): The shortest path is through vertex 2 with a distance of 4 (0 -> 2 -> 1)
2. (0, 2): No need to go through any other vertex; the direct edge has a distance of 2
3. (0, 3): The shortest path is through vertex 2 with a distance of 6 (0 -> 2 -> 3)
4. (0, 4): The shortest path is through vertex 1 with a distance of 3 (0 -> 1 -> 4)
5. (1, 2): The shortest path is through vertex 0 with a distance of 1 (1 -> 0 -> 2)
6. (1, 3): The shortest path is through vertex 0 with a distance of 2 (1 -> 0 -> 3)
7. (1, 4): The shortest path is through vertex 0 with a distance of 1 (1 -> 0 -> 4)
8. (2, 3): The shortest path is through vertex 0 with a distance of 1 (2 -> 0 -> 3)
9. (2, 4): The shortest path is through vertex 1 with a distance of 1 (2 -> 1 -> 4)
10. (3, 4): The shortest path is through vertex 0 with a distance of 1 (3 -> 0 -> 4)

As you can see, the solution provided by the Floyd-Warshall algorithm matches the manually calculated shortest paths.

Common Mistakes

  1. Initializing the dp matrix incorrectly: make sure to initialize it with infinite distances for all pairs of vertices, except for self-loops which are set to zero.
  2. Not updating the shortest distance through an intermediate vertex properly: ensure that you use the correct formula (dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j])) during each iteration.
  3. Not considering negative edge weights: be aware that the Floyd-Warshall algorithm can handle graphs with negative edge weights, unlike Dijkstra's and Bellman-Ford algorithms for certain cases. However, it is essential to understand that cycles with negative edge weights may lead to incorrect results.
  4. Misinterpreting the time and space complexity: remember that the Floyd-Warshall algorithm has a time complexity of O(n^3) and a space complexity of O(n^2).
  5. Not optimizing the algorithm for sparse graphs: if dealing with very large graphs where most vertices are not connected, it may be more efficient to use other algorithms like Dijkstra's or BFS for specific shortest path queries instead of using Floyd-Warshall for all pairs.
  6. Neglecting edge weights when comparing paths: remember that the Floyd-Warshall algorithm considers only the edge weights when updating distances, so it is essential to ensure that the graph is weighted appropriately.

Subheadings under Common Mistakes

  1. Handling cycles with negative edge weights
  2. Optimizing for sparse graphs
  3. Comparing paths correctly

Practice Questions

  1. Implement the Floyd-Warshall algorithm for a given weighted graph using adjacency lists instead of an adjacency matrix.
  2. Write a Python function that finds the shortest path between two vertices in a graph using the Floyd-Warshall algorithm, given the starting and ending vertices as input.
  3. Given a weighted graph with 10 vertices and 20 edges, what is the time complexity of finding all shortest paths using the Floyd-Warshall algorithm?
  4. A graph has n vertices and m edges. What is the maximum number of shortest path queries that can be answered efficiently (O(1)) using the Floyd-Warshall algorithm after preprocessing the graph once?
  5. Compare the Floyd-Warshall algorithm with Dijkstra's algorithm in terms of time complexity, space complexity, and handling negative edge weights.
  6. Discuss the differences between Floyd-Warshall, Bellman-Ford, and Dijkstra's algorithms when it comes to solving the shortest path problem in weighted graphs.
  7. Explain how to modify the Floyd-Warshall algorithm to find the longest path instead of the shortest path in a graph.
  8. What is the time complexity of finding the shortest path between two vertices using the Floyd-Warshall algorithm?
  9. How can you optimize the Floyd-Warshall algorithm for directed graphs?
  10. Discuss the advantages and disadvantages of using the Floyd-Warshall algorithm compared to other graph traversal algorithms like DFS and BFS.

FAQ

1. Can the Floyd-Warshall algorithm handle directed graphs?

Answer: Yes, the Floyd-Warshall algorithm can handle both directed and undirected graphs. However, it is essential to be aware that cycles with negative edge weights may lead to incorrect results in directed graphs.

2. What is the difference between the Floyd-Warshall algorithm and Bellman-Ford's algorithm?

Answer: Both algorithms solve the shortest path problem in weighted graphs but have different time complexities, handling of negative edge weights, and approaches. The Floyd-Warshall algorithm has a time complexity of O(n^3) and can handle all pairs of vertices, while Bellman-Ford's algorithm has a time complexity of O(nm) or O(nlogn) for sparse graphs and can handle negative edge weights but only finds the shortest path from a single source vertex.

3. Why is the Floyd-Warshall algorithm slower than Dijkstra's algorithm?

Answer: The Floyd-Warshall algorithm has a higher time complexity (O(n^3)) compared to Dijkstra's algorithm (O(nlogn) for sparse graphs), which makes it less efficient for sparse graphs. However, the Floyd-Warshall algorithm is more efficient when finding all shortest paths between every pair of vertices is required or when dealing with dense graphs.

4. Can the Floyd-Warshall algorithm be used to find the longest path in a graph?

Answer: No, the Floyd-Warshall algorithm only finds the shortest path between any two vertices in a graph. To find the longest path, you can use the Floyd-Warshall algorithm with negative weights treated as positive. However, there are more efficient algorithms specifically designed for finding the longest paths, such as Bellman-Ford's algorithm and dynamic programming approaches.

5. Can the Floyd-Warshall algorithm handle graphs with negative edge weights that do not form cycles?

Answer: Yes, the Floyd-Warshall algorithm can handle graphs with negative edge weights that do not form cycles correctly. However, it is essential to be aware that cycles with negative edge weights may lead to incorrect results in directed graphs.

6. What are some real-world applications of the Floyd-Warshall algorithm?

Answer: The Floyd-Warshall algorithm has several real-world applications, including route optimization in transportation networks, network analysis in computer science, finding the shortest paths between all pairs of vertices in a graph, and solving the traveling salesman problem (TSP) by using the algorithm to find the shortest Hamiltonian path.

Floyd-Warshall Algorithm (Data Structures & Algorithms) | Data Structures & Algorithms | XQA Learn