MCPcopy Create free account
hub / github.com/subbarayudu-j/TheAlgorithms-Python / BellmanFord

Function BellmanFord

Graphs/bellman_ford.py:12–33  ·  view source on GitHub ↗
(graph, V, E, src)

Source from the content-addressed store, hash-verified

10 print()
11
12def BellmanFord(graph, V, E, src):
13 mdist=[float('inf') for i in range(V)]
14 mdist[src] = 0.0
15
16 for i in range(V-1):
17 for j in range(V):
18 u = graph[j]["src"]
19 v = graph[j]["dst"]
20 w = graph[j]["weight"]
21
22 if mdist[u] != float('inf') and mdist[u] + w < mdist[v]:
23 mdist[v] = mdist[u] + w
24 for j in range(V):
25 u = graph[j]["src"]
26 v = graph[j]["dst"]
27 w = graph[j]["weight"]
28
29 if mdist[u] != float('inf') and mdist[u] + w < mdist[v]:
30 print("Negative cycle found. Solution not possible.")
31 return
32
33 printDist(mdist, V)
34
35
36

Callers 1

bellman_ford.pyFile · 0.85

Calls 1

printDistFunction · 0.70

Tested by

no test coverage detected