Misplaced Pages

LCP array

Article snapshot taken from Wikipedia with creative commons attribution-sharealike license. Give it a read and then ask your questions in the chat. We can research this topic together.
This article includes a list of general references, but it lacks sufficient corresponding inline citations. Please help to improve this article by introducing more precise citations. (September 2012) (Learn how and when to remove this message)
LCP array
TypeArray
Invented byManber & Myers (1993)
Time complexity and space complexity in big O notation
Average Worst case
Space O ( n ) {\displaystyle {\mathcal {O}}(n)} O ( n ) {\displaystyle {\mathcal {O}}(n)}
Construction O ( n ) {\displaystyle {\mathcal {O}}(n)} O ( n ) {\displaystyle {\mathcal {O}}(n)}

In computer science, the longest common prefix array (LCP array) is an auxiliary data structure to the suffix array. It stores the lengths of the longest common prefixes (LCPs) between all pairs of consecutive suffixes in a sorted suffix array.

For example, if A := is a suffix array, the longest common prefix between A = aab and A = ab is a which has length 1, so H = 1 in the LCP array H. Likewise, the LCP of A = ab and A = abaab is ab, so H = 2.

Augmenting the suffix array with the LCP array allows one to efficiently simulate top-down and bottom-up traversals of the suffix tree, speeds up pattern matching on the suffix array and is a prerequisite for compressed suffix trees.

History

The LCP array was introduced in 1993, by Udi Manber and Gene Myers alongside the suffix array in order to improve the running time of their string search algorithm.

Definition

Let A {\displaystyle A} be the suffix array of the string S = s 1 , s 2 , s n 1 $ {\displaystyle S=s_{1},s_{2},\ldots s_{n-1}\$} of length n {\displaystyle n} , where $ {\displaystyle \$} is a sentinel letter that is unique and lexicographically smaller than any other character. Let S [ i , j ] {\displaystyle S} denote the substring of S {\displaystyle S} ranging from i {\displaystyle i} to j {\displaystyle j} . Thus, S [ A [ i ] , n ] {\displaystyle S,n]} is the i {\displaystyle i} th smallest suffix of S {\displaystyle S} .

Let lcp ( v , w ) {\displaystyle \operatorname {lcp} (v,w)} denote the length of the longest common prefix between two strings v {\displaystyle v} and w {\displaystyle w} . Then the LCP array H [ 1 , n ] {\displaystyle H} is an integer array of size n {\displaystyle n} such that H [ 1 ] {\displaystyle H} is undefined and H [ i ] = lcp ( S [ A [ i 1 ] , n ] , S [ A [ i ] , n ] ) {\displaystyle H=\operatorname {lcp} (S,n],S,n])} for every 1 < i n {\displaystyle 1<i\leq n} . Thus H [ i ] {\displaystyle H} stores the length of longest common prefix of the lexicographically i {\displaystyle i} th smallest suffix and its predecessor in the suffix array.

Difference between LCP array and suffix array:

  • Suffix array: Represents the lexicographic rank of each suffix of an array.
  • LCP array: Contains the maximum length prefix match between two consecutive suffixes, after they are sorted lexicographically.

Example

Consider the string S = banana$ {\displaystyle S={\textrm {banana\$}}} :

i 1 2 3 4 5 6 7
S b a n a n a $

and its corresponding sorted suffix array A {\displaystyle A}  :

i 1 2 3 4 5 6 7
A 7 6 4 2 1 5 3

Suffix array with suffixes written out underneath vertically:

i 1 2 3 4 5 6 7
A 7 6 4 2 1 5 3
S, n] $ a a a b n n
S, n] $ n n a a a
S, n] a a n $ n
S, n] $ n a a
S, n] a n $
S, n] $ a
S, n] $

Then the LCP array H {\displaystyle H} is constructed by comparing lexicographically consecutive suffixes to determine their longest common prefix:

i 1 2 3 4 5 6 7
H undefined 0 1 3 0 0 2

