WRITE · TRACE · UNDERSTAND
Bellman–Ford
Relax weighted edges and detect reachable negative cycles
Your experiment
shortest(n, edges, start) returns distances for nodes 0..n-1, using float("inf") for unreachable nodes. Each edge is (from, to, weight). Return None if a negative cycle is reachable from start.
Python runs in your browser. Your draft stays on this device.
What to look for
One full pass can extend a shortest route by another edge. A simple path has at most n-1 edges. Any reachable edge that can still improve a distance after those passes proves a reachable negative cycle; an unreachable cycle must not invalidate the result.
Watch your code work
Ready to experimentWrite a solution or load the worked answer, then choose Trace & check.
Step through arrays, variables and training curves produced by your own Python.