Python 3.11+
The code uses standard Python syntax and containers supported by the browser Judge. The Bellman-Ford proof does not depend on a minor Python release.
Verify in Python docs(opens in a new tab)Relax every edge repeatedly to support negative weights and detect reachable negative cycles. Learn the algorithm's preconditions, state transition, correctness argument, Python cost model, and transfer from one focused drill to a full Interview Problem.
Recognize it when
Consider Bellman-Ford when the prompt's constraints and required operations match this shape: Relax every edge repeatedly to support negative weights and detect reachable negative cycles.

Sign in to save your code and progress across devices. The lesson and problem statement remain public.
Any simple shortest path contains at most V minus one edges. Bellman-Ford performs enough full edge passes for improvements to propagate across paths of increasing edge count.
If an entire pass produces no improvement, all reachable shortest distances are already stable and later passes cannot change them.
def bellman_passes(node_count,edges,start):
distance=[None]*node_count;distance[start]=0;trace=[]
for _ in range(node_count-1):
changed=False;next_distance=distance.copy()
for source,target,weight in edges:
if distance[source] is not None:
candidate=distance[source]+weight
if next_distance[target] is None or candidate<next_distance[target]:next_distance[target]=candidate;changed=True
distance=next_distance;trace.append(distance.copy())
if not changed:break
return traceImplement bellman_passes(node_count,edges,start). Return distances after each full pass, using None for unreachable, and stop when unchanged.
Loading interactive editor.If it does not appear, the reference and starter code remain readable.Reload editor
After V minus one passes, one more successful relaxation from a reachable source proves a reachable negative cycle. Cycles in unreachable components do not invalidate source distances.
def bellman_ford(node_count,edges,start):
distance=[None]*node_count;distance[start]=0
for _ in range(node_count-1):
changed=False
for source,target,weight in edges:
if distance[source] is not None and (distance[target] is None or distance[source]+weight<distance[target]):distance[target]=distance[source]+weight;changed=True
if not changed:break
for source,target,weight in edges:
if distance[source] is not None and (distance[target] is None or distance[source]+weight<distance[target]):return None
return distanceImplement bellman_ford(node_count,edges,start). Return distances or None if a reachable negative cycle exists.
Loading interactive editor.If it does not appear, the reference and starter code remain readable.Reload editor
Choose Bellman-Ford for possible negative edges and negative-cycle detection at O(VE). Choose Dijkstra for nonnegative edges and better heap-based performance. DAGs permit one topological relaxation pass.
Say: “After pass i, paths using at most i edges are represented. V minus one covers every simple path; another improvement requires a reachable negative cycle.” Explain unreachable sentinels and early stopping.
Python 3.11+
The code uses standard Python syntax and containers supported by the browser Judge. The Bellman-Ford proof does not depend on a minor Python release.
Verify in Python docs(opens in a new tab)Interview bounds depend on the stated representation, input model, and real Python operations.
| Operation | Average | Worst | Interview note |
|---|---|---|---|
| Bellman-Ford complete workflow | O(VE) | O(VE) | Relax every reachable directed edge for at most V-1 passes, then perform one proof pass for a negative cycle. |
O(V) for the focused Relax Signed Edges with Bellman-Ford implementation.
These are the mistakes most likely to survive a happy-path example and fail a boundary case.
Verify algorithm preconditions such as sorted input, nonnegative weights, acyclicity, or admissible heuristics before applying it.
Prevent it: State and verify this precondition before coding: Edge direction and weight constraints match the relaxation and finalization rules of the chosen method.
Avoid
def bellman_ford_distances(node_count, edges, source):
passUse instead
def bellman_ford_distances(node_count, edges, source):
distances = [float('inf')] * node_count
distances[source] = 0
for _ in range(node_count - 1):
changed = False
for start, end, weight in edges:
if distances[start] != float('inf') and distances[start] + weight < distances[end]:
distances[end] = distances[start] + weight
changed = True
if not changed:
break
for start, end, weight in edges:
if distances[start] != float('inf') and distances[start] + weight < distances[end]:
return []
return [-1 if value == float('inf') else value for value in distances]Where you will hit this: Relax Signed Edges with Bellman-Ford(opens in a new tab)
Skips negative-cycle detection and returns unstable distances.
Prevent it: Preserve this proof obligation: Every recorded distance is a valid path cost, and the method eventually considers every path that could improve it.
Avoid
def bellman_ford_distances(n,edges,source):
d=[0]*n
return dUse instead
def bellman_ford_distances(node_count, edges, source):
distances = [float('inf')] * node_count
distances[source] = 0
for _ in range(node_count - 1):
changed = False
for start, end, weight in edges:
if distances[start] != float('inf') and distances[start] + weight < distances[end]:
distances[end] = distances[start] + weight
changed = True
if not changed:
break
for start, end, weight in edges:
if distances[start] != float('inf') and distances[start] + weight < distances[end]:
return []
return [-1 if value == float('inf') else value for value in distances]Where you will hit this: Relax Signed Edges with Bellman-Ford(opens in a new tab)
Include visited state, boundary cases, recursion depth, and hidden copying costs in correctness and complexity analysis.
Prevent it: Count every sort, slice, copy, membership check, heap update, and recursive frame before claiming O(VE).
Avoid
def bellman_ford_distances(n,edges,source):
d=[0]*n
return dUse instead
def bellman_ford_distances(node_count, edges, source):
distances = [float('inf')] * node_count
distances[source] = 0
for _ in range(node_count - 1):
changed = False
for start, end, weight in edges:
if distances[start] != float('inf') and distances[start] + weight < distances[end]:
distances[end] = distances[start] + weight
changed = True
if not changed:
break
for start, end, weight in edges:
if distances[start] != float('inf') and distances[start] + weight < distances[end]:
return []
return [-1 if value == float('inf') else value for value in distances]Where you will hit this: Lowest Common Ancestor in a BST(opens in a new tab)
Official Python documentation supports language behavior. Canonical problem pages provide additional practice context.
python-docs · checked 2026-07-27