Dijkstra on a six-node graph
There is no code here. This is what Dijkstra does — step by step, on a small graph and with both queues: the heap and the linear scan.
The graph we will watch
Six books. Every edge has a cost — the days between two borrows:
adj[0] = [1:4 2:1]
adj[1] = [0:4 2:2 3:5]
adj[2] = [0:1 1:2 4:12]
adj[3] = [1:5 4:3 5:2]
adj[4] = [2:12 3:3]
adj[5] = [3:2]
(1:4 — neighbour 1, four days.) We look for a route from 0 to 4.
Two answers to the same question
Lesson 14's BFS looks for the fewest hops:
BFS 0 → 2 → 4 2 hops 1 + 12 = 13 days
Dijkstra looks for the fewest days:
Dijkstra 0 → 2 → 1 → 3 → 4 4 hops 1 + 2 + 5 + 3 = 11 days
BFS's route is shorter in hops and more expensive in days. How Dijkstra finds its route — here.
Dijkstra with a heap
Every heap entry is node:distance. You take the cheapest; on finding a
cheaper route to a node you push it again and leave the old entry alone.
start heap [0:0]
pop 0:0 push 1:4 2:1 heap [2:1 1:4] 1
pop 2:1 push 1:3 4:13 heap [1:3 1:4 4:13] 2
pop 1:3 push 3:8 heap [1:4 4:13 3:8] 2
pop 1:4 STALE — skip heap [3:8 4:13] 1
pop 3:8 push 4:11 5:10 heap [5:10 4:13 4:11] 2
pop 5:10 nothing cheaper heap [4:11 4:13] 1
pop 4:11 that is the target — STOP
total: 9
route 0 → 2 → 1 → 3 → 4 cost 11 settled 6 nodes
The last column is the heap's work: how many comparisons the heap itself made
(the same counter you will see in code as c.Hit()).
Dijkstra without a heap
The same search, but every time the cheapest is needed, all six nodes are looked at:
choose 0:0 looked at 6
choose 2:1 looked at 6
choose 1:3 looked at 6
choose 3:8 looked at 6
choose 5:10 looked at 6
choose 4:11 that is the target — STOP looked at 6
total: 36
The same numbers side by side
0 → 4 |
route | days | settled | queue work |
|---|---|---|---|---|
| BFS (fewest hops) | 0 2 4 |
13 | — | — |
| Dijkstra, heap | 0 2 1 3 4 |
11 | 6 | 9 |
| Dijkstra, scan | 0 2 1 3 4 |
11 | 6 | 36 |
Three observations. Worth not skipping, because the next steps are made of nothing else.
1. Fewest hops is not cheapest. BFS's route has two hops, but one of them is twelve days. Dijkstra takes four, and two days cheaper. In the library's network the same thing is 12 hops and 402 days against 191.
2. Node 1 entered the heap twice. First at 4 days straight from 0, then
at 3 through 2. The old entry 1:4 stayed in the heap and was popped after 1
was already settled — we simply skipped it. That is lazy deletion: the heap
ends up with more entries than nodes — here 8 pushes for six nodes.
3. Both queues gave the same route and the same cost. Only the work differs: 9 against 36. The heap finds the cheapest with a few comparisons; the scan walks everything every time, settled nodes included.
Why it is correct
When you take the cheapest unprocessed node next, its distance is already final.
The proof fits in a sentence. Suppose there were a cheaper route to node v that
we have not found. That route must cross the boundary between processed and
unprocessed nodes — and its first unprocessed node u is cheaper than v. So u
would have been taken before v, not after.
Which is impossible, provided no edge is negative. Because then "further along the route" means "not cheaper". Step 3 shows what happens when that condition is broken.
Compare with lesson 14: BFS's layers were the same argument in a simpler form. Dijkstra is BFS with cost in place of layers.
Stale entries: why the heap holds more elements than nodes
When a cheaper route to w is found, w may already be in the heap with an older,
dearer value — like 1:4 in the table. What to do?
There are two answers.
Decrease-key: find w in the heap and correct its value. But the heap does
not know where w is — that needs a separate index, kept in step with every
swap. In lesson 13 every Push and Pop swapped elements as it sifted up or
down; each of those swaps would get more expensive.
Lazy deletion: simply push w again. The heap will hold several entries for
the same node; the first one popped is the cheapest, and the rest are
recognised by done[v] and skipped.
The second is the one transit writes, and the one you will write. The price: the
heap holds somewhat more than it has nodes. How much, you measure in step 3, on
your library's network.
The cost
V extractions at O(log V) each, E pushes at O(log V) each:
O((V + E) log V) with a heap. O(V² + E) with a linear scan.
In a sparse graph — and yours has mean degree 1.49 — the first is nearly linear, the second quadratic.
What comes next
Now you know what Dijkstra does. In the next step you write it as Go code, on the heap you already have from lesson 13.