Advertisement
Guest User

Untitled

a guest
Feb 26th, 2020
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.61 KB | None | 0 0
  1. vector <bool> visited;
  2. vector <vector <int> > G;
  3.  
  4. void bfs(int v){
  5. queue <int> Q; //u otra estructura que sirva de cola
  6. Q.push(v); //empezamos con el nodo origen
  7.  
  8. while (not Q.empty()){ //mientras nuestra lista tenga nodos
  9. int u = Q.front(); //seleccionamos el primer nodo de la lista
  10. Q.pop(); //y lo eliminamos
  11.  
  12. if (not visited[u]){ //si no lo hemos visitado
  13. visited[u] = true;
  14.  
  15. for (int i = 0; i < G[u].size(); ++i){
  16. int w = G[u][i];
  17.  
  18. Q.push(w); //ponemos a sus vecinos en la lista
  19. }
  20. }
  21. }
  22. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement