keymasterviriya1150

Untitled

Sep 8th, 2016
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.81 KB | None | 0 0
  1. #include<iostream>
  2. #include<vector>
  3. #include<stack>
  4. #include<queue>
  5. using namespace std;
  6. void IterativeDFSusingStack(vector<pair<int, int> >edges[],int V)
  7. {
  8.     stack<int> s;
  9.     bool visited[V]={0};
  10.     s.push(0); //start vertex
  11.    
  12.     while(!s.empty())
  13.     {
  14.         //cout <<"TOP="<<s.top()<<endl;
  15.         int current_vertex=s.top(); //current_vertex=0,3,5
  16.        
  17.         s.pop();
  18.         if(!visited[current_vertex]) // visited[0]
  19.                                      // visited[3]
  20.                                      // visited[5]
  21.                                      // visited[2]
  22.                                      // visited[1]
  23.                                      // visited[3]
  24.                                      // visited[2]
  25.                                      // visited[4]
  26.                                      //  all  true
  27.             visited[current_vertex]=true; // visited[0]=true
  28.                                           // visited[3]=true
  29.                                           // visited[5]=true
  30.                                           // visited[2]=true
  31.                                           // visited[1]=true
  32.                                           // visited[4]=true
  33.         else continue;
  34.        
  35.         cout << current_vertex << " "; // 0,3,5,2,1,4
  36.         for(int i=0;i<edges[current_vertex].size();i++)  //edges[0]
  37.         {                                                //edges[3]
  38.                                                          //edges[5]
  39.                                                          //edges[2]
  40.                                                          //edges[1]
  41.                                                          //edges[4]
  42.             int adjacent_vertex=edges[current_vertex][i].first; //adjacent_vertex=edges[0][0].first=1
  43.             s.push(adjacent_vertex);                            //adjacent_vertex=edges[0][1].first=3
  44.                                                                     //adjacent_vertex=edges[3][0].first=2
  45.                                                                     //adjacent_vertex=edges[3][1].first=4  
  46.                                                                     //adjacent_vertex=edges[3][2].first=5
  47.                                                                         //adjacent_vertex=edges[5][0].first=2
  48.                                                                             //adjacent_vertex=edges[2][0].first=1
  49.                                                                                 //adjacent_vertex=edges[1][0].first=2
  50.                                                                                 //adjacent_vertex=edges[1][1].first=3
  51.                                                                                     //adjacent_vertex=edges[4][0].first=2
  52.         }
  53.     }
  54.     /*
  55.         s={1,3}
  56.             s={1}
  57.                 s={1,2,4,5}
  58.                     s={1,2,4,1}
  59.                         s={1,2,4,2,3}
  60.                             s={1,2,4,2}
  61.                                 s={1,2,4}
  62.                                     s={1,2,}
  63.                                         s={1}
  64.                                             s={}
  65.     */
  66. }
  67. void inputIterativeDFSusingStack()
  68. {
  69.     int V, E;
  70.     cin >> V;
  71.     vector<pair<int, int> > edges[V];
  72.     for (int v = 0; v < V; v++)
  73.     {
  74.         cin >> E;
  75.          for (int e = 0; e < E; e++)
  76.          {
  77.             int k;
  78.             cin >> k;
  79.             edges[v].push_back(make_pair(k, 0));
  80.         }
  81.     }
  82.     IterativeDFSusingStack(edges,V);
  83.     /*
  84.     6           edges[6]
  85.     E=2   1 3   edges[0][0].push_back(make_pair(1, 0));
  86.                 edges[0][1].push_back(make_pair(3, 0));
  87.                
  88.     E=2   2 3   edges[1][0].push_back(make_pair(2, 0));
  89.                 edges[1][1].push_back(make_pair(3, 0));
  90.                
  91.     E=1   1     edges[2][0].push_back(make_pair(2, 0));
  92.    
  93.     E=3   2 4 5 edges[3][0].push_back(make_pair(2, 0));
  94.                 edges[3][1].push_back(make_pair(4, 0));
  95.                 edges[3][2].push_back(make_pair(5, 0));
  96.                
  97.     E=1   2     edges[4][0].push_back(make_pair(2, 0));
  98.     E=1   2     edges[5][0].push_back(make_pair(2, 0));
  99.     */
  100.  
  101. }  
  102. void RecursiveDFS(vector<vector<int> > edges,   vector<bool> &visited, int current)
  103. {
  104.     if (!visited[current])
  105.     {
  106.    
  107.         visited[current]=true;
  108.         cout << current<<" [";
  109.         for (int adj = 0; adj < int(edges[current].size()); adj++)
  110.             RecursiveDFS(edges, visited, edges[current][adj]);
  111.         cout <<"] ";
  112.     }
  113. }
  114. void InputRecursiveDFS()
  115. {  
  116.     int V,E;
  117.     cin >> V;
  118.     vector<vector<int> > edges;
  119.     vector<bool> visited;
  120.    
  121.     edges.assign(V,vector<int>());
  122.     visited.assign(V,false);
  123.     for(int v=0;v<V;v++)
  124.     {
  125.         cin >>E;
  126.         for(int e=0;e<E;e++)
  127.         {
  128.             int k;
  129.             cin >>k;
  130.             edges[v].push_back(k);
  131.         }
  132.     }
  133.     cout <<"Traversal Order:";
  134.     RecursiveDFS(edges,visited,0);
  135.     cout <<"\n";
  136. }
  137. void BFS()
  138. {
  139.     int V, E;
  140.     cin >> V >> E;
  141.     vector<int> edges[V];
  142.     for (int e = 0; e < E; e++)
  143.     {
  144.         int k, m;
  145.         cin >> k >> m;
  146.         edges[k].push_back(m);
  147.         edges[m].push_back(k);
  148.     }
  149.     cout << "\nTraversal Order: ";
  150.     queue<int> q;
  151.      bool visited[V] = { 0 };
  152.  
  153.     int start;
  154.     cin >> start;
  155.  
  156.     q.push(start); // starting vertex
  157.    
  158.   while (!q.empty())
  159.   {
  160.     int current_vertex = q.front();
  161.    
  162.     q.pop();
  163.    
  164.     if (visited[current_vertex])
  165.       continue;
  166.  
  167.       visited[current_vertex] = true;
  168.    
  169.     cout << current_vertex << " ";
  170.    
  171.     for (size_t i = 0; i < edges[current_vertex].size(); i++)
  172.     {
  173.       int adjacent_vertex = edges[current_vertex][i];
  174.      
  175.       q.push(adjacent_vertex);
  176.     }
  177.   }
  178. }
  179.        
  180.  
  181. int main()
  182. {
  183.    
  184.     BFS();
  185.     //InputRecursiveDFS();
  186.     //inputIterativeDFSusingStack();
  187.     /*input :
  188.     7
  189.     2 1 2
  190.     2 3 4
  191.     2 5 6
  192.     0
  193.     0
  194.     0
  195.     0
  196.     output : 0 2 6 5 1 4 3
  197.                             input :
  198.                             8
  199.                             6 2 3 4 5 6 7
  200.                             2 2 7
  201.                             3 0 1 3
  202.                             3 0 2 4
  203.                             3 0 3 5
  204.                             3 0 4 6
  205.                             3 0 5 7
  206.                             3 0 1 6
  207.                             output : 0 7 6 5 4 3 2 1
  208.                                             input:
  209.                                             14
  210.                                             2 1 10
  211.                                             2 2 9
  212.                                             2 3 6
  213.                                             2 4 5
  214.                                             0
  215.                                             0
  216.                                             2 7 8
  217.                                             0
  218.                                             0
  219.                                             0
  220.                                             2 11 12
  221.                                             0
  222.                                             1 13
  223.                                             0
  224.                                             output : 0 10 12 13 11 1 9 2 6 8 7 3 5 4
  225.     */
  226. }
Add Comment
Please, Sign In to add comment