Card 02/ 11
Dijkstra's Algorithm: Settling the Closest Vertex Next
Take the unsettled vertex with the smallest distance, settle it, and relax its edges; with no negative edges, nothing can later beat it.
Dijkstra's algorithm finds the shortest path from one source to every vertex of a graph whose weights are not negative. It repeatedly picks the vertex with the smallest distance that is not yet final, makes it final, and relaxes the edges out of it. This card traces it on the topic's graph from A and keeps its four parts apart, because confusing them is the usual way a trace goes wrong.
The four parts are: the distances, the best length found yet for each vertex; the settled set, the vertices whose distance is final; the parents, which rebuild the paths; and a priority queue, a structure that hands back its smallest entry first, holding (distance, vertex) pairs for vertices not yet settled. A vertex is settled when it leaves the queue with its smallest distance. The queue is not the paths and not the settled set: it is only a way to find the next vertex to settle.
When a distance is lowered, the new pair goes into the queue and the old one stays there, stale, until it surfaces and is skipped. The steps below are from a run, one per vertex settled.
Dijkstra's algorithm on the topic's graph from A: the distances, settled set and parents after each vertex is settled, and the queue
Settle A at 0
| Vertex | Distance | Settled | Parent |
|---|---|---|---|
| A | 0 | yes | |
| B | 2 | no | A |
| C | 7 | no | A |
| D | ∞ | no | |
| E | ∞ | no |
queue: (2,B) (7,C)Settle B at 2
| Vertex | Distance | Settled | Parent |
|---|---|---|---|
| A | 0 | yes | |
| B | 2 | yes | A |
| C | 5 | no | B |
| D | 10 | no | B |
| E | ∞ | no |
queue: (5,C) (7,C) (10,D)(7,C) is now stale: C's distance is 5.
Settle C at 5
| Vertex | Distance | Settled | Parent |
|---|---|---|---|
| A | 0 | yes | |
| B | 2 | yes | A |
| C | 5 | yes | B |
| D | 7 | no | C |
| E | 11 | no | C |
queue: (7,C) (7,D) (10,D) (11,E)(7,C) surfaces next, but C is settled, so it is skipped.
Settle D at 7
| Vertex | Distance | Settled | Parent |
|---|---|---|---|
| A | 0 | yes | |
| B | 2 | yes | A |
| C | 5 | yes | B |
| D | 7 | yes | C |
| E | 8 | no | D |
queue: (8,E) (10,D) (11,E)Settle E at 8
| Vertex | Distance | Settled | Parent |
|---|---|---|---|
| A | 0 | yes | |
| B | 2 | yes | A |
| C | 5 | yes | B |
| D | 7 | yes | C |
| E | 8 | yes | D |
queue: (10,D) (11,E)Both entries are stale, and each is skipped as it surfaces.
Each vertex's distance was lowered more than once before it settled: C from 7 to 5, D from 10 to 7, and E from 11 to 8. Settling is what makes a distance final, and the queue's order is what makes settling safe. When a vertex leaves the queue with the smallest distance of all the unsettled ones, any other route to it would have to leave the settled set through a vertex at least as far away and then add edges. With no negative weights, adding edges never makes a path shorter, so no such route can beat it.
The same argument marks the algorithm's limit: with one negative edge, a route that arrives later can be shorter, which Running Dijkstra's Algorithm on a Negative Edge shows. Dijkstra's algorithm is greedy in the sense of Greedy Algorithms: it commits to the closest vertex and never revisits it.
Water poured in at A fills the nearest pools first and reaches E last, along the cheapest channels. The picture breaks because water spreads along every channel at once, while the algorithm settles exactly one vertex at a time.
Reach for Dijkstra's algorithm when a problem asks for the cheapest, fastest or shortest route from one place, and every cost is 0 or more: road distances, travel times, network delays.
- Time
- O((V + E) log V)
- each edge may push one entry, and each push or pop costs O(log V)
- Extra space
- O(V + E)
- the heap keeps one entry per lowered distance until it is popped
- Works on
- Non-negative weights only
- directed or undirected
← → move through sections and cards