Price your future right. 60% off with code BLACKSCHOLES at USD checkout. Offer ends in 1d 16h 01m 41s1 days 16 hours 1 minutes 41 seconds.
Price your future right. 60% off with code BLACKSCHOLES at USD checkout. Offer ends in 1d 16h 01m 41s1 days 16 hours 1 minutes 41 seconds.

Sign in to watch this lesson
Advanced - Graphs · 5:00
Bellman–Ford finds shortest paths when some edges are negative by relaxing every edge of an edge list in up to n − 1 phases: after phase i, every vertex whose shortest path has at most i edges is final, and a phase that changes nothing ends the run early. One more phase that still relaxes reveals a negative cycle reachable from the source, which we print by walking n parent steps first. SPFA re-checks only the vertices whose label just dropped, which is fast on typical graphs but still O(nm) in the worst case.