# Route Between nodes | Solution of Cracking the coding Interview

Problem:-

Given a directed Graph , Design an algorithm to find out whether there is a route between nodes.

Code:-

#include<bits/stdc++.h>
using namespace std;
vector<int> arr;

// function for checking there is any edges or not
int isedge(int a,int b)
{
for(int child:arr[b])
{
if(child==a)
return 1;
return isedge(a,child);
}
return 0;
}

//Driver program
int main()
{
int n,e;
cout<<"Enter the number of vertex and Edges"<<endl;
cin>>n>>e;
int a,b;
cout<<"Enter the "<<e<<"Edges"<<endl;
for(int i=0;i<n-1;i++)
{
cin>>a>>b;
arr[a].push_back(b);
}
cout<<"Enter the vertexes to check"<<endl;
cin>>a>>b;
if(isedge(b,a))
cout<<"There is an Edge between "<<a<<" and "<<b;
else
cout<<"There is no any Edge between "<<a<<" and "<<b;
return 0;
}

Output:-

Enter the number of vertex and Edges
5 6
Enter the 6 Edges
1 2
3 1
3 5
5 4
2 4
2 3
Enter the vertexes to check
2 4
There is an Edge between 2 and 4

Recommended Post:-

• Hackerearth Problems:-

Key points:-

MCQs:-