So, for example, H [ 4 ] = 3 {\displaystyle H=3} is the length of the longest common prefix ana {\displaystyle {\text{ana}}} shared by the suffixes A [ 3 ] = S [ 4 , 7 ] = ana$ {\displaystyle A=S={\textrm {ana\$}}} and A [ 4 ] = S [ 2 , 7 ] = anana$ {\displaystyle A=S={\textrm {anana\$}}} . Note that H [ 1 ] {\displaystyle H} is undefined, since there is no lexicographically smaller suffix.

Efficient construction algorithms

LCP array construction algorithms can be divided into two different categories: algorithms that compute the LCP array as a byproduct to the suffix array and algorithms that use an already constructed suffix array in order to compute the LCP values.

Manber & Myers (1993) provide an algorithm to compute the LCP array alongside the suffix array in O ( n log n ) {\displaystyle O(n\log n)} time. Kärkkäinen & Sanders (2003) show that it is also possible to modify their O ( n ) {\displaystyle O(n)} time algorithm such that it computes the LCP array as well. Kasai et al. (2001) present the first O ( n ) {\displaystyle O(n)} time algorithm (FLAAP) that computes the LCP array given the text and the suffix array.

Assuming that each text symbol takes one byte and each entry of the suffix or LCP array takes 4 bytes, the major drawback of their algorithm is a large space occupancy of 13 n {\displaystyle 13n} bytes, while the original output (text, suffix array, LCP array) only occupies 9 n {\displaystyle 9n} bytes. Therefore, Manzini (2004) created a refined version of the algorithm of Kasai et al. (2001) (lcp9) and reduced the space occupancy to 9 n {\displaystyle 9n} bytes. Kärkkäinen, Manzini & Puglisi (2009) provide another refinement of Kasai's algorithm ( Φ {\displaystyle \Phi } -algorithm) that improves the running time. Rather than the actual LCP array, this algorithm builds the permuted LCP (PLCP) array, in which the values appear in text order rather than lexicographical order.

Gog & Ohlebusch (2011) provide two algorithms that although being theoretically slow ( O ( n 2 ) {\displaystyle O(n^{2})} ) were faster than the above-mentioned algorithms in practice.

As of 2012, the currently fastest linear-time LCP array construction algorithm is due to Fischer (2011), which in turn is based on one of the fastest suffix array construction algorithms (SA-IS) by Nong, Zhang & Chan (2009). Fischer & Kurpicz (2017) based on Yuta Mori's DivSufSort is even faster.

Applications

As noted by Abouelhoda, Kurtz & Ohlebusch (2004) several string processing problems can be solved by the following kinds of tree traversals:

  • bottom-up traversal of the complete suffix tree
  • top-down traversal of a subtree of the suffix tree
  • suffix tree traversal using the suffix links.

Kasai et al. (2001) show how to simulate a bottom-up traversal of the suffix tree using only the suffix array and LCP array. Abouelhoda, Kurtz & Ohlebusch (2004) enhance the suffix array with the LCP array and additional data structures and describe how this enhanced suffix array can be used to simulate all three kinds of suffix tree traversals. Fischer & Heun (2007) reduce the space requirements of the enhanced suffix array by preprocessing the LCP array for range minimum queries. Thus, every problem that can be solved by suffix tree algorithms can also be solved using the enhanced suffix array.

Deciding if a pattern P {\displaystyle P} of length m {\displaystyle m} is a substring of a string S {\displaystyle S} of length n {\displaystyle n} takes O ( m log n ) {\displaystyle O(m\log n)} time if only the suffix array is used. By additionally using the LCP information, this bound can be improved to O ( m + log n ) {\displaystyle O(m+\log n)} time. Abouelhoda, Kurtz & Ohlebusch (2004) show how to improve this running time even further to achieve optimal O ( m ) {\displaystyle O(m)} time. Thus, using suffix array and LCP array information, the decision query can be answered as fast as using the suffix tree.

The LCP array is also an essential part of compressed suffix trees which provide full suffix tree functionality like suffix links and lowest common ancestor queries. Furthermore, it can be used together with the suffix array to compute the Lempel-Ziv LZ77 factorization in O ( n ) {\displaystyle O(n)} time.

