Advertisement
keverman

Breadth First Search

Jan 11th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.53 KB | None | 0 0
  1. // Function of a graph class with 2-dimensional vector acting as an edge net
  2. // Edges are assumed to be undirected and unweighted
  3.  
  4. void BFS(int src)
  5. {
  6.     std::vector<int> visited(size(), false);
  7.     visited[src] = true;
  8.  
  9.     std::queue<int> Q;
  10.     Q.push(src);
  11.  
  12.     while(!Q.empty())
  13.     {
  14.         int from = Q.front();
  15.         Q.pop();
  16.  
  17.         for(int to : edges[from])
  18.         {
  19.             if(!visited[to])
  20.             {
  21.                 visited[to] = true;
  22.                 Q.push(to);
  23.             }
  24.         }
  25.     }
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement