Recherche dichotomique dans le tableau des suffixes
Transcription
Recherche dichotomique dans le tableau des suffixes
Table des suffixes Thierry Lecroq [email protected] Laboratoire d’Informatique, du Traitement de l’Information et des Systèmes – EA 4108 Université de Rouen Plan Introduction Recherche d’un mot Plan Introduction Recherche d’un mot Table des suffixes I Texte y ∈ A∗ de longueur n I Permutation des suffixes p : {0, 1, . . . n − 1} 7−→ {0, 1, . . . n − 1} telle que y[p[0]..n − 1] < y[p[1]..n − 1] < · · · < y[p[n − 1]..n − 1] I LCP (Longest Common Prefixes) LCP [i] = |lcp(y[p[i − 1]..n − 1], y[p[i]..n − 1])| Table des suffixes Exemple pour y = aabaabaabba i p[i] LCP [i] 0 10 0 a 1 0 1 a a b a 2 3 6 a a b a 3 6 3 a a b b 4 1 1 a b a a 5 4 5 a b a a 6 7 2 a b b a 7 9 0 b a 8 2 2 b a a b 9 5 4 b a a b 10 8 1 b b a a a a b b b b a b a a b b a b a a b b a a b a a b b a a Plan Introduction Recherche d’un mot Recherche dichotomique La recherche d’un mot x de longueur m dans y en utilisant le tableau des suffixes de y peut être réalisée par une simple recherche dichotomique. L’algorithme Binary-Search(y, n, p, x, m) suivant retourne : I (−1, 0) si x < y[p[0] . . n − 1] ; I (n − 1, n) si x > y[p[n − 1] . . n − 1] ; I i si x est un préfixe de x[p[i] . . n − 1] ; I (i, i + 1) si y[p[i] . . n − 1] < x < y[p[i + 1] . . n − 1]. Détails des étapes Binary-Search(y, n, p, x, m) 1 ` ← −1 2 r←n 3 tantque ` + 1 < r faire 4 i ← b(` + r)/2c 5 k ← |lcp(x, y[p[i] . . n − 1])| 6 si k = m alors 7 retourner i 8 sinon si x ≤ y[p[i] . . n − 1] alors 9 r←i 10 sinon ` ← i 11 retourner (`, r) Complexité de la recherche dichotomique La complexité temporelle de l’algorithme Binary-Search(y, n, p, x, m) est O(m × log n). Analyse de la recherche dichotomique Le nombre de couples (`, r) considérés par l’algorithme Binary-Search(y, n, p, x, m) est borné par 2n + 1 : il y a n + 1 couples de la forme (i, i + 1) qui constituent les feuilles d’un arbre binaire donc il y au plus n nœuds internes. Arbre de la recherche dichotomique Extension du tableau LCP Il est donc possible d’étendre le tableau LCP comme suit : LCP [n + 1 + i] = lcp(p[`] . . n − 1], y[p[r] . . n − 1]) où i = b(` + r)/2c. La longueur du plus long préfixe commun entre y[p[`] . . n − 1] et y[p[r] . . n − 1] quand ` < r − 1 est égale à la valeur minimum parmi les LCP [i] où ` < i ≤ r. Alors ces valeurs peuvent être calculées par un appel à l’algorithme Def-LCP(y, 0, n − 1, LCP ). Extension du tableau LCP Def-LCP(y, `, r, LCP ) .`<r 1 si ` + 1 = r alors 2 retourner LCP [r] 3 sinon i ← b(` + r)/2c 4 LCP [n + 1 + i] ← min{Def-LCP(y, `, i, LCP ) Def-LCP(y, i, r, LCP )} 5 retourner LCP [n + 1 + i] Recherche Soient 3 entiers `, r, i tels que 0 ≤ ` < i < r < n. Si y[p[`] . . n − 1] < x < y[p[r] . . n − 1] soient ld = |lcp(x, y[p[`] . . n − 1])| et lf = |lcp(x, y[p[r] . . n − 1])| satisfaisant ld ≤ lf. Alors on a : |lcp(y[p[i] . . n − 1], y[p[r] . . n − 1])| < lf implique y[p[i] . . n − 1] < x < y[p[r] . . n − 1] et |lcp(y[p[i] . . n − 1], y[p[r] . . n − 1])| > lf implique y[p[`] . . n − 1] < x < y[p[i] . . n − 1]. Algorithme Search(y, n, p, LCP, x, m) (`, ld) ← (−1, 0) (r, lf) ← (n, 0) tantque ` + 1 < r faire i ← b(` + r)/2c g ← Lcp(`, i) h ← Lcp(i, r) si ld ≤ h and h < lf alors (`, ld) ← (i, h) sinon si ld ≤ lf and lf < h alors r←i sinon si lf ≤ g and g < ld alors (r, lf) ← (i, g) sinon si lf ≤ ld and ld < g alors `←i sinon k ← max{ld, lf} k ← k + |lcp(x[k . . m − 1], y[p[i] + [k . . n − 1])| si k = m alors retourner i sinon si x < y[p[i] . . n − 1] alors (r, lf) ← (i, k) sinon (`, ld) ← (i, k) retourner (`, r) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 Lcp ( LCP [j] Lcp(i, j) = LCP [n + 1 + b(i + j)/2c] si j = i + 1 sinon. Complexité La recherche peut être faite en temps O(m + log n) avec l’algorithme Search(y, n, p, LCP , x, m)