Understanding Dijkstra Algorithm

主宰稳场 提交于 2019-12-13 05:43:29

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!