⚡ Build the skills to land top-paying quant jobs. Offer ends in 17d 22h 19m 18s17 days 22 hours 19 minutes 18 seconds.
⚡ Build the skills to land top-paying quant jobs. Offer ends in 17d 22h 19m 18s17 days 22 hours 19 minutes 18 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.