Flexible pattern matching in strings Practical online search
文字列照合アルゴリズム 情報知識ネットワーク特論 喜田拓也 参考文献: Flexible pattern matching in strings: Practical on-line search algorithm for texts and biological sequences, Gonzalo Navarro and Mathieu Raffinot, Cambridge University Press, 2002, ISBN 0 -521 -81307 -7. 10/29/2020 情報知識ネットワーク特論 授業資料 1
Prefix, Factor, Suffix の例 w = cocoa factor prefix c co cocoa o oc oco suffix a oa coa ocoa cocoa 演習:w=cacao のprefix, factor, suffix を列挙せよ。 4
文字列照合問題 (Pattern Matching Problem)とは? • テキストT中に含まれるパタンPの出現を求める問題 有名なアルゴリズム: – KMP法 (Knuth&Morris&Pratt[1974]) – BM法 (Boyer&Moore[1977])とその変種 – Karp-Rabin法 (Karp&Rabin[1987]) パタン P compress テキスト T 6 We introduce a general framework which is suitable to capture an essence of compressed pattern matching according to various dictionary based compressions. The goal is to find all occurrences of a pattern in a text without decompression, which is one of the most active topics in string matching. Our framework includes such compression methods as Lempel-Ziv family, (LZ 77, LZSS, LZ 78, LZW), byte-pair encoding, and the static dictionary based method. Technically, our pattern matching algorithm extremely extends that for LZW compressed text presented by Amir, Benson and Farach [Amir 94].
Knuth-Morris-Pratt アルゴリズム D. E. Knuth, J. H. Morris, Jr, and V. R. Pratt. Fast pattern matching in strings. SIAM Journal on Computing, 6(1): 323 -350, 1977. KMP-String-Matching (T, P) 1 n ← length[T]. 2 m ← length[P]. 3 q ← 1. 4 next ← Compute. Next(P). 5 for i ← 1 to n do 6 while q>0 かつ P[q]≠T[i] do q ← next[q]; 7 if q=m then report an occurrence at i-m; 8 q ← q+1. テキストT: ababbabcabbaa… ababc next[5] = 3 ababc パタンP: next[3] = 0 11 next関数によって次にPの何文字目とテキストを 比較するかがわかる。(シフト量は q-next[q]) 値が0のときは、テキストの次の文字と比較する。 テキストの各文字との比較はO(1)回ずつである。 最悪の場合でも O(n+m) 時間 (nextはあらかじめ配列として計算)
next関数の計算 Compute. Next (P) 1 m ← length[P]. 2 next[1] ← 0. 3 k ← 0. 4 for q ← 1 to m do 5 while k>0 かつ P[q]≠P[k] do k ← next[k]; 6 k ← k+1; 7 if P[q+1]=P[k] then 8 next[q+1] ← next[k] 9 else パタンのどこまで一致して 10 next[q+1] ← k; いたかを記録している q+1 パタンP: 12 a b a b a a b k a c b a パタンをずらしながら比較し、 next[q]を計算 c b c a b c b a b c q 1 2 3 4 5 6 P[q] a b c next(q) 0 1 3 1
Aho-Corasick(AC) アルゴリズム A. V. Aho and M. J. Corasick. Efficient string matching: an aid to bibliographic search. Communications of the ACM, 18(6): 333 -340, 1975. パタン集合 ={AC, BA, BB, BAA, BACD} を受理する(ε遷移付き)順序機械 (AC照合機械) A 1 B 2 4 C A 3 5 ¬{A, B} A BA C B 6 : goto関数 : failure関数 7 AC 7 8 ※状態1へのfailureは省略 BAA D AC 9 BACD BB KMPオートマトンは、パタンが一つの場合のAC照合機械に等しい ※ 初期状態のとりかたが違うように見えるが、原理的には同じように構成できる 14
擬似コード Horspool (P, T) 1 m ← length[P]. 2 n ← length[T]. 3 Preprocessing: 4 For each c∈∑ do delta 1’[c] ← m. 5 For j← 1 to m – 1 do delta 1’[ P[j] ] ← m – j. 6 Searching: 7 i ← 0. 8 while i ≦ n – m do 9 j ← m; 10 while j > 0 かつ T[i+j] = P[j] do j ← j – 1; 11 if j = 0 then report an occurrence at i+1; 12 i ← i + delta 1’[ T[i+m] ]. 25
Suffix Automaton A. Blumer, J. Blumer, D. Haussler, A. Ehrenfeucht, M. T. Chen and J. Seiferas. The smallest automation recognizing the subwords of a text. Theoretical Computer Science (40): 31 -55, 1985. a o c e 0 c 1 n 2 c n u u o o n n a 29 a 4 o n 5 n u n a c n 6 7 a n u o o Suffix tree u o n n a a 8 a 9 e Suffix trei 3 u n n o u n c a e n u n o a n n a c a u o n n a o n n u n a o n n a
On-line 構築アルゴリズム とっても複雑なので、 Suffix. Automaton(P=p 1 p 2…pm) 構築するのは 1 Create the one-node graph G=DAWG(e). 2 root ← sink ← the node of G. suf[root] ←θ. 結構たいへん! 3 for i ← 1 to m do 4 create a new node newsink; 5 make a solid edge (sink, newsink) labeled by a; 6 w ← suf[sink]; 7 while w≠θ かつ son(w, a)=θ do 8 make a non-solid a-edge (w, newsink); 9 w ← suf[w]; 10 v ← son(w, a); 11 If w=θthen suf[newsink] ← root 12 else if (w, v) is a solid edge then suf[newsink] ← v 13 else 14 create a node newnode; 15 newnode has the same outgoing edges as v except that they are all non-solid; 16 change (w, v) into a solid edge (w, newnode); 17 suf[newsink] ← newnode; 18 suf[newnode] ← suf[v]; suf[v] ← newnode; 19 w ← suf[w]; 20 while w≠θかつ (w, v) is a non-solid a-edge do 21 redirect this edge to newnode; w ← suf[w]. 22 sink ← newsink. 30
Backward Oracle Matching (BOM)アルゴリズム C. Allauzen, M. Crochemore, and M. Raffinot. Efficient experimental string matching by weak factor recognition. In Proceedings of the 12 th Annual Symposium on Combinatorial Pattern Matching, LNCS 2089: 51 -72, 2001. • BDMとアイデアは同じ • 異なる点 – 複雑なSuffix automatonではなく、Factor oracleを使う • BDMにおいて必要なことは、文字列uがFactorであることではなく、 σuがFactorではないこと。 • Factor oracleの性質 – パタンPのFactor以外の文字列も受理してしまう可能性がある • 例:下の図で、cnnはPrvのFactorではない – O(m)時間で構築できるうえに、実装が容易で少メモリ • 状態数m+1個、遷移関数の実現サイズ 2 m-1 a n c 0 31 e 1 c 2 a n u 3 u 4 n o 5 n 6 n o P=announceの場合の、PRvに対するFactor oracle 7 a 8
Factor oracleの構築アルゴリズム Oracle-on-line (P=p 1 p 2…pm) 1 Create Oracle(ε) with 2 One single initial state 0, S(0) ←θ. 3 for i∈1…m do 4 Oracle(P=p 1 p 2…pj) 5 ← Oracle_add_letter (Oracle(P=p 1 p 2…pj-1), pj). Oracle_add_letter (Oracle(P=p 1 p 2…pm), σ) 1 Create a new state m+1. 2 δ(m, σ) ← m+1. 3 k ← S(m) 4 while k≠θかつδ(k, σ)=θ do 5 δ(k, σ) ← m+1; 6 k ← S(k). 7 If k =θthen s ← 0; 8 else s ← δ(k, σ). 9 S(m+1) ← s. 10 return Oracle(P=p 1 p 2…pmσ). 32
Suffix・Factor型アルゴリズムの 複数パタン照合への拡張 Commentz-Walterアルゴリズム B. Commentz-Walter. A string matching algorithm fast on the average. In Proceedings of the 6 th International Colloquium on Automata, Languages and Programming, LNCS 71: 118 -132, 1979. • BMアルゴリズムの直接的な拡張 Set Horspoolアルゴリズム • Commentz-WalterのアルゴリズムをHorspoolのアイデアに基づいて簡略化したもの Uratani-Takedaアルゴリズム • ACアルゴリズムのアイデアをBM型に転用したもの。CWより高速 Set Backward Oracle Matching (SBOM)アルゴリズム C. Allauzen and M. Raffinot. Factor oracle of a set of words. Techinical report 99 -11, Institut Gaspard-Monge, Universite de Marne-la-Vallee, 1999. • Factor oracleを複数文字列に拡張したものを利用。 Wu-Manberアルゴリズム S. Wu and U. Manber. A fast algorithm for multi-pattern searching. Report TR-94 -17, Department of Computer Science, University of Arizona, Tucson, AZ, 1994. • 実用的に高速なアルゴリズム。Agrepにも用いられている 33
擬似コード ※agrep ver 4. 02 の実装(mgrep. c)では、SFHIT・HASH・B はそれぞれ、4096、8192、3となっている(ようだ) Construct_SHIFT (P={p 1, p 2, …, pr}) 1 initialize SHIFT table by ℓmin–B+1. 2 For each Bl=pi[j–B+1…j] do 3 If SHIFT[h 1(Bl)] > mi – j do SHIFT[h 1(Bl)] = mi – j. Wu-Manber (P={p 1, p 2, …, pr}, T=T[1…n]) 1 Preprocessing: 2 Computation of B. 3 Construction of the hash tables SHIFT and HASH. 4 Searching: 5 pos ← ℓmin. 6 while pos≦n do 7 i ← h 1( T[pos–B+1…pos] ); 8 If SHIFT[i] = 0 then 9 list ← HASH[ h 2( T[pos–B+1…pos] ) ]; 10 Verify all the patterns in list one by one against the text; 11 pos ← pos + 1; 12 else pos ← pos + SHIFT[i]. 37
擬似コード Karp-Rabin (P, T, d, q) 1 m ← length[P]. 2 n ← length[T]. 3 h ← dm– 1 mod q. 4 p ← 0. 5 t 0 ← 0. 6 for i ← 1 to m do 7 p ← (d・p + P[i]) mod q; 8 t 0 ← (d・t 0 + T[i]) mod q. 正しい出現かど 9 for s ← 0 to n – m do うかを確認 10 if p = ts then 11 if P[1…m] = T[s+1…s+m] then 12 report an occurrence at s; 13 else if s < n – m then 14 ts+1 ← (d・(ts – T[s+1]・h)+T[s+m+1]) mod q. 40
参考: FFTを利用した確率的近似文字列照合 K. Baba, A. Shinohara, M. Takeda, S. Inenaga, and S. Arikawa. A Note on Randomized Algorithm for String Matching with Mismatches. Nordic Journal of Computing, 10(1): 2 -12, 2003. • Fast Fourier Transform (FFT)は ハードウェア上で高速に計算可能 • 文字列を数値に置き換え、スコアベクトルの列 をFFTにより高速に計算することで、(近似)文 字列照合を行う 馬場(九州大) i 1 T a P a 2 c b a 3 b b b a 4 a a b b a 5 b c a b b a スコアベクトル ci 3 41 1 1 5 2 6 b 7 a 8 9 10 c c b c a b b a 0 c a b b c a c
Shift-And アルゴリズム R. A. Baeza-Yates and G. H. Gonnet. A new approach to text searching. Proceedings of the 12 th International Conference on Research and Development in Information Retrieval, 168 -175. ACM Press, 1989. S. Wu and U. Manber. Fast text searching allowing errors. Communications of the ACM, 35(10): 83 -91, 1992. • レジスタ長のビット演算が並列に計算されることを利用 • パタン長mがワード長wよりも短い場合には、 O(n)時間で非常に高速に動作する – 一般には O(n・ m/w )時間、前処理はO(m+|∑|) パタン P = ababb を受理する決定性有限オートマトン 任意の 文字 0 a 1 b 2 a 3 b 4 b 5 -1 このNFAを シミュレートする パタン P = ababb を受理する非決定性有限オートマトン 任意の 文字 43 0 a 1 b 2 a 3 b 4 b 5
擬似コード Shift-And (P, T) 1 m ← length[P]. 2 n ← length[T]. 3 Preprocessing: 4 for c ∈ ∑ do M[c] ← 0 m. 5 for j ← 1 to m do M[ P[j] ] ← M[ P[j] ] | 0 m–j 10 j– 1. 6 Searching: 7 R ← 0 m. 8 for s ← 1 to n do 9 R ← ((R << 1) | 0 m-11) & M[ T[s] ]; 10 If R & 10 m-1 ≠ 0 m then report an occurrence at s. 46
Bit-parallel手法の文字クラスへの拡張 パタン P: ab[ab]bb テキスト T: a 1 2 3 4 5 0 1 0 48 0 0 0 ba 1 0 0 0 1 1 0 0 0 0 0 1 1 0 1 & 1 1 0 0 1 Mask table M b b 0 1 0 0 ba 0 0 0 1 1 0 0 a b [ab] b b a b 1 0 0 0 1 1 これだけ! ここは同じ Ri = (Ri-1<<1 | 1) & M(T[i])
BNDM アルゴリズム G. Navarro and M. Raffinot. Fast and flexible string matching by combining bit-parallelism and suffix automata. ACM Journal of Experimental Algorithmics (JEA), 5(4), 2000. • 基本はBDMと同じ • 異なる点 – パタンのfactorかどうかを調べるため、 非決定性(nondeterministic)のSuffix automataを用いる – そのNFAの状態遷移をBit-parallel手法でシミュレートする パタンP = announce の反転Prvのsuffixを受理する非決定性オートマトン I ε ε 0 e ε 1 c ε 2 n ε 3 u ε 4 o ε 5 n ε 6 n 49 7 a 8 このNFAを シミュレートする 初期状態:R 0 = 1 m 状態遷移:R = (R << 1) & M[ T[i] ] ε Shift-Andと同じ Mask table
擬似コード BNDM (P, T) 1 m ← length[P]. 2 n ← length[T]. 3 Preprocessing: 4 for c ∈ ∑ do M[c] ← 0 m. 5 for j ← 1 to m do M[ P[j] ] ← M[ P[j] ] | 0 m–j 10 j– 1. 6 Searching: 7 s ← 0. 8 while s ≦ n – m do 9 j ← m, last ← m, R ← 1 m; 10 while R ≠ 0 m do 11 R ← (R << 1) & M[ T[s+j] ]; 12 j ← j – 1; 13 If R & 10 m-1 ≠ 0 m then 14 If j > 0 then last ← j; 15 else report an occurrence at s+1; 16 R ← R << 1; 17 s ← s + last. 50
擬似コード Thompson_recur (v) 1 if v = “|”(v. L, v. R) or v = “・”(v. L, v. R) then 2 Th(v. L) ← Thompson_recur(v. L); 3 Th(v. R) ← Thompson_recur(v. R); 4 else if v=“*”(v. C) then Th(v) ← Thompson_recur(v. C); 5 /* ここまでが再帰的な処理 (post order traverse) */ 6 if v=(ε) then return construction (i); 7 if v=(α), α∈Σ then return construction (ii); 8 if v=“・”(v. L, v. R) then return construction (iii); 9 if v=“|”(v. L, v. R) then return construction (iv); 10 if v=“*”(v. C) then return construction (v); Thompon(RE) 11 v. RE ← Parse(RE$, 1); /* 構文木を構築する */ 12 Th(v. RE) ← Thompson_recur(v. RE); 60
- Slides: 58