[英]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: 对于每个顶点,请跟踪:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.