The longest repeated substring problem for a string S {\displaystyle S} of length n {\displaystyle n} can be solved in Θ ( n ) {\displaystyle \Theta (n)} time using both the suffix array A {\displaystyle A} and the LCP array. It is sufficient to perform a linear scan through the LCP array in order to find its maximum value v m a x {\displaystyle v_{max}} and the corresponding index i {\displaystyle i} where v m a x {\displaystyle v_{max}} is stored. The longest substring that occurs at least twice is then given by S [ A [ i ] , A [ i ] + v m a x 1 ] {\displaystyle S,A+v_{max}-1]} .

The remainder of this section explains two applications of the LCP array in more detail: How the suffix array and the LCP array of a string can be used to construct the corresponding suffix tree and how it is possible to answer LCP queries for arbitrary suffixes using range minimum queries on the LCP array.

Find the number of occurrences of a pattern

This article may require cleanup to meet Misplaced Pages's quality standards. The specific problem is: this section is a straight-up copy of a StackOverflow answer so it has the form of a reply to a question. Please help improve this article if you can. (June 2016) (Learn how and when to remove this message)

In order to find the number of occurrences of a given string P {\displaystyle P} (length m {\displaystyle m} ) in a text T {\displaystyle T} (length N {\displaystyle N} ),

  • We use binary search against the suffix array of T {\displaystyle T} to find the starting and end position of all occurrences of P {\displaystyle P} .
  • Now to speed up the search, we use LCP array, specifically a special version of the LCP array (LCP-LR below).

The issue with using standard binary search (without the LCP information) is that in each of the O ( log N ) {\displaystyle O(\log N)} comparisons needed to be made, we compare P to the current entry of the suffix array, which means a full string comparison of up to m characters. So the complexity is O ( m log N ) {\displaystyle O(m\log N)} .

The LCP-LR array helps improve this to O ( m + log N ) {\displaystyle O(m+\log N)} , in the following way:

At any point during the binary search algorithm, we consider, as usual, a range ( L , , R ) {\displaystyle (L,\dots ,R)} of the suffix array and its central point M {\displaystyle M} , and decide whether we continue our search in the left sub-range ( L , , M ) {\displaystyle (L,\dots ,M)} or in the right sub-range ( M , , R ) {\displaystyle (M,\dots ,R)} . In order to make the decision, we compare P {\displaystyle P} to the string at M {\displaystyle M} . If P {\displaystyle P} is identical to M {\displaystyle M} , our search is complete. But if not, we have already compared the first k {\displaystyle k} characters of P {\displaystyle P} and then decided whether P {\displaystyle P} is lexicographically smaller or larger than M {\displaystyle M} . Let's assume the outcome is that P {\displaystyle P} is larger than M {\displaystyle M} . So, in the next step, we consider ( M , , R ) {\displaystyle (M,\dots ,R)} and a new central point M {\displaystyle M'} in the middle:

             M ...... M' ...... R
             |
      we know:
         lcp(P,M)==k

The trick now is that LCP-LR is precomputed such that an O ( 1 ) {\displaystyle O(1)} -lookup tells us the longest common prefix of M {\displaystyle M} and M {\displaystyle M'} , l c p ( M , M ) {\displaystyle \mathrm {lcp} (M,M')} .

