-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBellman Ford_Algorithm.py
52 lines (43 loc) · 1.47 KB
/
Bellman Ford_Algorithm.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
class Graph:
def __init__(self, vertices):
self.V = vertices
self.graph = []
def addEdge(self, u, v, w):
self.graph.append([u, v, w])
def printArr(self, dist, parent):
print("Vertex Distance from Source Optimal Path")
for i in range(self.V):
print("{0}\t\t\t\t\t{1}\t\t\t\t\t".format(i, dist[i]), end="")
self.print_path(parent, i)
print()
def print_path(self, parent, j):
if parent[j] == -1:
print(j, end="")
return
self.print_path(parent, parent[j])
print(" ->", j, end="")
def BellmanFord(self, src):
dist = [float("Inf")] * self.V
dist[src] = 0
parent = [-1] * self.V
for _ in range(self.V - 1):
for u, v, w in self.graph:
if dist[u] != float("Inf") and dist[u] + w < dist[v]:
dist[v] = dist[u] + w
parent[v] = u
for u, v, w in self.graph:
if dist[u] != float("Inf") and dist[u] + w < dist[v]:
print("Graph contains negative weight cycle")
return
self.printArr(dist, parent)
if __name__ == '__main__':
g = Graph(5)
g.addEdge(0, 1, -1)
g.addEdge(0, 2, 4)
g.addEdge(1, 2, 3)
g.addEdge(1, 3, 2)
g.addEdge(1, 4, 2)
g.addEdge(3, 2, 5)
g.addEdge(3, 1, 1)
g.addEdge(4, 3, -3)
g.BellmanFord(0)