Practice in Lab: Bellman–Ford →
Handle negative costs with Bellman-Ford
Dijkstra's algorithm breaks when an edge can lower a cost after a node is settled. Bellman-Ford instead relaxes every edge again and again. A shortest path uses at most n - 1 edges, so n - 1 rounds are enough.
edges = [("a", "b", 4), ("a", "c", 2),
("c", "b", -3)]
# a -> c -> b costs -1, cheaper than a -> bThink before coding
If any edge still improves a distance after n - 1 rounds, a cycle with negative total cost is reachable and no cheapest path exists. Report that instead of returning wrong numbers. Work through the example by hand before changing the starter. Decide what should happen for an empty input, a single value and a repeated value where those cases apply. The checks use several inputs so a hard-coded answer cannot stand in for the rule.
Your task
Write bellman_ford(nodes, edges, start) with edges as (u, v, cost). Return a dict of cheapest costs to reachable nodes, or None when a negative cycle is reachable from start.
The explanation opens here once every check passes.