-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBellman-Ford.py
62 lines (48 loc) · 1.86 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
class Graph:
def __init__(self):
self.graph = []
def add_edges_with_weight(self, u, v, w):
self.graph.append([u, v, w])
def print_result(self, distance):
print("Vertex\t\tDistance")
for i in range(len(distance)):
print(f"{i}\t\t{distance[i]}")
# Step 1: Initialize distances from src to all other vertices
# as INFINITE
def initialize_source(self, src, V):
distance = [float("Inf")]*V
distance[src] = 0
return distance
# Step 2: Relax all edges |V| - 1 times. A simple shortest
# path from src to any other vertex can have at-most |V| - 1
# edges
def relax(self, distance, u, v, w):
if distance[v] > distance[u]+w:
distance[v] = distance[u]+w
return distance
def bellman_ford(self, src, V):
dist = self.initialize_source(src, V)
for i in range(V-1):
for u, v, w in self.graph:
dist = self.relax(dist, u, v, w)
# Step 3: check for negative-weight cycles. The above step
# guarantees shortest distances if graph doesn't contain
# negative weight cycle. If we get a shorter path, then there
# is a cycle.
for u, v, w in self.graph:
if dist[v] > dist[u]+w:
print("\nGraph contains negative weight cycle")
return
self.print_result(dist)
my_graph = Graph()
my_graph.add_edges_with_weight(0, 1, 4)
my_graph.add_edges_with_weight(0, 2, 2)
my_graph.add_edges_with_weight(1, 2, 3)
my_graph.add_edges_with_weight(1, 3, 2)
my_graph.add_edges_with_weight(1, 4, 3)
my_graph.add_edges_with_weight(2, 1, 1)
my_graph.add_edges_with_weight(2, 4, 5)
my_graph.add_edges_with_weight(4, 3, -5)
# uncomment below line to create negative weight cycle among node 3 & 4
# my_graph.add_edges_with_weight(3, 4, -2)
my_graph.bellman_ford(0, 5)