1 / 34

Busca em Largura

Busca em Largura. BFS(G) 1 for every vertex s of G not explored yet 2 do Enqueue( S , s ) 3 mark vertex s as visited 4 while S is not empty do 5 u ← Dequeue( S ); 6 For each v in Adj[u] then 7 if v is unexplored then 8 mark edge (v,u) as tree edge

sheryl
Download Presentation

Busca em Largura

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Busca em Largura BFS(G) 1 for every vertex s of G not explored yet 2 doEnqueue(S,s) 3 mark vertex s as visited 4 whileS is not empty do 5u← Dequeue(S); 6For each v in Adj[u] then 7if v is unexplored then 8 mark edge (v,u) as tree edge 9 mark vertex v as visited 10Enqueue(S,v) Running time: O(n + m) • Notação • Adj [u ] : lista dos vértices adjacentes a u em alguma ordem • Dequeue(S): Remove o primeiro elemento da fila S • Enqueue (S,v) : Adiciona o nó v na fila S

  2. Exemplo S 3 4 1 BFS(G) 1 for every vertex s of G not explored yet 2 doEnqueue(S,s); 3 mark vertex s as visited 4 whileS is not empty do 5u← Dequeue(S); 6For each v in Adj[u] then 7if v is unexplored then 8 mark edge (v,u) as tree edge 9 mark vertex v as visited 10Enqueue(S,v) 2 7 5 6

  3. Exemplo S 1 3 4 1 BFS(G) 1 for every vertex s of G not explored yet 2 doEnqueue(S,s); 3 mark vertex s as visited 4 whileS is not empty do 5u← Dequeue(S); 6For each v in Adj[u] then 7if v is unexplored then 8 mark edge (v,u) as tree edge 9 mark vertex v as visited 10Enqueue(S,v) 2 7 5 6

  4. Exemplo S 3 4 1 BFS(G) 1 for every vertex s of G not explored yet 2 doEnqueue(S,s); 3 mark vertex s as visited 4 whileS is not empty do 5u← Dequeue(S); 6For each v in Adj[u] then 7if v is unexplored then 8 mark edge (v,u) as tree edge 9 mark vertex v as visited 10Enqueue(S,v) 2 7 5 6

  5. Exemplo S 4 3 4 1 BFS(G) 1 for every vertex s of G not explored yet 2 doEnqueue(S,s); 3 mark vertex s as visited 4 whileS is not empty do 5u← Dequeue(S); 6For each v in Adj[u] then 7if v is unexplored then 8 mark edge (v,u) as tree edge 9 mark vertex v as visited 10Enqueue(S,v) 2 7 5 6

  6. Exemplo S 4 5 3 4 1 BFS(G) 1 for every vertex s of G not explored yet 2 doEnqueue(S,s); 3 mark vertex s as visited 4 whileS is not empty do 5u← Dequeue(S); 6For each v in Adj[u] then 7if v is unexplored then 8 mark edge (v,u) as tree edge 9 mark vertex v as visited 10Enqueue(S,v) 2 7 5 6

  7. Exemplo S 4 5 2 3 4 1 BFS(G) 1 for every vertex s of G not explored yet 2 doEnqueue(S,s); 3 mark vertex s as visited 4 whileS is not empty do 5u← Dequeue(S); 6For each v in Adj[u] then 7if v is unexplored then 8 mark edge (v,u) as tree edge 9 mark vertex v as visited 10Enqueue(S,v) 2 7 5 6

  8. Exemplo S 5 2 3 4 1 BFS(G) 1 for every vertex s of G not explored yet 2 doEnqueue(S,s); 3 mark vertex s as visited 4 whileS is not empty do 5u← Dequeue(S); 6For each v in Adj[u] then 7if v is unexplored then 8 mark edge (v,u) as tree edge 9 mark vertex v as visited 10Enqueue(S,v) 2 7 5 6

  9. Exemplo S 5 2 3 4 1 BFS(G) 1 for every vertex s of G not explored yet 2 doEnqueue(S,s); 3 mark vertex s as visited 4 whileS is not empty do 5u← Dequeue(S); 6For each v in Adj[u] then 7if v is unexplored then 8 mark edge (v,u) as tree edge 9 mark vertex v as visited 10Enqueue(S,v) 2 7 5 6

  10. Exemplo S 5 2 3 4 1 BFS(G) 1 for every vertex s of G not explored yet 2 doEnqueue(S,s); 3 mark vertex s as visited 4 whileS is not empty do 5u← Dequeue(S); 6For each v in Adj[u] then 7if v is unexplored then 8 mark edge (v,u) as tree edge 9 mark vertex v as visited 10Enqueue(S,v) 2 7 5 6

  11. Exemplo S 2 3 4 1 BFS(G) 1 for every vertex s of G not explored yet 2 doEnqueue(S,s); 3 mark vertex s as visited 4 whileS is not empty do 5u← Dequeue(S); 6For each v in Adj[u] then 7if v is unexplored then 8 mark edge (v,u) as tree edge 9 mark vertex v as visited 10Enqueue(S,v) 2 7 5 6

  12. Exemplo S 3 4 1 BFS(G) 1 for every vertex s of G not explored yet 2 doEnqueue(S,s); 3 mark vertex s as visited 4 whileS is not empty do 5u← Dequeue(S); 6For each v in Adj[u] then 7if v is unexplored then 8 mark edge (v,u) as tree edge 9 mark vertex v as visited 10Enqueue(S,v) 2 7 5 6

  13. Exemplo S 6 3 4 1 BFS(G) 1 for every vertex s of G not explored yet 2 doEnqueue(S,s); 3 mark vertex s as visited 4 whileS is not empty do 5u← Dequeue(S); 6For each v in Adj[u] then 7if v is unexplored then 8 mark edge (v,u) as tree edge 9 mark vertex v as visited 10Enqueue(S,v) 2 7 5 6

  14. Exemplo S 3 4 1 BFS(G) 1 for every vertex s of G not explored yet 2 doEnqueue(S,s); 3 mark vertex s as visited 4 whileS is not empty do 5u← Dequeue(S); 6For each v in Adj[u] then 7if v is unexplored then 8 mark edge (v,u) as tree edge 9 mark vertex v as visited 10Enqueue(S,v) 2 7 5 6

  15. Exemplo S 3 3 4 1 BFS(G) 1 for every vertex s of G not explored yet 2 doEnqueue(S,s); 3 mark vertex s as visited 4 whileS is not empty do 5u← Dequeue(S); 6For each v in Adj[u] then 7if v is unexplored then 8 mark edge (v,u) as tree edge 9 mark vertex v as visited 10Enqueue(S,v) 2 7 5 6

  16. Exemplo S 3 3 3 4 1 BFS(G) 1 for every vertex s of G not explored yet 2 doEnqueue(S,s); 3 mark vertex s as visited 4 whileS is not empty do 5u← Dequeue(S); 6For each v in Adj[u] then 7if v is unexplored then 8 mark edge (v,u) as tree edge 9 mark vertex v as visited 10Enqueue(S,v) 2 7 5 6

  17. Exemplo S 3 4 1 BFS(G) 1 for every vertex s of G not explored yet 2 doEnqueue(S,s); 3 mark vertex s as visited 4 whileS is not empty do 5u← Dequeue(S); 6For each v in Adj[u] then 7if v is unexplored then 8 mark edge (v,u) as tree edge 9 mark vertex v as visited 10Enqueue(S,v) 2 7 5 6

  18. Busca em Profundidade DFS(G) 1Para todo v em G 2 Se v não visitado então 3 DFS-Visit(G, v) DFS-Visit(G, v) 1 Marque v como visitado 2Para todow em Adj(v) 3Se w não visitado então 4 Insira aresta (v, w) na árvore 5 DFS-Visit(G, w) 3 4 1 2 7 5 6

  19. Busca em Profundidade : Exemplo DFS(G) 1Para todo v em G 2 Se v não visitado então 3 DFS-Visit(G, v) DFS-Visit(G, v) 1 Marque v como visitado 2Para todow em Adj(v) 3Se w não visitado então 4 Insira aresta (v, w) na árvore 5 DFS-Visit(G, w) 3 4 1 2 7 5 6

  20. Busca em Profundidade : Exemplo DFS(G) 1Para todo v em G 2 Se v não visitado então 3 DFS-Visit(G, v) DFS-Visit(G, v) 1 Marque v como visitado 2Para todow em Adj(v) 3Se w não visitado então 4 Insira aresta (v, w) na árvore 5 DFS-Visit(G, w) 3 4 1 2 7 5 6

  21. Busca em Profundidade : Exemplo DFS(G) 1Para todo v em G 2 Se v não visitado então 3 DFS-Visit(G, v) DFS-Visit(G, v) 1 Marque v como visitado 2Para todow em Adj(v) 3Se w não visitado então 4 Insira aresta (v, w) na árvore 5 DFS-Visit(G, w) 3 4 1 2 7 5 6

  22. Busca em Profundidade : Exemplo DFS(G) 1Para todo v em G 2 Se v não visitado então 3 DFS-Visit(G, v) DFS-Visit(G, v) 1 Marque v como visitado 2Para todow em Adj(v) 3Se w não visitado então 4 Insira aresta (v, w) na árvore 5 DFS-Visit(G, w) 3 4 1 2 7 5 6

  23. Busca em Profundidade : Exemplo DFS(G) 1Para todo v em G 2 Se v não visitado então 3 DFS-Visit(G, v) DFS-Visit(G, v) 1 Marque v como visitado 2Para todow em Adj(v) 3Se w não visitado então 4 Insira aresta (v, w) na árvore 5 DFS-Visit(G, w) 3 4 1 2 7 5 6

  24. Busca em Profundidade : Exemplo DFS(G) 1Para todo v em G 2 Se v não visitado então 3 DFS-Visit(G, v) DFS-Visit(G, v) 1 Marque v como visitado 2Para todow em Adj(v) 3Se w não visitado então 4 Insira aresta (v, w) na árvore 5 DFS-Visit(G, w) 3 4 1 2 7 5 6

  25. Busca em Profundidade : Exemplo DFS(G) 1Para todo v em G 2 Se v não visitado então 3 DFS-Visit(G, v) DFS-Visit(G, v) 1 Marque v como visitado 2Para todow em Adj(v) 3Se w não visitado então 4 Insira aresta (v, w) na árvore 5 DFS-Visit(G, w) 3 4 1 2 7 5 6

  26. Busca em Profundidade : Exemplo DFS(G) 1Para todo v em G 2 Se v não visitado então 3 DFS-Visit(G, v) DFS-Visit(G, v) 1 Marque v como visitado 2Para todow em Adj(v) 3Se w não visitado então 4 Insira aresta (v, w) na árvore 5 DFS-Visit(G, w) 3 4 1 2 7 5 6

  27. Topological Ordering Algorithm: Example v2 v3 v6 v5 v4 v7 v1 v1 Topological order:

  28. Topological Ordering Algorithm: Example v2 v2 v3 v6 v5 v4 v7 Topological order: v1

  29. Topological Ordering Algorithm: Example v3 v3 v6 v5 v4 v7 Topological order: v1, v2

  30. Topological Ordering Algorithm: Example v6 v5 v4 v4 v7 Topological order: v1, v2, v3

  31. Topological Ordering Algorithm: Example v6 v5 v5 v7 Topological order: v1, v2, v3, v4

  32. Topological Ordering Algorithm: Example v6 v6 v7 Topological order: v1, v2, v3, v4, v5

  33. Topological Ordering Algorithm: Example v7 v7 Topological order: v1, v2, v3, v4, v5, v6

  34. Topological Ordering Algorithm: Example v2 v3 v6 v5 v4 v1 v2 v3 v4 v5 v6 v7 v7 v1 Topological order: v1, v2, v3, v4, v5, v6, v7.

More Related