-
Notifications
You must be signed in to change notification settings - Fork 82
Expand file tree
/
Copy pathalgorithm-floyd.py
More file actions
37 lines (29 loc) · 1.3 KB
/
algorithm-floyd.py
File metadata and controls
37 lines (29 loc) · 1.3 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
37
import math
def get_path(P, u, v):
path = [u]
while u != v:
u = P[u][v]
path.append(u)
return path
V = [[0, 2, math.inf, 3, 1, math.inf, math.inf, 10],
[2, 0, 4, math.inf, math.inf, math.inf, math.inf, math.inf],
[math.inf, 4, 0, math.inf, math.inf, math.inf, math.inf, 3],
[3, math.inf, math.inf, 0, math.inf, math.inf, math.inf, 8],
[1, math.inf, math.inf, math.inf, 0, 2, math.inf, math.inf],
[math.inf, math.inf, math.inf, math.inf, 2, 0, 3, math.inf],
[math.inf, math.inf, math.inf, math.inf, math.inf, 3, 0, 1],
[10, math.inf, 3, 8, math.inf, math.inf, 1, 0],
]
N = len(V) # число вершин в графе
P = [[v for v in range(N)] for u in range(N)] # начальный список предыдущих вершин для поиска кратчайших маршрутов
for k in range(N):
for i in range(N):
for j in range(N):
d = V[i][k] + V[k][j]
if V[i][j] > d:
V[i][j] = d
P[i][j] = k # номер промежуточной вершины при движении от i к j
# нумерацця вершин начинается с нуля
start = 1
end = 4
print(get_path(P, end, start))