Академический Документы
Профессиональный Документы
Культура Документы
Matemtica Discreta 12
Prof. Jorge Cavalcanti
jorge.cavalcanti@univasf.edu.br - www.univasf.edu.br/~jorge.cavalcanti
1
Hashing
Introduo
c
h
a
v
e
s
Tabela Hashing
Funo Hashing
h(k)
2
Subconjuntos
Hashing
Ex: rvores
5 2 6 1 7 8 4 9
2
5
9
6
4
3
Hashing
20 ?
64
45 % 8 = 5
3
11
20
7
7
45 ?
4
Hashing
237
360
387
452
766
1000
Tabela Hashing
16
20
23
Funo Hashing
h(k)
(c mod 23) + 1
Chave
End
Subconjuntos
452
623
766
237
134
360
285
16
20
16
10
Hashing
Hashing
Tabela Hashing
Funo Hashing
h(k)
c
h
a
v
e
s
Tabela Hashing
Funo Hashing
h(k)
Vetor de listas
Hashing
Ex.:
Hashing
Funo de Hashing
Hashing
Hashing
11
Hashing
Chaves no-numricas:
Cod ASCII 2
primeiras letras
Produto
Endereo
BALL
66 65
66 x 65 = 4.290
290
LOWELL
76 96
76 x 96 = 6.004
004
TREE
84 82
84 x 82 = 6.888
888
12
Hashing
Continuao Exemplo:
Temos, h(LOWELL)=h(LOCK)=h(OLIVER)
13
Hashing
Colises
Problemas de hashing:
14
Hashing
Colises
15
Hashing
Endereamento Fechado (Closed Addressing)
20 % 5 = 0
18 % 5 = 3
20
25
1
2
25 % 5 = 0
coliso com 20
18
4
16
Hashing
Endereamento Fechado (Closed Addressing)
20
15
11
88
32
60
1
2
3
17
Hashing
Endereamento Fechado (Closed Addressing)
15
10
13
31
88
20
18
Hashing
Endereamento Aberto (Open Addressing)
27 % 8 = 3
0
27 ?
64
11 20 27
19
Hashing
Endereamento Aberto (Open Addressing)
Valores: 52, 78, 48, 61, 81, 120, 79, 121, 92
Funo: hash(k) = k % 13
Tamanho da tabela: 13
0
52 78 79 81 120 121 92
52 92
78
79
81 121
120
10
11
12
48 61
48
61
20
Hashing
Endereamento Aberto Remoo
Insero do 27
27? No
Remoo do 20
64
11 20 27
Busca pelo 27
Hashing
Endereamento Aberto Remoo
64
11 20
X 27
Fim da busca? No
Hashing
Endereamento Aberto Expanso
9?
64 1
X 11 X
23
Hashing
Endereamento Aberto Expanso
34 40 X 11
95
Tamanho = 7
Tamanho = 13
40
95
34
11
24
Hashing
Endereamento Aberto Expanso
25
Hashing
Quando no usar Hashing?
26
Hashing
Exerccios:
1 - Ilustre a organizao final de uma Tabela Hash aps a
insero das seguintes chaves: 35, 99, 27, 18, 65, 45.
Considere a tabela com tamanho 6 (posies 0 a 5), o
mtodo da diviso inteira como funo de hashing e
tratamento de coliso por endereamento fechado.
Considere tambm que os nmeros possveis de chaves
esto no intervalo entre 1 a 100.
2 - Idem questo anterior, porm o tratamento de
coliso ser por endereamento aberto (linear
probing).
27
Hashing
Exerccios:
3 Seja uma funo h(k) = k % 11 e os dados abaixo
obtidos para uma seqncia de chaves:
key
82
31
28
45
27
59
79
35
h(key)
28