[英]k-Edge Shortest Path in Positive Weighted Directed Acyclic Graph
I am given a graph, G = (V, E), that is positive weighted, directed, and acyclic. 我得到一个图,G =(V,E),即正加权,有向和非循环。 I am to design an algorithm that runs in O(k(m + n)) for reporting a k-edge shortest path from s to t. 我将设计一个在O(k(m + n))中运行的算法,用于报告从s到t的k边缘最短路径。 A k-edge shortest path is defined as a path from s to t with k edges and the total weight of the path must also be minimum for all paths from s to t. k边缘最短路径被定义为从s到t的具有k个边缘的路径,并且对于从s到t的所有路径,路径的总权重也必须是最小的。
Since BFS can't be used alone to find shortest paths (unless the weights are equal), I think that the running time implies using BFS to find paths with k edges. 由于BFS不能单独用于查找最短路径(除非权重相等),我认为运行时间意味着使用BFS来查找具有k个边缘的路径。 What's throwing me off is the k, as I think it implies performing BFS k times. 什么让我失望的是k,因为我认为它意味着执行BFS k次。
My possible idea would be to run a BFS from the source to find all possible k-link paths. 我可能的想法是从源运行BFS以找到所有可能的k-link路径。 By keeping track of the level along the way and storing the total path weight to each node when I add it to my queue, I can find all possible k-link paths and their weights. 通过跟踪沿途的水平并将每个节点的总路径权重存储到我的队列中,我可以找到所有可能的k-link路径及其权重。 Obviously, if I encounter the destination at a lower level with lower path weight, there is no k-edge shortest path by definition. 显然,如果我在较低级别遇到具有较低路径权重的目的地,则根据定义没有k边缘最短路径。 What about cases where there are paths with more than k edges that are less total weight? 如果有超过k个边的路径总重量减少的情况怎么办? It also is not O(k(m + n)). 它也不是O(k(m + n))。 Any helpful hints would be appreciated. 任何有用的提示将不胜感激。
Let f[i][j]
be the i-link shortest path from s
to j
, initially we have 设f[i][j]
是从s
到j
的i-link最短路径,最初我们有
f[1][x] = e(s, x);
Then iterate K - 1
times, each round we use f[i][]
to compute f[i + 1][]
, which can be done by 然后迭代K - 1
次,每轮我们使用f[i][]
计算f[i + 1][]
,这可以通过
for each node v:
f[i + 1][v] = INF;
for each edge e[u][v]:
f[i + 1][v] = min(f[i + 1][v], f[i][u] + e[u][v]);
thus takes O(k(n + m))
. 因此取O(k(n + m))
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.