We already know (from the previous step) that M {\displaystyle M} itself has a prefix of k {\displaystyle k} characters in common with P {\displaystyle P} : l c p ( P , M ) = k {\displaystyle \mathrm {lcp} (P,M)=k} . Now there are three possibilities:

  • Case 1: k < l c p ( M , M ) {\displaystyle k<\mathrm {lcp} (M,M')} , i.e. P {\displaystyle P} has fewer prefix characters in common with M than M has in common with M'. This means the (k+1)-th character of M' is the same as that of M, and since P is lexicographically larger than M, it must be lexicographically larger than M', too. So we continue in the right half (M',...,R).
  • Case 2: k > l c p ( M , M ) {\displaystyle k>\mathrm {lcp} (M,M')} , i.e. P {\displaystyle P} has more prefix characters in common with M {\displaystyle M} than M {\displaystyle M} has in common with M {\displaystyle M'} . Consequently, if we were to compare P {\displaystyle P} to M {\displaystyle M'} , the common prefix would be smaller than k {\displaystyle k} , and M {\displaystyle M'} would be lexicographically larger than P {\displaystyle P} , so, without actually making the comparison, we continue in the left half ( M , , M ) {\displaystyle (M,\dots ,M')} .
  • Case 3: k = l c p ( M , M ) {\displaystyle k=\mathrm {lcp} (M,M')} . So M and M' are both identical with P {\displaystyle P} in the first k {\displaystyle k} characters. To decide whether we continue in the left or right half, it suffices to compare P {\displaystyle P} to M {\displaystyle M'} starting from the ( k + 1 ) {\displaystyle (k+1)} th character.
  • We continue recursively.

The overall effect is that no character of P {\displaystyle P} is compared to any character of the text more than once (for details see ). The total number of character comparisons is bounded by m {\displaystyle m} , so the total complexity is indeed O ( m + log N ) {\displaystyle O(m+\log N)} .

We still need to precompute LCP-LR so it is able to tell us in O ( 1 ) {\displaystyle O(1)} time the lcp between any two entries of the suffix array. We know the standard LCP array gives us the lcp of consecutive entries only, i.e. l c p ( i 1 , i ) {\displaystyle \mathrm {lcp} (i-1,i)} for any i {\displaystyle i} . However, M {\displaystyle M} and M {\displaystyle M'} in the description above are not necessarily consecutive entries.

The key to this is to realize that only certain ranges ( L , , R ) {\displaystyle (L,\dots ,R)} will ever occur during the binary search: It always starts with ( 0 , , N ) {\displaystyle (0,\dots ,N)} and divides that at the center, and then continues either left or right and divide that half again and so forth. Another way of looking at it is : every entry of the suffix array occurs as central point of exactly one possible range during binary search. So there are exactly N distinct ranges ( L M R ) {\displaystyle (L\dots M\dots R)} that can possibly play a role during binary search, and it suffices to precompute l c p ( L , M ) {\displaystyle \mathrm {lcp} (L,M)} and l c p ( M , R ) {\displaystyle \mathrm {lcp} (M,R)} for those N {\displaystyle N} possible ranges. So that is 2 N {\displaystyle 2N} distinct precomputed values, hence LCP-LR is O ( N ) {\displaystyle O(N)} in size.

Moreover, there is a straightforward recursive algorithm to compute the 2 N {\displaystyle 2N} values of LCP-LR in O ( N ) {\displaystyle O(N)} time from the standard LCP array.

To sum up:

  • It is possible to compute LCP-LR in O ( N ) {\displaystyle O(N)} time and O ( 2 N ) = O ( N ) {\displaystyle O(2N)=O(N)} space from LCP.
  • Using LCP-LR during binary search helps accelerate the search procedure from O ( M log N ) {\displaystyle O(M\log N)} to O ( M + log N ) {\displaystyle O(M+\log N)} .
  • We can use two binary searches to determine the left and right end of the match range for P {\displaystyle P} , and the length of the match range corresponds with the number of occurrences for P.

Suffix tree construction

Given the suffix array A {\displaystyle A} and the LCP array H {\displaystyle H} of a string S = s 1 , s 2 , s n $ {\displaystyle S=s_{1},s_{2},\ldots s_{n}\$} of length n + 1 {\displaystyle n+1} , its suffix tree S T {\displaystyle ST} can be constructed in O ( n ) {\displaystyle O(n)} time based on the following idea: Start with the partial suffix tree for the lexicographically smallest suffix and repeatedly insert the other suffixes in the order given by the suffix array.

Let S T i {\displaystyle ST_{i}} be the partial suffix tree for 0 i n {\displaystyle 0\leq i\leq n} . Further let d ( v ) {\displaystyle d(v)} be the length of the concatenation of all path labels from the root of S T i {\displaystyle ST_{i}} to node v {\displaystyle v} .

Case 1 ( d ( v ) = H [ i + 1 ] {\displaystyle d(v)=H} ): Suppose the suffixes a $ {\displaystyle a\$} , a n a $ {\displaystyle ana\$} , a n a n a $ {\displaystyle anana\$} and b a n a n a $ {\displaystyle banana\$} of the string S = b a n a n a $ {\displaystyle S=banana\$} are already added to the suffix tree. Then the suffix n a $ {\displaystyle na\$} is added to the tree as shown in the picture. The rightmost path is highlighted in red.

Start with S T 0 {\displaystyle ST_{0}} , the tree consisting only of the root. To insert A [ i + 1 ] {\displaystyle A} into S T i {\displaystyle ST_{i}} , walk up the rightmost path beginning at the recently inserted leaf A [ i ] {\displaystyle A} to the root, until the deepest node v {\displaystyle v} with d ( v ) H [ i + 1 ] {\displaystyle d(v)\leq H} is reached.

We need to distinguish two cases:

  • d ( v ) = H [ i + 1 ] {\displaystyle d(v)=H} : This means that the concatenation of the labels on the root-to- v {\displaystyle v} path equals the longest common prefix of suffixes A [ i ] {\displaystyle A} and A [ i + 1 ] {\displaystyle A} .
    In this case, insert A [ i + 1 ] {\displaystyle A} as a new leaf x {\displaystyle x} of node v {\displaystyle v} and label the edge ( v , x ) {\displaystyle (v,x)} with S [ A [ i + 1 ] + H [ i + 1 ] , n ] {\displaystyle S+H,n]} . Thus the edge label consists of the remaining characters of suffix A [ i + 1 ] {\displaystyle A} that are not already represented by the concatenation of the labels of the root-to- v {\displaystyle v} path.
    This creates the partial suffix tree S T i + 1 {\displaystyle ST_{i+1}} .
    Case 2 ( d ( v ) < H [ i + 1 ] {\displaystyle d(v)<H} ): In order to add suffix n a n a $ {\displaystyle nana\$} , the edge to the previously inserted suffix n a $ {\displaystyle na\$} has to be split up. The new edge to the new internal node is labeled with the longest common prefix of the suffixes n a $ {\displaystyle na\$} and n a n a $ {\displaystyle nana\$} . The edges connecting the two leaves are labeled with the remaining suffix characters that are not part of the prefix.
  • d ( v ) < H [ i + 1 ] {\displaystyle d(v)<H} : This means that the concatenation of the labels on the root-to- v {\displaystyle v} path displays less characters than the longest common prefix of suffixes A [ i ] {\displaystyle A} and A [ i + 1 ] {\displaystyle A} and the missing characters are contained in the edge label of v {\displaystyle v} 's rightmost edge. Therefore, we have to split up that edge as follows:
    Let w {\displaystyle w} be the child of v {\displaystyle v} on S T i {\displaystyle ST_{i}} 's rightmost path.
  1. Delete the edge ( v , w ) {\displaystyle (v,w)} .
  2. Add a new internal node y {\displaystyle y} and a new edge ( v , y ) {\displaystyle (v,y)} with label S [ A [ i ] + d ( v ) , A [ i ] + H [ i + 1 ] 1 ] {\displaystyle S+d(v),A+H-1]} . The new label consists of the missing characters of the longest common prefix of A [ i ] {\displaystyle A} and A [ i + 1 ] {\displaystyle A} . Thus, the concatenation of the labels of the root-to- y {\displaystyle y} path now displays the longest common prefix of A [ i ] {\displaystyle A} and A [ i + 1 ] {\displaystyle A} .
  3. Connect w {\displaystyle w} to the newly created internal node y {\displaystyle y} by an edge ( y , w ) {\displaystyle (y,w)} that is labeled S [ A [ i ] + H [ i + 1 ] , A [ i ] + d ( w ) 1 ] {\displaystyle S+H,A+d(w)-1]} . The new label consists of the remaining characters of the deleted edge ( v , w ) {\displaystyle (v,w)} that were not used as the label of edge ( v , y ) {\displaystyle (v,y)} .
  4. Add A [ i + 1 ] {\displaystyle A} as a new leaf x {\displaystyle x} and connect it to the new internal node y {\displaystyle y} by an edge ( y , x ) {\displaystyle (y,x)} that is labeled S [ A [ i + 1 ] + H [ i + 1 ] , n ] {\displaystyle S+H,n]} . Thus the edge label consists of the remaining characters of suffix A [ i + 1 ] {\displaystyle A} that are not already represented by the concatenation of the labels of the root-to- v {\displaystyle v} path.
  5. This creates the partial suffix tree S T i + 1 {\displaystyle ST_{i+1}} .

A simple amortization argument shows that the running time of this algorithm is bounded by O ( n ) {\displaystyle O(n)} :

The nodes that are traversed in step i {\displaystyle i} by walking up the rightmost path of S T i {\displaystyle ST_{i}} (apart from the last node v {\displaystyle v} ) are removed from the rightmost path, when A [ i + 1 ] {\displaystyle A} is added to the tree as a new leaf. These nodes will never be traversed again for all subsequent steps j > i {\displaystyle j>i} . Therefore, at most 2 n {\displaystyle 2n} nodes will be traversed in total.

LCP queries for arbitrary suffixes

The LCP array H {\displaystyle H} only contains the length of the longest common prefix of every pair of consecutive suffixes in the suffix array A {\displaystyle A} . However, with the help of the inverse suffix array A 1 {\displaystyle A^{-1}} ( A [ i ] = j A 1 [ j ] = i {\displaystyle A=j\Leftrightarrow A^{-1}=i} , i.e. the suffix S [ j , n ] {\displaystyle S} that starts at position j {\displaystyle j} in S {\displaystyle S} is stored in position A 1 [ j ] {\displaystyle A^{-1}} in A {\displaystyle A} ) and constant-time range minimum queries on H {\displaystyle H} , it is possible to determine the length of the longest common prefix of arbitrary suffixes in O ( 1 ) {\displaystyle O(1)} time.

Because of the lexicographic order of the suffix array, every common prefix of the suffixes S [ i , n ] {\displaystyle S} and S [ j , n ] {\displaystyle S} has to be a common prefix of all suffixes between i {\displaystyle i} 's position in the suffix array A 1 [ i ] {\displaystyle A^{-1}} and j {\displaystyle j} 's position in the suffix array A 1 [ j ] {\displaystyle A^{-1}} . Therefore, the length of the longest prefix that is shared by all of these suffixes is the minimum value in the interval H [ A 1 [ i ] + 1 , A 1 [ j ] ] {\displaystyle H+1,A^{-1}]} . This value can be found in constant time if H {\displaystyle H} is preprocessed for range minimum queries.

Thus given a string S {\displaystyle S} of length n {\displaystyle n} and two arbitrary positions i , j {\displaystyle i,j} in the string S {\displaystyle S} with A 1 [ i ] < A 1 [ j ] {\displaystyle A^{-1}<A^{-1}} , the length of the longest common prefix of the suffixes S [ i , n ] {\displaystyle S} and S [ j , n ] {\displaystyle S} can be computed as follows: LCP ( i , j ) = H [ RMQ H ( A 1 [ i ] + 1 , A 1 [ j ] ) ] {\displaystyle \operatorname {LCP} (i,j)=H+1,A^{-1})]} .

Notes

  1. Kasai et al. 2001.
  2. ^ Abouelhoda, Kurtz & Ohlebusch 2004.
  3. ^ Manber & Myers 1993.
  4. Ohlebusch, Fischer & Gog 2010.
  5. Sadakane 2007.
  6. Fischer, Mäkinen & Navarro 2009.
  7. Crochemore & Ilie 2008.
  8. Crochemore, Ilie & Smyth 2008.
  9. Chen, Puglisi & Smyth 2008.

References

External links

Categories: