2

How can we count number of node - disjoint paths between any two nodes such that distance between two nodes is maximum 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). Number of vertices in graph can be up to than 10^5 and edges can be 6 * 10^5. I thought of implementing BFS for every node until maximum distance from source node is less than 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.

Dominique Fortin
  • 2,212
  • 15
  • 20
Pradyumn
  • 17
  • 4

1 Answers1

0

DFS is the key to solve such problems. We can easily enumerate all the possible paths between 2 vertices using DFS, but we have to take care of the distance constraint.

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. If e becomes greater than K - 2, we terminate that recursive DFS call.

To maintain that a vertex has been visited we keep a boolean array visited. But if a recursive call terminates without finding a successful path, we discard any changes made to the array 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.

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
 }  

}
Sumeet
  • 8,086
  • 3
  • 25
  • 45
  • Thanks for your reply. Can you convert this algorithm such that we don't know the destination vertex. I have one test case for this. – Pradyumn May 18 '17 at 03:42
  • 1 - 2,3 || 2 - 4,9,11 || 3 - 5 || 4 - 6 || 5 - 8 || 6 - 7,11 || 7 - 8 || 9 - 4 || 10 - 7 || 11 - 8 || (This is a directed graph and source vertex is 1 and value of `k` is 5). How many such node - disjoint paths are possible and between which vertices? – Pradyumn May 18 '17 at 03:47