Dijkstra's Algorithm and Cycles

北城余情 提交于 2020-01-03 10:57:20

问题


It's stated in a book that "Dijkstra's algorithm only works with Directed Acyclic Graphs".

It appears the algorithm works for graphs with cycles too as long as there are no negative cycles. Is that correct?

Edit 1: The book "Grokking Algorithms" -Aditya Bhargava. Chapter 7. Page 122.


回答1:


I'm the author of Grokking Algorithms. Sorry for this error—Dijkstra's algorithm does work on graphs with cycles, as long as it is a positive weight cycle. I have updated the errata page to reflect this error. Dijkstra's doesn't work on negative weight cycles, and here's an image that explains why:




回答2:


Actually, it works as long as all edge weights are non-negative. This is a stronger condition as "no negative cycles". On the other hand it would not work on a DAG with negative weights. So, provided you cited correctly, the statement from the book is wrong for two reasons.

Btw. if you have negative cycles, there may no longer be a shortest path since you may cycle an infinite number of times and go down with your cost as much as you like.



来源:https://stackoverflow.com/questions/43394847/dijkstras-algorithm-and-cycles

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