简体   繁体   English

可以使用Bellman-Ford算法在只有正边的图形上找到最短路径吗?

[英]Can Bellman-Ford algorithm be used to find shorthest path on a graph with only positive edges?

I know that Dijkstra's algorithm can be used only on positive lengths of edges, and Bellman-Ford can be used when the graph also has negative ones. 我知道Dijkstra的算法只能在正的边长上使用,而Bellman-Ford可以在图形也有负的边上使用。

Suppose we have a graph with only positive edges, though. 假设我们有一个只有正边的图。 Will Bellman-Ford give the same results as Dijkstra? Bellman-Ford会给出与Dijkstra相同的结果吗?

Yes, it will give the same results. 是的,它将给出相同的结果。 It will run slower, though, as it could also have been used for graphs with negative edges (subject to the absence of negative cycles). 但是,它的运行速度会变慢,因为它也可能用于具有负边的图形(取决于没有负循环)。 If you look at the proof of BF's correctness, there is no assumption there that some of the edges are negative. 如果您查看BF正确性的证明,则不能假设某些边缘为负。

I want to add something to Ami Tavory's answer. 我想在Ami Tavory的答案中添加一些内容。 Bellman-ford's algorithm can be made a little bit faster if you can detect that on any pass, there is no node value update, then return from there. 如果您可以检测到任何遍,没有节点值更新,然后从那里返回,则可以使Bellman-ford算法更快一些。 If there is no node update then it proves that every node traversal is complete. 如果没有节点更新,则证明每个节点遍历都已完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM