Grafos Msc. Daniele Carvalho Oliveira 1. Pontes de Königsberg (1736) 2.
Transcript of Grafos Msc. Daniele Carvalho Oliveira 1. Pontes de Königsberg (1736) 2.
![Page 1: Grafos Msc. Daniele Carvalho Oliveira 1. Pontes de Königsberg (1736) 2.](https://reader035.fdocument.pub/reader035/viewer/2022062512/552fc182497959413d8f475b/html5/thumbnails/1.jpg)
1
GrafosMsc. Daniele Carvalho Oliveira
![Page 2: Grafos Msc. Daniele Carvalho Oliveira 1. Pontes de Königsberg (1736) 2.](https://reader035.fdocument.pub/reader035/viewer/2022062512/552fc182497959413d8f475b/html5/thumbnails/2.jpg)
2
Pontes de Königsberg (1736)
![Page 3: Grafos Msc. Daniele Carvalho Oliveira 1. Pontes de Königsberg (1736) 2.](https://reader035.fdocument.pub/reader035/viewer/2022062512/552fc182497959413d8f475b/html5/thumbnails/3.jpg)
3
O que são Grafos Conjunto de pontos e linhas que conectam os
pontos.
Grafo é um modelo matemático que representa relações entre objetos.
![Page 4: Grafos Msc. Daniele Carvalho Oliveira 1. Pontes de Königsberg (1736) 2.](https://reader035.fdocument.pub/reader035/viewer/2022062512/552fc182497959413d8f475b/html5/thumbnails/4.jpg)
4
O que são Grafos Grafos são estruturas de dados largamente
usadas em computação.
Exemplos de aplicações são: Modelagem de circuitos digitais Representação de processos em sistemas
paralelos ou distribuídos, Redes Redes Sociais Recuperação de Informação
![Page 5: Grafos Msc. Daniele Carvalho Oliveira 1. Pontes de Königsberg (1736) 2.](https://reader035.fdocument.pub/reader035/viewer/2022062512/552fc182497959413d8f475b/html5/thumbnails/5.jpg)
5
O que são Grafos Similar a árvores e listas, com menos
restrições
![Page 6: Grafos Msc. Daniele Carvalho Oliveira 1. Pontes de Königsberg (1736) 2.](https://reader035.fdocument.pub/reader035/viewer/2022062512/552fc182497959413d8f475b/html5/thumbnails/6.jpg)
6
![Page 7: Grafos Msc. Daniele Carvalho Oliveira 1. Pontes de Königsberg (1736) 2.](https://reader035.fdocument.pub/reader035/viewer/2022062512/552fc182497959413d8f475b/html5/thumbnails/7.jpg)
7
Definição Um Grafo é um par ordenado
é um conjunto finito não-vazio. Os elementos de são denominados vértices.
é um conjunto finito de pares ordenados de vértices. Os elementos de são denominados arestas. Dois vértices ligados por uma aresta são
denominados adjacentes
![Page 8: Grafos Msc. Daniele Carvalho Oliveira 1. Pontes de Königsberg (1736) 2.](https://reader035.fdocument.pub/reader035/viewer/2022062512/552fc182497959413d8f475b/html5/thumbnails/8.jpg)
8
Exemplo
1
2 0
3
4
![Page 9: Grafos Msc. Daniele Carvalho Oliveira 1. Pontes de Königsberg (1736) 2.](https://reader035.fdocument.pub/reader035/viewer/2022062512/552fc182497959413d8f475b/html5/thumbnails/9.jpg)
9
Conceitos de Grafos
![Page 10: Grafos Msc. Daniele Carvalho Oliveira 1. Pontes de Königsberg (1736) 2.](https://reader035.fdocument.pub/reader035/viewer/2022062512/552fc182497959413d8f475b/html5/thumbnails/10.jpg)
10
Laços, Arestas Múltiplas e Multigrafo
![Page 11: Grafos Msc. Daniele Carvalho Oliveira 1. Pontes de Königsberg (1736) 2.](https://reader035.fdocument.pub/reader035/viewer/2022062512/552fc182497959413d8f475b/html5/thumbnails/11.jpg)
11
Grafo Trivial e Grafo Simples
A B C
![Page 12: Grafos Msc. Daniele Carvalho Oliveira 1. Pontes de Königsberg (1736) 2.](https://reader035.fdocument.pub/reader035/viewer/2022062512/552fc182497959413d8f475b/html5/thumbnails/12.jpg)
12
Grafo Completo
![Page 13: Grafos Msc. Daniele Carvalho Oliveira 1. Pontes de Königsberg (1736) 2.](https://reader035.fdocument.pub/reader035/viewer/2022062512/552fc182497959413d8f475b/html5/thumbnails/13.jpg)
13
Grafo Regular
Não é Grafo Regular Grafo Regular de grau 3
1
2 0
3
4
![Page 14: Grafos Msc. Daniele Carvalho Oliveira 1. Pontes de Königsberg (1736) 2.](https://reader035.fdocument.pub/reader035/viewer/2022062512/552fc182497959413d8f475b/html5/thumbnails/14.jpg)
14
Subgrafo
1
2 0
3
43
4
1
3
4
0
![Page 15: Grafos Msc. Daniele Carvalho Oliveira 1. Pontes de Königsberg (1736) 2.](https://reader035.fdocument.pub/reader035/viewer/2022062512/552fc182497959413d8f475b/html5/thumbnails/15.jpg)
15
Caminho simples, trajeto e ciclos
Um Caminho é uma sequência de vértices , tal que
Quando todos os vértices do caminho são distintos, recebe o nome de caminho simples.
Quando todas as arestas são distintas, o caminho recebe o nome de trajeto
Um ciclo é um caminho , tal que e
![Page 16: Grafos Msc. Daniele Carvalho Oliveira 1. Pontes de Königsberg (1736) 2.](https://reader035.fdocument.pub/reader035/viewer/2022062512/552fc182497959413d8f475b/html5/thumbnails/16.jpg)
16
Caminho simples:
Trajeto:
Ciclo:
1
2 0
3
4
![Page 17: Grafos Msc. Daniele Carvalho Oliveira 1. Pontes de Königsberg (1736) 2.](https://reader035.fdocument.pub/reader035/viewer/2022062512/552fc182497959413d8f475b/html5/thumbnails/17.jpg)
17
Grafo Conexo e Desconexo
1
2 0
3
4
2
3 4
1
![Page 18: Grafos Msc. Daniele Carvalho Oliveira 1. Pontes de Königsberg (1736) 2.](https://reader035.fdocument.pub/reader035/viewer/2022062512/552fc182497959413d8f475b/html5/thumbnails/18.jpg)
18
Grafo Ponderado
C
F E
B
DA
5
7
3
2
4
8
1
6
![Page 19: Grafos Msc. Daniele Carvalho Oliveira 1. Pontes de Königsberg (1736) 2.](https://reader035.fdocument.pub/reader035/viewer/2022062512/552fc182497959413d8f475b/html5/thumbnails/19.jpg)
19
Dígrafo
1
2
3
5
6
4
![Page 20: Grafos Msc. Daniele Carvalho Oliveira 1. Pontes de Königsberg (1736) 2.](https://reader035.fdocument.pub/reader035/viewer/2022062512/552fc182497959413d8f475b/html5/thumbnails/20.jpg)
20
Dígrafo Grafo Orientado
Deve-se respeitar a orientação das arestas
![Page 21: Grafos Msc. Daniele Carvalho Oliveira 1. Pontes de Königsberg (1736) 2.](https://reader035.fdocument.pub/reader035/viewer/2022062512/552fc182497959413d8f475b/html5/thumbnails/21.jpg)
21
Dígrafo Grau de entrada: número de arestas que
incidem no vértice Vértice Fonte: grau de entrada = 0
Grau de saída: número de arestas que partem do vértice Vértice Sumidouro: grau de saída = 0
![Page 22: Grafos Msc. Daniele Carvalho Oliveira 1. Pontes de Königsberg (1736) 2.](https://reader035.fdocument.pub/reader035/viewer/2022062512/552fc182497959413d8f475b/html5/thumbnails/22.jpg)
22
Árvores Um Grafo
Não possui ciclos Conexo Seja , se possui grau menor ou igual a 1, então
é uma folha Uma árvore com vértices possui arestas. Um grafo é uma árvore, se, e somente se, existir
um único caminho entre cada par de vértices de .
![Page 23: Grafos Msc. Daniele Carvalho Oliveira 1. Pontes de Königsberg (1736) 2.](https://reader035.fdocument.pub/reader035/viewer/2022062512/552fc182497959413d8f475b/html5/thumbnails/23.jpg)
23
![Page 24: Grafos Msc. Daniele Carvalho Oliveira 1. Pontes de Königsberg (1736) 2.](https://reader035.fdocument.pub/reader035/viewer/2022062512/552fc182497959413d8f475b/html5/thumbnails/24.jpg)
24
Representação de Grafos
![Page 25: Grafos Msc. Daniele Carvalho Oliveira 1. Pontes de Königsberg (1736) 2.](https://reader035.fdocument.pub/reader035/viewer/2022062512/552fc182497959413d8f475b/html5/thumbnails/25.jpg)
25
Como representar grafos em nossos algoritmos?
Estruturas de dados! Matrizes
Matriz de Adjacência Matriz de Incidência
Listas Listas de Adjacência
![Page 26: Grafos Msc. Daniele Carvalho Oliveira 1. Pontes de Königsberg (1736) 2.](https://reader035.fdocument.pub/reader035/viewer/2022062512/552fc182497959413d8f475b/html5/thumbnails/26.jpg)
26
Matriz de Adjacência Dado um grafo Uma Matriz de Adjacência é formada por
linhas e colunas = número de vértices do grafo
![Page 27: Grafos Msc. Daniele Carvalho Oliveira 1. Pontes de Königsberg (1736) 2.](https://reader035.fdocument.pub/reader035/viewer/2022062512/552fc182497959413d8f475b/html5/thumbnails/27.jpg)
27
1 2 3 4 5
1 0 1 0 0 1
2 1 0 1 1 0
3 0 1 0 1 0
4 0 1 1 0 1
5 1 0 0 1 1
1
5 2
34
Simétrica
![Page 28: Grafos Msc. Daniele Carvalho Oliveira 1. Pontes de Königsberg (1736) 2.](https://reader035.fdocument.pub/reader035/viewer/2022062512/552fc182497959413d8f475b/html5/thumbnails/28.jpg)
28
1 2 3 4 5
1 0 1 0 0 1
2 0 0 1 0 0
3 0 1 0 0 0
4 0 1 1 0 0
5 0 0 0 1 1
1
5 2
34
![Page 29: Grafos Msc. Daniele Carvalho Oliveira 1. Pontes de Königsberg (1736) 2.](https://reader035.fdocument.pub/reader035/viewer/2022062512/552fc182497959413d8f475b/html5/thumbnails/29.jpg)
29
Matriz de Adjacência O espaço reservado para armazenar as
informações da matriz é da ordem Para buscar uma aresta:
![Page 30: Grafos Msc. Daniele Carvalho Oliveira 1. Pontes de Königsberg (1736) 2.](https://reader035.fdocument.pub/reader035/viewer/2022062512/552fc182497959413d8f475b/html5/thumbnails/30.jpg)
30
Matriz de Incidência Dado um grafo de vértices e arestas
![Page 31: Grafos Msc. Daniele Carvalho Oliveira 1. Pontes de Königsberg (1736) 2.](https://reader035.fdocument.pub/reader035/viewer/2022062512/552fc182497959413d8f475b/html5/thumbnails/31.jpg)
31
e1
e2
e3
e4
e5
e6
e7
e8
1 1 0 0 0 0 0 1 0
2 1 1 1 0 1 0 0 0
3 0 1 1 1 0 0 0 0
4 0 0 0 1 1 1 0 0
5 0 0 0 0 0 1 1 0
1
5 2
34 e4
e2 e3
e1
e5e6
e7e8
![Page 32: Grafos Msc. Daniele Carvalho Oliveira 1. Pontes de Königsberg (1736) 2.](https://reader035.fdocument.pub/reader035/viewer/2022062512/552fc182497959413d8f475b/html5/thumbnails/32.jpg)
32
![Page 33: Grafos Msc. Daniele Carvalho Oliveira 1. Pontes de Königsberg (1736) 2.](https://reader035.fdocument.pub/reader035/viewer/2022062512/552fc182497959413d8f475b/html5/thumbnails/33.jpg)
33
e1
e2
e3
e4
e5
e6
e7
e8
1 1 0 0 0 0 0 1 0
2 -1 1 -1 0 -1 0 0 0
3 0 -1 1 -1 0 0 0 0
4 0 0 0 1 1 -1 0 0
5 0 0 0 0 0 1 -1 0e4
e2 e3
e1
e5e6
e7e8 1
5 2
34
![Page 34: Grafos Msc. Daniele Carvalho Oliveira 1. Pontes de Königsberg (1736) 2.](https://reader035.fdocument.pub/reader035/viewer/2022062512/552fc182497959413d8f475b/html5/thumbnails/34.jpg)
34
Matriz de Incidência O espaço reservado para armazenar as
informações da matriz é da ordem Para buscar uma aresta:
![Page 35: Grafos Msc. Daniele Carvalho Oliveira 1. Pontes de Königsberg (1736) 2.](https://reader035.fdocument.pub/reader035/viewer/2022062512/552fc182497959413d8f475b/html5/thumbnails/35.jpg)
35
Lista de Adjacência Consiste em um vetor com entradas Cada entrada possui uma lista encadeada de
vértices adjacentes à .
![Page 36: Grafos Msc. Daniele Carvalho Oliveira 1. Pontes de Königsberg (1736) 2.](https://reader035.fdocument.pub/reader035/viewer/2022062512/552fc182497959413d8f475b/html5/thumbnails/36.jpg)
36
1
5 2
34
1
2
3
4
5
5 2
1
2
5
1
4
4
2
4
3
3
![Page 37: Grafos Msc. Daniele Carvalho Oliveira 1. Pontes de Königsberg (1736) 2.](https://reader035.fdocument.pub/reader035/viewer/2022062512/552fc182497959413d8f475b/html5/thumbnails/37.jpg)
37
1
2
3
4
5
5 2
3
2
2
4
3
1
5 2
34
![Page 38: Grafos Msc. Daniele Carvalho Oliveira 1. Pontes de Königsberg (1736) 2.](https://reader035.fdocument.pub/reader035/viewer/2022062512/552fc182497959413d8f475b/html5/thumbnails/38.jpg)
38
Matriz de Incidência O espaço reservado para armazenar as
informações da matriz é da ordem Para buscar uma aresta:
Para descobrir se existe a aresta deve-se percorrer a lista do nó até encontrar (ou não)
![Page 39: Grafos Msc. Daniele Carvalho Oliveira 1. Pontes de Königsberg (1736) 2.](https://reader035.fdocument.pub/reader035/viewer/2022062512/552fc182497959413d8f475b/html5/thumbnails/39.jpg)
39
Busca em Grafos
![Page 40: Grafos Msc. Daniele Carvalho Oliveira 1. Pontes de Königsberg (1736) 2.](https://reader035.fdocument.pub/reader035/viewer/2022062512/552fc182497959413d8f475b/html5/thumbnails/40.jpg)
40
Algoritmos de Busca Operação mais comum em Grafos
Visita sistemática a seus nós (uma única vez!) Similar às buscas em árvore
Para passear ou caminhar pelos vértices e arestas, utiliza-se marcar um vértice quando ele já foi visitado.
![Page 41: Grafos Msc. Daniele Carvalho Oliveira 1. Pontes de Königsberg (1736) 2.](https://reader035.fdocument.pub/reader035/viewer/2022062512/552fc182497959413d8f475b/html5/thumbnails/41.jpg)
41
Algoritmos de Busca Dois tipos básicos de busca
Busca em Profundidade Busca em Largura
![Page 42: Grafos Msc. Daniele Carvalho Oliveira 1. Pontes de Königsberg (1736) 2.](https://reader035.fdocument.pub/reader035/viewer/2022062512/552fc182497959413d8f475b/html5/thumbnails/42.jpg)
42
Busca em Profundidade
![Page 43: Grafos Msc. Daniele Carvalho Oliveira 1. Pontes de Königsberg (1736) 2.](https://reader035.fdocument.pub/reader035/viewer/2022062512/552fc182497959413d8f475b/html5/thumbnails/43.jpg)
43
1
5 2
34
1
2
3
4
5
5 2
1
2
5
1
4
4
2
4
3
3
![Page 44: Grafos Msc. Daniele Carvalho Oliveira 1. Pontes de Königsberg (1736) 2.](https://reader035.fdocument.pub/reader035/viewer/2022062512/552fc182497959413d8f475b/html5/thumbnails/44.jpg)
44
Busca em Profundidade Para acessar todos os vértices do grafo, a
busca em profundidade varre a lista de adjacência de cada vértice do grafo Assim, o tempo gasto para a operação é
![Page 45: Grafos Msc. Daniele Carvalho Oliveira 1. Pontes de Königsberg (1736) 2.](https://reader035.fdocument.pub/reader035/viewer/2022062512/552fc182497959413d8f475b/html5/thumbnails/45.jpg)
45
Busca em Largura
![Page 46: Grafos Msc. Daniele Carvalho Oliveira 1. Pontes de Königsberg (1736) 2.](https://reader035.fdocument.pub/reader035/viewer/2022062512/552fc182497959413d8f475b/html5/thumbnails/46.jpg)
46
1
2
3
4
5
5 2
3
2
2
4
3
1
5 2
34
1Fila
1 2 3 4 5
∞ ∞ ∞ ∞ ∞Distâncias
![Page 47: Grafos Msc. Daniele Carvalho Oliveira 1. Pontes de Königsberg (1736) 2.](https://reader035.fdocument.pub/reader035/viewer/2022062512/552fc182497959413d8f475b/html5/thumbnails/47.jpg)
47
1
2
3
4
5
5 2
3
2
2
4
3
1
5 2
34
Fila
1 2 3 4 5
0 ∞ ∞ ∞ ∞Distâncias
![Page 48: Grafos Msc. Daniele Carvalho Oliveira 1. Pontes de Königsberg (1736) 2.](https://reader035.fdocument.pub/reader035/viewer/2022062512/552fc182497959413d8f475b/html5/thumbnails/48.jpg)
48
1
2
3
4
5
5 2
3
2
2
4
3
1
5 2
34
5 2Fila
1 2 3 4 5
0 1 ∞ ∞ 1Distâncias
![Page 49: Grafos Msc. Daniele Carvalho Oliveira 1. Pontes de Königsberg (1736) 2.](https://reader035.fdocument.pub/reader035/viewer/2022062512/552fc182497959413d8f475b/html5/thumbnails/49.jpg)
49
1
2
3
4
5
5 2
3
2
2
4
3
1
5 2
34
2Fila
1 2 3 4 5
0 1 ∞ ∞ 1Distâncias
![Page 50: Grafos Msc. Daniele Carvalho Oliveira 1. Pontes de Königsberg (1736) 2.](https://reader035.fdocument.pub/reader035/viewer/2022062512/552fc182497959413d8f475b/html5/thumbnails/50.jpg)
50
1
2
3
4
5
5 2
3
2
2
4
3
1
5 2
34
2Fila
1 2 3 4 5
0 1 ∞ ∞ 1Distâncias
![Page 51: Grafos Msc. Daniele Carvalho Oliveira 1. Pontes de Königsberg (1736) 2.](https://reader035.fdocument.pub/reader035/viewer/2022062512/552fc182497959413d8f475b/html5/thumbnails/51.jpg)
51
1
2
3
4
5
5 2
3
2
2
4
3
1
5 2
34
2 4Fila
1 2 3 4 5
0 1 ∞ 2 1Distâncias
![Page 52: Grafos Msc. Daniele Carvalho Oliveira 1. Pontes de Königsberg (1736) 2.](https://reader035.fdocument.pub/reader035/viewer/2022062512/552fc182497959413d8f475b/html5/thumbnails/52.jpg)
52
1
2
3
4
5
5 2
3
2
2
4
3
1
5 2
34
4Fila
1 2 3 4 5
0 1 ∞ 2 1Distâncias
![Page 53: Grafos Msc. Daniele Carvalho Oliveira 1. Pontes de Königsberg (1736) 2.](https://reader035.fdocument.pub/reader035/viewer/2022062512/552fc182497959413d8f475b/html5/thumbnails/53.jpg)
53
1
2
3
4
5
5 2
3
2
2
4
3
1
5 2
34
4Fila
1 2 3 4 5
0 1 ∞ 2 1Distâncias
![Page 54: Grafos Msc. Daniele Carvalho Oliveira 1. Pontes de Königsberg (1736) 2.](https://reader035.fdocument.pub/reader035/viewer/2022062512/552fc182497959413d8f475b/html5/thumbnails/54.jpg)
54
1
2
3
4
5
5 2
3
2
2
4
3
1
5 2
34
4 3Fila
1 2 3 4 5
0 1 2 2 1Distâncias
![Page 55: Grafos Msc. Daniele Carvalho Oliveira 1. Pontes de Königsberg (1736) 2.](https://reader035.fdocument.pub/reader035/viewer/2022062512/552fc182497959413d8f475b/html5/thumbnails/55.jpg)
55
1
2
3
4
5
5 2
3
2
2
4
3
1
5 2
34
3Fila
1 2 3 4 5
0 1 2 2 1Distâncias
![Page 56: Grafos Msc. Daniele Carvalho Oliveira 1. Pontes de Königsberg (1736) 2.](https://reader035.fdocument.pub/reader035/viewer/2022062512/552fc182497959413d8f475b/html5/thumbnails/56.jpg)
56
1
2
3
4
5
5 2
3
2
2
4
3
1
5 2
34
3Fila
1 2 3 4 5
0 1 2 2 1Distâncias
![Page 57: Grafos Msc. Daniele Carvalho Oliveira 1. Pontes de Königsberg (1736) 2.](https://reader035.fdocument.pub/reader035/viewer/2022062512/552fc182497959413d8f475b/html5/thumbnails/57.jpg)
57
1
2
3
4
5
5 2
3
2
2
4
3
1
5 2
34
Fila
1 2 3 4 5
0 1 2 2 1Distâncias
![Page 58: Grafos Msc. Daniele Carvalho Oliveira 1. Pontes de Königsberg (1736) 2.](https://reader035.fdocument.pub/reader035/viewer/2022062512/552fc182497959413d8f475b/html5/thumbnails/58.jpg)
58
1
2
3
4
5
5 2
3
2
2
4
3
1
5 2
34
Fila
1 2 3 4 5
0 1 2 2 1Distâncias
![Page 59: Grafos Msc. Daniele Carvalho Oliveira 1. Pontes de Königsberg (1736) 2.](https://reader035.fdocument.pub/reader035/viewer/2022062512/552fc182497959413d8f475b/html5/thumbnails/59.jpg)
59
Busca em Largura Primeiro os vértices são inicializados, ou seja,
marcados como não visitados. O vetor de distâncias é inicializado com ∞ em
todas as posições Este processo de inicialização gasta tempo
![Page 60: Grafos Msc. Daniele Carvalho Oliveira 1. Pontes de Königsberg (1736) 2.](https://reader035.fdocument.pub/reader035/viewer/2022062512/552fc182497959413d8f475b/html5/thumbnails/60.jpg)
60
Busca em Largura A medida que os vértices são encontrados, são
colocados em uma fila As operações de inserção e remoção da fila
gastam tempo Como todos os vértices são colocados e retirados
da fila, o tempo total é
![Page 61: Grafos Msc. Daniele Carvalho Oliveira 1. Pontes de Königsberg (1736) 2.](https://reader035.fdocument.pub/reader035/viewer/2022062512/552fc182497959413d8f475b/html5/thumbnails/61.jpg)
61
Busca em Largura Como cada vértice é colocado na fila apenas 1
vez, a lista de adjacência de cada um só é analisado 1 vez Tempo
Portanto o tempo de execução da busca em largura é