Skip to content

Commit 40c9853

Browse files
authored
Merge pull request codeharborhub#4109 from AKSHITHA-CHILUKA/patch-9
Create floyd-warshall.md
2 parents 7920157 + 590a075 commit 40c9853

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed

dsa/Algorithms/floyd-warshall.md

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# Floyd-Warshall Algorithm
2+
3+
The Floyd-Warshall algorithm is an all-pairs shortest path algorithm that finds the shortest paths between all pairs of vertices in a weighted graph. It is particularly useful for dense graphs or when you need to calculate the shortest path between every pair of vertices.
4+
5+
## Algorithm Description
6+
7+
The Floyd-Warshall algorithm works by iteratively improving the shortest path between any two vertices (i, j) by considering an intermediate vertex k. The algorithm updates the shortest path matrix with the shortest distance found so far.
8+
9+
The algorithm uses a matrix `dist` to store the shortest distances between pairs of vertices. The key idea is to update this matrix by checking if a path through an intermediate vertex offers a shorter route than the direct path.
10+
11+
## Steps
12+
13+
1. Initialize the distance matrix `dist` with direct distances between vertices. If there is no direct edge between vertices, initialize it with infinity.
14+
2. Set the distance from a vertex to itself as 0.
15+
3. Iterate over all pairs of vertices (i, j) for each intermediate vertex k.
16+
4. Update the distance matrix `dist[i][j]` if a shorter path is found through vertex k.
17+
18+
## Time Complexity
19+
20+
The time complexity of the Floyd-Warshall algorithm is O(V^3), where V is the number of vertices in the graph.
21+
22+
## Python Implementation
23+
24+
Here is a Python implementation of the Floyd-Warshall algorithm:
25+
26+
```python
27+
def floyd_warshall(graph):
28+
"""
29+
Floyd-Warshall algorithm to find the shortest paths between all pairs of vertices.
30+
31+
:param graph: 2D list representing the adjacency matrix of the graph
32+
:return: 2D list representing the shortest distance matrix
33+
"""
34+
# Number of vertices in the graph
35+
V = len(graph)
36+
37+
# Initialize the distance matrix with the input graph matrix
38+
dist = [list(row) for row in graph]
39+
40+
# Iterate over all intermediate vertices
41+
for k in range(V):
42+
# Iterate over all pairs of vertices
43+
for i in range(V):
44+
for j in range(V):
45+
# Update the distance matrix if a shorter path is found
46+
dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j])
47+
48+
return dist
49+
```
50+
# Example usage
51+
if __name__ == "__main__":
52+
# Define the adjacency matrix of the graph
53+
# Infinity represents no direct path between vertices
54+
INF = float('inf')
55+
graph = [
56+
[0, 3, INF, 5],
57+
[2, 0, INF, 4],
58+
[INF, 1, 0, INF],
59+
[INF, INF, 2, 0]
60+
]
61+
62+
# Find the shortest paths between all pairs of vertices
63+
shortest_paths = floyd_warshall(graph)
64+
65+
# Print the shortest distance matrix
66+
for row in shortest_paths:
67+
print(row)
68+
## Explanation
69+
The floyd_warshall function takes a graph represented as an adjacency matrix and returns the shortest distance matrix.
70+
The outer loop iterates over all possible intermediate vertices k.
71+
The nested loops iterate over all pairs of vertices (i, j) and update the distance dist[i][j] if a shorter path is found through vertex k.
72+
The example usage demonstrates how to use the function and print the shortest distance matrix.
73+
74+
## Conclusion
75+
The Floyd-Warshall algorithm is a powerful and simple algorithm for finding shortest paths in weighted graphs. Its cubic time complexity makes it suitable for smaller graphs, but it can handle dense graphs effectively.

0 commit comments

Comments
 (0)