简体   繁体   English

在有向无环图中查找两个节点之间的路径数

[英]Finding number of paths between two nodes in a directed acyclic graph

My idea of solving this problem is to adjust DFS so that it stops when we hit the destination node, then set up a counter that adds up all of the neighbors of the starting node, then the neighbors of the starting node and its neighrbours recursively.我解决这个问题的想法是调整 DFS 使其在我们到达目标节点时停止,然后设置一个计数器,将起始节点的所有邻居相加,然后递归地将起始节点的邻居及其邻居相加。

I'm just wondering if this will only count the paths from the source to the destination, and not any stray paths that don't lead to the destination node.我只是想知道这是否只会计算从源到目标的路径,而不计算任何不通向目标节点的杂散路径。

Thank you for the help.感谢您的帮助。

You can use dynamic programming.您可以使用动态规划。 You have a directed acyclic graph so you have a node (say s) with no arcs pointing into s.你有一个有向无环图,所以你有一个节点(比如 s),没有指向 s 的弧。 You also have a node (say t) that has no arcs pointing out of t.您还有一个节点(比如 t),它没有指向 t 的弧线。 Because it is acyclic, you can use a topological sorting algorithm to find an ordering of the nodes such that every arc points away from s and towards t.因为它是非循环的,所以您可以使用拓扑排序算法来找到节点的排序,使得每个弧都远离 s 指向 t。

So start at s.所以从s开始。 The number of paths from s to s is 1, the empty path.从 s 到 s 的路径数为 1,即空路径。 Because the graph is acyclic, s must have a neighbour u such that the only arc pointing into u is su.因为该图是无环的,所以 s 必须有一个邻居 u,这样唯一指向 u 的弧就是 su。 Now you just repeat.现在你只需重复一遍。 In general, for a node w that has arcs from v1,...vk pointing into it.一般来说,对于一个节点 w,它的弧从 v1,...vk 指向它。 Then the number of paths from s to w is just the sum of the number of sv1 paths, ..., svk paths.那么从 s 到 w 的路径数就是 sv1 路径数之和,..., svk 路径数。

This is in the case of a single arc between each node.这是在每个节点之间的单个弧的情况下。 If there are multiple arcs you multiply so it would be (number of v1w arcs)(number of sv1paths) + ... + (number of vkw arcs)(number of svk paths)如果有多个弧相乘,那么它将是(v1w 弧的数量)(sv1path 的数量)+ ... +(vkw 弧的数量)(svk 路径的数量)

And at each step you can use the fact that it is acyclic to find the node w such that you have already calculated all the sv1 to svk paths.并且在每个步骤中,您都可以使用它是非循环的这一事实来找到节点 w,这样您就已经计算了所有 sv1 到 svk 的路径。

I would use BFS.我会使用 BFS。

Let's call the source node s and the target node t .我们称源节点为s ,目标节点为t

When you BFS starting from s , all the paths with length 1 will be found and put into a queue.当您从s开始 BFS 时,将找到所有长度为 1 的路径并将其放入队列。 You can, then, take the first element of the queue (call it u ) and find all the paths of size 2 ( s -> u -> ... ).然后,您可以获取队列的第一个元素(称为u )并找到所有大小为 2 的路径( s -> u -> ... )。 Repeat the same thing for each distance, until you find all paths of all lengths from s to t .对每个距离重复相同的事情,直到找到从st的所有长度的所有路径。

A trick to speed it up would be: after you exhausted all the paths of a node w , store how many paths from w to t there are, and when another node (above w ) reach w , you won't need to recompute all the paths.加速它的一个技巧是:在你用尽节点w所有路径后,存储从wt路径有多少条,当另一个节点(高于w )到达w ,你不需要重新计算所有路径。

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

相关问题 在有向无权图中找到两个节点之间所有最短路径的数量 - Finding the number of all the shortest paths between two nodes in directed unweighted graph 有向循环图中两个节点之间的路径数 - Number of paths between two nodes in a Directed Cyclic Graph 在有向图中有效查找2个节点之间的所有路径-RGL Gem - Efficiently Finding all paths between 2 nodes in a directed graph - RGL Gem 计数节点数-有向图中任意两个节点之间的不相交路径,以使距离&lt;= K - Count number of Node - Disjoint Paths between any two nodes in directed graph such that there distance is <=K 用于计算有向图上非循环路径数的快速算法 - Fast algorithm for counting the number of acyclic paths on a directed graph 有向无环图中多个节点之间的最低公共祖先 - Lowest Common Ancestor between multiple nodes in a Directed Acyclic Graph 连接有向无环加权图中的两个随机节点 - Connecting Two Random Nodes in a Directed Acyclic Weighted Graph 在有向图中找到两个特定顶点之间的所有节点 - Finding all the nodes between two certain vertices in a directed graph 有向非循环图中两个顶点之间的最大加权路径 - Maximum weighted path between two vertices in a directed acyclic Graph 检查有向无环图中两个顶点之间是否存在路径 - 查询 - Check if there exist a path between two vertices in directed acyclic graph - queries
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM