简体   繁体   English

计数节点数-有向图中任意两个节点之间的不相交路径,以使距离<= K

[英]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 ). 我们给出一个有向图,其中我们必须计算从顶点uv的节点不相交路径的数量,以使它们之间的最大节点数为K-2( uvK递减,因此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.

相关问题 有向循环图中两个节点之间的路径数 - Number of paths between two nodes in a Directed Cyclic Graph 在有向无环图中查找两个节点之间的路径数 - Finding number of paths between two nodes in a directed acyclic graph 有向图中的 K 条边不相交路径 - K edge disjoint paths in a directed graph 加权有向图中两个节点之间的距离 - Distance between two nodes in weighted directed graph 有向图中的深度:在距离给定节点 k 处查找节点 - Depth in Directed Graph: Finding nodes at k distance from a given node 在有向无权图中找到两个节点之间所有最短路径的数量 - Finding the number of all the shortest paths between two nodes in directed unweighted graph 计算简单有向图的两个给定顶点之间的所有边不相交路径 - Compute all edge-disjoint paths between two given vertices of a simple directed graph 利用至少K个节点和给定起始节点的贪婪方法在有向图中寻找路径 - Finding Paths in Directed Graph with Greedy Approach With At Least K Nodes and a Given Starting Node 计算有向图中欧拉路径的数量? - Count the number of Euler PATHs in directed graph? 找到属于图的两个不相交子集的任何两个节点之间的最短路径 - Finding the shortest path between any two nodes belonging to two disjoint subsets of a graph
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM