This repository has been archived on 2025-12-15. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
LUT-yliopisto/BM40A1500 Data Structures and Algorithms/Assignments/Week 10/floyd.py
2023-01-08 18:43:01 +02:00

46 lines
1.2 KiB
Python

from sys import maxsize
from graph import Graph
def floyd(graph: Graph):
distance = graph.matrix
for x in range(graph.V):
for y in range(graph.V):
if x != y and distance[x][y] == 0:
distance[x][y] = maxsize
for k in range(graph.V):
for i in range(graph.V):
for j in range(graph.V):
distance[i][j] = min(
distance[i][j], distance[i][k] + distance[k][j])
for x in range(graph.V):
for y in range(graph.V):
if x != y and distance[x][y] == maxsize:
distance[x][y] = 0
return distance
if __name__ == "__main__":
matrix = [
# 0 1 2 3 4 5
[0, 0, 7, 0, 9, 0], # 0
[0, 0, 0, 0, 0, 0], # 1
[0, 5, 0, 1, 0, 2], # 2
[6, 0, 0, 0, 0, 2], # 3
[0, 0, 0, 0, 0, 1], # 4
[0, 6, 0, 0, 0, 0] # 5
]
graph = Graph(matrix)
D = floyd(graph)
for i in range(6):
for j in range(6):
print(f"{D[i][j]:2d}", end=" ")
print()
# 0 12 7 8 9 9
# 0 0 0 0 0 0
# 7 5 0 1 16 2
# 6 8 13 0 15 2
# 0 7 0 0 0 1
# 0 6 0 0 0 0