Graph Theory37 sections · 1633 units
Open in Course

When to Use Bellman-Ford

(Dijkstra vs Bellman-Ford)

Use Dijkstra when all edge weights are non-negative. It is faster: O((V+E)logV)O((V + E) \log V) vs O(VE)O(VE). Dijkstra is the default choice for shortest paths. Use Bellman-Ford when edges can be negative or when you need to detect negative cycles. Bellman-Ford is the only standard algorithm that handles negative weights correctly.

Use SPFA when you need Bellman-Ford's correctness but want better average-case performance. SPFA is often faster in practice but has the same worst-case complexity.