-
Notifications
You must be signed in to change notification settings - Fork 1
/
shortest_path.py
30 lines (22 loc) · 900 Bytes
/
shortest_path.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
from collections import defaultdict
class Graph:
def __init__(self, vertices):
self.V = vertices
self.graph = []
self.parent_node = [-1] * self.V
def addEdge(self, u, v, w):
self.graph.append([u, v, w])
def printArr(self, dist):
print("Vertex Distance from Source")
for i in range(self.V):
print("% d \t\t % d" % (i, dist[i]))
def BellmanFord(self, src):
dist = [float("Inf")] * self.V
dist[(int)(src)] = 0
self.parent_node[(int)(src)] = (int)(src)
for i 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
self.parent_node[v] = u
return dist, self.parent_node