Suppose we are given a directed graph G = (V, E)
with potentially positive and negative edge lengths, but no negative cycles. Let s ∈ V
be a given source vertex. 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
?
Here`s O(k(|V | + |E|)) approach:
Because any su shortest path is atmost k edges, we can end algorithm after k iterations over edges
Pseudocode:
for each vertex v in vertices:
D[v] := +ooD[s] = 0
repeat k times:
for each edge (u, v) with weight w in edges:
if D[u] + w < D[v]:
D[v] = D[u] + w
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.