SlideShare a Scribd company logo
1 of 57
Download to read offline
Projeto e Análise de Algoritmos
www.nakamura.eti.br/eduardo                        Projeto e Análise de Algoritmos   2




         • O que é
             ▫ Uma tabela (arranjo) onde qualquer chave pode ser
               localizado através de acesso direto em tempo O(1)

         • Aplicações
             ▫ Dicionário: consulta a elementos em tempo constante
             ▫ Ex: Tabela de símbolos em compiladores
www.nakamura.eti.br/eduardo                         Projeto e Análise de Algoritmos   3




         • Em resumo
             ▫ Cada chave possível tem um endereço único
             ▫ O acesso à chave é direto, pelo endereço

         • Problema
             ▫ Universo de possíveis chaves/itens é muito grande
             ▫ O arranjo precisa ser muito grande
www.nakamura.eti.br/eduardo                            Projeto e Análise de Algoritmos   4




         • Universo de chaves
             ▫ Nomes com 10 caracteres
             ▫ 26 possíveis caracteres (alfabeto)

         • Tamanho do arranjo
             ▫ 2610 = 141.167.095.653.376 elementos!!!
             ▫ 140 TB??



                              Quais os problemas que
                                 você identifica?
www.nakamura.eti.br/eduardo                           Projeto e Análise de Algoritmos   5




         • Dados
             ▫ Cada elemento possui uma chave
             ▫ Não existem elementos com chaves repetidas
             ▫ O conjunto universo de chaves (chaves possíveis) é
               definido por U = {0, 1, 2, ..., m-1}, onde m não é muito
               grande

         • A tabela de endereçamento direto
             ▫ Arranjo dinâmico T [0...m-1]
             ▫ Cada posição corresponde a uma chave no universo U
www.nakamura.eti.br/eduardo                                   Projeto e Análise de Algoritmos          6




 U = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
 K = {2, 3, 5, 7}                                                                                dados
                                                                              chave
                                                                                                satélites
                                                               T
                                  U
                                                               /             0
                     (universo das chaves)
                                                               /             1
                                                          6
                    1                         4                              2            2
                              8                       9
                0                                                            3            3
                                          2                    /             4

                                                                             5            5
                K                             3
             (chaves                                           /             6
              reais)                  5                                      7            7
                                                  7
                                                               /             8

                                                               /             9
www.nakamura.eti.br/eduardo            Projeto e Análise de Algoritmos   7




         DIRECT-ADDRESS-SEARCH(T,k)
          1: return T[k];
                                      Qual o custo destas?



         DIRECT-ADDRESS-INSERT(T,x)
          1: T[x.chave]  x;




         DIRECT-ADDRESS-DELETE(T,x)
          1: T[x.chave]  NIL;
www.nakamura.eti.br/eduardo                        Projeto e Análise de Algoritmos   8




         • O que é
             ▫ Adaptação do endereçamento direto
             ▫ Reduzir espaço, impactando pouco no desempenho

         • Objetivo
             ▫ Mapear um espaço enorme de chaves em um espaço de
               inteiros relativamente pequeno

         • Como é feito?
             ▫ Através de uma função chamada função hash
             ▫ O inteiro gerado pela função hash é chamado valor hash e
               é usado para encontrar a localização do item
www.nakamura.eti.br/eduardo                                   Projeto e Análise de Algoritmos   9




         • Tabela de endereçamento direto
             ▫ Espaço necessário (|U|)
             ▫ Tempo de acesso é sempre O(1)
             ▫ Elemento de chave k se encontra na posição k

         • Tabela hashing
             ▫ Espaço necessário (|K|)
             ▫ Tempo médio de acesso O(1)
             ▫ Elemento de chave k se encontra na posição h(k)

                      h(k) é uma função hash que mapeia o universo U de chaves
                                 nas posições da tabela hash T[0..m-1]
www.nakamura.eti.br/eduardo                  Projeto e Análise de Algoritmos   10




        Qual problema do hashing
             você identifica?




                                             COLISÃO
                                     Eventualmente duas chaves
                                   podem obter o mesmo valor hash
                                       k1  k2  h(k1) = h(k2)
www.nakamura.eti.br/eduardo                             Projeto e Análise de Algoritmos   11




                                                                 T
                                         U
                                                                           0
                              (universo das chaves)
                                                                           ...
                                                                           h(k1)
                                         k1
                                                                           h(k3)
                                    k3                                     ...
                        K
                     (chaves                  k2                           h(k2), h(k5)
                      reais)
                                                                           ...
                                         k5
                                                                           h(k4)
                                                   k4
                                                                           ...
                                                                           m
www.nakamura.eti.br/eduardo                        Projeto e Análise de Algoritmos   12




         • Interpretando as chaves
             ▫ As funções hash normalmente assumem que o conjunto
               universo das chaves é o conjunto N = {0, 1, 2, 3, ...}
             ▫ Se as chaves não são números naturais, então precisamos
               encontrar uma forma de interpretá-las como números
               naturais

         • ASCII
             ▫ 128 caracteres (ASCII simples - sem acentuação)
             ▫ 256 caracteres (ASCII estendido - com acentuação)
www.nakamura.eti.br/eduardo   Projeto e Análise de Algoritmos   13
www.nakamura.eti.br/eduardo                              Projeto e Análise de Algoritmos   14




         • ASCII
             ▫ 128 caracteres (ASCII simples - sem acentuação)
             ▫ 256 caracteres (ASCII estendido - com acentuação)

         • Como mapear a chave cara usando o ASCII simples
           (128 caracteres)?
             Caractere        ASCII                    99 x 1283
                   c           99                +     97 x 1282
                   a           97                +    114 x 1281
                    r         114                +     97 x 1280
                   a           97     cara   =       209.221.985
www.nakamura.eti.br/eduardo                         Projeto e Análise de Algoritmos   15




         • Mapeia uma chave k para uma das m posições da
           tabela hash, tomando o resto de k dividido por m
                                 h(k) = k mod m

         • A escolha de m
             ▫ Se m é uma potência de 2, ou seja, m = 2p, então a função
               hash irá usar somente as posições representadas pelos p
               bits de mais baixa ordem
             ▫ Escolha para m um número primo não muito próximo de
               uma potência de 2
www.nakamura.eti.br/eduardo                         Projeto e Análise de Algoritmos   16




         • Suponha que
             ▫ O universo de chaves: números inteiros de quatro dígitos
             ▫ Deseja-se mapeá-los na tabela hash T [0...10]

         • Função hash (m = 11)

                                 h(k) = k mod 11
www.nakamura.eti.br/eduardo                          Projeto e Análise de Algoritmos   17




                                                                                 T
              k = 1826  h(1826) = 1826 mod 11 = 0
                                                                                  /    0

                                                                                  /    1

                                                                                  /    2

                                                                                  /    3

                                                                                  /    4

                                                                                  /    5

                                                                                  /    6

                                                                                  /    7

                                                                                  /    8

                                                                                  /    9

                                                                                  /    10
www.nakamura.eti.br/eduardo                          Projeto e Análise de Algoritmos   18




                                                                                 T
              k = 1826  h(1826) = 1826 mod 11 = 0
                                                                               1826    0

                                                                                  /    1

                                                                                  /    2

                                                                                  /    3

                                                                                  /    4

                                                                                  /    5

                                                                                  /    6

                                                                                  /    7

                                                                                  /    8

                                                                                  /    9

                                                                                  /    10
www.nakamura.eti.br/eduardo                          Projeto e Análise de Algoritmos   19




                                                                                 T
              k = 1826  h(1826) = 1826 mod 11 = 0
                                                                               1826    0

              k = 1981  h(1981) = 1981 mod 11 = 1                                /    1

                                                                                  /    2

                                                                                  /    3

                                                                                  /    4

                                                                                  /    5

                                                                                  /    6

                                                                                  /    7

                                                                                  /    8

                                                                                  /    9

                                                                                  /    10
www.nakamura.eti.br/eduardo                          Projeto e Análise de Algoritmos   20




                                                                                 T
              k = 1826  h(1826) = 1826 mod 11 = 0
                                                                               1826    0

              k = 1981  h(1981) = 1981 mod 11 = 1                             1981    1

                                                                                  /    2

                                                                                  /    3

                                                                                  /    4

                                                                                  /    5

                                                                                  /    6

                                                                                  /    7

                                                                                  /    8

                                                                                  /    9

                                                                                  /    10
www.nakamura.eti.br/eduardo                          Projeto e Análise de Algoritmos   21




                                                                                 T
              k = 1826  h(1826) = 1826 mod 11 = 0
                                                                               1826    0

              k = 1981  h(1981) = 1981 mod 11 = 1                             1981    1

                                                                                  /    2
              k = 1500  h(1500) = 1500 mod 11 = 4
                                                                                  /    3

                                                                                  /    4

                                                                                  /    5

                                                                                  /    6

                                                                                  /    7

                                                                                  /    8

                                                                                  /    9

                                                                                  /    10
www.nakamura.eti.br/eduardo                          Projeto e Análise de Algoritmos   22




                                                                                 T
              k = 1826  h(1826) = 1826 mod 11 = 0
                                                                               1826    0

              k = 1981  h(1981) = 1981 mod 11 = 1                             1981    1

                                                                                  /    2
              k = 1500  h(1500) = 1500 mod 11 = 4
                                                                                  /    3

                                                                               1500    4

                                                                                  /    5

                                                                                  /    6

                                                                                  /    7

                                                                                  /    8

                                                                                  /    9

                                                                                  /    10
www.nakamura.eti.br/eduardo                          Projeto e Análise de Algoritmos   23




                                                                                 T
              k = 1826  h(1826) = 1826 mod 11 = 0
                                                                               1826    0

              k = 1981  h(1981) = 1981 mod 11 = 1                             1981    1

                                                                                  /    2
              k = 1500  h(1500) = 1500 mod 11 = 4
                                                                                  /    3

                                                                               1500    4

                                                                                  /    5

                                                                                  /    6

                                                                                  /    7

                                                                                  /    8
              k = 1945  h(1945) = 1945 mod 11 = 9
                                                                                  /    9

                                                                                  /    10
www.nakamura.eti.br/eduardo                          Projeto e Análise de Algoritmos   24




                                                                                 T
              k = 1826  h(1826) = 1826 mod 11 = 0
                                                                               1826    0

              k = 1981  h(1981) = 1981 mod 11 = 1                             1981    1

                                                                                  /    2
              k = 1500  h(1500) = 1500 mod 11 = 4
                                                                                  /    3

                                                                               1500    4

                                                                                  /    5

                                                                                  /    6

                                                                                  /    7

                                                                                  /    8
              k = 1945  h(1945) = 1945 mod 11 = 9
                                                                               1945    9

                                                                                  /    10
www.nakamura.eti.br/eduardo                          Projeto e Análise de Algoritmos   25




                                                                                 T
              k = 1826  h(1826) = 1826 mod 11 = 0
                                                                               1826    0

              k = 1981  h(1981) = 1981 mod 11 = 1                             1981    1

                                                                                  /    2
              k = 1500  h(1500) = 1500 mod 11 = 4
                                                                                  /    3

                                                                               1500    4

                                                                                  /    5

                                                                                  /    6

                                                                                  /    7

                                                                                  /    8
              k = 1945  h(1945) = 1945 mod 11 = 9
                                                                               1945    9

              k = 2011  h(2011) = 2011 mod 11 = 9                                /    10
www.nakamura.eti.br/eduardo                            Projeto e Análise de Algoritmos   26




                                                                                   T
              k = 1826  h(1826) = 1826 mod 11 = 0
                                                                                 1826    0

              k = 1981  h(1981) = 1981 mod 11 = 1                               1981    1

                                                                                    /    2
              k = 1500  h(1500) = 1500 mod 11 = 4
                                                                                    /    3

                                                                                 1500    4

                                                                                    /    5

                                                                                    /    6
                                                     colisão
                                                                                    /    7

                                                                                    /    8
              k = 1945  h(1945) = 1945 mod 11 = 9
                                                                            1945, 2011   9

              k = 2011  h(2011) = 2011 mod 11 = 9                                  /    10
www.nakamura.eti.br/eduardo                      Projeto e Análise de Algoritmos   27




         • O desejável seria que a função fosse injetora
             ▫ Evitaria colisões
             ▫ Na prática isso é muito difícil

         • Esquemas para trabalhar colisões
             ▫ Endereçamento fechado
             ▫ Endereçamento aberto
www.nakamura.eti.br/eduardo                         Projeto e Análise de Algoritmos   28




         • Forma simples de tratamento de colisões
             ▫ Listas encadeadas
             ▫ Cada entrada T [i] da tabela hash é uma lista encadeada,
               cujos elementos possuem valor hash i

         • Inserção do elemento k
             ▫ Calcule o valor hash i da chave do elemento k
             ▫ Insira o elemento k na lista encadeada T [i]
