简体   繁体   English

加权有向图最短路径的最佳方法

[英]Weighted Directed Graph best method for shortest path

For a question I was doing I'm confused about why the answer would be a BFS and not Dijkstra's algorithm.对于我正在做的一个问题,我很困惑为什么答案是 BFS 而不是 Dijkstra 的算法。

The question was: There is a weighted digraph G=(V,E) with n nodes and m edges.问题是:有一个加权有向图 G=(V,E),具有 n 个节点和 m 个边。 Each node has a weight of 1 or 2. The question was to figure out which algorithm to use to find the shortest path in G from a given vetex u to a given vertex v. The options were:每个节点的权重为 1 或 2。问题是要找出使用哪种算法在 G 中找到从给定顶点 u 到给定顶点 v 的最短路径。选项有:

a) O(n+m) time using a modified BFS
b) O(n+m) time using a modified DFS
c) O(mlogn) time using Dijkstra's Algorithm
d) O(n^3) time using modified Floyd-Warshall algorithm

The answer is a) O(n+m) time using a modified BFS,答案是 a) O(n+m) 时间使用修改后的 BFS,

I know that when comparing BFS to DFS, BFS is better for shorter paths.我知道在将 BFS 与 DFS 进行比较时,BFS 更适合较短的路径。 I also know Dijkstra's algorithm is similar to a BFS and if I'm not mistaken Dijkstra's algorithm is better for weighted graphs like in this case.我也知道 Dijkstra 的算法类似于 BFS,如果我没记错的话,Dijkstra 的算法更适合这种情况下的加权图。 I'm assuming BFS is better because it says modified BFS but what would modified exactly mean or is there another reason BFS would be better.我假设 BFS 更好,因为它说修改后的 BFS,但修改的确切含义是什么,或者还有另一个原因 BFS 会更好。

Since all paths are limited to either a distance of 1 or 2, for every edge of length 2 from nodes a to b you can just create a new node c with an edge from a to c of length 1 and an edge from c to b of length 1, and then this becomes a graph with only edges of weight 1 which can be BFS 'd normally to find shortest path from u to v . Since all paths are limited to either a distance of 1 or 2, for every edge of length 2 from nodes a to b you can just create a new node c with an edge from a to c of length 1 and an edge from c to b长度为 1,然后这变成一个只有权重为 1 的边的图,通常可以BFS找到从uv的最短路径。 Since you only add O(m) new nodes and O(m) new edges, this keeps the BFS's time complexity of O(n+m) .由于您只添加了O(m)个新节点和O(m)个新边,因此 BFS 的时间复杂度为O(n+m)

Another possibility is to, at each layer of BFS, store another list of nodes that are attained by edges with a weight of 2 from the current layer, and consider them at the same time as nodes attained two layers later.另一种可能性是,在 BFS 的每一层,存储另一个节点列表,这些节点由当前层的权重为 2 的边获得,并与稍后获得两层的节点同时考虑它们。 This approach is a bit more finicky though.这种方法虽然有点挑剔。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 加权有向图中的最短路径 - Shortest path in weighted directed graph 在非常大规模的加权和有向图中获得所有对最短路径的最佳方法是什么? - what is the best way to get all pair shortest path in very large scale weighted and directed graph? 具有设定权重数的有向加权图中的最短路径 - Shortest path in a directed, weighted graph with set number of weights 最多使用 k 个顶点的有向加权图中的最短路径 - Shortest path in a directed weighted graph that uses at most k vertices 在有向完整加权图中访问所有顶点的最短路径 - Shortest path that visits all vertices in a directed complete weighted graph 正加权有向无环图中的k边最短路径 - k-Edge Shortest Path in Positive Weighted Directed Acyclic Graph 定向加权图最短路径,强制节点通过 - Directed weighted graph shortest path with obligatory nodes to pass 直径为k &lt;| V |的连通加权有向图,找到最短路径 - connected weighted directed graph with diameter k< |V|, find the shortest path Dijkstra源到目标有向加权图中的最短路径 - Dijkstra source to destination shortest path in directed, weighted graph 在有向加权图中找到两个节点之间的最短路径 - Find shortest path between two nodes in directed weighted graph
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM