[英]Count number of Node - Disjoint Paths between any two nodes in directed graph such that there distance is <=K
How can we count number of node - disjoint paths between any two nodes such that distance between two nodes is maximum K ? 我们如何计算两个节点之间不相交的路径数,使两个节点之间的距离最大为K ?
Details about node - disjoint path can be found here . 有关节点不相交路径的详细信息,请参见此处 。
We are given a directed graph where we have to count number of node - disjoint path from vertex u
to v
such that maximum number of nodes between them is K - 2 ( u
and v
are decremented from K , therefore K - 2 ). 我们给出一个有向图,其中我们必须计算从顶点
u
到v
的节点不相交路径的数量,以使它们之间的最大节点数为K-2( u
和v
从K递减,因此K-2 )。 Number of vertices in graph can be up to than 10^5 and edges can be 6 * 10^5 . 图中的顶点数最多可以超过10 ^ 5,并且边可以是6 * 10 ^ 5 。 I thought of implementing BFS for every node until maximum distance from source node is less than K .
我考虑为每个节点实施BFS,直到与源节点的最大距离小于K为止。 But I am not getting idea for implementation.
但是我没有实现的想法。 Anybody please help me?
有人请帮我吗?
If anybody have idea to solve it using DFS, please share it. 如果有人有想法使用DFS解决它,请分享。
DFS is the key to solve such problems. DFS是解决此类问题的关键。 We can easily enumerate all the possible paths between 2 vertices using
DFS
, but we have to take care of the distance constraint . 我们可以使用
DFS
轻松枚举2个顶点之间的所有可能路径,但是必须注意距离约束 。
My algorithm considers the number of edges traversed as a constraint. 我的算法将遍历的边数视为约束。 You can easily convert it to number of nodes traversed.
您可以轻松地将其转换为遍历的节点数。 Take that as an excercise.
以此作为锻炼。
We keep track of the number of edges traversed by variable e
. 我们跟踪变量
e
遍历的边数。 If e
becomes greater than K - 2
, we terminate that recursive DFS
call. 如果
e
大于K - 2
,我们将终止该递归DFS
调用。
To maintain that a vertex has been visited we keep a boolean
array visited
. 为了保持顶点已被访问,我们保持一个
boolean
数组被visited
。 But if a recursive call terminates without finding a successful path, we discard any changes made to the array visited
. 但是,如果递归调用在没有找到成功路径的情况下终止,我们将放弃对
visited
的数组所做的任何更改。
Only if a recursive DFS
call is successful in finding a path, then we retain the visited
array for the rest of the program. 仅当递归
DFS
调用成功找到路径时,我们才保留程序其余部分的visited
数组。
So the pseudocode for the algorithm would be: 因此,该算法的伪代码为:
main function()
{
visited[source] = 1
e = 0//edges traversed so far.
sum = 0// the answer
found = false// found a path.
dfs(source,e)
print sum
.
.
.
}
dfs(source,e)
{
if(e > max_distance)
{
return
}
if(e <= max_distance and v == destination)
{
found = true
sum++
return
}
for all unvisited neighbouring vertices X of v
{
if(found and v != source)
return;
if(found and v == source)
{
found = false
visited[destination] = 0
}
visited[X] = 1
dfs(X , e + 1)
if(!found)
visited[X] = 1
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.