www.nakamura.eti.br/eduardo                        Projeto e Análise de Algoritmos            29




                                                   T
                                   U
                                                   /
                    (universo das chaves)
                                                   /
                                                                k1                   k3   /
                                   k1
                              k3                                k2       /

             K
                                        k2
          (chaves                                               k5                   k4   /
           reais)                  k5              /
                                              k4
                                                                k7                   k6   /
                                         k6
                                              k7   /
                                                   /
www.nakamura.eti.br/eduardo                           Projeto e Análise de Algoritmos   30




         • Função hash bem escolhida promove um bom
           balanceamento
             ▫ Não há como garantir que as listas terão tamanhos
               próximos
             ▫ Listas podem ficar grandes
             ▫ Perda de eficiência nas listas grandes

         • Como melhorar?
             ▫ Substituir a lista ligada por árvores balanceadas?
             ▫ Na prática não se faz
www.nakamura.eti.br/eduardo                           Projeto e Análise de Algoritmos   31




         • Guarda todas as chaves na tabela
             ▫   Mesmo quando ocorre colisão
             ▫   T [i] contém uma chave, ao invés da lista encadeada
             ▫   Não usa espaço extra
             ▫   Em caso de colisão, um novo endereço é computado
             ▫   Esse processo é chamado rehashing
www.nakamura.eti.br/eduardo                           Projeto e Análise de Algoritmos   32




         • Forma mais simples de rehashing
             ▫ Se h(k) = z e a posição T [z] estiver ocupada
             ▫ Então a próxima posição disponível na tabela T será
               ocupada pela chave k

                                h'(k,i) = (h(k) + i) mod m

             ▫ para i = 0, 1, ..., m-1
www.nakamura.eti.br/eduardo                            Projeto e Análise de Algoritmos   33




         • Funcionamento
             ▫ Na primeira tentativa, aplicamos h'(k,0) , cujo resultado
               será o próprio h(k) original,
             ▫ Se T [h'(k,0)] estiver ocupada, procuramos T [h'(k,1)]
             ▫ Se T [h'(k,1)] estiver ocupada, procuramos T [h'(k,2)]
             ▫ Se T [h'(k,2)] estiver ocupada, procuramos T [h'(k,3)]
             ▫ ...

         • Note que
             ▫   T [h'(k,1)] = T [h'(k,0) + 1]
             ▫   T [h'(k,2)] = T [h'(k,1) + 1]
             ▫   T [h'(k,3)] = T [h'(k,2) + 1]
             ▫   ...
www.nakamura.eti.br/eduardo                         Projeto e Análise de Algoritmos   34




         • Suponha que
             ▫ O universo de chaves: números inteiros de quatro dígitos
             ▫ Deseja-se mapeá-los na tabela hash T [0...10]

         • Função hash (m = 11)

                                 h(k) = k mod 11
www.nakamura.eti.br/eduardo           Projeto e Análise de Algoritmos   35




         HASH-INSERT(T,k)           Qual a complexidade?
          1: i  0;
          2: do j  h’(k,i)
          3:   if T[j] = NIL then
          4:     T[j]  k;
          5:     return j;
          6:   else
          7:     i  i+1;
          8:   end if
          9: until i = m;
         10: error “overflow”;
www.nakamura.eti.br/eduardo             Projeto e Análise de Algoritmos   36




         HASH-SEARCH(T,k)             Qual a complexidade?
          1: i  0;
          2: do j  h’(k,i)
          3:   if T[j] = k then
          4:     return j;
          5:   end if
          6:   i  i+1;
          8:   end if
          9: until T[j]=NIL or i=m;
         10: return NIL;
www.nakamura.eti.br/eduardo                         Projeto e Análise de Algoritmos   37




                                                                                T
              k = 1981 h(1981) = 1981 mod 11 = 1                                /    0

                                                                                 /    1

                                                                                 /    2

                                                                                 /    3

                                                                                 /    4

                                                                                 /    5

                                                                                 /    6

                                                                                 /    7

                                                                                 /    8

                                                                                 /    9

                                                                                 /    10
www.nakamura.eti.br/eduardo                         Projeto e Análise de Algoritmos   38




                                                                                T
              k = 1981 h(1981) = 1981 mod 11 = 1                                /    0

                                                                              1981    1

                                                                                 /    2

                                                                                 /    3

                                                                                 /    4

                                                                                 /    5

                                                                                 /    6

                                                                                 /    7

                                                                                 /    8

                                                                                 /    9

                                                                                 /    10
www.nakamura.eti.br/eduardo                         Projeto e Análise de Algoritmos   39




                                                                                T
              k = 1981 h(1981) = 1981 mod 11 = 1                                /    0

                                                                              1981    1

              k = 1500 h(1500) = 1500 mod 11 = 4                                /    2

                                                                                 /    3

                                                                                 /    4

                                                                                 /    5

                                                                                 /    6

                                                                                 /    7

                                                                                 /    8

                                                                                 /    9

                                                                                 /    10
www.nakamura.eti.br/eduardo                         Projeto e Análise de Algoritmos   40




                                                                                T
              k = 1981 h(1981) = 1981 mod 11 = 1                                /    0

                                                                              1981    1

              k = 1500 h(1500) = 1500 mod 11 = 4                                /    2

                                                                                 /    3

                                                                              1500    4

                                                                                 /    5

                                                                                 /    6

                                                                                 /    7

                                                                                 /    8

                                                                                 /    9

                                                                                 /    10
www.nakamura.eti.br/eduardo                         Projeto e Análise de Algoritmos   41




                                                                                T
              k = 1981 h(1981) = 1981 mod 11 = 1                                /    0

                                                                              1981    1

              k = 1500 h(1500) = 1500 mod 11 = 4                                /    2

                                                                                 /    3

              k = 1962 h(1962) = 1962 mod 11 = 4                             1500    4

                                                                                 /    5

                                                                                 /    6

                                                                                 /    7

                                                                                 /    8

                                                                                 /    9

                                                                                 /    10
www.nakamura.eti.br/eduardo                         Projeto e Análise de Algoritmos   42




                                                                                T
              k = 1981 h(1981) = 1981 mod 11 = 1                                /    0

                                                                              1981    1

              k = 1500 h(1500) = 1500 mod 11 = 4                                /    2

                                                                                 /    3

              k = 1962 h(1962) = 1962 mod 11 = 4                             1500    4

                                                                              1962    5

                                                                                 /    6

                                                                                 /    7

                                                                                 /    8

                                                                                 /    9

                                                                                 /    10
www.nakamura.eti.br/eduardo                         Projeto e Análise de Algoritmos   43




                                                                                T
              k = 1981 h(1981) = 1981 mod 11 = 1                                /    0

                                                                              1981    1

              k = 1500 h(1500) = 1500 mod 11 = 4                                /    2

                                                                                 /    3

              k = 1962 h(1962) = 1962 mod 11 = 4                             1500    4

                                                                              1962    5

              k = 1809 h(1809) = 1809 mod 11 = 5                                /    6

                                                                                 /    7

                                                                                 /    8

                                                                                 /    9

                                                                                 /    10
www.nakamura.eti.br/eduardo                         Projeto e Análise de Algoritmos   44




                                                                                T
              k = 1981 h(1981) = 1981 mod 11 = 1                                /    0

                                                                              1981    1

              k = 1500 h(1500) = 1500 mod 11 = 4                                /    2

                                                                                 /    3

              k = 1962 h(1962) = 1962 mod 11 = 4                             1500    4

                                                                              1962    5

              k = 1809 h(1809) = 1809 mod 11 = 5                                /    6

                                                                                 /    7

                                                                                 /    8

                                                                                 /    9

                                                                                 /    10
www.nakamura.eti.br/eduardo                         Projeto e Análise de Algoritmos   45




                                                                                T
              k = 1981 h(1981) = 1981 mod 11 = 1                                /    0

                                                                              1981    1

              k = 1500 h(1500) = 1500 mod 11 = 4                                /    2

                                                                                 /    3

              k = 1962 h(1962) = 1962 mod 11 = 4                             1500    4

                                                                              1962    5

              k = 1809 h(1809) = 1809 mod 11 = 5                             1809    6

                                                                                 /    7

                                                                                 /    8

                                                                                 /    9

                                                                                 /    10
www.nakamura.eti.br/eduardo                         Projeto e Análise de Algoritmos   46




                                                                                T
              k = 1981 h(1981) = 1981 mod 11 = 1                                /    0

                                                                              1981    1

              k = 1500 h(1500) = 1500 mod 11 = 4                                /    2

                                                                                 /    3

              k = 1962 h(1962) = 1962 mod 11 = 4                             1500    4

                                                                              1962    5

              k = 1809 h(1809) = 1809 mod 11 = 5                             1809    6

                                                                                 /    7

              k = 1941 h(1941) = 1941 mod 11 = 5                                /    8

                                                                                 /    9

                                                                                 /    10
www.nakamura.eti.br/eduardo                         Projeto e Análise de Algoritmos   47




                                                                                T
              k = 1981 h(1981) = 1981 mod 11 = 1                                /    0

                                                                              1981    1

              k = 1500 h(1500) = 1500 mod 11 = 4                                /    2

                                                                                 /    3

              k = 1962 h(1962) = 1962 mod 11 = 4                             1500    4

                                                                              1962    5

              k = 1809 h(1809) = 1809 mod 11 = 5                             1809    6

                                                                                 /    7

              k = 1941 h(1941) = 1941 mod 11 = 5                                /    8

                                                                                 /    9

                                                                                 /    10
www.nakamura.eti.br/eduardo                         Projeto e Análise de Algoritmos   48




                                                                                T
              k = 1981 h(1981) = 1981 mod 11 = 1                                /    0

                                                                              1981    1

              k = 1500 h(1500) = 1500 mod 11 = 4                                /    2

                                                                                 /    3

              k = 1962 h(1962) = 1962 mod 11 = 4                             1500    4

                                                                              1962    5

              k = 1809 h(1809) = 1809 mod 11 = 5                             1809    6

                                                                              1941    7

              k = 1941 h(1941) = 1941 mod 11 = 5                                /    8

                                                                                 /    9

                                                                                 /    10
www.nakamura.eti.br/eduardo                         Projeto e Análise de Algoritmos   49




                                                                                T
              k = 1981 h(1981) = 1981 mod 11 = 1                                /    0

                                                                              1981    1

              k = 1500 h(1500) = 1500 mod 11 = 4                                /    2

                                                                                 /    3

              k = 1962 h(1962) = 1962 mod 11 = 4                             1500    4

                                                                              1962    5

              k = 1809 h(1809) = 1809 mod 11 = 5                             1809    6

                                                                              1941    7

              k = 1941 h(1941) = 1941 mod 11 = 5                                /    8

                                                                                 /    9

                                                                                 /    10
              k = 2007 h(2007) = 2007 mod 11 = 5
www.nakamura.eti.br/eduardo                         Projeto e Análise de Algoritmos   50




                                                                                T
              k = 1981 h(1981) = 1981 mod 11 = 1                                /    0

                                                                              1981    1

              k = 1500 h(1500) = 1500 mod 11 = 4                                /    2

                                                                                 /    3

              k = 1962 h(1962) = 1962 mod 11 = 4                             1500    4

                                                                              1962    5

              k = 1809 h(1809) = 1809 mod 11 = 5                             1809    6

                                                                              1941    7

              k = 1941 h(1941) = 1941 mod 11 = 5                                /    8

                                                                                 /    9

                                                                                 /    10
              k = 2007 h(2007) = 2007 mod 11 = 5
www.nakamura.eti.br/eduardo                         Projeto e Análise de Algoritmos   51




                                                                                T
              k = 1981 h(1981) = 1981 mod 11 = 1                                /    0

                                                                              1981    1

              k = 1500 h(1500) = 1500 mod 11 = 4                                /    2

                                                                                 /    3

              k = 1962 h(1962) = 1962 mod 11 = 4                             1500    4

                                                                              1962    5

              k = 1809 h(1809) = 1809 mod 11 = 5                             1809    6

                                                                              1941    7

              k = 1941 h(1941) = 1941 mod 11 = 5                                /    8

                                                                                 /    9

                                                                                 /    10
              k = 2007 h(2007) = 2007 mod 11 = 5
www.nakamura.eti.br/eduardo                         Projeto e Análise de Algoritmos   52




                                                                                T
              k = 1981 h(1981) = 1981 mod 11 = 1                                /    0

                                                                              1981    1

              k = 1500 h(1500) = 1500 mod 11 = 4                                /    2

                                                                                 /    3

              k = 1962 h(1962) = 1962 mod 11 = 4                             1500    4

                                                                              1962    5

              k = 1809 h(1809) = 1809 mod 11 = 5                             1809    6

                                                                              1941    7

              k = 1941 h(1941) = 1941 mod 11 = 5                             2007    8

                                                                                 /    9

                                                                                 /    10
              k = 2007 h(2007) = 2007 mod 11 = 5
