Count the paths
PROBLEM :
Given a directed graph, a source vertex ‘s’ and a destination vertex ‘d’, print the count of all paths from given ‘s’ to ‘d’.
Input:
First line consists of T test cases. First line of every test case consists of V and E, denoting the vertices and edges. Second line of every test case consists of 2*E spaced integers denoting the edge between vertices. Third line of every test case consists of S and D.
Output:
Single line output, print the count of all paths from 's' to 'd'.
Constraints:
1<=T<=100
1<=V,E<=10
Example:
Input:
1
4 6
0 1 0 2 0 3 2 0 2 1 1 3
2 3
Output:
3
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
#include <list>
class graph
{
int v ;
list<int> *adj ;
public :
graph(int v) ;
void addEdges(int s,int d) ;
int CountPaths(int s,int d) ;
void UtilCountPaths(int s,int d,bool visited[],int *count) ;
} ;
graph::graph(int v)
{
this->v=v ;
adj=new list<int>[v] ;
}
void graph:: addEdges(int s,int d)
{
adj[s].push_back(d) ;
}
int graph:: CountPaths(int s,int d)
{
bool visited[v] ;
for(int i=0;i<v;i++)
visited[i]=false ;
int count=0 ;
UtilCountPaths(s,d,visited,&count) ;
return count ;
}
void graph:: UtilCountPaths(int s,int d,bool visited[],int *count)
{
visited[s]=true ;
if(s==d)
(*count)++ ;
else
{
list<int>::iterator i ;
for(i=adj[s].begin();i!=adj[s].end();i++)
{
if(!visited[*i])
UtilCountPaths(*i,d,visited,&(*count)) ;
}
}
visited[s]=false ;
}
int main()
{
int t ;
cin>>t ;
while(t--)
{
int v,e ;
cin>>v>>e ;
graph g(v) ;
int s,d ;
for(int i=0;i<e;i++)
{
cin>>s>>d ;
g.addEdges(s,d) ;
}
cin>>s>>d ;
int path=g.CountPaths(s,d) ;
cout<<path<<endl ;
}
return 0;
}
--------------------------------------------------------------------------------
Given a directed graph, a source vertex ‘s’ and a destination vertex ‘d’, print the count of all paths from given ‘s’ to ‘d’.
Input:
First line consists of T test cases. First line of every test case consists of V and E, denoting the vertices and edges. Second line of every test case consists of 2*E spaced integers denoting the edge between vertices. Third line of every test case consists of S and D.
Output:
Single line output, print the count of all paths from 's' to 'd'.
Constraints:
1<=T<=100
1<=V,E<=10
Example:
Input:
1
4 6
0 1 0 2 0 3 2 0 2 1 1 3
2 3
Output:
3
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
#include <list>
class graph
{
int v ;
list<int> *adj ;
public :
graph(int v) ;
void addEdges(int s,int d) ;
int CountPaths(int s,int d) ;
void UtilCountPaths(int s,int d,bool visited[],int *count) ;
} ;
graph::graph(int v)
{
this->v=v ;
adj=new list<int>[v] ;
}
void graph:: addEdges(int s,int d)
{
adj[s].push_back(d) ;
}
int graph:: CountPaths(int s,int d)
{
bool visited[v] ;
for(int i=0;i<v;i++)
visited[i]=false ;
int count=0 ;
UtilCountPaths(s,d,visited,&count) ;
return count ;
}
void graph:: UtilCountPaths(int s,int d,bool visited[],int *count)
{
visited[s]=true ;
if(s==d)
(*count)++ ;
else
{
list<int>::iterator i ;
for(i=adj[s].begin();i!=adj[s].end();i++)
{
if(!visited[*i])
UtilCountPaths(*i,d,visited,&(*count)) ;
}
}
visited[s]=false ;
}
int main()
{
int t ;
cin>>t ;
while(t--)
{
int v,e ;
cin>>v>>e ;
graph g(v) ;
int s,d ;
for(int i=0;i<e;i++)
{
cin>>s>>d ;
g.addEdges(s,d) ;
}
cin>>s>>d ;
int path=g.CountPaths(s,d) ;
cout<<path<<endl ;
}
return 0;
}
--------------------------------------------------------------------------------
Comments
Post a Comment