Carregar apresentação
A apresentação está carregando. Por favor, espere
PublicouAna do Carmo Bergmann Back Alterado mais de 9 anos atrás
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); 6 For each v in Adj[u] then 7 if v is unexplored then 8 mark edge (v,u) as tree edge 9 mark vertex v as visited 10 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
2
Exemplo 1 2 3 4 5 7 6 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 9 mark vertex v as visited 10 Enqueue(S,v) S
3
Exemplo 1 2 3 4 5 7 6 1 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 9 mark vertex v as visited 10 Enqueue(S,v) S
4
1 2 3 4 5 7 6 Exemplo 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 9 mark vertex v as visited 10 Enqueue(S,v) S
5
Exemplo 1 2 3 4 5 7 6 4 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 9 mark vertex v as visited 10 Enqueue(S,v) S
6
Exemplo 1 2 3 4 5 7 6 4 5 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 9 mark vertex v as visited 10 Enqueue(S,v) S
7
Exemplo 1 2 3 4 5 7 6 4 5 2 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 9 mark vertex v as visited 10 Enqueue(S,v) S
8
Exemplo 1 2 3 4 5 7 6 5 2 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 9 mark vertex v as visited 10 Enqueue(S,v) S
9
Exemplo 1 2 3 4 5 7 6 5 2 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 9 mark vertex v as visited 10 Enqueue(S,v) S
10
Exemplo 1 2 3 4 5 7 6 5 2 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 9 mark vertex v as visited 10 Enqueue(S,v) S
11
Exemplo 1 2 3 4 5 7 6 2 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 9 mark vertex v as visited 10 Enqueue(S,v) S
12
1 2 3 4 5 7 6 Exemplo 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 9 mark vertex v as visited 10 Enqueue(S,v) S
13
Exemplo 1 2 3 4 5 7 6 6 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 9 mark vertex v as visited 10 Enqueue(S,v) S
14
Exemplo 1 2 3 4 5 7 6 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 9 mark vertex v as visited 10 Enqueue(S,v) S
15
1 2 3 4 5 7 6 3 Exemplo 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 9 mark vertex v as visited 10 Enqueue(S,v) S
16
1 2 3 4 5 7 6 3 Exemplo 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 9 mark vertex v as visited 10 Enqueue(S,v) S 3
17
Exemplo 1 2 3 4 5 7 6 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 9 mark vertex v as visited 10 Enqueue(S,v) S
18
Busca em Profundidade 1 2 3 4 5 7 6 DFS(G) 1Para todo v em G 2Se v não visitado então 3DFS-Visit(G, v) DFS-Visit(G, v) 1Marque v como visitado 2 Para todo w em Adj(v) 3 Se w não visitado então 4 Insira aresta (v, w) na árvore 5 DFS-Visit(G, w)
19
Busca em Profundidade : Exemplo 1 2 3 4 5 7 6 DFS(G) 1Para todo v em G 2Se v não visitado então 3DFS-Visit(G, v) DFS-Visit(G, v) 1Marque v como visitado 2 Para todo w em Adj(v) 3 Se w não visitado então 4 Insira aresta (v, w) na árvore 5 DFS-Visit(G, w)
20
Busca em Profundidade : Exemplo 1 2 3 4 5 7 6 DFS(G) 1Para todo v em G 2Se v não visitado então 3DFS-Visit(G, v) DFS-Visit(G, v) 1Marque v como visitado 2 Para todo w em Adj(v) 3 Se w não visitado então 4 Insira aresta (v, w) na árvore 5 DFS-Visit(G, w)
21
Busca em Profundidade : Exemplo 1 2 3 4 5 7 6 DFS(G) 1Para todo v em G 2Se v não visitado então 3DFS-Visit(G, v) DFS-Visit(G, v) 1Marque v como visitado 2 Para todo w em Adj(v) 3 Se w não visitado então 4 Insira aresta (v, w) na árvore 5 DFS-Visit(G, w)
22
Busca em Profundidade : Exemplo 1 2 3 4 5 7 6 DFS(G) 1Para todo v em G 2Se v não visitado então 3DFS-Visit(G, v) DFS-Visit(G, v) 1Marque v como visitado 2 Para todo w em Adj(v) 3 Se w não visitado então 4 Insira aresta (v, w) na árvore 5 DFS-Visit(G, w)
23
Busca em Profundidade : Exemplo 1 2 3 4 5 7 6 DFS(G) 1Para todo v em G 2Se v não visitado então 3DFS-Visit(G, v) DFS-Visit(G, v) 1Marque v como visitado 2 Para todo w em Adj(v) 3 Se w não visitado então 4 Insira aresta (v, w) na árvore 5 DFS-Visit(G, w)
24
Busca em Profundidade : Exemplo 1 2 3 4 5 7 6 DFS(G) 1Para todo v em G 2Se v não visitado então 3DFS-Visit(G, v) DFS-Visit(G, v) 1Marque v como visitado 2 Para todo w em Adj(v) 3 Se w não visitado então 4 Insira aresta (v, w) na árvore 5 DFS-Visit(G, w)
25
Busca em Profundidade : Exemplo 1 2 3 4 5 7 6 DFS(G) 1Para todo v em G 2Se v não visitado então 3DFS-Visit(G, v) DFS-Visit(G, v) 1Marque v como visitado 2 Para todo w em Adj(v) 3 Se w não visitado então 4 Insira aresta (v, w) na árvore 5 DFS-Visit(G, w)
26
Busca em Profundidade : Exemplo 1 2 3 4 5 7 6 DFS(G) 1Para todo v em G 2Se v não visitado então 3DFS-Visit(G, v) DFS-Visit(G, v) 1Marque v como visitado 2 Para todo w em Adj(v) 3 Se w não visitado então 4 Insira aresta (v, w) na árvore 5 DFS-Visit(G, w)
27
27 v1v1 Topological Ordering Algorithm: Example Topological order: v2v2 v3v3 v6v6 v5v5 v4v4 v7v7 v1v1
28
28 v2v2 Topological Ordering Algorithm: Example Topological order: v 1 v2v2 v3v3 v6v6 v5v5 v4v4 v7v7
29
29 v3v3 Topological Ordering Algorithm: Example Topological order: v 1, v 2 v3v3 v6v6 v5v5 v4v4 v7v7
30
30 v4v4 Topological Ordering Algorithm: Example Topological order: v 1, v 2, v 3 v6v6 v5v5 v4v4 v7v7
31
31 v5v5 Topological Ordering Algorithm: Example Topological order: v 1, v 2, v 3, v 4 v6v6 v5v5 v7v7
32
32 v6v6 Topological Ordering Algorithm: Example Topological order: v 1, v 2, v 3, v 4, v 5 v6v6 v7v7
33
33 v7v7 Topological Ordering Algorithm: Example Topological order: v 1, v 2, v 3, v 4, v 5, v 6 v7v7
34
34 Topological Ordering Algorithm: Example Topological order: v 1, v 2, v 3, v 4, v 5, v 6, v 7. v2v2 v3v3 v6v6 v5v5 v4v4 v7v7 v1v1 v1v1 v2v2 v3v3 v4v4 v5v5 v6v6 v7v7
Apresentações semelhantes
© 2024 SlidePlayer.com.br Inc.
All rights reserved.