Advertisement
vkichukova

Untitled

Feb 9th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.32 KB | None | 0 0
  1. // graph
  2. void BFS(int a, vector<int>& vec)
  3.     {
  4.         queue<int> q; q.push(a);
  5.         vec.push_back(a);
  6.         while(!q.empty())
  7.         {
  8.             int x = q.front();
  9.             q.pop();
  10.             cout << x << " ";
  11.  
  12.             vector<int> helper;
  13.             for(int i = 0; i < numOfVertices; i++)
  14.             {
  15.                 if(graph[i].front() == x)
  16.                 {
  17.                     helper = graph[i];
  18.                     break;
  19.                 }
  20.             }
  21.  
  22.             int size = helper.size();
  23.             for(int i = 0; i < size; i++)
  24.             {
  25.                 if(!isMember(helper[i],vec))
  26.                 {
  27.                     q.push(helper[i]);
  28.                     vec.push_back(helper[i]);
  29.                 }
  30.             }
  31.         }
  32.     }
  33.  
  34.     void DFS(int a, vector<int>& vec)
  35.     {
  36.         vec.push_back(a);
  37.         cout << a << " ";
  38.  
  39.         vector<int> helper;
  40.         for(int i = 0; i < numOfVertices; i++)
  41.         {
  42.             if(graph[i].front() == a)
  43.             {
  44.                 helper = graph[i];
  45.                 break;
  46.             }
  47.         }
  48.         int size = helper.size();
  49.         for(int i = 0; i < size; i++)
  50.         {
  51.             if(!isMember(helper[i],vec))
  52.             {
  53.                 DFS(helper[i],vec);
  54.             }
  55.         }
  56.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement