Skip to article content
Interactive Notes

Algorithms Visualized

Graph Shortest Paths

Finding the cheapest route with BFS, Dijkstra, and Bellman–Ford

When every edge has the same weight, breadth-first search already finds shortest paths. Dijkstra’s algorithm handles non-negative weights by always settling the closest unsettled node, in O((n+m)log⁡n)O((n + m) \log n) time with a heap. Bellman–Ford relaxes every edge n−1n - 1 times, which takes O(nm)O(nm) time but also works with negative weights.