[英]Design an algorithm for the single source shortest path problem that runs in time O(k(|V|+|E|))
Suppose we are given a directed graph G = (V, E)
with potentially positive and negative edge lengths, but no negative cycles. 假设我们给出了有向图
G = (V, E)
具有潜在的正和负边长,但没有负循环。 Let s ∈ V
be a given source vertex. 设
s ∈ V
是给定的源顶点。 How to design an algorithm for the single-source shortest path problem that runs in time O(k(|V | + |E|))
if the shortest paths from s to any other vertex takes at most k edges
? 如果从s到任何其他顶点的最短路径最多需要
k edges
如何设计一个在时间O(k(|V | + |E|))
中运行的单源最短路径问题的算法?
Here`s O(k(|V | + |E|)) approach: 这里是O(k(| V | + | E |))方法:
Because any su shortest path is atmost k edges, we can end algorithm after k iterations over edges 因为任何su最短路径最多是k个边缘,所以我们可以在边缘上的k次迭代之后结束算法
Pseudocode: 伪代码:
for each vertex v in vertices:
对于顶点中的每个顶点v:
D[v] := +ooD [v]:= + oo
D[s] = 0
D [s] = 0
repeat k times:
重复k次:
for each edge (u, v) with weight w in edges:对于边缘重量为w的每条边(u,v):
if D[u] + w < D[v]:如果D [u] + w <D [v]:
D[v] = D[u] + wD [v] = D [u] + w
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.