algorithm - Dijkstra 最短路径算法

标签 algorithm dijkstra shortest-path

以下是我们教授给我们的算法总结。

第 3 步中提到的图中节点的父节点是什么? 我有点困惑,因为我认为节点只有邻居而没有父节点?

我的第二个问题是关于第 3 步,“在堆栈中取出索引的第一个记录”。由于堆栈只允许您查看顶部,我不确定拾取索引的第记录是什么意思?

Dijkstra 的最短路径:

Step 0: if s == d, stop.
Step 1: current node c= s, c.length = 0, stack.push (c, its length, and parent). 
        If u is the source s then no need for parent.
Step 2: min = 0, hop = infinite, index = 1
Step 3: pick up the index’th record in stack, say node u, its length u.length, 
        and its parent w.
Step 4: find a neighbor of u in the table of neighbors, say v, such that v is 
        not found in any item in stack and <u,v> +u.length< hop. 
Step 5: if such a neighbor is found, hop=min=u.length + <u,v> and record_node = v
Step 6: go to step 4 until all the neighbors of u have been tried (all can be 
        found in stack).
Step 7: index ++, go to step 3 until all the nodes have been tried (found in 
        stack).
Step 8: c = record_node, c.length = min, stack_push(c, c.length, u). If c == d 
        stop the entire process and goes to step 9 for data collection, 
        otherwise go to step 2.
Step 9: (t, d.length, and t.parent) = (d, d.length, and d.parent) in stack, 
        keep searching on (t.parent, t.parent.length, t.parent.parent), 
        until t.parent = s.

最佳答案

在图中,节点只有邻居,但在运行 Dijkstra 算法时,您构建了一个“树”,描述了从起始节点到原始图中所有节点的最短路径。

在算法运行开始时,所有节点都将其前任节点设置为空,并且在每次迭代中将父节点设置为通向最短路径的节点。

看看这个Visualization of Dijkstra's Algorithm并注意算法的结果实际上是图的子

希望这能回答您的问题:)

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

相关文章:

algorithm - 使用一组禁止节点查找两个节点之间的最短路径

r - 从 nxm 矩阵(表示 map )计算 R 中的邻接矩阵

graph - 给定最短路径修改图边权重的算法

c# - 如果仅删除一条边,Dijkstra 最短路径快速重新计算

algorithm - 使用 Dijkstra 算法的负权重

algorithm - 将实数转换为有理数时检查终止

algorithm - 一般位计数

algorithm - 我们如何找到带有两个不同 "ID"的图的最大连续区域?

c++ - 如何使用基于数组的版本使用 Dijkstra C++ 代码

algorithm - 理解为什么 Dijkstra 算法在具有负边的图上失败的解释?