ritesh1340

DFS nahii chal rahaa

Dec 24th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.93 KB | None | 0 0
  1. // Draw a Graph where connections are both ways
  2.  
  3. #include<bits/stdc++.h>
  4. using namespace std;
  5.  
  6. vector <int> vis;
  7. vector < vector <int> > adj;
  8.  
  9. void dfs( int t )
  10. {
  11.     vis[t]=1;
  12.     for ( auto i:adj[t] )
  13.         if ( vis[i]==0 )
  14.         {
  15.             cout<<i<<" ";
  16.             dfs(i);
  17.         }
  18. }
  19.  
  20. int main()
  21. {
  22.  
  23.     cout<<"Enter number of nodes and edges respectively (Zero Based Indexing) : \n";
  24.     int n,e;
  25.  
  26.     cin>>n>>e;
  27.     int a,b;
  28.  
  29.     cout<<"Start Entering Node number and the node connected to that node\n";
  30.     adj = vector<vector<int>> (n,vector <int> ());
  31.     for ( int i=0 ; i<e ; i++)
  32.     {
  33.         cin>>a>>b;
  34.         adj[a].push_back(b);
  35.         adj[b].push_back(a);
  36.     }
  37.  
  38.     cout<<"All connections have been established...!!!";
  39.  
  40.     vis=vector < int > ( n , 0 );
  41.     cout<<0<<" ";
  42.     dfs(0);
  43.  
  44.     return 0;
  45. }
  46.  
  47. // Applying DFS ----->> Starting from 0 ( We have 0 based indexing )
Add Comment
Please, Sign In to add comment