且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何保存在Dijkstra算法最短路径

更新时间:2022-12-17 11:01:54

如果你看的从你给了***的链接伪code ,你会看到有一个数组名为 preV [] 。该芯片包括,对每个节点的 v 在图中,在 previous 节点的 U 在源节点之间的最短路径取值和 v 。 (此阵也被称为 predecessor ​​阵列。)

If you look at the pseudocode from the Wikipedia link you gave, you'll see an array in there called prev[]. This array contains, for each node v in the graph, the previous node u in the shortest path between the source node s and v. (This array is also called the predecessor or parent array.)

在换句话说,之间的最短路径的取值 v 是:

In other words, the shortest path between s and v is:

s -> u -> v
where u = prev[v]

这是该路径的取值 U 可能有几个节点之间,所以要重建,从取值的路径为 v ,您只需步行回顺使用下面的主伪code中的code段由 preV [] 数组定义的路径( 目标 v ):

The path from s to u might have several nodes in between, so to reconstruct the path from s to v, you just walk back along the path defined by the prev[] array using the code snippet below the main pseudocode (target is v):

1  S ← empty sequence
2  u ← target
3  while prev[u] is defined:                 // Construct the shortest path with a stack S
4      insert u at the beginning of S          // Push the vertex onto the stack
5      u ← prev[u]                             // Traverse from target to source
6  end while