Program to find total number of Paths between two points in a graph.( Recursive method )


If the "source" Pointer will become "destination" (after the cursion calls ) the path array will get printed.



#include<bits/stdc++.h>

using namespace std;

int fin(vector<int> a[],int src,int dest,vector<int> path,int visited[])
{
    path.push_back(src);

    if(src==dest)
    {
      for(int i=0;i<path.size();i++)
             cout<<path[i]<<" ";
               cout<<endl;
               return 1;
    }

    visited[src]=1;
    int ct=0;

    for(int i=0;i<a[src].size();i++)
        if(visited[a[src][i]]==0)
            ct+=fin(a,a[src][i],dest,path,visited);

     visited[src]=0;
     return ct;
}



int main()
{
 vector<int> v[4];

 v[0].push_back(1);
 v[0].push_back(2);
 v[0].push_back(3);
 v[1].push_back(2);
 v[1].push_back(3);
 v[3].push_back(2);

  int visited[10];  for(int i=0;i<10;i++) visited[i]=0;

 vector<int> path;
 cout<<fin(v,0,2,path,visited)<<endl;

return 0;
}

Comments

Popular posts from this blog

Getting Started With MEAN App Development with AngularJs , ExpressJs , NodeJs and MongoDB.

B. Dreamoon and WiFi :calculate no. of ways : recursive solution (branch and bound )

A. Dreamoon and Stairs : minimum steps to reach : recursion solution.