-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path16. Bellman Ford.py
More file actions
50 lines (37 loc) · 1.16 KB
/
Copy path16. Bellman Ford.py
File metadata and controls
50 lines (37 loc) · 1.16 KB
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
import sys
input = sys.stdin.readline
def multi_input():
return map(int, input().split())
def array_print(arr):
print(' '.join(map(str, arr)))
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):
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[src] = 0
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
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)
print("Number of Vertices (1-N)")
V = int(input())
g = Graph(V)
print("Number of edges")
edges = int(input())
for i in range(edges):
n1, n2, w = multi_input()
g.addEdge(n1, n2, w)
g.BellmanFord(1)