Skip to content

Instantly share code, notes, and snippets.

@shubham100795
Created July 31, 2017 12:08
Show Gist options
  • Save shubham100795/e65fed2588ffee2042428ecdd58e0a3c to your computer and use it in GitHub Desktop.
Save shubham100795/e65fed2588ffee2042428ecdd58e0a3c to your computer and use it in GitHub Desktop.
Print all possible paths from source to destination in a directed graph
#include<bits/stdc++.h>
using namespace std;
int adj[10][10];
int visited[10];
void create_graph(int n,int m)
{
int i,j,k,src,dest;
for(j=0;j<n;j++)
{
for(k=0;k<n;k++)
{
adj[j][k]=0;
}
}
for(i=0;i<m;i++)
{
cout<<"enter the source and destination \n";
cin>>src>>dest;
if(src<0||src>=n||dest<0||dest>=n)
{
cout<<"invalid vertex \n";
i--;
}
adj[src][dest]=1;
}
for(j=0;j<n;j++)
{
for(k=0;k<n;k++)
{
cout<<adj[j][k]<<"\t";
}
cout<<endl;
}
}
void DFS(int s,int d,int n,int& index,int* path)
{
path[index++]=s;
visited[s]=true;
if(s==d)
{
for(int i=0;i<index;i++)
cout<<path[i]<<" ";
cout<<endl;
}
else
{
for(int i=0;i<n;i++)
{
if(adj[s][i]==1&&visited[i]==false)
DFS(i,d,n,index,path);
}
}
index--;
visited[s]=false;
}
int main()
{
int n,m,z,s,d,index=0;
int path[10];
cout<<"enter the no of vertices \n";
cin>>n;
cout<<"Enter the number of edges\n";
cin>>m;
create_graph(n,m);
cout<<"Enter the source and destination of path to be found\n";
cin>>s>>d;
for(int i=0;i<10;i++)
{
visited[i]=false;
}
DFS(s,d,n,index,path);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment