-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path08_Floyd's_algorithm.py
68 lines (50 loc) · 1.52 KB
/
08_Floyd's_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
60
61
62
63
64
65
66
67
68
# Floyd's All pair shortest path algorithm
print("\nRUBAN GINO SINGH.A - URK20CS2001 | Floyd's All pair Shortest Path Algorithm\n")
INF = float('inf')
def printmatrix(m):
row, column = len(m), len(m[0])
for i in range(row):
for j in range(column):
print(m[i][j], end=" ")
print()
vertices, edges = map(int, input("\nEnter the number of Vertices and Edges: ").split())
m=[[None]*vertices for i in range(vertices)]
for i in range(vertices):
for j in range(vertices):
if i == j:
m[i][j] = 0
else:
m[i][j] = INF
print()
for i in range(edges):
source, destination, weight = map(int, input("Enter Source, Destination and Weight: ").split())
m[source][destination] = weight
print("\nMatrix 1: ")
printmatrix(m)
print("\n----------\n")
for k in range(vertices):
for i in range(vertices):
for j in range(vertices):
if m[i][k] + m[k][j] < m[i][j]:
m[i][j] = m[i][k] + m[k][j]
print("Matrix 2: ")
printmatrix(m)
# Enter the number of Vertices and Edges: 4 7
# Enter Source, Destination and Weight: 0 2 3
# Enter Source, Destination and Weight: 0 3 7
# Enter Source, Destination and Weight: 1 2 2
# Enter Source, Destination and Weight: 1 0 8
# Enter Source, Destination and Weight: 2 0 5
# Enter Source, Destination and Weight: 2 3 1
# Enter Source, Destination and Weight: 3 0 2
# Matrix 1:
# 0 inf 3 7
# 8 0 2 inf
# 5 inf 0 1
# 2 inf inf 0
# ----------
# Matrix 2:
# 0 inf 3 4
# 5 0 2 3
# 3 inf 0 1
# 2 inf 5 0