algorithm - 在图算法中找到最短路径

标签 algorithm shortest-path dijkstra

我刚刚看过这个视频:https://youtu.be/2E7MmKv0Y24?t=1335
它讨论了一种在 DAG 中找到源和给定顶点之间的最短距离的算法:

1.Sort the graph in topological order and express the graph in its linear form
2.Initialize all of the vertex to infinity except the source, which is initialized to 0
3.Iterate from the source to the rightmost vertex. For every vertex u, update the distance of all of its neighbor v to min((distance between source and v), (distance between source and u) + (distance between u and v))

大约在22:00,教授说这个算法适用于负边但图不能包含循环,但我认为该算法适用于包含非负循环的图。我说得对吗?

最佳答案

..., but i think the algorithm works for graphs that contain non-negative cycle. Am I right?

是的,你是对的。参见 this post获取更多信息。

Another question is why do I need to topologically sort the array first? Why can't I just loop through every neighbour and calculate the distance to them?

如果我正确理解了这个问题,你不能只去任何下一个节点,因为可能有一个更短的方式到这个节点首先使用另一个节点(例如,到达一个节点的成本是 5,还有另一种方式到使用成本为 1 + 1 = 2 的两个节点的节点;如果在这种情况下您不先排序,您将使用错误的路径)

关于algorithm - 在图算法中找到最短路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58929683/

相关文章:

algorithm - 图遍历 - 为 2 个实体找到 2 条最短路径,使它们永远不会接触(都在同一节点)

python - 有人可以检测到此代码中的错误以使用 python 实现 dijkstra 算法吗?

c - 带隧道的路线图

java - 原始地理坐标和图形节点之间的最短路径

dijkstra - 关于 Dijkstra 的论文

javascript - 使用 JS 将页面中的每个字母设置为随机颜色。为什么在页面的每个开始标签之前总是有一组额外的 span 标签?

java - 如何通过坐标查看路线

python - 具有定向字符的任意字符串的多序列比较

algorithm - 图像背景去除算法

java - 网格上具有阻挡单元和移动单元的最短路径