-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbellman_ford.py
More file actions
28 lines (23 loc) · 796 Bytes
/
bellman_ford.py
File metadata and controls
28 lines (23 loc) · 796 Bytes
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
class graph:
def __init__(self):
self.nodes = []
self.edges = {}
self.weights = {}
def add_node(self,node):
self.nodes.append(node)
def add_edge(self,from_node,to_node,weight):
self.weights[(from_node,to_node)] = weight
if from_node not in self.edges:
self.edges[from_node] = [to_node]
else:
self.edges[from_node].append(to_node)
def bellman(graph,src):
nodes = graph.nodes
weights = graph.weights
dt = [None]*len(nodes)
dt[src] = 0
for i in range(len(nodes)):
for edge in weights:
if dt[edge[0]] != None and (dt[edge[1]]==None or dt[edge[0]] + weights[edge] < dt[edge[1]]):
dt[edge[1]] = dt[edge[0]] + weights[edge]
return dt