forked from souravjain540/Basic-Python-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFloyd_Warshall_Algorithm.py
More file actions
36 lines (29 loc) · 1.04 KB
/
Copy pathFloyd_Warshall_Algorithm.py
File metadata and controls
36 lines (29 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
class Graph:
def __init__(self, num_vertices):
self.num_vertices = num_vertices
self.graph = [[float('inf')] * num_vertices for _ in range(num_vertices)]
for i in range(num_vertices):
self.graph[i][i] = 0
def add_edge(self, u, v, weight):
self.graph[u][v] = weight
self.graph[v][u] = weight # Symmetric for undirected graph
def floyd_warshall(self):
dist = self.graph
for k in range(self.num_vertices):
for i in range(self.num_vertices):
for j in range(self.num_vertices):
dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j])
return dist
# Example usage:
if __name__ == "__main__":
graph = Graph(4)
graph.add_edge(0, 1, 5)
graph.add_edge(0, 2, 9)
graph.add_edge(0, 3, 4)
graph.add_edge(1, 2, 2)
graph.add_edge(2, 3, 3)
# Run Floyd-Warshall algorithm
result = graph.floyd_warshall()
print("Shortest distances between every pair of vertices:")
for row in result:
print(row)