Algorithm 线性时间图算法
给定一个无向图hg=(V,E),是否有算法计算两个任意顶点u&V之间的最短路径总数?我认为我们可以利用Dijkstra算法。是的,你可以使用Dijkstra算法。创建存储到任何节点的最短路径总数的数组。称之为总数。所有数组成员的初始值均为0,但total[s]=1除外,其中s为源 在dijkstra循环中,当比较节点的最小路径时,如果比较结果较小,则使用当前节点的总数更新该节点的总数组。如果等于,则将该节点的数组总数与当前节点的总数相加 伪代码取自维基百科,经过一些修改:Algorithm 线性时间图算法,algorithm,graph,shortest-path,breadth-first-search,depth-first-search,Algorithm,Graph,Shortest Path,Breadth First Search,Depth First Search,给定一个无向图hg=(V,E),是否有算法计算两个任意顶点u&V之间的最短路径总数?我认为我们可以利用Dijkstra算法。是的,你可以使用Dijkstra算法。创建存储到任何节点的最短路径总数的数组。称之为总数。所有数组成员的初始值均为0,但total[s]=1除外,其中s为源 在dijkstra循环中,当比较节点的最小路径时,如果比较结果较小,则使用当前节点的总数更新该节点的总数组。如果等于,则将该节点的数组总数与当前节点的总数相加 伪代码取自维基百科,经过一些修改: function Di
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[]
函数Dijkstra(图,源):
创建顶点集Q
对于图形中的每个顶点v://初始化
区[v]← 无穷远//从源到v的未知距离
总计[v]← 0//最短路径的总数
将v添加到Q//最初在Q中的所有节点(未访问的节点)
地区[来源]← 0//源到源的距离
总数[来源]← 1//源的最短路径总数设置为1
Q不是空的:
U← Q中具有最小距离[u]//的顶点将首先选择源节点
从Q中删除u
对于u://的每个相邻v,其中v仍然在Q中。
中高音← 距离[u]+长度(u,v)
如果alt
是的,您可以使用dijkstra。创建存储到任何节点的最短路径总数的数组。称之为总数。所有数组成员的初始值均为0,但total[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[]
函数Dijkstra(图,源):
创建顶点集Q
对于图形中的每个顶点v://初始化
区[v]← 无穷远//从源到v的未知距离
总计[v]← 0//最短路径的总数
将v添加到Q//最初在Q中的所有节点(未访问的节点)
地区[来源]← 0//源到源的距离
总数[来源]← 1//源的最短路径总数设置为1
Q不是空的:
U← Q中具有最小距离[u]//的顶点将首先选择源节点
从Q中删除u
对于u://的每个相邻v,其中v仍然在Q中。
中高音← 距离[u]+长度(u,v)
如果alt
Dijkstra本身不会给出任意两个顶点之间的最短路径计数,只是在运行它的一个顶点和任何其他顶点之间。是的,因为Dijkstra是单源最短路径算法。但我认为问题是关于在两个任意顶点u&v之间寻找最短路径的总数,没有任何限制。如果打算“任意两个顶点”,可以使用floyd-warshall算法,并进行一些修改。这不是线性的。上述算法的运行时间是多少?floyd warshall算法的时间复杂度是O(V^3)。Dijkstra算法本身不会给出任意两个顶点之间的最短路径数,只是在运行它的一个顶点和任何其他顶点之间。是的,因为Dijkstra是单源最短路径算法。但我认为问题是关于在两个任意顶点u&v之间寻找最短路径的总数,没有任何限制。如果打算“任意两个顶点”,可以使用floyd-warshall算法,并进行一些修改。上述算法的运行时间是多少?floyd-warshall算法的时间复杂度是O(V^3)