hachage_4
TRANSCRIPT
-
8/12/2019 hachage_4
1/21
126
UMLV
1. Introduction
2. Hachage ouvert
3. Hachage ferm
4. Implmentation des fonctions
Mthodes de hachage
-
8/12/2019 hachage_4
2/21
127
UMLV Recherche dichotomique
3 4 8 9 10 20 40 50 70 75 80 83 85 901 2 3 4 5 6 7 8 9 10 11 12 13 14
d i f 4 ? d i fd i fdi f
df
table
fonction ELEMENT ( x, table, d, f ) ;dbut
si (d = f ) alors si ( x = table [d ]) alors retour (vrai)sinon retour (faux)
sinon { i (d + f ) / 2 ;si ( x > table [i]) alors
retour (ELEMENT ( x, table, i +1, f ))sinon retour (ELEMENT ( x, table, d, i ))
} fin
Temps (ELEMENT sur table [1 n]) = O (log n)
-
8/12/2019 hachage_4
3/21
128
3 4 8 9 10 20 40 50 70 75 80 83 85 90
UMLV Recherche par interpolation
table 1 2 3 4 5 6 7 8 9 10 11 12 13 14di f
di f 4 ? df
fonction ELEMENT ( x, table, d, f ) ;dbut
si ( d = f ) alors si ( x = table [d ] alors retour (vrai)sinon retour (faux)
sinon {
i d +
si ( x > table [i] alors retour (ELEMENT ( x, table , i+1, f ))
sinon retour (ELEMENT ( x, table, d, i ))}
fin
);( d f d table f table
d table x
-
8/12/2019 hachage_4
4/21
129
Ide : tablir une relation entre un lment et ladresse laquelle il est rang en mmoire
Thorme : Si les lments de table [1 n] et x sont choisisuniformment dans un intervalle [ a,b ], le temps moyendexcution de la recherche par interpolation est O (log logn)
UMLV
-
8/12/2019 hachage_4
5/21
130
Type abstrait dictionnaire
Ensembles avec les oprations principalesELEMENT ( x, A)
AJOUTER ( x, A) temps constant en moyenne ENLEVER ( x, A)
Implmentation non adapt au classement.
Table de hachage table dont les indices sont dans [0 .. B-1]
Fonction de hachage h : lments [0 .. B-1] non injective en gnral
Rsolution des collisions Hachage ouvert : avec listes, tries ou non.Hachage ferm : linaire, quadratique, alatoire,
uniforme, double, ...
UMLV Hachage
-
8/12/2019 hachage_4
6/21
131
UMLV Hachage ouvert
table01
B-1
Liens explicitesTaille variable
fonction h ( x : mot) : indice ;dbut
somme : = 0 ;
pour i 1 longueurmaxi faire somme somme + ord ( x [i]) ;
retour ( somme mod B) ;fin
-
8/12/2019 hachage_4
7/21
132
UMLV
int hash (char * s) {char * p ;unsigned h = 0, g ;for (p = s ; * p ; p++) {
h = (h> 24) ;h = h ^ g ;
}}return (h % PRIME) ;
}
Voir Aho, Sethi, Ullman, Compilers, Addison-Wesley, 1986
-
8/12/2019 hachage_4
8/21
133
UMLV const B = {constante ad hoc } ;type liste = cellule ;
cellule = record
elt : lment ;suivant : listeend ;
dictionnaire = array [0 .. B-1] of liste ;
fonction VIDER () : dictionnaire ;
dbut pour i 0 B-1 faire A [ i ] NULL ;retour A ;
fin
fonction ELEMENT ( x lment, A dictionnaire) : boolen ;dbut p A [ h( x ) ] ;
tantque p NULL faire si p ->elt = x alors retour vraisinon p p ->suivant ;
retour faux ;fin
-
8/12/2019 hachage_4
9/21
134
UMLV
fonction AJOUTER ( x lment, A dictionnaire) ;dbut
AJOUTERLISTE ( x , h( x ) ) ;retour A ;
fin
x
h( x )
fonction AJOUTER ( x lment, A dictionnaire) ;dbut
si non ELEMENT ( x , A) alors { i h ( x ) ;
p A [ i ] ;
allouer A [ i ] ; A [ i ]->elt x ; A [ i ]->suivant p ;
}retour A ;
fin
-
8/12/2019 hachage_4
10/21
135
Hachage ouvert - initialisation (VIDER) : O ( B)
- ajout : temps constant (aprs test d appartenance)
- appartenance
- suppressionsi les vnements " h ( x ) = i " sont quiprobables
Cration d'une table de n lments : ))+(1(O Bnn
)+(1O Bn
UMLV Temps des oprations
-
8/12/2019 hachage_4
11/21
136
UMLV
Hachage ferm
table
01
B-1
liens implicites : gain de place taille limite r-allocation en cas de dbordement
Re-hachage
h0 ( x ) = h ( x ), h1 ( x ), h i ( x ), o h i ( x ) dpend gnralement de x et de i
Suppression d un lment
distinction entre vide et disponible facteur de charge : n / B o n = nombre d lments
-
8/12/2019 hachage_4
12/21
137
UMLV
Hachage linaire
Re-hachage : h i ( x ) = ( h( x ) + i ) mod B
FORWARD
disponible
THEN
vide
vide
FOR
TO
0
1
23
4
5
B-1= 6
EXEMPLE
h ( x ) = (ord ( c ) - ord (a)) mod B o c premire lettre de x .
AJOUTER (BEGIN) AJOUTER (FOR) AJOUTER (FUNCTION) AJOUTER (FORWARD) AJOUTER (THEN) ENLEVER (FUNCTION) ENLEVER (BEGIN)
AJOUTER (TO)
-
8/12/2019 hachage_4
13/21
138
UMLV const B = {constante ad hoc} ;
vide = {constantes particulires } ;disponible = {distinctes des lments } ;
{mais de mme type } ;type dictionnaire = array [0 B-1] of lments ;
fonction VIDER () : dictionnaire ;dbut
pour i 0 B-1 faire
A [ i ] vide ;retour A ;fin
fonction POSITION ( x lment, A dictionnaire) : indice ;/* calcule la seule position possible o ajouter x dans A */
dbut i 0 ;tantque (i < B) et A [h i ( x )] x et A [h i ( x )] vide et A [h i ( x )] disponiblefaire i i + 1 ;
retour (h i ( x )) ;fin
-
8/12/2019 hachage_4
14/21
139
UMLV
fonction POSITIONE ( x lment, A dictionnaire) : indice ;dbut hi h (x) ; dernier (hi + B-1) mod B ;
tantque hi dernier et ( A [hi ] { x, vide }) faire hi (hi + 1) mod B ;
retour (hi )fin
fonction ELEMENT ( x lment, A dictionnaire) : boolean ;
dbut si A [POSITIONE ( x, A)] = x retour vrai ;sinon retour faux ;
fin
fonction ENLEVER ( x lment, A dictionnaire) : dictionnaire ;
dbut i POSITIONE ( x, A ) ;si A [ i ] = x alors A [ i ] disponible ;retour A ;
fin
-
8/12/2019 hachage_4
15/21
140
UMLV
fonction POSITIONA ( x lment, A dictionnaire) : indice ;
dbut hi h ( x ) ; dernier := ( hi + B-1) mod B ;tantque hi dernier et ( A [hi ] { x , vide, disponible}) faire hi (hi + 1) mod B ;
retour hi ;fin
fonction AJOUTER ( x lment, A dictionnaire) : dictionnaire ;dbut i POSITIONA ( x, A) ;si A [ i ] {vide, disponible} alors
A [ i ] x ;retour A ;
sinon si A [ i ] x alors erreur ( " table pleine " ) ;fin
-
8/12/2019 hachage_4
16/21
141
UMLV
Hachage quadratique
Re-hachage : h i ( x ) = ( h ( x ) + i ) mod B
fonction POSITION ( x lment, A dictionnaire) : indice ;dbut
hi h ( x ) ; inc 1 ;tantque (inc < B) et ( A [hi ] { x , vide, ? }) faire
hi (hi + inc ) mod B ;inc inc + 2 ;
retour hi ;fin
seule la moiti de la table est examine par re-hachage utiliser la suite :
h ( x ), h ( x ) + 1, h ( x ) - 1, h ( x ) + 4, h ( x ) - 4, avec B premier.
-
8/12/2019 hachage_4
17/21
-
8/12/2019 hachage_4
18/21
143
UMLV Hachage alatoire
Re-hachage : h i ( x ) = ( h ( x ) + d i ) mod B,
d 1, d 2, , d B-1 permutation alatoire de (1,..., B-1)
Gnration des d i par dcalage .
- choix de k (1,, B-1)
2 . d i si 2 . d i B-1 (2 . d i - B) k sinonExemple B = 8 k = 3 = 11 2
d 1 = 5 10 = 101 2d 2 = 1 10 = 001 2
d 3 = 2 10 = 010 2d 4 = 4 10 = 100 2d 5 = 3 10 = 011 2d 6 = 6 10 = 110 2d 7 = 7 10 = 111 2
- d i + 1 = {
-
8/12/2019 hachage_4
19/21
-
8/12/2019 hachage_4
20/21
145
F tableau associatif : nombre fini d'indices de type quelconque F fonction de domaine fini F reprsentable par l'ensembleE = { ( x , F [ x ]) / x domaine de F }
Oprations
- INIT ( F ) rendre F [ x ] non dfini pour tout x - DEFINIR ( F, x, y ) poser F [ x ] = y - CALCULER ( F, x ) = y si F [ x ] = y
nul sinon
Implmentation
- reprsenter E par hachage sur le domaine de F
UMLV Tableau associatif
-
8/12/2019 hachage_4
21/21
146
Traducteur mot--motdico
if si then alors else sinon
entre
text ---
if --- then
sortie
texte ---
Trad si --- alors
UMLV TRADUCTEUR