Dijkstra: finish the closest node first
After this: Work out the cheapest cost to each node by hand, and say why the closest node is safe to finalise.
On this page · 9 sections · 2 checks
In the last lesson, BFS gave the wrong answer because it counts edges and ignores weights. We need a method that adds up weights. That method is , named after the computer scientist Edsger Dijkstra. The name is said "DIKE-stra".
It answers one question: starting from one node, what is the cheapest cost to reach each other node?
In this lesson we work it out by hand on three nodes. The next lesson runs it on the full graph and in code.
Start with what you know
Here are the three nodes again. We start at A.
We keep a table of the best we know so far for each node. In these lessons, distance means the total weight of a path from the start.
At the beginning we know one fact: A is 0 away from itself. We know no route to B or C yet, so we write ∞, the sign for infinity. Here ∞ means "no route known yet".
| A | B | C |
|---|---|---|
| 0 | ∞ | ∞ |
The idea: finish the closest node first
Look at the two edges that leave A. They tell us that B can be reached at 4 and C can be reached at 1. Both are better than ∞, so we write them in the table.
Which of these numbers can we trust? Look at C, at 1. Could another path reach C for less? Any other path from A must begin with the edge A→B, which already costs 4. Further edges can only add to that. So nothing can beat 1.
This means we can stop working on C. To a node means to decide that its distance is the true cheapest cost and will never change.
We cannot say the same about B at 4, because a path through C might be cheaper. So the safe node to finalise is always the closest one that is not finished yet. That gives us the whole method.
Take the closest unfinished node and finalise it. Then look at each edge that leaves it, and see whether that edge gives a shorter way to its neighbour. Repeat.
This works because weights in this tutorial are , which means zero or more. A road cannot have a length below zero, so a path never gets cheaper as it grows.
Three nodes, one row per step
We need one more thing: a waiting list of nodes to look at. Each entry in it is a note with a node and a distance. So (C, 1) means "C can be reached at distance 1". The waiting list always gives us the entry with the smallest distance.
Read this table one row at a time.
| Step | Take from the list | What happens | Table: A B C | List afterwards |
|---|---|---|---|---|
| Start | nothing | A is at 0 and goes on the list | 0 ∞ ∞ | (A, 0) |
| 1 | (A, 0) | Finalise A. A→B: 0 + 4 = 4, better than ∞. A→C: 0 + 1 = 1, better than ∞. Add both to the list | 0 4 1 | (C, 1) (B, 4) |
| 2 | (C, 1) | Finalise C. C→B: 1 + 2 = 3, better than 4. Change B to 3 and add (B, 3) to the list | 0 3 1 | (B, 3) (B, 4) |
| 3 | (B, 3) | Finalise B. No edges leave B in this small graph | 0 3 1 | (B, 4) |
| 4 | (B, 4) | B is already final at 3, so this note is out of date. Ignore it | 0 3 1 | empty |
The list is empty, so we stop. The final distances are A 0, C 1 and B 3. The cheapest way to B costs 3, not the 4 of the direct edge.
Why was (B, 4) still there in step 4? We added it in step 1, and it stayed on the list after step 2 found a better distance. So we compared it with the table and ignored it. The next lesson says more about these notes.
A name: relaxing an edge
In step 2, we checked whether the edge C→B gave a shorter way to B. It did, so we wrote down the new distance. This check is called . The word has nothing to do with resting, but you will meet it in books and interviews. To relax the edge from a node to its neighbour means:
- Add the node's distance and the edge's weight.
- If that total is smaller than the neighbour's distance in the table, write the new distance and add a new entry to the list.
- If it is not smaller, change nothing.
Watch it happen
This is the table above, running in front of you.
- Dijkstra from AThis is the table you read above. The strip called Heap is the waiting list, B:4 means the entry (B, 4), and pop means take out the smallest entry. A step labelled Stale is an out-of-date note. Step through and find each row of the table.
Pick a start node and run an algorithm.
Why the closest node is safe
The reasoning we used for C works at every step. The node we take has the smallest distance of everything still waiting. Any path the table has not counted yet would have to go through something still waiting, and that is at the same distance or further away. Each further edge adds zero or more. So no other path can be cheaper, and we can trust a node's distance as soon as we take it.
When a weight is negative
That reasoning needs one fact: edges never take anything away. A is a weight below zero, such as a refund on a route. It makes a path cheaper as it grows. Take the edges A→B 1, A→C 5 and C→B −10.
- Finalise A. B gets 1 and C gets 5.
- The smallest entry is (B, 1), so B is finalised at 1.
- But the path A → C → B costs 5 − 10 = −5, which is less than 1.
B was called final, and that was wrong. So Dijkstra's method can be trusted only when no weight is negative.
Check yourself
Now use the full graph from the first lesson. Dijkstra's algorithm starts from A. It finalises A and relaxes A's edges, then it finalises C and relaxes C's edges. What does the distance table hold now?
Why does Dijkstra's algorithm need non-negative edge weights, meaning weights of zero or more?
Connect
- Run Dijkstra's algorithm by hand: take the closest unfinished node, finalise it, and relax its edges.
- Say what relaxing an edge means: check whether it gives a shorter way, and if so, write down the new distance.
- Say why the closest node is safe to finalise, and why that reasoning needs non-negative weights, meaning zero or more.