Vous êtes sur la page 1sur 3

« Skip lists »

Algorithme et Structures de
Données • Table des symboles à accès rapide
• proposé par Pugh en 1990
Wurtz Jean-Marie • Rajout de liens supplémentaires
• rechercher M rapidement :
Université Louis Pasteur
Wurtz@igbmc.u-strasbg.fr

Copyright " 'Algorithms in Java'; Robert Sedgewick & Michael Shildlowsky; Third edition, Parts 1-4; Addison-Wesley "
Reproduction ULP Strasbourg. Autorisation CFC - Paris

private class Node {


« Skip List » : Définition ITEM item;
Node[] next;
« Skip List » :
• Une liste chaînée ordonnée où chaque noeud à une nombre variable de
liens. Les liens en position i forment une liste simplement chaînée qui
int sz;
Node(ITEM x, int k) {
structure de données
permettent de sauter des nœuds ayant moins de i nœuds item = x;
• Création d’un nœud : décider combien de liens ? sz = k;
– Tous les nœuds ont au moins un lien (1er niveau) next = new Node[sz];
}
– 2ème niveau : saut de t noeuds (ici t1 = 2)
}
– 3ème niveau : saut de t2 noeuds (ici t2=4)
private static final int L = 50;
– 4ème niveau : saut de t3 noeuds (ici t3=8)
private Node head; L : taille max du tableau
– On assigne ainsi j+1 liens avec une probabilité de tj :
private int N, lgN;
• ex : avec t=2, et j=1 , proba = 1/2 ; j=4, proba=1/8 ST(int maxN){ N : le nb d’éléments dans l’arbre
4 N = 0; lgN : hauteur de l’arbre
3 lgN = 0;
2 head = new Node(null, L);
1
}

Copyright " 'Algorithms in Java'; Robert Sedgewick & Michael Shildlowsky; Third edition, Parts 1-4; Addison-Wesley " Copyright " 'Algorithms in Java'; Robert Sedgewick & Michael Shildlowsky; Third edition, Parts 1-4; Addison-Wesley "
Reproduction ULP Strasbourg. Autorisation CFC - Paris Reproduction ULP Strasbourg. Autorisation CFC - Paris

private ITEM searchR(Node t, KEY v, int k) {


if (t == null) return null; Rechercher Insertion dans une « Skip List »
if (t != head)
if (equals(t.item.key(), v)) return t.item; dans une
if (k >= t.sz) k = t.sz-1;
if (t.next[k] != null) « Skip List »
if (!less(v, t.next[k].item.key()))
return searchR(t.next[k], v, k); // poursuite au niveau k
return (k == 0) ? null : searchR(t, v, k-1); // k-1 le niveau
}
ITEM search(KEY v) { return searchR(head, v, lgN - 1); }

> >
= < • Insertion de l’élément L
> – tout d’abord recherche (cercle rouge)
– puis insertion de L avec 3 liens
– cercles noirs pointent sur L et L pointe sur les éléments précédents
(G et S au niveau 3; H et M au niveau 2)
Copyright " 'Algorithms in Java'; Robert Sedgewick & Michael Shildlowsky; Third edition, Parts 1-4; Addison-Wesley " Copyright " 'Algorithms in Java'; Robert Sedgewick & Michael Shildlowsky; Third edition, Parts 1-4; Addison-Wesley "
Reproduction ULP Strasbourg. Autorisation CFC - Paris Reproduction ULP Strasbourg. Autorisation CFC - Paris

