(graph, V, E, src)
| 10 | print() |
| 11 | |
| 12 | def 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 |
no test coverage detected