www.nakamura.eti.br/eduardo                        Projeto e Análise de Algoritmos   53




         • Problemas de colisão
             ▫ Quando a tabela está com alta taxa de ocupação, as
               colisões são mais freqüentes
             ▫ Colisões afetam a busca

         • Estratégia para desempenho
             ▫ Manter uma taxa de ocupação abaixo de 50%
www.nakamura.eti.br/eduardo                               Projeto e Análise de Algoritmos   54




         • Sondagem quadrática
                              h'(k,i) = (h(k) + c1 i + c2 i2) mod m

             ▫ para i = 0, 1, ..., m-1; c10 e c20
www.nakamura.eti.br/eduardo                             Projeto e Análise de Algoritmos   55




         • Hash duplo (duas funções hash)
                              h'(k,i) = (h1(k) + h2(k) i) mod m

             ▫ para i = 0, 1, ..., m-1;

         • Uma possibilidade
             ▫ h1(k) = k mod m
             ▫ h2(k) = 1+ ( k mod (m-1) )
www.nakamura.eti.br/eduardo                 Projeto e Análise de Algoritmos   56




         HASH-INSERT(T,k)           HASH-SEARCH(T,k)
          1: i  0;                  1: i  0;
          2: do j  h’(k,i)          2: do j  h’(k,i)
          3:   if T[j] = NIL then    3:   if T[j] = k then
          4:     T[j]  k;           4:     return j;
          5:     return j;           5:   end if
          6:   else                  6:   i  i+1;
          7:     i  i+1;            8:   end if
          8:   end if                9: until T[j]=NIL or i=m;
          9: until i = m;           10: return NIL;
         10: error “overflow”;
www.nakamura.eti.br/eduardo                      Projeto e Análise de Algoritmos   57




         • Considere um conjunto dinâmico S, representado por uma
           tabela de endereço direto T de comprimento m. Descreva um
           algoritmo para encontrar o maior elemento de S. Qual o
           desempenho no pior caso?

         • Demonstre a inserção das chaves 5, 28, 19, 15, 20, 33, 12,
           17, 10 em uma tabela hash com colisões resolvidas por
           encadeamento. Seja a tabela com 9 posições, e seja a
           função hash h(k)= k mod 9.

         • Considere a inserção das chaves 10, 22, 31, 4, 15, 28, 17,
           88, 59 em uma tabela hash de comprimento m = 11 usando o
           endereçamento aberto com a função hash primário h(k)= k
           mod m. Ilustre o resultado da inserção dessas chaves com o
           uso da sondagem linear, sondagem quadrática (usando c1 =
           1 e c2 = 3) e hash duplo com as funções sugeridas na aula.

More Related Content

Featured

Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTExpeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Applitools
 

Featured (20)

Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 

