Advertisement
Guest User

Untitled

a guest
Dec 7th, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.66 KB | None | 0 0
  1. #include <vector>
  2. #include <queue>
  3. using namespace std;
  4.  
  5. //adj[a],push_back(b);for an edge from a to b//
  6.  
  7. vector<int>adj[100];
  8. int visited[100];
  9.  
  10. //s is the number of starting vertex//
  11. //n is the number of vertices (0....n-1)//
  12. void bfs(int s,int n)
  13. {
  14. for(int i=0; i<n; i++)visited[i]=0;
  15. queue<int>Q;
  16. Q.push(s);
  17. visited[s]=1;
  18.  
  19. while(!Q.empty())
  20. {
  21. int u=Q.front();
  22. Q.pop();
  23.  
  24. for(int i=0; i<adj[u].size(); i++)
  25. {
  26. if(visited[adj[u][i]]==0)
  27. {
  28. int v=adj[u][i];
  29. visited[v]=1;
  30. Q.push(v);
  31. }
  32. }
  33. }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement