What's happening?
- Every distance starts at infinity, except the start at 0.
- Settle the unsettled node with the smallest distance — no other route to it can be shorter.
- For each of its neighbours, check whether going through it is cheaper than the best route known so far; if so, update.
Complexity
- Time
- O((V + E) log V) with a priority queue
- Space
- O(V)
Where you'll meet it
Maps and navigation, network routing (OSPF), game path-finding (A* is Dijkstra with a hint), and cheapest-flight style searches.
Common mistake
Using it with negative edge weights. "Closest first" is only safe when costs never go down — use Bellman-Ford instead.
FAQ
Why can a settled node never change?
Every other route would have to pass through an unsettled node that is already at least as far, and weights cannot be negative.
What is the priority queue for?
Finding the closest unsettled node quickly instead of scanning all of them each time.
Dijkstra vs BFS?
BFS counts edges; Dijkstra adds up their costs. With all costs equal they give the same answer.