1
private int randX() {
int i, j; double t = Math.random(); Insertion Construction d’une « Skip List »
for (i = 1, j = 2; i < L; i++, j += j)
if (t*j > 1.0) break;
if (i > lgN) lgN = i;
dans une
}
return i;
« Skip List »
private void insertR(Node t, Node x, int k) {
• t*j > 1.0 ou encore
KEY v = x.item.key();
Node tk = t.next[k]; t > 1.0/j avec j=2k
if ((tk == null) || less(v, tk.item.key())) {
if (k < x.sz) { x.next[k] = tk; t.next[k] = x; }
if (k == 0) return;
insertR(t, x, k-1); return; // Recherche niveau k-1
} •
insertR(tk, x, k); // Recherche au même niveau • Insertion des clefs : A S E R C H I N G
} • lgN : hauteur de l’arbre • Skip List initialement vide
void insert(ITEM v) • Les nœuds ont (j+1) liens) avec une
{ insertR(head, new Node(v, randX()), lgN); N++; } probabilité de 1/2j
Copyright " 'Algorithms in Java'; Robert Sedgewick & Michael Shildlowsky; Third edition, Parts 1-4; Addison-Wesley " Copyright " 'Algorithms in Java'; Robert Sedgewick & Michael Shildlowsky; Third edition, Parts 1-4; Addison-Wesley "
Reproduction ULP Strasbourg. Autorisation CFC - Paris Reproduction ULP Strasbourg. Autorisation CFC - Paris

• Insertion des clefs : A C E G H I N Comparaison des 2 insertions


RS
• Skip List initialement vide
• Les noeuds ont (j+1) liens avec une
probabilité de 1/2j
• l’ordre initial n’influence pas la
construction de larbre

Construction d’une • Les structures sont similaires


« Skip List » (trié)

• noeuds avec 1 lien : 4 4 1/2 4/9


• noeuds avec 2 lien : 2 2 1/4 2/9
• noeuds avec 3 lien : 3 3 1/8 3/9

Copyright " 'Algorithms in Java'; Robert Sedgewick & Michael Shildlowsky; Third edition, Parts 1-4; Addison-Wesley " Copyright " 'Algorithms in Java'; Robert Sedgewick & Michael Shildlowsky; Third edition, Parts 1-4; Addison-Wesley "
Reproduction ULP Strasbourg. Autorisation CFC - Paris Reproduction ULP Strasbourg. Autorisation CFC - Paris

Suppression dans une Propriété des Skip List


« Skip List » • La recherche et l’insertion dans une Skip Liste aléatoire avec un
paramètre « t » nécessite :
private void removeR(Node t, KEY v, int k) {
Node x = t.next[k]; t * ( logt N ) / 2 = ( t / ( 2 * lg t ) * lg N comparaisons en moyenne
if ( !less(x.item.key(), v) ) {
if ( equals(v, x.item.key()) ) { t.next[k] = x.next[k]; }
if (k == 0) return;
removeR(t, v, k-1); // recherche niveau k-1 • Une « Skip List » a (t/(t-1))N liens en moyenne
return; – N liens au niveau 1
} – N/t liens au niveau 2
removeR(t.next[k], v, k); // recherche même niveau – N/t2 liens au niveau 3
} – ...
lgN : Hauteur de l’arbre – N/tn liens au niveau n
void remove(ITEM x)
{ removeR(head, x.key(), lgN); N--; }
Copyright " 'Algorithms in Java'; Robert Sedgewick & Michael Shildlowsky; Third edition, Parts 1-4; Addison-Wesley " Copyright " 'Algorithms in Java'; Robert Sedgewick & Michael Shildlowsky; Third edition, Parts 1-4; Addison-Wesley "
Reproduction ULP Strasbourg. Autorisation CFC - Paris Reproduction ULP Strasbourg. Autorisation CFC - Paris

2
Performances des algorithmes sur les arbres
Propriété des Skip List (2)
• Choix du paramètre t
• Nombre de comparaisons en moyenne
t * ( logt N ) / 2 = ( t / ( 2 * lg t ) * lg N = ( t / lg t )*( lg N /2 )

Minimum
Copyright " 'Algorithms in Java'; Robert Sedgewick & Michael Shildlowsky; Third edition, Parts 1-4; Addison-Wesley " Copyright " 'Algorithms in Java'; Robert Sedgewick & Michael Shildlowsky; Third edition, Parts 1-4; Addison-Wesley "
Reproduction ULP Strasbourg. Autorisation CFC - Paris Reproduction ULP Strasbourg. Autorisation CFC - Paris

Généralisation des 2-
2-3-4 Arbres

Vous aimerez peut-être aussi