给定无向图G =(V,E),是否存在计算两个任意顶点u&之间的最短路径总数的算法。 v?我想我们可以利用Dijkstra的算法。
答案 0 :(得分:0)
是的,你可以使用dijkstra。创建一个数组,用于存储任何节点的最短路径总数。总称它。所有数组成员的初始值为0,但总[s] = 1,其中s是源。
在dijkstra循环中,当比较节点的最小路径时,如果比较结果较小,则用当前节点的总数更新该节点的总数组。如果等于,则添加该节点的总数组,其中包含当前节点的总数。
从维基百科中获取的伪代码进行了一些修改:
function Dijkstra(Graph, source):
create vertex set Q
for each vertex v in Graph: // Initialization
dist[v] ← INFINITY // Unknown distance from source to v
total[v] ← 0 // total number of shortest path
add v to Q // All nodes initially in Q (unvisited nodes)
dist[source] ← 0 // Distance from source to source
total[source] ← 1 // total number of shortest path of source is set to 1
while Q is not empty:
u ← vertex in Q with min dist[u] // Source node will be selected first
remove u from Q
for each neighbor v of u: // where v is still in Q.
alt ← dist[u] + length(u, v)
if alt < dist[v]: // A shorter path to v has been found
dist[v] ← alt
total[v] ← total[u] // update the total array of that node with the number of total array of current node
elseif alt = dist[v]
total[v] ← total[v] + total[u] // add the total array of that node with the number of total array of current node
return dist[], total[]