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