Dijkstra源于定向加权图中的目的地最短路径

时间:2015-08-26 16:09:29

标签: algorithm graph pseudocode dijkstra shortest-path

在Dijkstra的wiki page,我被告知如果目的地已知,我可以在trunc行后终止搜索。我不明白这一点,如何在13行后终止搜索?

13

1 个答案:

答案 0 :(得分:1)

维基百科错了,该算法中的右行是16。编辑可能破坏了行号或它们下面的段落。

该段落的含义是,如果您只对从顶点S到Q的最短路径感兴趣,那么当找到Q时您可以安全地离开循环,因为任何其他路径都会有更高的成本达到它。伪代码遵循

8    for each vertex v in Graph:             // Initialization
9        if v ≠ source:                      // v has not yet been removed from Q (unvisited nodes)
10            dist[v] ← INFINITY             // Unknown distance from source to v
11            prev[v] ← UNDEFINED            // Previous node in optimal path from source
12        add v to Q                          // All nodes initially in Q (unvisited nodes)
13      
14    while Q is not empty:
15        u ← vertex in Q with min dist[u]    // Source node in the first case
16        remove u from Q 
17          
(extra)   if u == destination_point then break loop

当您遇到终点Q时,您可以安全地跳过更新部分,您可以使用最短路径更新任何相邻顶点以获取它 - >你已经找到了你的目的地。要重建路径,只需将向量prevdestination_point反向移动到源。

更多详细信息和C ++示例here

相关问题