forked from jainaman224/Algo_Ds_Notes
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Floyd_Warshall_Algorithm.py
59 lines (47 loc) · 1.44 KB
/
Floyd_Warshall_Algorithm.py
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
class FloydWarshall:
"""
Implements : Floyd Warshall Algorithm
Inputs : Adjaceny Matrix (list of lists)
Outputs : Shortest distance between all pairs
"""
def __init__(self, adj_matrix):
"""
Initialises distance matrix (list of lists)
"""
self.adj_matrix = adj_matrix
self.distance = adj_matrix
self.num_vertices = len(adj_matrix)
def run(self):
""""
Implements Floyd Warshall Algorithm
"""
for k in xrange(0, self.num_vertices):
for i in xrange(0, self.num_vertices):
for j in xrange(0, self.num_vertices):
if self.distance[i][k] + self.distance[k][j] < self.distance[i][j]:
self.distance[i][j] = self.distance[i][k] + self.distance[k][j]
def get_distance(self):
"""
Returns the distance list
"""
return self.distance
def print_distance(self):
for node in self.distance:
for each in node:
print each,
print
def main():
graph = [[0, 5, float('inf'), 10],
[float('inf'), 0, 3, float('inf')],
[float('inf'), float('inf'), 0, 1],
[float('inf'), float('inf'), float('inf'), 0]]
floyd = FloydWarshall(graph)
floyd.run()
floyd.print_distance()
if __name__ == '__main__':
main()
#OUTPUT
#0 5 8 9
#inf 0 3 4
#inf inf 0 1
#inf inf inf 0