Aula12

  • 1. Projeto e Análise de Algoritmos
  • 2. www.nakamura.eti.br/eduardo Projeto e Análise de Algoritmos 2 • O que é ▫ Uma tabela (arranjo) onde qualquer chave pode ser localizado através de acesso direto em tempo O(1) • Aplicações ▫ Dicionário: consulta a elementos em tempo constante ▫ Ex: Tabela de símbolos em compiladores
  • 3. www.nakamura.eti.br/eduardo Projeto e Análise de Algoritmos 3 • Em resumo ▫ Cada chave possível tem um endereço único ▫ O acesso à chave é direto, pelo endereço • Problema ▫ Universo de possíveis chaves/itens é muito grande ▫ O arranjo precisa ser muito grande
  • 4. www.nakamura.eti.br/eduardo Projeto e Análise de Algoritmos 4 • Universo de chaves ▫ Nomes com 10 caracteres ▫ 26 possíveis caracteres (alfabeto) • Tamanho do arranjo ▫ 2610 = 141.167.095.653.376 elementos!!! ▫ 140 TB?? Quais os problemas que você identifica?
  • 5. www.nakamura.eti.br/eduardo Projeto e Análise de Algoritmos 5 • Dados ▫ Cada elemento possui uma chave ▫ Não existem elementos com chaves repetidas ▫ O conjunto universo de chaves (chaves possíveis) é definido por U = {0, 1, 2, ..., m-1}, onde m não é muito grande • A tabela de endereçamento direto ▫ Arranjo dinâmico T [0...m-1] ▫ Cada posição corresponde a uma chave no universo U
  • 6. www.nakamura.eti.br/eduardo Projeto e Análise de Algoritmos 6 U = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9} K = {2, 3, 5, 7} dados chave satélites T U / 0 (universo das chaves) / 1 6 1 4 2 2 8 9 0 3 3 2 / 4 5 5 K 3 (chaves / 6 reais) 5 7 7 7 / 8 / 9
  • 7. www.nakamura.eti.br/eduardo Projeto e Análise de Algoritmos 7 DIRECT-ADDRESS-SEARCH(T,k) 1: return T[k]; Qual o custo destas? DIRECT-ADDRESS-INSERT(T,x) 1: T[x.chave]  x; DIRECT-ADDRESS-DELETE(T,x) 1: T[x.chave]  NIL;
  • 8. www.nakamura.eti.br/eduardo Projeto e Análise de Algoritmos 8 • O que é ▫ Adaptação do endereçamento direto ▫ Reduzir espaço, impactando pouco no desempenho • Objetivo ▫ Mapear um espaço enorme de chaves em um espaço de inteiros relativamente pequeno • Como é feito? ▫ Através de uma função chamada função hash ▫ O inteiro gerado pela função hash é chamado valor hash e é usado para encontrar a localização do item
  • 9. www.nakamura.eti.br/eduardo Projeto e Análise de Algoritmos 9 • Tabela de endereçamento direto ▫ Espaço necessário (|U|) ▫ Tempo de acesso é sempre O(1) ▫ Elemento de chave k se encontra na posição k • Tabela hashing ▫ Espaço necessário (|K|) ▫ Tempo médio de acesso O(1) ▫ Elemento de chave k se encontra na posição h(k) h(k) é uma função hash que mapeia o universo U de chaves nas posições da tabela hash T[0..m-1]
  • 10. www.nakamura.eti.br/eduardo Projeto e Análise de Algoritmos 10 Qual problema do hashing você identifica? COLISÃO Eventualmente duas chaves podem obter o mesmo valor hash k1  k2  h(k1) = h(k2)
  • 11. www.nakamura.eti.br/eduardo Projeto e Análise de Algoritmos 11 T U 0 (universo das chaves) ... h(k1) k1 h(k3) k3 ... K (chaves k2 h(k2), h(k5) reais) ... k5 h(k4) k4 ... m
  • 12. www.nakamura.eti.br/eduardo Projeto e Análise de Algoritmos 12 • Interpretando as chaves ▫ As funções hash normalmente assumem que o conjunto universo das chaves é o conjunto N = {0, 1, 2, 3, ...} ▫ Se as chaves não são números naturais, então precisamos encontrar uma forma de interpretá-las como números naturais • ASCII ▫ 128 caracteres (ASCII simples - sem acentuação) ▫ 256 caracteres (ASCII estendido - com acentuação)
  • 13. www.nakamura.eti.br/eduardo Projeto e Análise de Algoritmos 13
  • 14. www.nakamura.eti.br/eduardo Projeto e Análise de Algoritmos 14 • ASCII ▫ 128 caracteres (ASCII simples - sem acentuação) ▫ 256 caracteres (ASCII estendido - com acentuação) • Como mapear a chave cara usando o ASCII simples (128 caracteres)? Caractere ASCII 99 x 1283 c 99 + 97 x 1282 a 97 + 114 x 1281 r 114 + 97 x 1280 a 97 cara = 209.221.985
  • 15. www.nakamura.eti.br/eduardo Projeto e Análise de Algoritmos 15 • Mapeia uma chave k para uma das m posições da tabela hash, tomando o resto de k dividido por m h(k) = k mod m • A escolha de m ▫ Se m é uma potência de 2, ou seja, m = 2p, então a função hash irá usar somente as posições representadas pelos p bits de mais baixa ordem ▫ Escolha para m um número primo não muito próximo de uma potência de 2
  • 16. www.nakamura.eti.br/eduardo Projeto e Análise de Algoritmos 16 • Suponha que ▫ O universo de chaves: números inteiros de quatro dígitos ▫ Deseja-se mapeá-los na tabela hash T [0...10] • Função hash (m = 11) h(k) = k mod 11
  • 17. www.nakamura.eti.br/eduardo Projeto e Análise de Algoritmos 17 T k = 1826  h(1826) = 1826 mod 11 = 0 / 0 / 1 / 2 / 3 / 4 / 5 / 6 / 7 / 8 / 9 / 10
  • 18. www.nakamura.eti.br/eduardo Projeto e Análise de Algoritmos 18 T k = 1826  h(1826) = 1826 mod 11 = 0 1826 0 / 1 / 2 / 3 / 4 / 5 / 6 / 7 / 8 / 9 / 10
  • 19. www.nakamura.eti.br/eduardo Projeto e Análise de Algoritmos 19 T k = 1826  h(1826) = 1826 mod 11 = 0 1826 0 k = 1981  h(1981) = 1981 mod 11 = 1 / 1 / 2 / 3 / 4 / 5 / 6 / 7 / 8 / 9 / 10
  • 20. www.nakamura.eti.br/eduardo Projeto e Análise de Algoritmos 20 T k = 1826  h(1826) = 1826 mod 11 = 0 1826 0 k = 1981  h(1981) = 1981 mod 11 = 1 1981 1 / 2 / 3 / 4 / 5 / 6 / 7 / 8 / 9 / 10
  • 21. www.nakamura.eti.br/eduardo Projeto e Análise de Algoritmos 21 T k = 1826  h(1826) = 1826 mod 11 = 0 1826 0 k = 1981  h(1981) = 1981 mod 11 = 1 1981 1 / 2 k = 1500  h(1500) = 1500 mod 11 = 4 / 3 / 4 / 5 / 6 / 7 / 8 / 9 / 10
  • 22. www.nakamura.eti.br/eduardo Projeto e Análise de Algoritmos 22 T k = 1826  h(1826) = 1826 mod 11 = 0 1826 0 k = 1981  h(1981) = 1981 mod 11 = 1 1981 1 / 2 k = 1500  h(1500) = 1500 mod 11 = 4 / 3 1500 4 / 5 / 6 / 7 / 8 / 9 / 10
  • 23. www.nakamura.eti.br/eduardo Projeto e Análise de Algoritmos 23 T k = 1826  h(1826) = 1826 mod 11 = 0 1826 0 k = 1981  h(1981) = 1981 mod 11 = 1 1981 1 / 2 k = 1500  h(1500) = 1500 mod 11 = 4 / 3 1500 4 / 5 / 6 / 7 / 8 k = 1945  h(1945) = 1945 mod 11 = 9 / 9 / 10
  • 24. www.nakamura.eti.br/eduardo Projeto e Análise de Algoritmos 24 T k = 1826  h(1826) = 1826 mod 11 = 0 1826 0 k = 1981  h(1981) = 1981 mod 11 = 1 1981 1 / 2 k = 1500  h(1500) = 1500 mod 11 = 4 / 3 1500 4 / 5 / 6 / 7 / 8 k = 1945  h(1945) = 1945 mod 11 = 9 1945 9 / 10
  • 25. www.nakamura.eti.br/eduardo Projeto e Análise de Algoritmos 25 T k = 1826  h(1826) = 1826 mod 11 = 0 1826 0 k = 1981  h(1981) = 1981 mod 11 = 1 1981 1 / 2 k = 1500  h(1500) = 1500 mod 11 = 4 / 3 1500 4 / 5 / 6 / 7 / 8 k = 1945  h(1945) = 1945 mod 11 = 9 1945 9 k = 2011  h(2011) = 2011 mod 11 = 9 / 10
  • 26. www.nakamura.eti.br/eduardo Projeto e Análise de Algoritmos 26 T k = 1826  h(1826) = 1826 mod 11 = 0 1826 0 k = 1981  h(1981) = 1981 mod 11 = 1 1981 1 / 2 k = 1500  h(1500) = 1500 mod 11 = 4 / 3 1500 4 / 5 / 6 colisão / 7 / 8 k = 1945  h(1945) = 1945 mod 11 = 9 1945, 2011 9 k = 2011  h(2011) = 2011 mod 11 = 9 / 10
  • 27. www.nakamura.eti.br/eduardo Projeto e Análise de Algoritmos 27 • O desejável seria que a função fosse injetora ▫ Evitaria colisões ▫ Na prática isso é muito difícil • Esquemas para trabalhar colisões ▫ Endereçamento fechado ▫ Endereçamento aberto
  • 28. www.nakamura.eti.br/eduardo Projeto e Análise de Algoritmos 28 • Forma simples de tratamento de colisões ▫ Listas encadeadas ▫ Cada entrada T [i] da tabela hash é uma lista encadeada, cujos elementos possuem valor hash i • Inserção do elemento k ▫ Calcule o valor hash i da chave do elemento k ▫ Insira o elemento k na lista encadeada T [i]
  • 29. www.nakamura.eti.br/eduardo Projeto e Análise de Algoritmos 29 T U / (universo das chaves) / k1 k3 / k1 k3 k2 / K k2 (chaves k5 k4 / reais) k5 / k4 k7 k6 / k6 k7 / /
  • 30. www.nakamura.eti.br/eduardo Projeto e Análise de Algoritmos 30 • Função hash bem escolhida promove um bom balanceamento ▫ Não há como garantir que as listas terão tamanhos próximos ▫ Listas podem ficar grandes ▫ Perda de eficiência nas listas grandes • Como melhorar? ▫ Substituir a lista ligada por árvores balanceadas? ▫ Na prática não se faz
  • 31. www.nakamura.eti.br/eduardo Projeto e Análise de Algoritmos 31 • Guarda todas as chaves na tabela ▫ Mesmo quando ocorre colisão ▫ T [i] contém uma chave, ao invés da lista encadeada ▫ Não usa espaço extra ▫ Em caso de colisão, um novo endereço é computado ▫ Esse processo é chamado rehashing
  • 32. www.nakamura.eti.br/eduardo Projeto e Análise de Algoritmos 32 • Forma mais simples de rehashing ▫ Se h(k) = z e a posição T [z] estiver ocupada ▫ Então a próxima posição disponível na tabela T será ocupada pela chave k h'(k,i) = (h(k) + i) mod m ▫ para i = 0, 1, ..., m-1
  • 33. www.nakamura.eti.br/eduardo Projeto e Análise de Algoritmos 33 • Funcionamento ▫ Na primeira tentativa, aplicamos h'(k,0) , cujo resultado será o próprio h(k) original, ▫ Se T [h'(k,0)] estiver ocupada, procuramos T [h'(k,1)] ▫ Se T [h'(k,1)] estiver ocupada, procuramos T [h'(k,2)] ▫ Se T [h'(k,2)] estiver ocupada, procuramos T [h'(k,3)] ▫ ... • Note que ▫ T [h'(k,1)] = T [h'(k,0) + 1] ▫ T [h'(k,2)] = T [h'(k,1) + 1] ▫ T [h'(k,3)] = T [h'(k,2) + 1] ▫ ...
  • 34. www.nakamura.eti.br/eduardo Projeto e Análise de Algoritmos 34 • Suponha que ▫ O universo de chaves: números inteiros de quatro dígitos ▫ Deseja-se mapeá-los na tabela hash T [0...10] • Função hash (m = 11) h(k) = k mod 11
  • 35. www.nakamura.eti.br/eduardo Projeto e Análise de Algoritmos 35 HASH-INSERT(T,k) Qual a complexidade? 1: i  0; 2: do j  h’(k,i) 3: if T[j] = NIL then 4: T[j]  k; 5: return j; 6: else 7: i  i+1; 8: end if 9: until i = m; 10: error “overflow”;
  • 36. www.nakamura.eti.br/eduardo Projeto e Análise de Algoritmos 36 HASH-SEARCH(T,k) Qual a complexidade? 1: i  0; 2: do j  h’(k,i) 3: if T[j] = k then 4: return j; 5: end if 6: i  i+1; 8: end if 9: until T[j]=NIL or i=m; 10: return NIL;
  • 37. www.nakamura.eti.br/eduardo Projeto e Análise de Algoritmos 37 T k = 1981 h(1981) = 1981 mod 11 = 1 / 0 / 1 / 2 / 3 / 4 / 5 / 6 / 7 / 8 / 9 / 10
  • 38. www.nakamura.eti.br/eduardo Projeto e Análise de Algoritmos 38 T k = 1981 h(1981) = 1981 mod 11 = 1 / 0 1981 1 / 2 / 3 / 4 / 5 / 6 / 7 / 8 / 9 / 10
  • 39. www.nakamura.eti.br/eduardo Projeto e Análise de Algoritmos 39 T k = 1981 h(1981) = 1981 mod 11 = 1 / 0 1981 1 k = 1500 h(1500) = 1500 mod 11 = 4 / 2 / 3 / 4 / 5 / 6 / 7 / 8 / 9 / 10
  • 40. www.nakamura.eti.br/eduardo Projeto e Análise de Algoritmos 40 T k = 1981 h(1981) = 1981 mod 11 = 1 / 0 1981 1 k = 1500 h(1500) = 1500 mod 11 = 4 / 2 / 3 1500 4 / 5 / 6 / 7 / 8 / 9 / 10
  • 41. www.nakamura.eti.br/eduardo Projeto e Análise de Algoritmos 41 T k = 1981 h(1981) = 1981 mod 11 = 1 / 0 1981 1 k = 1500 h(1500) = 1500 mod 11 = 4 / 2 / 3 k = 1962 h(1962) = 1962 mod 11 = 4 1500 4 / 5 / 6 / 7 / 8 / 9 / 10
  • 42. www.nakamura.eti.br/eduardo Projeto e Análise de Algoritmos 42 T k = 1981 h(1981) = 1981 mod 11 = 1 / 0 1981 1 k = 1500 h(1500) = 1500 mod 11 = 4 / 2 / 3 k = 1962 h(1962) = 1962 mod 11 = 4 1500 4 1962 5 / 6 / 7 / 8 / 9 / 10
  • 43. www.nakamura.eti.br/eduardo Projeto e Análise de Algoritmos 43 T k = 1981 h(1981) = 1981 mod 11 = 1 / 0 1981 1 k = 1500 h(1500) = 1500 mod 11 = 4 / 2 / 3 k = 1962 h(1962) = 1962 mod 11 = 4 1500 4 1962 5 k = 1809 h(1809) = 1809 mod 11 = 5 / 6 / 7 / 8 / 9 / 10
  • 44. www.nakamura.eti.br/eduardo Projeto e Análise de Algoritmos 44 T k = 1981 h(1981) = 1981 mod 11 = 1 / 0 1981 1 k = 1500 h(1500) = 1500 mod 11 = 4 / 2 / 3 k = 1962 h(1962) = 1962 mod 11 = 4 1500 4 1962 5 k = 1809 h(1809) = 1809 mod 11 = 5 / 6 / 7 / 8 / 9 / 10
  • 45. www.nakamura.eti.br/eduardo Projeto e Análise de Algoritmos 45 T k = 1981 h(1981) = 1981 mod 11 = 1 / 0 1981 1 k = 1500 h(1500) = 1500 mod 11 = 4 / 2 / 3 k = 1962 h(1962) = 1962 mod 11 = 4 1500 4 1962 5 k = 1809 h(1809) = 1809 mod 11 = 5 1809 6 / 7 / 8 / 9 / 10
  • 46. www.nakamura.eti.br/eduardo Projeto e Análise de Algoritmos 46 T k = 1981 h(1981) = 1981 mod 11 = 1 / 0 1981 1 k = 1500 h(1500) = 1500 mod 11 = 4 / 2 / 3 k = 1962 h(1962) = 1962 mod 11 = 4 1500 4 1962 5 k = 1809 h(1809) = 1809 mod 11 = 5 1809 6 / 7 k = 1941 h(1941) = 1941 mod 11 = 5 / 8 / 9 / 10
  • 47. www.nakamura.eti.br/eduardo Projeto e Análise de Algoritmos 47 T k = 1981 h(1981) = 1981 mod 11 = 1 / 0 1981 1 k = 1500 h(1500) = 1500 mod 11 = 4 / 2 / 3 k = 1962 h(1962) = 1962 mod 11 = 4 1500 4 1962 5 k = 1809 h(1809) = 1809 mod 11 = 5 1809 6 / 7 k = 1941 h(1941) = 1941 mod 11 = 5 / 8 / 9 / 10
  • 48. www.nakamura.eti.br/eduardo Projeto e Análise de Algoritmos 48 T k = 1981 h(1981) = 1981 mod 11 = 1 / 0 1981 1 k = 1500 h(1500) = 1500 mod 11 = 4 / 2 / 3 k = 1962 h(1962) = 1962 mod 11 = 4 1500 4 1962 5 k = 1809 h(1809) = 1809 mod 11 = 5 1809 6 1941 7 k = 1941 h(1941) = 1941 mod 11 = 5 / 8 / 9 / 10
  • 49. www.nakamura.eti.br/eduardo Projeto e Análise de Algoritmos 49 T k = 1981 h(1981) = 1981 mod 11 = 1 / 0 1981 1 k = 1500 h(1500) = 1500 mod 11 = 4 / 2 / 3 k = 1962 h(1962) = 1962 mod 11 = 4 1500 4 1962 5 k = 1809 h(1809) = 1809 mod 11 = 5 1809 6 1941 7 k = 1941 h(1941) = 1941 mod 11 = 5 / 8 / 9 / 10 k = 2007 h(2007) = 2007 mod 11 = 5
  • 50. www.nakamura.eti.br/eduardo Projeto e Análise de Algoritmos 50 T k = 1981 h(1981) = 1981 mod 11 = 1 / 0 1981 1 k = 1500 h(1500) = 1500 mod 11 = 4 / 2 / 3 k = 1962 h(1962) = 1962 mod 11 = 4 1500 4 1962 5 k = 1809 h(1809) = 1809 mod 11 = 5 1809 6 1941 7 k = 1941 h(1941) = 1941 mod 11 = 5 / 8 / 9 / 10 k = 2007 h(2007) = 2007 mod 11 = 5
  • 51. www.nakamura.eti.br/eduardo Projeto e Análise de Algoritmos 51 T k = 1981 h(1981) = 1981 mod 11 = 1 / 0 1981 1 k = 1500 h(1500) = 1500 mod 11 = 4 / 2 / 3 k = 1962 h(1962) = 1962 mod 11 = 4 1500 4 1962 5 k = 1809 h(1809) = 1809 mod 11 = 5 1809 6 1941 7 k = 1941 h(1941) = 1941 mod 11 = 5 / 8 / 9 / 10 k = 2007 h(2007) = 2007 mod 11 = 5
  • 52. www.nakamura.eti.br/eduardo Projeto e Análise de Algoritmos 52 T k = 1981 h(1981) = 1981 mod 11 = 1 / 0 1981 1 k = 1500 h(1500) = 1500 mod 11 = 4 / 2 / 3 k = 1962 h(1962) = 1962 mod 11 = 4 1500 4 1962 5 k = 1809 h(1809) = 1809 mod 11 = 5 1809 6 1941 7 k = 1941 h(1941) = 1941 mod 11 = 5 2007 8 / 9 / 10 k = 2007 h(2007) = 2007 mod 11 = 5
  • 53. www.nakamura.eti.br/eduardo Projeto e Análise de Algoritmos 53 • Problemas de colisão ▫ Quando a tabela está com alta taxa de ocupação, as colisões são mais freqüentes ▫ Colisões afetam a busca • Estratégia para desempenho ▫ Manter uma taxa de ocupação abaixo de 50%
  • 54. www.nakamura.eti.br/eduardo Projeto e Análise de Algoritmos 54 • Sondagem quadrática h'(k,i) = (h(k) + c1 i + c2 i2) mod m ▫ para i = 0, 1, ..., m-1; c10 e c20
  • 55. www.nakamura.eti.br/eduardo Projeto e Análise de Algoritmos 55 • Hash duplo (duas funções hash) h'(k,i) = (h1(k) + h2(k) i) mod m ▫ para i = 0, 1, ..., m-1; • Uma possibilidade ▫ h1(k) = k mod m ▫ h2(k) = 1+ ( k mod (m-1) )
  • 56. www.nakamura.eti.br/eduardo Projeto e Análise de Algoritmos 56 HASH-INSERT(T,k) HASH-SEARCH(T,k) 1: i  0; 1: i  0; 2: do j  h’(k,i) 2: do j  h’(k,i) 3: if T[j] = NIL then 3: if T[j] = k then 4: T[j]  k; 4: return j; 5: return j; 5: end if 6: else 6: i  i+1; 7: i  i+1; 8: end if 8: end if 9: until T[j]=NIL or i=m; 9: until i = m; 10: return NIL; 10: error “overflow”;
  • 57. www.nakamura.eti.br/eduardo Projeto e Análise de Algoritmos 57 • Considere um conjunto dinâmico S, representado por uma tabela de endereço direto T de comprimento m. Descreva um algoritmo para encontrar o maior elemento de S. Qual o desempenho no pior caso? • Demonstre a inserção das chaves 5, 28, 19, 15, 20, 33, 12, 17, 10 em uma tabela hash com colisões resolvidas por encadeamento. Seja a tabela com 9 posições, e seja a função hash h(k)= k mod 9. • Considere a inserção das chaves 10, 22, 31, 4, 15, 28, 17, 88, 59 em uma tabela hash de comprimento m = 11 usando o endereçamento aberto com a função hash primário h(k)= k mod m. Ilustre o resultado da inserção dessas chaves com o uso da sondagem linear, sondagem quadrática (usando c1 = 1 e c2 = 3) e hash duplo com as funções sugeridas na aula.