This repository has been archived by the owner on Jul 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbellman_ford.py
72 lines (60 loc) · 1.47 KB
/
bellman_ford.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
"""
The Bellman-Ford algorithm
Graph API:
iter(graph) gives all nodes
iter(graph[u]) gives neighbours of u
graph[u][v] gives weight of edge (u, v)
props to: http://hetland.org/coding/python/bellman_ford.py
"""
def initialize(graph, source):
d = {}
p = {}
for node in graph:
d[node] = float('inf')
p[node] = None
d[source] = 0
return d, p
def relax(u, v, graph, d, p):
if d[v] > d[u] + graph[u][v]:
d[v] = d[u] + graph[u][v]
p[v] = u
def bellman_ford(graph, source):
d, p = initialize(graph, source)
start = None
for i in range(len(graph)-1):
for u in graph:
for v in graph[u]:
relax(u, v, graph, d, p)
# one more relaxation to find negative weight cycle
for u in graph:
for v in graph[u]:
if d[v] > d[u] + graph[u][v]:
#relax(u, v, graph, d, p)
start = u
return d,p,start
return d,p,start
def test():
graph = {
'a': {'b': -1, 'c': 4},
'b': {'c': 3, 'd': 2, 'e': 2},
'c': {},
'd': {'b': 1, 'c': 5},
'e': {'d': -3}
}
d, p, _ = bellman_ford(graph, 'a')
assert d == {
'a': 0,
'b': -1,
'c': 2,
'd': -2,
'e': 1
}
assert p == {
'a': None,
'b': 'a',
'c': 'b',
'd': 'e',
'e': 'b'
}
if __name__ == '__main__':
test()