Dijkstras算法似乎不起作用,我的理解一定有缺陷

发布于 2024-12-22 10:13:15 字数 614 浏览 1 评论 0原文

这是我对 Dijkstra 算法维基百科描述的如何处理下图的解释。

首先,它标记到所有邻居节点的最短距离,因此 A 得到 1,C 得到 7。然后它选择具有当前最短路径的邻居。这是A。原点被标记为已访问,不会再次考虑。从原点通过 A 到 B 的最短(且唯一)路径现在为 12。A 被标记为已访问。从起点经过 B 到目的地的最短路径是 13。B 被标记为已访问。从起始点到目的地 C 的最短路径是 14,但是这是之前到 C 的最短路径(即 7)的两倍,因此 14 被丢弃。目的地现已标记为已访问。

目的地已被标记为已访问,因此算法已完成。结果如下:

Suppositly failed case for Dijkstra

那么我是否误解了描述?维基百科上的描述是错误的吗?或者我是否以某种方式偶然发现了一个表明 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:

Supposedly failing case for Dijkstra

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

街角卖回忆 2024-12-29 10:13:15

<块引用>

为每个节点分配一个暂定距离值:将初始节点设置为零,将所有其他节点设置为无穷大。将所有节点标记为未访问。将初始节点设置为当前节点。创建未访问节点的集合,称为未访问集,由除初始节点之外的所有节点组成。 (维基)

使用您的示例:

未访问的

A = inf;
B = inf;
C = inf;
Dest = inf;

已访问

Origin = 0;

对于当前节点,考虑其所有未访问的邻居并计算它们的暂定距离。

O => A = 1;
O => C = 7;

我们现在在 A。

从 A 出发,唯一的路径是 B,这给了我们

O => AB = 12;
O => C = 7

C 现在是最小距离,并且是新的当前节点,

O => CD = 8

因为 D 是目的地并且 8 < 。 12、选择路线CD。

Assign to every node a tentative distance value: set it to zero for our initial node and to infinity for all other nodes. Mark all nodes unvisited. Set the initial node as current. Create a set of the unvisited nodes called the unvisited set consisting of all the nodes except the initial node. (Wiki)

Using your example:

Unvisited

A = inf;
B = inf;
C = inf;
Dest = inf;

Visited

Origin = 0;

For the current node, consider all of its unvisited neighbors and calculate their tentative distances.

O => A = 1;
O => C = 7;

We are now at A.

From A, the only path is B, this gives us

O => AB = 12;
O => C = 7

C is now lowest distance, and is the new current node

O => CD = 8

Since D is destination and 8 < 12, the route CD is chosen.

浅语花开 2024-12-29 10:13:15

访问 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.

江南烟雨〆相思醉 2024-12-29 10:13:15

然后它选择具有当前最短路径的邻居。

不,它会选择具有当前最短路径的未访问节点。

维基百科写道:

将暂定距离最小的未访问节点设置为下一个“当前节点”,并返回步骤3。

它并没有说它是邻居。

维基百科的伪代码说:

 8      while Q is not empty:                  // The main loop
 9          u := vertex in Q with smallest distance in dist[] ;

其中 Q 包含所有尚未访问的节点。

Then it picks the neighbour with the current shortest path.

No, it picks the unvisited node with the current shortest path.

Wikipedia writes:

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

It doesn't say it is a neighbor.

And the pseudocode from Wikipedia says:

 8      while Q is not empty:                  // The main loop
 9          u := vertex in Q with smallest distance in dist[] ;

where Q contains all nodes that have not yet been visited.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文