running time: o(n + m) bfs(g) 1 for every vertex s of g not explored yet 2 do enqueue(s,s) 3 mark...

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); 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

Upload: ana-do-carmo-bergmann-back

Post on 07-Apr-2016

215 views

Category:

Documents


0 download

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);

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);

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);

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);

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);

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);

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);

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);

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);

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);

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);

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);

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);

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);

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);

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);

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);

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);

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);

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);

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);

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);

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);

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);

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);

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);

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);

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);

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);

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);

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);

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);

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);

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);

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