Dijkstra's algorithm: What to do if there are two or more nodes with minimal weight?

风流意气都作罢 提交于 2019-12-01 15:26:43

问题


In Dijkstra's algorithm what should I do if there are two or more nodes with minimal weights at a point in the algorithm?

In wikipedia: http://en.wikipedia.org/wiki/Dijkstra%27s_algorithm at step no. 6, it says

"Set the unvisited node marked with the smallest tentative distance as the next 'current node' and go back to step 3."

What if there are two or more nodes with "smallest tentative distance".

Can anyone help me with the algorithm?


回答1:


Short Answer

Just pick either. Unless you have another heuristic to work with, you can't tell which would be better to pick.


Bit More Explaination

Think about sorting some elements into an array:

9 6 3 3 8

sorted with lowest first is

3 3 6 8 9

If you were to query that array to determine the lowest, the answer is 3. Which 3 doesn't matter.


Similarly if you were to have some more information. Say for example that those ints were actually floats and were sorted by their integer parts. You might end up with the array:

3.2  3.1  6.0  8.5  9.2

Here you have another heuristic to work with, you can check the decimal part too, and you can determine that 3.1 is the lowest.



来源:https://stackoverflow.com/questions/9264799/dijkstras-algorithm-what-to-do-if-there-are-two-or-more-nodes-with-minimal-wei

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