Dijkstras算法似乎不起作用,我的理解一定有缺陷
这是我对 Dijkstra 算法维基百科描述的如何处理下图的解释。
首先,它标记到所有邻居节点的最短距离,因此 A 得到 1,C 得到 7。然后它选择具有当前最短路径的邻居。这是A。原点被标记为已访问,不会再次考虑。从原点通过 A 到 B 的最短(且唯一)路径现在为 12。A 被标记为已访问。从起点经过 B 到目的地的最短路径是 13。B 被标记为已访问。从起始点到目的地 C 的最短路径是 14,但是这是之前到 C 的最短路径(即 7)的两倍,因此 14 被丢弃。目的地现已标记为已访问。
目的地已被标记为已访问,因此算法已完成。结果如下:
那么我是否误解了描述?维基百科上的描述是错误的吗?或者我是否以某种方式偶然发现了一个表明 Dijkstra 算法不正确的案例(我对此非常怀疑)?我注意到所选择的路径似乎是以贪婪的方式选择的,但显然这是该算法的定义特征之一,但是在这种情况下,它似乎给出了错误的结果。
Here's my interpretation of how Dijkstra's algorithm as described by wikipedia would deal with the below graph.
First it marks the shortest distance to all neighbouring nodes, so A gets 1 and C gets 7. Then it picks the neighbour with the current shortest path. This is A. The origin is marked as visited and will not be considered again. The shortest (and only) path from the origin to B through A is now 12. A is marked as visited. The shortest path from the origin to the Destination through B is 13. B is marked as visited. The shortest path from the Origin to C through the destination is 14, but this is double the previous shortest path to C, which is 7, so the 14 is discarded. The destination is now marked as visited.
Destination has been marked visited, therefore the alogorithm is complete. Here's the result:
So am I misinterpretting the description? Is the description on wikipedia wrong? Or have I somehow stumbled upon a case that shows Dijkstra's algorithm is incorrect (which I highly doubt)? I've noticed that the path chosen seems to be picked in a greedy manner, but apparently this is one of the defining characteristics of the algorithm, however in this case it seems to give the wrong result.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用您的示例:
未访问的
已访问
对于当前节点,考虑其所有未访问的邻居并计算它们的暂定距离。
我们现在在 A。
从 A 出发,唯一的路径是 B,这给了我们
C 现在是最小距离,并且是新的当前节点,
因为 D 是目的地并且 8 < 。 12、选择路线CD。
Using your example:
Unvisited
Visited
For the current node, consider all of its unvisited neighbors and calculate their tentative distances.
We are now at A.
From A, the only path is B, this gives us
C is now lowest distance, and is the new current node
Since D is destination and 8 < 12, the route CD is chosen.
访问 B 后,到目前为止所经过的总距离为 12。7 小于 12,因此搜索将在 C 处恢复。
只有在考虑了节点的所有邻居后,才会将节点标记为已访问。一开始,原点被标记为已访问,并分配距离为零。所有其他节点都标记为未访问。
After visiting B, the total distance travelled so far is 12. 7 is smaller than 12, and hence the search will resume at C.
A node is only marked visited after all its neighbours have been considered. In the beginning, the origin is marked as visited and is assigned a distance of zero. All the other nodes are marked unvisited.
不,它会选择具有当前最短路径的未访问节点。
维基百科写道:
它并没有说它是邻居。
维基百科的伪代码说:
其中 Q 包含所有尚未访问的节点。
No, it picks the unvisited node with the current shortest path.
Wikipedia writes:
It doesn't say it is a neighbor.
And the pseudocode from Wikipedia says:
where Q contains all nodes that have not yet been visited.