bellman

Run Settings
LanguagePython
Language Version
Run Command
def bellman(l,adj,source): dist = [100000000] * l dist[source]=0 for i in range(l-1): for u, v, w in adj: if dist[u] + w < dist[v]: dist[v] = dist[u] + w for u, v, w in adj: if dist[u] + w < dist[v]: print("Negative weight cycle detected!") return return dist V = 4 edges = [ [0, 1, -4], # Edge from 0 to 1 with weight 4 [0, 2, 1], # Edge from 0 to 2 with weight 1 [2, 1, 2], # Edge from 2 to 1 with weight 2 [1, 3, 1], # Edge from 1 to 3 with weight 1 [3, 2, -3] # Edge from 3 to 2 with weight -3 (negative cycle detection) ] source = 0 for i in range(4): result=bellman(V,edges,i) print(result)
Editor Settings
Theme
Key bindings
Full width
Lines