为什么我们不能对权重为负的图应用 Dijkstra 算法?

发布于 2024-09-08 06:51:58 字数 38 浏览 4 评论 0原文

为什么我们不能将 Dijkstra 算法应用于具有负权重的图?

Why can't we apply Dijkstra's algorithm for a graph with negative weights?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

为你鎻心 2024-09-15 06:51:58

如果每次从 C 到 D 都获得报酬,那么找到从 A 到 B 的最便宜路径意味着什么?

如果两个节点之间存在负权重,则“最短路径”就是在这两个节点之间永远来回循环。跳数越多,路径就越“短”。

这与算法无关,而与无法回答这样的问题有关。

编辑

上述声明假设双向链接。如果没有整体权重为负的循环,那么您就无法永远循环并获得报酬。

在这种情况下,Dijkstra 算法仍可能失败:

考虑两条路径:

  • 一条最优路径,在穿过权重为 -25 的最终边缘之前,成本为 100,总共为 75;另
  • 一条次优路径,其成本为 100。没有负权边,总成本为 90。Dijkstra

算法将首先研究次优路径,并在找到它时宣布自己已完成。它永远不会跟踪比找到的第一个解决方案更糟糕的子路径

What does it mean to find the least expensive path from A to B, if every time you travel from C to D you get paid?

If there is a negative weight between two nodes, the "shortest path" is to loop backwards and forwards between those two nodes forever. The more hops, the "shorter" the path gets.

This is nothing to do with the algorithm, and all to do with the impossibility of answering such a question.

Edit:

The above claim assumes bidirectional links. If there is no cycles which have an overall negative weight, you do not have a way to loop around forever, being paid.

In such a case, Dijkstra's algorithm may still fail:

Consider two paths:

  • an optimal path that racks up a cost of 100, before crossing the final edge which has a -25 weight, giving a total of 75, and
  • a suboptimal path that has no negatively-weighted edges with a total cost of 90.

Dijkstra's algorithm will investigate the suboptimal path first, and will declare itself finished when it finds it. It will never follow up the subpath that is worse than the first solution found

北音执念 2024-09-15 06:51:58

我给你举一个反例。考虑下图

http://img853.imageshack.us/img853/7318/3fk8.png

假设您从顶点 A 开始,并且想要到达 D 的最短路径。 Dijkstra 的算法将执行以下步骤:

  1. A 标记为已访问,并将顶点 BC 添加到队列
  2. 从具有最小距离的队列顶点获取。它是 B
  3. B 标记为已访问并将顶点 D 添加到队列。
  4. 从队列中获取。不是,它是顶点D
  5. D 标记为已访问

Dijkstra 说从 AD 的最短路径的长度为 2,但这显然不是真的。

I will give you an counterexample. Consider following graph

http://img853.imageshack.us/img853/7318/3fk8.png

Suppose you begun in vertex A and you want shortest path to D. Dijkstra's algorithm would do following steps:

  1. Mark A as visited and add vertices B and C to queue
  2. Fetch from queue vertex with minimal distance. It is B
  3. Mark B as visited and add vertex D to queue.
  4. Fetch from queue. Not it is vertex D.
  5. Mark D as visited

Dijkstra says shortest path from A to D has length 2 but it is obviously not true.

别挽留 2024-09-15 06:51:58

想象一下,其中有一个有向循环的有向图,并且围绕该循环的总“距离”是负权重。如果在从“开始”顶点到“结束”顶点的途中可以经过该有向循环,则可以简单地围绕该有向循环任意次数。

这意味着您可以使穿过图表的路径具有无限负距离(或实际上如此)。

然而,只要你的图周围没有有向循环,你就可以使用 Dijkstra 算法,而不会发生任何爆炸。

话虽这么说,如果你有一个负权重的图表,你可以使用 Belman-Ford< /a> 算法。然而,由于该算法的通用性,它的速度有点慢。 Bellman-Ford 算法需要 O(V·E),其中 Dijkstra 算法需要 O(E + VlogV) 时间

Imagine you had a directed graph in it with a directed cycle, and the total "distance" around that was a negative weight. If on your way from the Start to the End vertex you could pass through that directed cycle, you could simply go around and around the directed cycle an arbitrary number of times.

And that means you could make you path across the graph have an infinitely negative distance (or effectively so).

However, as long as there are no directed cycles around your graph, you could get away with using Dijkstra's Algorithm without anything exploding on you.

All that being said, there if you have a graph with negative weights, you could use the Belman-Ford algorithm. Because of the generality of this algorithm, however, it is a bit slower. The Bellman-Ford algorithm takes O(V·E), where the Dijkstra's takes O(E + VlogV) time

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