-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbellman_ford.py
46 lines (40 loc) · 1.11 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
'''
Bellford's Algorithm for finding the shortest
path from a vertex to all other vertices of a
weighted graph
'''
class Graph:
def __init__(self, vertices):
self.V = vertices #total no. of vertices
self.graph = []
#add edges
def addEdge(self, s,d,w):
self.graph.append([s,d,w])
#print solution
def print_solution(self, dist):
print('Vertex Distance from the source')
for i in range(self.V):
print(f'{i}\t\t{dist[i]}')
def bellmanFord(self,src):
#1-> Fill the distance array & predecessor array
dist = [float('Inf')] * self.V
#mark the source vertex
dist[src] = 0
#2-> relax edges |V| - 1 times
for _ in range(self.V-1):
for s,d,w in self.graph:
if dist[s] != float('Inf') and dist[s] + w < dist[d]:
dist[d] = dist[s] + w
#3-> detect negative cycle
for s,d,w in self.graph:
if dist[s] != float('Inf') and dist[s] + w < dist[d]:
print('Graph contains negative cycles')
return
self.print_solution(dist)
g = Graph(5)
g.addEdge(0, 1, 5)
g.addEdge(0, 2, 4)
g.addEdge(1, 3, 3)
g.addEdge(2, 1, 6)
g.addEdge(3, 2, 2)
g.bellmanFord(0)