daily pastebin goal
66%
SHARE
TWEET

Breadth First Search

keverman Jan 11th, 2019 51 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top