简体   繁体   English

O(E + V)算法计算给定图上2个节点之间的最短路径数

[英]O(E+V) algorithm to compute number of shortest paths between 2 nodes on given graph

When Given a graph G with vertices and edges |V| 给定具有顶点和边| V |的图G时 and |E| 和| E | respectively and vertices u and t, write a O(|E|+|V|) algorithm to compute the number of shortest paths from u to t, ie if there are 5 paths of length 4 with length 4 being the shortest path from u to t then the algorithm will output 5. 分别用顶点u和t编写O(| E | + | V |)算法以计算从u到t的最短路径数,即,如果有5个长度为4的路径,而长度4是从u到最短路径的路径到t时,算法将输出5。

I know that the algorithm must somehow incorporate either a DFS or a BFS due to its run-time as each have a O(|E|+|V|) run-time, but I am a bit stuck. 我知道该算法由于其运行时而必须以某种方式合并DFS或BFS,因为它们每个都具有O(| E | + | V |)运行时,但是我有点受阻。 I tried implementing something where it would do a DFS repeatedly with the algorithm terminating at t but it this became problematic for deciding which nodes to set as visited and which to reset after each iteraition. 我尝试实现某种方法,使其在算法终止于t的情况下重复执行DFS,但这对于确定将哪些节点设置为已访问节点以及在每次迭代后重置哪些节点变得很麻烦。

Thanks in advance! 提前致谢!

You can use breadth-first search. 您可以使用广度优先搜索。 For each vertex, keep track of: 对于每个顶点,请跟踪:

  • the shortest path length from u to that vertex u到该顶点的最短路径长度
    • Whenever you process a given vertex, you set this property for all of its neighbors that don't already have this property set. 每当处理给定顶点时,都为尚未设置此属性的所有邻居设置此属性。
    • This doubles as a "has already been enqueued" flag: you initially set to a sentinel value such as ɴɪʟ or ∞, and only update it once for any given vertex. 这是“已被排队”标志的两倍:您最初设置为哨兵值,例如ɴɪʟ或∞,并且仅对任何给定顶点更新一次。 So you don't need a separate flag to track visited vertices. 因此,您不需要单独的标志即可跟踪访问的顶点。
  • the number of shortest paths from u to that vertex u到该顶点的最短路径数
    • Whenever you process a given vertex, you increase this property appropriately for all of its neighbors whose shortest path length from u is greater than that of the vertex you're processing. 每当处理给定的顶点时,都应为其从u出发的最短路径长度大于要处理的顶点的最短路径长度的所有邻居适当增加此属性。
    • Note that you'll update this property many times for some vertices, but that's OK, because you only update it once per edge. 请注意,对于某些顶点,您将多次更新此属性,但这没关系,因为每个边缘仅更新一次。

暂无
暂无

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

相关问题 Dijkstra算法在O((V + E)log W)时间内计算给定源顶点的最短路径 - Dijkstra’s algorithm to compute the shortest paths from a given source vertex s in O ((V+E) log W) time 对于给定的图G =(V,E),您如何在O(E + V)时间中对其邻接列表表示进行排序? - For a given graph G = (V,E) how can you sort its adjacency list representation in O(E+V) time? 对于给定的图 G=(V,E) 和边 e∈E,设计一个 O(n+m) 时间的算法来寻找(如果存在)包含 e 的最短循环 - For a given graph G=(V,E) and an edge e∈E, design an O(n+m)-time algorithm to find, if it exists, the shortest cycle that contains e 查找未加权有向图中两个节点之间最短路径的最有效算法(Big O) - Most efficient (Big O) algorithm for finding the shortest path between two nodes in an unweighted directed graph 找到从给定节点 s 到任何其他节点 v 的最短路径数的算法? - algorithm that finds the number of the shortest paths from given node s to any other node v? 给定多图的邻接表,在 O(|V|+|E|) 时间内计算等效(简单)无向图的邻接表 - Given an adjacency list for multigraph, compute adjacency list for equivalent (simple) undirected graph in O(|V|+|E|) time 在有向无权图中找到两个节点之间所有最短路径的数量 - Finding the number of all the shortest paths between two nodes in directed unweighted graph 是否有一种图算法可以找到节点之间的最短路径,并避免合并节点? - Is there a graph algorithm to find shortest path between nodes, incorporating nodes to avoid? 在 O(V + E) 中只有两个成本的图上的最短路径 - Shortest Path on Graph with only two costs in O(V + E) 在无权无向图中找到两个节点之间的所有最短路径 - Finding all the shortest paths between two nodes in unweighted undirected graph
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM