[IR] Suffix Trees and Suffix Arrays

时间:2021-08-02 18:36:32

前缀树

匹配前缀字符串是不言自明的道理。

1. 字符串的快速检索

2. 最长公共前缀(LCP)

等等

树的压缩

[IR] Suffix Trees and Suffix Arrays

后缀树

Let s=abab, a suffix tree of s is a compressed trie of all suffixes of s=abab$

[IR] Suffix Trees and Suffix Arrays[IR] Suffix Trees and Suffix Arrays

后缀树的构建:由长到短,逐渐增加。完毕后进行“树的压缩”。

右图绿色leaf node表示对应子串的起始位置。【ith】


Construct Suffix Tree

O(n2) --> O(n), how?

在 1995 年,Esko Ukkonen 发表了论文《On-line construction of suffix trees》,描述了在线性时间内构建后缀树的方法。下面尝试描述 Ukkonen 算法 的基本实现原理,从简单的字符串开始描述,然后扩展到更复杂的情形。

Ref: http://www.cnblogs.com/gaochundong/p/suffix_tree.html

Ref: https://*.com/questions/9452701/ukkonens-suffix-tree-algorithm-in-plain-english

鉴于Suffix Array要优于Suffix Tree,这篇文章只考虑Auffix Array的快速构建。Ukkonen 算法参见以上链接。

查找字符串o是否在字符串S中

方案:用S构造后缀树,按在trie中搜索子串的方法搜索o即可。

原理:若o在S中,则o必然是S的某个后缀的前缀。

指定字符串T在字符串S中的重复次数

方案:用S+’$'构造后缀树,搜索T结点下的叶节点数目即为重复次数 (虽然是公共枝,但其实string中位置不同)

原理:如果T在S中重复了两次,则S应有两个后缀以T为前缀,重复次数就自然统计出来了。

最长公共子串 (LCS)

* 自身内部比较 (最长重复子串)

方案:找到最深的非叶节点。   // 深度:从root所经历过的字符个数,最深非叶节点所经历的字符串起来就是最长重复子串

[IR] Suffix Trees and Suffix Arrays

s=abab, 可见最深的非leaf node经过的path是"ab",故,"ab"即是最长重复子串。

* 俩字符串比较

方案:将S1#,S2$作为字符串压入后缀树,找到最深的非叶节点,且该节点的叶节点既有#也有$。

[IR] Suffix Trees and Suffix Arrays

S1=abab, S2=aab,可见,最深的非leaf node经过的path是"ab"。

最长回文 (maximal palindromes)

方案:用S+"$"及其反转字符串+"#"构造后缀树,找到最深的非叶节点,且该节点的叶节点既有#也有$。

[IR] Suffix Trees and Suffix Arrays

Let s = cbaaba$ then sr = abaabc#,


Construct Suffix Array

Drawbacks

  • Suffix trees consume a lot of space  // 一个字符串的后缀形式毕竟很多
  • It is O(n) but the constant is quite big
  • Notice that if we indeed want to traverse an edge in O(1) time then we need an array of ptrs. of size |Σ| in each node

That's why Suffix Array. We loose some of the functionality but we save space.

理解后缀数组

Ref: http://cse.unl.edu/~lksoh/Classes/CSCE410_810_Fall03/sup7.html

后缀数组 vs 倒排表

Inverted indices assume that the text can be seen as a sequence of words.

Other queries such as phrases are expensive to solve.

One approach to address this problem is to use the suffix string approach.  The index sees the text as one long string.

Each position in the text is a considered as a text suffix (i.e., a string that goes from the text position to the end of the text).

Each suffix is thus uniquely identified by its position.

This type of index allows us to answer efficiently more complex queries.

The main drawbacks of this approach are:

(a) costly construction process,

(b) the text must be readily available at query time,

(c) the results are not delivered in text position order.

Unless complex queries are an important issue, for word-based applications, inverted files still perform better.

后缀数组与后缀树

Suffix arrays are a space efficient implementation of suffix trees.

Also, not all text positions need to be indexed.  Index points are selected from the text, which point to the beginning of the text positions which will be retrievable.

[IR] Suffix Trees and Suffix Arrays

以上是11, 19, 28, 33, 40, 50, 60为起点的若干个后缀。如果用后缀树表示,就是如下大概的样子:

[IR] Suffix Trees and Suffix Arrays

Suffix Tree 在 DFS 后就是 Suffix Array。

[IR] Suffix Trees and Suffix Arrays

可见,只是字符串后缀的两种等价的表达形式,即:数据结构不同。

但实际上,只需要记录以下绿色的部分index即可。

Simply an array containing all the pointers to the
text suffixes listed in lexicographical order.

通过这个index array 并结合"原字符串",即可推出所有必要信息。

[IR] Suffix Trees and Suffix Arrays

节省了空间,但失去了一定的功能上的便捷性,比如树结构的快速查找。

后缀数组,需采用二分查找find指定的后缀。

但问题是:

If the suffix array is large, this binary search can
perform poorly because of the number of random disk accesses.

Suffix arrays are designed to allow binary searches
done by comparing the contents of each pointer.

To remedy this situation, the use of supra-indices over the suffix array has been proposed.

后缀数组的应用

最长公共子串 (LCS)

* 自身内部比较 (最长重复子串)

字符串内部比较,后缀树表示时比较方便(见上)。

变为后缀数组后,功能性换取了空间节省,该怎么办?

[IR] Suffix Trees and Suffix Arrays

(1) 可见,从左到右查看最长common的部分即可。

(2) 方便使用并行策略。

* 俩字符串比较

合并后还是采取如上策略即可。

其实,既然保有原字符串,再装备上suffix array这样的类似指针的数组就好了。

suffix tree因为空间大,只需要“遍历”操作即可。

suffix array需通过“遍历with比较”就能达到相同效果。

其他待解决问题的sol思想类似。

Supra-indices

借助skip pointer的思想来加速查询。

Supra-Index相当于提取了文本中的一部分sub-string,

注意,这个(任意的)sub-string其实代表了文本字符串的某个后缀的前缀!

那么,Supra-Index可以快速地初步定位目标的大概区间段,然后在该区间再进行“精细地”二分查找。

[IR] Suffix Trees and Suffix Arrays

快速创建后缀数组

Space Efficient Linear Time Construction of Suffix Arrays - Pang Ko and Srinivas

Video: https://www.youtube.com/watch?v=m2-N853rS6U

Algorithm: Difference Cover modulo 3 - O(n)

Suffix Array <--> BWT Matrix

(1) 后缀Set --(sort)--> 后缀数组 --(Suffix array-1)--> BWT

[IR] Suffix Trees and Suffix Arrays

后缀排序后-->BWT matrix(的一半显示部分,非绿色字体)

BWT matrix:第一列:suffix Array,但记录的是index,而不是char。

BWT matrix:第尾列:BWT,通过suffix array-1获得。例如,6-1=,"BANA]=N

(2) BWT --(C Table) --> char 的顺序性,即: Index for 后缀数组

[IR] Suffix Trees and Suffix Arrays

所以,Suffix Array若能O(n)创建,那么BWT的创建直接由SA转化后即可,故也能O(n)。

实战(O(n))

string = "MISSISSIPPI$",

Tast: construct its suffix array.

Step 1, mark L and S

type L S L L S L L S L L L L/S
index  1   2       5       8   9      12 
text M I S S I S S I P P I $
distance 0 0 1 2 3 1 2 3 1 2 3 4

Step 2, create bucket

 Bucket   
 代表了顺序性 $ I M P S
  2         1              

Step 3, D lists

 D lists  不考虑 [2] [1]
  [9] [3, 6] 对应的char都是'S',暂没法判断
  [10] [4, 7] 对应的char都是'S',暂没法判断
  [5, 8, 11] 对应的char都是'I',暂没法判断
  [12]  

Step 4, S-Substring

S对应的项:2, 5, 8, 12.

其实就是进一步判断, 行。主要还是第行。

 2   5   8  全都是I
3 6 9 9:P  靠前;故不用再判断
4 7   全都是S
5 8   全都是I
6 9   6:S;9:P,故,5靠前

结果就是:8, 5, 2

11也是'I',但11是L类,2、5、8是S类,根据规则1

  In one bucket(肯定对应一个char),L类在S类的左边。

 Bucket   
代表了顺序性 $ I M P S
12  11  8   5   2                              

填好的部分。这构成了下一步的起始基础。

Step 5, complete others

 Bucket   
  $ I M P S
12  11  8   5   2    1  10  9   7   4   6   3  
规则2 11 10 7 4 1   9 8 7 4 5  

Finally, Suffix Array: [12, 11, 8, 5, 2, 1, 10, 9, 7, 4, 6, 3]

-----------------------------------------------------------------

关于规则1

[IR] Suffix Trees and Suffix Arrays

可见,$(字典排序)优先级肯定大于P,故,L类一定在S类前面。

关于规则2 

[IR] Suffix Trees and Suffix Arrays

‘小’后缀已比较,且前端加一个char而且这个char一致,那么,

‘小’后缀的比较结果决定‘大’后缀的比较结果。