I found this code on google but i couldn't find how does the recursive part actually work. The code is about finding the possible path between 2 vertices on a directed graph. Here is the method to print the total possible path :
public void total_path(int source, int destination)
{
result = 0;
boolean[] visit = new boolean[size];
for(int i = 0; i < size; i++)
{
visit[i] = false;
}
count_path(source, destination, visit);
System.out.println(result);
}
Here is the recursive method to count all possible paths :
public void count_path(int start, int end, boolean[] visit)
{
if(start > size || end > size || start < 0 || end < 0 || node == null)
{
return ;
}
if(visit[start]==true)
{
return;
}
if(start == end)
{
result = result + 1;
}
visit[start] = true;
AjlistNode temp = node[start].next;
while(temp!=null)
{
count_path(temp.id, end, visit);
temp = temp.next;
}
visit[start]=false;
}
Here is the graph which i created :
g.addEdge(0, 1);
g.addEdge(1, 2);
g.addEdge(1, 3);
g.addEdge(2, 5);
g.addEdge(3, 4);
g.addEdge(4, 2);
g.addEdge(4, 5);
g.addEdge(5, 6);
I already compiled and run the program which start point is 0 and end point is 6. The result is 3, already looked up on YouTube about the algorithm but i still can't understand on how to visualize and how does the code flow on the recursion part inside a while loop.
