I understand what Dijkstra’s algorithm is, but I don’t understand why it works.
When selecting the next vertex to examine, why does Dijkstra’s algorithm select the one with the smallest weight? Why not just select a vertex arbitrarily, since the algorithm visits all vertices anyway?
You can think of Djikstra’s algorithm as a water-filling algorithm (i.e. a pruned breadth-first search). At each stage, the goal is to cover more of the whole graph with the lowest-cost path possible. Suppose you have vertices at the edge of the area you’ve filled in, and you list them in terms of distance:
Could there possibly be a cheaper way to get to vertex
v1? If so, the path must go throughv0, since no untested vertex could be closer. So you examine vertexv0to see where you can get to, checking if any path throughv0is cheaper (to any other vertex one step away).If you peel away the problem this way, you’re guaranteed that your distances are all minimums, because you always check exactly that vertex that could lead to a shortest path. Either you find that shortest path, or you rule it out, and move on to the next vertex. Thus, you’re guaranteed to consume one vertex per step.
And you stop without doing any more work than you need to, because you stop when your destination vertex occupies the “I am smallest”
v0slot.Let’s look at a brief example. Suppose we’re trying to get from
1to12by multiplication, and the cost between nodes is the number you have to multiply by. (We’ll restrict the vertices to the numbers from1to12.)We start with
1, and we can get to any other node by multiplying by that value. So node2has cost2,3has cost3, …12has cost12if you go in one step.Now, a path through
2could (without knowing about the structure) get to12fastest if there was a free link from2to12. There isn’t, but if there was, it would be fastest. So we check2. And we find that we can get to4for cost2, to6for3, and so on. We thus have a table of costs like so:Okay, now maybe we can get to
12from3for free! Better check. And we find that3*2==6so the cost to6is the cost to3plus2, and to9is plus3, and12is plus4.Fair enough. Now we test
4, and we see we can get to8for an extra2, and to12for an extra3. Again, the cost to get to12is thus no more than4+3=7:Now we try
5and6–no improvements so far. This leaves us withNow, for the first time, we see that the cost of getting to
8is less than the cost of getting to7, so we had better check that there isn’t some free way to get to12from8. There isn’t–there’s no way to get there at all with integers–so we throw it away.And now we see that
12is as cheap as any path left, so the cost to reach12must be7. If we’d kept track of the cheapest path so far (only replacing the path when it’s strictly better), we’d find that3*4is the first cheapest way to hit12.