Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Draw a Graph where connections are both ways
- #include<bits/stdc++.h>
- using namespace std;
- vector <int> vis;
- vector < vector <int> > adj;
- void dfs( int t )
- {
- vis[t]=1;
- for ( auto i:adj[t] )
- if ( vis[i]==0 )
- {
- cout<<i<<" ";
- dfs(i);
- }
- }
- int main()
- {
- cout<<"Enter number of nodes and edges respectively (Zero Based Indexing) : \n";
- int n,e;
- cin>>n>>e;
- int a,b;
- cout<<"Start Entering Node number and the node connected to that node\n";
- adj = vector<vector<int>> (n,vector <int> ());
- for ( int i=0 ; i<e ; i++)
- {
- cin>>a>>b;
- adj[a].push_back(b);
- adj[b].push_back(a);
- }
- cout<<"All connections have been established...!!!";
- vis=vector < int > ( n , 0 );
- cout<<0<<" ";
- dfs(0);
- return 0;
- }
- // Applying DFS ----->> Starting from 0 ( We have 0 based indexing )
Add Comment
Please, Sign In to add comment