forked from Firkraag/algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bellman_Ford_matrix.py
75 lines (67 loc) · 1.76 KB
/
Bellman_Ford_matrix.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
69
70
71
72
73
import numpy as np
def Bellman_Ford_matrix(W, s):
n = W.shape[0]
d = [float("Inf")] * n
p = [None] * n
d[s - 1] = 0
for k in range(1, n):
for i in range(1, n + 1):
for j in range(1, n + 1):
if d[j - 1] > d[i - 1] + W[i - 1, j - 1]:
d[j - 1] = d[i - 1] + W[i - 1, j - 1]
p[j - 1] = i
print d
print p
return d, p
def extend_shortest_paths(L, W):
n = W.shape[0]
LW = [float("Inf")] * n
for j in range(0, n):
for k in range(0, n):
LW[j] = min(LW[j], L[k] + W[k, j])
return LW
def slow_all_pairs_shortest_paths(W, s):
n = W.shape[0]
L = [0] * n
for i in range(0, n):
if i + 1 == s:
L[i] = 0
else:
L[i] = float("Inf")
print L
for m in range(1, n):
L = extend_shortest_paths(L, W)
print L
return L
def extend_shortest_paths_with_predecessor_subgraph(s, L, P, W):
n = W.shape[0]
LW = [float("Inf")] * n
PP = [None] * n
for j in range(0, n):
for k in range(0, n):
if LW[j] > L[k] + W[k, j]:
LW[j] = L[k] + W[k, j]
PP[j] = k + 1
if LW[j] == L[j]:
PP[j] = P[j]
return LW, PP
def slow_all_pairs_shortest_paths_with_predecessor_subgraph(W, s):
n = W.shape[0]
L = [float("Inf")] * n
L[s - 1] = 0
P = [None] * n
for m in range(1, n):
L, P = extend_shortest_paths_with_predecessor_subgraph(s, L, P, W)
print L
print P
return L, P
def faster_all_pairs_shortest_paths(W):
n = W.shape[0]
L = W
m = 1
while m < n - 1:
L = extend_shortest_paths(L, L)
m = 2 * m
print m
print L
return Ld, p