Upload
ana-do-carmo-bergmann-back
View
215
Download
0
Embed Size (px)
Citation preview
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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)
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)
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)
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)
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)
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)
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)
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)
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)
27
v1
Topological Ordering Algorithm: Example
Topological order:
v2 v3
v6 v5 v4
v7 v1
28
v2
Topological Ordering Algorithm: Example
Topological order: v1
v2 v3
v6 v5 v4
v7
29
v3
Topological Ordering Algorithm: Example
Topological order: v1, v2
v3
v6 v5 v4
v7
30
v4
Topological Ordering Algorithm: Example
Topological order: v1, v2, v3
v6 v5 v4
v7
31
v5
Topological Ordering Algorithm: Example
Topological order: v1, v2, v3, v4
v6 v5
v7
32
v6
Topological Ordering Algorithm: Example
Topological order: v1, v2, v3, v4, v5
v6
v7
33
v7
Topological Ordering Algorithm: Example
Topological order: v1, v2, v3, v4, v5, v6
v7
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