Dijkstra’s Algorithm is Basically a BFS Algorithm
A small note on a commonly mentioned algorithm — trying not to sound too pretentious 😅
If you, like me, get startled every time you see Dijkstra’s algorithm, forgetting how it works exactly — it is essentially a breadth-first search (BFS), but with two twists:
- The graph is weighted
- The queue is min-priority, not FIFO
So instead of blindly processing nodes in a queue one by one, we always pick the node with the lowest cumulative distance.
Once you realize this, the algorigthm becomes quite simple to implement, and the most of the complexity moves into building an efficient min-priority queue based on a Fibonacci or pairing heap.
Strictly speaking, it is BFS that is a special case of Dijkstra’s algorithm for unweighted graphs, not the other way around.