forked from anshrathod/Basic-Python-Scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfloyd_warshall.py
More file actions
31 lines (27 loc) · 996 Bytes
/
Copy pathfloyd_warshall.py
File metadata and controls
31 lines (27 loc) · 996 Bytes
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
def floyd_warshall_algorithm(distance, infinty: int) -> None:
dim = len(distance)
for k in range(0, dim):
for i in range(0, dim):
for j in range(0, dim):
if distance[i][k] < infinty and distance[k][j] < infinty:
if distance[i][j] > distance[i][k] + distance[k][j]:
distance[i][j] = distance[i][k] + distance[k][j]
def print_matrix(matrix) -> None:
dim = len(matrix)
for i in range(0, dim):
for j in range(0, dim):
print(matrix[i][j], end=' ')
print()
if __name__ == "__main__":
inf = 1000
matrix = [
[ 0, 3, 6, inf, inf, inf, inf],
[ 3, 0, 2, -1, inf, inf, inf],
[ 6, 2, 0, 1, 4, 2, inf],
[inf, 1, 1, 0, 2, inf, 4],
[inf, inf, 4, 2, 0, 2, 1],
[inf, inf, 2, inf, -2, 0, 1],
[inf, inf, inf, 4, 1, 1, 0]
]
floyd_warshall_algorithm(matrix, inf)
print_matrix(matrix)