1 / 40

大規模テキスト索引( suffix array) の構築法とその情報検索への応用 suffix array 構築アルゴリズムと実装

大規模テキスト索引( suffix array) の構築法とその情報検索への応用 suffix array 構築アルゴリズムと実装. 定兼 邦彦 東京大学理学系研究科 情報科学専攻. 内容. suffix array( 接尾辞配列)とは 関連研究 Bentley, Sedgewick 97 Manber, Myers 93 Larsson, Sadakane のアルゴリズム 計算量 実装 メモリ disk 上での構成アルゴリズム Application (proximity search ). 記号の説明. X = X [0.. n ] 文字列

kiley
Download Presentation

大規模テキスト索引( suffix array) の構築法とその情報検索への応用 suffix array 構築アルゴリズムと実装

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 大規模テキスト索引(suffix array)の構築法とその情報検索への応用suffix array構築アルゴリズムと実装 定兼 邦彦 東京大学理学系研究科 情報科学専攻

  2. 内容 • suffix array(接尾辞配列)とは • 関連研究 • Bentley, Sedgewick 97 • Manber, Myers 93 • Larsson, Sadakaneのアルゴリズム • 計算量 • 実装 • メモリ • disk上での構成アルゴリズム • Application (proximity search)

  3. 記号の説明 • X = X[0..n] 文字列 • Sjj 番目の接尾辞(suffix) X[j..n] • I [0..n] 接尾辞の添字 j の配列 X = BANaNa S0 S1 S2

  4. suffix array • 文字列の全てのsuffixのポインタを辞書順にソートした配列 • 省スペース(文字列自身と配列1つ) X suffix array I 1 ANaNa 0 BANaNa 4 Na 2 NaNa 5 a 3 aNa 0 BANaNa 1 ANaNa 2 NaNa 3 aNa 4 Na 5 a ソート

  5. suffix arrayの特徴 • 省スペース(文字列自身と配列1つ) • 任意の部分文字列の検索が可能 • O(|P|log n) 時間 • 補助配列を使うと O(|P|+log n) 時間 • 答えの列挙が簡単

  6. 関連研究 • Bentley, Sedgewick 97 • Manber, Myers 93

  7. Bentley, Sedgewick • quick sortの拡張(<, =, >に分ける) • 実際に高速 • 冗長な文字列で極端に遅くなる • O(n2) 時間

  8. t o b e o r n o t t o b e $ 0 1 2 3 4 5 6 7 8 9101112 0 5 8 9 2 3 6 11 12 o r n e t b 1 4 7 10 0 8 9 6 6 5 5 3 12 2 11 2 11 2 11 h=1 8 r o o e t t $ b 1 10 1 10 1 10 3 7 7 12 4 8 h=2 b e 10 10 11 11 12 o 0 9 0 9 0 9 $ h=3 e 1 2 o h=4 $ 9 o $ 9 0 h=5

  9. Manber, Myers • doubling techniqueを用いる • Radixソート • O(n logn) 時間 • 実際は遅い

  10. 9 5 3 6 7 10 9 5 1 2 1 11 7 11 6 1 2 12 11 doubling technique[Karp, Miller, Rosenberg 72] • 長さ 1, 2, 4, 8, ...の部分文字列に番号を割り当てる t o b e o r n o t t o b e $ 6 4 1 2 4 5 3 4 6 6 4 1 2 0 h=1 8 4 0 h=2 0 4 8 10 5 9 12 3 h=4 0 7 4 8 10 5 9 13 6 1 3 2 h=8

  11. Manber, Myers t o b e o r n o t t o b e $ 0 1 2 3 4 5 6 7 8 910111213 13 211 312 6 1 4 710 5 0 8 9 0 1 1 3 3 5 6 6 6 610111111 h=1 13 2 11 12 3 6 1 10 4 7 5 0 9 8 h=2 0 1 1 3 4 5 6 6 8 810111113 13 11 2 12 3 6 1 10 4 7 5 0 9 8 h=4 0 1 2 3 4 5 6 7 8 910111113 13 11 2 12 3 6 1 10 4 7 5 9 0 8 h=8 0 1 2 3 4 5 6 7 8 910111213

  12. Manber, Myersの問題点 • 各反復で全ての要素を見る • O ( n log 最大反復長) • 後のほうのパスでは無駄が多い • すでにソートされている部分をスキップする • 配列1つでRadixソートを行うために遅くなっている • in-placeのソートを使う (quick sortなど)

  13. 13 211 312 6 1 4 710 5 0 8 9 0 1 1 3 3 5 6 6 6 610111111 h=1 13 21112 3 6 110 4 7 5 0 9 8 1 1 3 4 6 6 8 8 111113 h=2 1311 212 3 6 110 4 7 5 0 9 8 1 2 6 7 8 9 1111 h=4 1311 212 3 6 110 4 7 5 9 0 8 1112 h=8 ソートされた部分のスキップ t o b e o r n o t t o b e $ 0 1 2 3 4 5 6 7 8 910111213

  14. Larsson, Sadakaneの方法 • 2つの方法を組合わせる • Bentley, Sedgewick • doubling technique

  15. 7000 6000 5000 4000 3000 2000 1000 0 Larsson Sadakane B&S M&M Kurtz 実験結果 Ultra60 (メモリ2GB) 時間(s) 新聞記事(109M) 特許 (89M) Reuters (27M) html (125M)

  16. t o b e o r n o t t o b e $ 0 1 2 3 4 5 6 7 8 9101112 0 5 8 9 2 3 6 11 12 t 2 11 6 5 o e r n b 2 11 3 12 6 1 4 7 10 5 0 8 9 1 1 3 3 5 6 6 6 6 10 111111 h=1 6 12 1 10 7 8 3 6 10 11 1 0 9 0 11 1111 8 2 11 12 3 1 10 7 4 1 13 1 1 6 6 9 3 4 h=2 8 0 9 8 4 1111 11 10 h=4 0 8 3 9 0 11 2 10 1 0 9 1 2 7 6 h=8 12 11

  17. 計算量 • O(n log n)時間 • 木の根から各葉へのパス上に • <, > に対応する枝の数 log2n +1以下 • = に対応する枝の数 log2n +1 以下 • 1回の分割は線形時間

  18. 比較回数の期待値 • = に対応する枝の数は平均的には少ない • 以下(Hは文字列のエントロピー) • Bentley, Sedgewickでは 以下

  19. 実装(基本) • 配列 • アルゴリズム • 配列の更新方法

  20. 配列 • X[0..n] 文字列 • I [0..n] 接尾辞の添え字の配列 • V[ j ] 接尾辞 jの先頭 h文字につける番号  = j を含むグループの左端の添え字 i • L[ i ] 辞書順で i 番目の接尾辞から始まるグループのサイズ

  21. アルゴリズム • 接尾辞を先頭の1文字でグループ分け • I, V, Lの計算, h=1 • 各グループの I[i] を V[I[i]+h] に従い並び替える • V が隣と異なる場合はグループを分け, Lを更新 • L を見て異なるグループに異なる V を書く • 隣り合うサイズ1のグループを一つにし, Lを更新 • サイズが2以上のグループがあれば h := 2h として1に戻る

  22. 6 0 1 1 3 3 6 6 6 10 11 11 11 5 先頭の2文字でソート I の並び替え(h=1) 1 3 3 6 0 10 11 1 6 11 6 V[I[i]+1] V[I[i]] 13 2 11 3 12 6 1 4 7 10 5 0 8 9 I[i] $ beor be$ eorn e$ nott obeo orno otto obe$ rnot tobe ttob tobe tobeornottobe$

  23. 1 1 3 4 6 6 8 9 11 11 13 先頭の4文字でソート I の並び替え(h=2) V[I[i]+2] 8 0 4 3 1 1 V[I[i]] 0 5 10 13 2 11 12 3 6 1 10 4 7 5 0 9 8 I[i] $ beor be$ e$ nott obeo obe$ rnot eorn orno otto tobe tobe ttob tobeornottobe$

  24. 1 2 3 4 6 7 8 9 11 11 13 先頭の8文字でソート I の並び替え(h=4) V[I[i]+4] 8 0 V[I[i]] 0 5 10 13 11 2 12 3 6 10 1 4 7 5 0 9 8 I[i] $ e$ nott rnot be$ beor eorn obe$ obeo orno otto tobeor tobe$ ttob tobeornottobe$

  25. 1 2 3 4 6 7 8 9 11 12 13 I の並び替え(h=8) V[I[i]] 0 5 10 13 11 2 12 3 6 10 1 4 7 5 9 0 8 I[i] $ e$ nott rnot be$ beor eorn obe$ obeo orno otto tobeor tobe$ ttob tobeornottobe$ ソート終了

  26. L 1 2 2 1 4 1 3 V 0 1 1 3 3 5 6 6 6 610111111 h=1 L 1 2 -3 2 2 1 2 1 V 0 1 1 3 4 5 6 6 8 810111113 L-11 2 1 V 0 1 2 3 4 5 6 7 8 910111113 L-14 V 0 1 2 3 4 5 6 7 8 910111213 h=2 h=4 h=8 Lの更新 Xt o b e o r n o t t o b e $ I 0 1 2 3 4 5 6 7 8 910111213

  27. 実装(改良) • 1パス化 • I, V, L の更新を一度に行う • L (グループのサイズの配列)を消去 • V で L を表す

  28. 1パス化 • I を並び替える際に V を更新 • <, =, > に分割 • > を再帰的にソート • = の V を更新 • < を再帰的にソート • 更新の順番が重要 • V は常に大きくなるので < を先に更新すると順序が狂う

  29. t o b e o r n o t t o b e $ 2 3 6 11 12 0 5 8 9 2 11 6 5 t o e r n b 2 11 3 12 6 1 4 7 10 5 0 8 9 1 1 3 3 5 6 6 6 6 10 111111 h=1 6 12 1 10 7 8 3 6 10 11 1 0 9 0 11 8 2 11 12 3 1 10 7 4 1 1 1 0 9 8 4 11 10 0 8 3 9 0 11 2 10 1 0 9 0 1 2 3 4 5 6 7 8 9101112 1111 13 6 6 9 3 4 h=2 8 1111 h=4 1 2 7 6 h=8 12 11

  30. j を含むグループの右端の添え字 i Lの消去 • V[ j ] = j を含むグループの左端の添え字 i • <, =, > の順に V を更新 • スキップするグループのサイズは I に格納 • スキップされる接尾辞の I は使われない • 最後に V から I を復元 • I[V[ j ]] = j

  31. I-1 211 312-1 1 4 710-1 0 8 9 V 0 2 2 4 4 5 9 9 9 910131313 h=1 -1 211-3 110 4 7-1 0 9-1 0 2 2 3 4 5 7 7 9 910121213 h=2 -11 0 9-1 0 1 2 3 4 5 6 7 8 910121213 h=4 -14 0 1 2 3 4 5 6 7 8 910111213 h=8 t o b e o r n o t t o b e $ 0 1 2 3 4 5 6 7 8 910111213

  32. アルゴリズム (改良版) 各反復で i = 0 while (i  n) { s = I[ i ] if (s < 0) i = i - s // ソート済みグループをスキップ else { ソート済みグループを連結 I [i .. i+V[i] ] をソート i = i + V[i] + 1 // i を次のグループの先頭に } }

  33. メモリ • 必要なメモリ • ディスク上での構成

  34. 必要メモリ • Bentley, Sedgewick 5n (8n) • Manber, Myers 8n • Larsson, Sadakane 8n • Kurtz >13n

  35. Suffix arrayのディスク上での構成 • Gonnet, Baeza-Yates, Snider 92 • diskはsequential accessのみ • Crauser, Ferragina 98 • doubling algorithm + discarding I/O (M: メモリサイズ, B:ページサイズ) I/O

  36. Doubling technique + discarding • doubling technique • 回の反復 • M/B-way マージソートを用いる メモリ内と異なる点 • すでにソートされている部分はスキップ

  37. 現実的な方法 • 文字列を分割 • メモリ内でsuffix arrayを作る • disk上のsuffix arrayにマージする • メモリ内のsuffixを辞書順にdisk上に挿入 • disk上の文字列がメモリに入るなら速い

  38. さらなる高速化 • 伊東の方法[2]と組み合わせる • suffixを2種類に分割 • 片方をBentley, Sedgewickなどでソート • もう片方はRadixソート

  39. Application (proximity search) • 指定したキーワードが近くに現れている場所を見つける • 検索結果を絞れる • 近くに現れている 関連がある • 「今井」+「ホームページ」+「東京」 • 「samba」+「98」

  40. 参考文献 [1] N. J. Larsson and K. Sadakane. Faster Suffix Sorting. Technical Report LU-CS-TR:99-214, LUNDFD6/(NFCS-3140)/1-20/(1999), Department of Computer Science, Lund University, Sweden, May 1999. http://www.cs.lth.se/home/Jesper_Larsson/ [2] 伊東秀夫. 大規模テキストに対する Suffix Array の効率的な構成法. SIGNL-129-5, IPSJ, January 1999.

More Related