问题
I'm trying to understand the Dijkstra Algorithm for finding the shortest path.
I've came up to this example, where the top table corresponds to the image in the bottom left corner.
Now, my problem is that I don't understand the transition from step 1 to step 2:
When we are in UX we can travel to UXV by adding the cost of X to V (which is 2) to our current cost (which is 1; the cost of UX). So the sum would be 3, but since this I bigger then the 2 we already found we don't change it. In step 1 we have two options which both have the same cost; UXY and UXV, but why does the algorithm choose to go to UXY instead of UXV?
Thanks in advance!
回答1:
When you have two or more options with the same cost, it does not make any difference with which option you proceed.
In the Dijkstra's algorithm Wikipedia article there is a section with a pseudocode for implementing the algorithm. You can see that in the pseudocode there is a line u ← vertex in Q with min dist[u]
, which means that you choose one option with the lowest costs. When you have more options with the same cost, you just take any of those.
For your concrete example, this means that you could also go to UXV instead of UXY. This might lead to more steps, but the end result is the same when the algorithm finishes.
来源:https://stackoverflow.com/questions/40247747/understanding-dijkstra-algorithm