running time: o(n + m) bfs(g) 1 for every vertex s of g not explored yet 2 do enqueue(s,s) 3 mark...
TRANSCRIPT
![Page 1: Running time: O(n + m) 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);](https://reader035.vdocument.in/reader035/viewer/2022070507/5706384b1a28abb8238f5ffe/html5/thumbnails/1.jpg)
Running time: O(n + m)
BFS(G)
1 for every vertex s of G not explored yet2 do Enqueue(S,s) 3 mark vertex s as visited4 while S is not empty do5 u ← Dequeue(S); 6 For each v in Adj[u] then 7 if v is unexplored then8 mark edge (v,u) as tree edge9 mark vertex v as visited10 Enqueue(S,v)
Busca em Largura
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
![Page 2: Running time: O(n + m) 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);](https://reader035.vdocument.in/reader035/viewer/2022070507/5706384b1a28abb8238f5ffe/html5/thumbnails/2.jpg)
Exemplo
12
34
57
6
BFS(G)
1 for every vertex s of G not explored yet2 do Enqueue(S,s); 3 mark vertex s as visited4 while S is not empty do5 u ← Dequeue(S); 6 For each v in Adj[u] then 7 if v is unexplored then8 mark edge (v,u) as tree edge9 mark vertex v as visited10 Enqueue(S,v)
S
![Page 3: Running time: O(n + m) 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);](https://reader035.vdocument.in/reader035/viewer/2022070507/5706384b1a28abb8238f5ffe/html5/thumbnails/3.jpg)
Exemplo
12
34
57
6
1
BFS(G)
1 for every vertex s of G not explored yet2 do Enqueue(S,s); 3 mark vertex s as visited4 while S is not empty do5 u ← Dequeue(S); 6 For each v in Adj[u] then 7 if v is unexplored then8 mark edge (v,u) as tree edge9 mark vertex v as visited10 Enqueue(S,v)
S
![Page 4: Running time: O(n + m) 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);](https://reader035.vdocument.in/reader035/viewer/2022070507/5706384b1a28abb8238f5ffe/html5/thumbnails/4.jpg)
12
34
57
6
Exemplo
BFS(G)
1 for every vertex s of G not explored yet2 do Enqueue(S,s); 3 mark vertex s as visited4 while S is not empty do5 u ← Dequeue(S); 6 For each v in Adj[u] then 7 if v is unexplored then8 mark edge (v,u) as tree edge9 mark vertex v as visited10 Enqueue(S,v)
S
![Page 5: Running time: O(n + m) 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);](https://reader035.vdocument.in/reader035/viewer/2022070507/5706384b1a28abb8238f5ffe/html5/thumbnails/5.jpg)
Exemplo
12
34
57
6
4
BFS(G)
1 for every vertex s of G not explored yet2 do Enqueue(S,s); 3 mark vertex s as visited4 while S is not empty do5 u ← Dequeue(S); 6 For each v in Adj[u] then 7 if v is unexplored then8 mark edge (v,u) as tree edge9 mark vertex v as visited10 Enqueue(S,v)
S
![Page 6: Running time: O(n + m) 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);](https://reader035.vdocument.in/reader035/viewer/2022070507/5706384b1a28abb8238f5ffe/html5/thumbnails/6.jpg)
Exemplo
12
34
57
6
4 5
BFS(G)
1 for every vertex s of G not explored yet2 do Enqueue(S,s); 3 mark vertex s as visited4 while S is not empty do5 u ← Dequeue(S); 6 For each v in Adj[u] then 7 if v is unexplored then8 mark edge (v,u) as tree edge9 mark vertex v as visited10 Enqueue(S,v)
S
![Page 7: Running time: O(n + m) 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);](https://reader035.vdocument.in/reader035/viewer/2022070507/5706384b1a28abb8238f5ffe/html5/thumbnails/7.jpg)
Exemplo
12
34
57
6
4 5 2
BFS(G)
1 for every vertex s of G not explored yet2 do Enqueue(S,s); 3 mark vertex s as visited4 while S is not empty do5 u ← Dequeue(S); 6 For each v in Adj[u] then 7 if v is unexplored then8 mark edge (v,u) as tree edge9 mark vertex v as visited10 Enqueue(S,v)
S
![Page 8: Running time: O(n + m) 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);](https://reader035.vdocument.in/reader035/viewer/2022070507/5706384b1a28abb8238f5ffe/html5/thumbnails/8.jpg)
Exemplo
12
34
57
6
5 2
BFS(G)
1 for every vertex s of G not explored yet2 do Enqueue(S,s); 3 mark vertex s as visited4 while S is not empty do5 u ← Dequeue(S); 6 For each v in Adj[u] then 7 if v is unexplored then8 mark edge (v,u) as tree edge9 mark vertex v as visited10 Enqueue(S,v)
S
![Page 9: Running time: O(n + m) 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);](https://reader035.vdocument.in/reader035/viewer/2022070507/5706384b1a28abb8238f5ffe/html5/thumbnails/9.jpg)
Exemplo
12
34
57
6
5 2
BFS(G)
1 for every vertex s of G not explored yet2 do Enqueue(S,s); 3 mark vertex s as visited4 while S is not empty do5 u ← Dequeue(S); 6 For each v in Adj[u] then 7 if v is unexplored then8 mark edge (v,u) as tree edge9 mark vertex v as visited10 Enqueue(S,v)
S
![Page 10: Running time: O(n + m) 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);](https://reader035.vdocument.in/reader035/viewer/2022070507/5706384b1a28abb8238f5ffe/html5/thumbnails/10.jpg)
Exemplo
12
34
57
6
5 2
BFS(G)
1 for every vertex s of G not explored yet2 do Enqueue(S,s); 3 mark vertex s as visited4 while S is not empty do5 u ← Dequeue(S); 6 For each v in Adj[u] then 7 if v is unexplored then8 mark edge (v,u) as tree edge9 mark vertex v as visited10 Enqueue(S,v)
S
![Page 11: Running time: O(n + m) 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);](https://reader035.vdocument.in/reader035/viewer/2022070507/5706384b1a28abb8238f5ffe/html5/thumbnails/11.jpg)
Exemplo
12
34
57
6
2
BFS(G)
1 for every vertex s of G not explored yet2 do Enqueue(S,s); 3 mark vertex s as visited4 while S is not empty do5 u ← Dequeue(S); 6 For each v in Adj[u] then 7 if v is unexplored then8 mark edge (v,u) as tree edge9 mark vertex v as visited10 Enqueue(S,v)
S
![Page 12: Running time: O(n + m) 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);](https://reader035.vdocument.in/reader035/viewer/2022070507/5706384b1a28abb8238f5ffe/html5/thumbnails/12.jpg)
12
34
57
6
Exemplo
BFS(G)
1 for every vertex s of G not explored yet2 do Enqueue(S,s); 3 mark vertex s as visited4 while S is not empty do5 u ← Dequeue(S); 6 For each v in Adj[u] then 7 if v is unexplored then8 mark edge (v,u) as tree edge9 mark vertex v as visited10 Enqueue(S,v)
S
![Page 13: Running time: O(n + m) 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);](https://reader035.vdocument.in/reader035/viewer/2022070507/5706384b1a28abb8238f5ffe/html5/thumbnails/13.jpg)
Exemplo
12
34
57
6
6
BFS(G)
1 for every vertex s of G not explored yet2 do Enqueue(S,s); 3 mark vertex s as visited4 while S is not empty do5 u ← Dequeue(S); 6 For each v in Adj[u] then 7 if v is unexplored then8 mark edge (v,u) as tree edge9 mark vertex v as visited10 Enqueue(S,v)
S
![Page 14: Running time: O(n + m) 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);](https://reader035.vdocument.in/reader035/viewer/2022070507/5706384b1a28abb8238f5ffe/html5/thumbnails/14.jpg)
Exemplo
12
34
57
6
BFS(G)
1 for every vertex s of G not explored yet2 do Enqueue(S,s); 3 mark vertex s as visited4 while S is not empty do5 u ← Dequeue(S); 6 For each v in Adj[u] then 7 if v is unexplored then8 mark edge (v,u) as tree edge9 mark vertex v as visited10 Enqueue(S,v)
S
![Page 15: Running time: O(n + m) 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);](https://reader035.vdocument.in/reader035/viewer/2022070507/5706384b1a28abb8238f5ffe/html5/thumbnails/15.jpg)
12
34
57
6
3
Exemplo
BFS(G)
1 for every vertex s of G not explored yet2 do Enqueue(S,s); 3 mark vertex s as visited4 while S is not empty do5 u ← Dequeue(S); 6 For each v in Adj[u] then 7 if v is unexplored then8 mark edge (v,u) as tree edge9 mark vertex v as visited10 Enqueue(S,v)
S
![Page 16: Running time: O(n + m) 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);](https://reader035.vdocument.in/reader035/viewer/2022070507/5706384b1a28abb8238f5ffe/html5/thumbnails/16.jpg)
12
34
57
6
3
Exemplo
BFS(G)
1 for every vertex s of G not explored yet2 do Enqueue(S,s); 3 mark vertex s as visited4 while S is not empty do5 u ← Dequeue(S); 6 For each v in Adj[u] then 7 if v is unexplored then8 mark edge (v,u) as tree edge9 mark vertex v as visited10 Enqueue(S,v)
S
3
![Page 17: Running time: O(n + m) 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);](https://reader035.vdocument.in/reader035/viewer/2022070507/5706384b1a28abb8238f5ffe/html5/thumbnails/17.jpg)
Exemplo
12
34
57
6
BFS(G)
1 for every vertex s of G not explored yet2 do Enqueue(S,s); 3 mark vertex s as visited4 while S is not empty do5 u ← Dequeue(S); 6 For each v in Adj[u] then 7 if v is unexplored then8 mark edge (v,u) as tree edge9 mark vertex v as visited10 Enqueue(S,v)
S
![Page 18: Running time: O(n + m) 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);](https://reader035.vdocument.in/reader035/viewer/2022070507/5706384b1a28abb8238f5ffe/html5/thumbnails/18.jpg)
Busca em Profundidade
12
34
57
6
DFS(G)1 Para todo v em G2 Se v não visitado então 3 DFS-Visit(G, v)
DFS-Visit(G, v)1 Marque v como visitado2 Para todo w em Adj(v)3 Se w não visitado então 4 Insira aresta (v, w) na árvore5 DFS-Visit(G, w)
![Page 19: Running time: O(n + m) 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);](https://reader035.vdocument.in/reader035/viewer/2022070507/5706384b1a28abb8238f5ffe/html5/thumbnails/19.jpg)
Busca em Profundidade : Exemplo
12
34
57
6
DFS(G)1 Para todo v em G2 Se v não visitado então 3 DFS-Visit(G, v)
DFS-Visit(G, v)1 Marque v como visitado2 Para todo w em Adj(v)3 Se w não visitado então 4 Insira aresta (v, w) na árvore5 DFS-Visit(G, w)
![Page 20: Running time: O(n + m) 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);](https://reader035.vdocument.in/reader035/viewer/2022070507/5706384b1a28abb8238f5ffe/html5/thumbnails/20.jpg)
Busca em Profundidade : Exemplo
12
34
57
6
DFS(G)1 Para todo v em G2 Se v não visitado então 3 DFS-Visit(G, v)
DFS-Visit(G, v)1 Marque v como visitado2 Para todo w em Adj(v)3 Se w não visitado então 4 Insira aresta (v, w) na árvore5 DFS-Visit(G, w)
![Page 21: Running time: O(n + m) 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);](https://reader035.vdocument.in/reader035/viewer/2022070507/5706384b1a28abb8238f5ffe/html5/thumbnails/21.jpg)
Busca em Profundidade : Exemplo
12
34
57
6
DFS(G)1 Para todo v em G2 Se v não visitado então 3 DFS-Visit(G, v)
DFS-Visit(G, v)1 Marque v como visitado2 Para todo w em Adj(v)3 Se w não visitado então 4 Insira aresta (v, w) na árvore5 DFS-Visit(G, w)
![Page 22: Running time: O(n + m) 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);](https://reader035.vdocument.in/reader035/viewer/2022070507/5706384b1a28abb8238f5ffe/html5/thumbnails/22.jpg)
Busca em Profundidade : Exemplo
12
34
57
6
DFS(G)1 Para todo v em G2 Se v não visitado então 3 DFS-Visit(G, v)
DFS-Visit(G, v)1 Marque v como visitado2 Para todo w em Adj(v)3 Se w não visitado então 4 Insira aresta (v, w) na árvore5 DFS-Visit(G, w)
![Page 23: Running time: O(n + m) 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);](https://reader035.vdocument.in/reader035/viewer/2022070507/5706384b1a28abb8238f5ffe/html5/thumbnails/23.jpg)
Busca em Profundidade : Exemplo
12
34
57
6
DFS(G)1 Para todo v em G2 Se v não visitado então 3 DFS-Visit(G, v)
DFS-Visit(G, v)1 Marque v como visitado2 Para todo w em Adj(v)3 Se w não visitado então 4 Insira aresta (v, w) na árvore5 DFS-Visit(G, w)
![Page 24: Running time: O(n + m) 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);](https://reader035.vdocument.in/reader035/viewer/2022070507/5706384b1a28abb8238f5ffe/html5/thumbnails/24.jpg)
Busca em Profundidade : Exemplo
12
34
57
6
DFS(G)1 Para todo v em G2 Se v não visitado então 3 DFS-Visit(G, v)
DFS-Visit(G, v)1 Marque v como visitado2 Para todo w em Adj(v)3 Se w não visitado então 4 Insira aresta (v, w) na árvore5 DFS-Visit(G, w)
![Page 25: Running time: O(n + m) 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);](https://reader035.vdocument.in/reader035/viewer/2022070507/5706384b1a28abb8238f5ffe/html5/thumbnails/25.jpg)
Busca em Profundidade : Exemplo
12
34
57
6
DFS(G)1 Para todo v em G2 Se v não visitado então 3 DFS-Visit(G, v)
DFS-Visit(G, v)1 Marque v como visitado2 Para todo w em Adj(v)3 Se w não visitado então 4 Insira aresta (v, w) na árvore5 DFS-Visit(G, w)
![Page 26: Running time: O(n + m) 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);](https://reader035.vdocument.in/reader035/viewer/2022070507/5706384b1a28abb8238f5ffe/html5/thumbnails/26.jpg)
Busca em Profundidade : Exemplo
12
34
57
6
DFS(G)1 Para todo v em G2 Se v não visitado então 3 DFS-Visit(G, v)
DFS-Visit(G, v)1 Marque v como visitado2 Para todo w em Adj(v)3 Se w não visitado então 4 Insira aresta (v, w) na árvore5 DFS-Visit(G, w)
![Page 27: Running time: O(n + m) 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);](https://reader035.vdocument.in/reader035/viewer/2022070507/5706384b1a28abb8238f5ffe/html5/thumbnails/27.jpg)
27
v1
Topological Ordering Algorithm: Example
Topological order:
v2 v3
v6 v5 v4
v7 v1
![Page 28: Running time: O(n + m) 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);](https://reader035.vdocument.in/reader035/viewer/2022070507/5706384b1a28abb8238f5ffe/html5/thumbnails/28.jpg)
28
v2
Topological Ordering Algorithm: Example
Topological order: v1
v2 v3
v6 v5 v4
v7
![Page 29: Running time: O(n + m) 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);](https://reader035.vdocument.in/reader035/viewer/2022070507/5706384b1a28abb8238f5ffe/html5/thumbnails/29.jpg)
29
v3
Topological Ordering Algorithm: Example
Topological order: v1, v2
v3
v6 v5 v4
v7
![Page 30: Running time: O(n + m) 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);](https://reader035.vdocument.in/reader035/viewer/2022070507/5706384b1a28abb8238f5ffe/html5/thumbnails/30.jpg)
30
v4
Topological Ordering Algorithm: Example
Topological order: v1, v2, v3
v6 v5 v4
v7
![Page 31: Running time: O(n + m) 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);](https://reader035.vdocument.in/reader035/viewer/2022070507/5706384b1a28abb8238f5ffe/html5/thumbnails/31.jpg)
31
v5
Topological Ordering Algorithm: Example
Topological order: v1, v2, v3, v4
v6 v5
v7
![Page 32: Running time: O(n + m) 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);](https://reader035.vdocument.in/reader035/viewer/2022070507/5706384b1a28abb8238f5ffe/html5/thumbnails/32.jpg)
32
v6
Topological Ordering Algorithm: Example
Topological order: v1, v2, v3, v4, v5
v6
v7
![Page 33: Running time: O(n + m) 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);](https://reader035.vdocument.in/reader035/viewer/2022070507/5706384b1a28abb8238f5ffe/html5/thumbnails/33.jpg)
33
v7
Topological Ordering Algorithm: Example
Topological order: v1, v2, v3, v4, v5, v6
v7
![Page 34: Running time: O(n + m) 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);](https://reader035.vdocument.in/reader035/viewer/2022070507/5706384b1a28abb8238f5ffe/html5/thumbnails/34.jpg)
34
Topological Ordering Algorithm: Example
Topological order: v1, v2, v3, v4, v5, v6, v7.
v2 v3
v6 v5 v4
v7 v1
v1 v2 v3 v4 v5 v6 v7