Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- using namespace std;
- int adj[100][100],v;
- int color[100],time,prev[100],d[100],f[100];
- void DFS_Visit(int u);
- void DFS(int s)
- {
- for(int i=1;i<=v;i++)
- {
- color[i]=0;
- prev[i]=-1;
- d[i]=-1;
- f[i]=-1;
- }
- time=0;
- for(int i=1;i<=v;i++)
- {
- if(color[i]==0)
- DFS_Visit(i);
- }
- }
- void DFS_Visit(int u)
- {
- color[u]=1;
- time++;
- d[u]=time;
- for(int i=1;i<=v;i++)
- {
- int k=adj[u][i];
- if(k==1&&color[i]==0)
- {
- prev[i]=u;
- DFS_Visit(i);
- }
- }
- color[u]=2;
- time++;
- f[u]=time;
- }
- void DFS_Display()
- {
- cout<<"veretex :";
- for(int i=1;i<=v;i++)
- {
- cout<<i<<" ";
- }
- cout<<endl;
- cout<<"Colour :";
- for(int i=1;i<=v;i++)
- {
- cout<<color[i]<<" ";
- }
- cout<<endl;
- cout<<"Discovering :";
- for(int i=1;i<=v;i++)
- {
- cout<<d[i]<<" ";
- }
- cout<<endl;
- cout<<"Finishing :";
- for(int i=1;i<=v;i++)
- {
- cout<<f[i]<<" ";
- }
- cout<<endl;
- cout<<"Prev :";
- for(int i=1;i<=v;i++)
- {
- cout<<prev[i]<<" ";
- }
- cout<<endl;
- }
- int main()
- {
- int a,b,i,j,k,s;
- cout<<"Input Vertex Number :";
- cin>>v;
- k=1;
- do
- {
- cout<<"Edge "<<k<<" : ";
- cin>>a>>b;
- if(a<=0 || b<=0 || a>v || b>v)
- {
- cout<<"Invalid Input,try again... "<<endl;
- }
- else
- {
- adj[a][b]=1;
- k++;
- }
- }while(a!=0 && b!=0);
- for(i=1;i<=v;i++)
- {
- for(j=1;j<=v;j++)
- {
- cout<<adj[i][j]<<" ";
- }
- cout<<endl;
- }
- cout<<"Source vertex :";
- cin>>s;
- DFS(s);
- DFS_Display();
- }
Add Comment
Please, Sign In to add comment