# LCP array

LCP array
Type Array
Invented by Manber & Myers (1990)
Time complexity and space complexity
in big O notation
Average Worst case
Space ${\displaystyle {\mathcal {O}}(n)}$ ${\displaystyle {\mathcal {O}}(n)}$
Construction ${\displaystyle {\mathcal {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 := [aab, ab, abaab, b, baab] is a suffix array, the longest common prefix between A[1] = aab and A[2] = ab is a which has length 1, so H[2] = 1 in the LCP array H. Likewise, the LCP of A[2] = ab and A[3] = abaab is ab, so H[3] = 2.

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

## 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.[3] Gene Myers later became the vice president of Informatics Research at Celera Genomics, and Udi Manber the vice president of engineering at Google.

## Definition

Let ${\displaystyle A}$ be the suffix array of the string ${\displaystyle S=s_{1},s_{2},\ldots s_{n}\}$ and let ${\displaystyle \operatorname {lcp} (v,w)}$ denote the length of the longest common prefix between two strings ${\displaystyle v}$ and ${\displaystyle w}$. Let further denote ${\displaystyle S[i,j]}$ the substring of ${\displaystyle S}$ ranging from ${\displaystyle i}$ to ${\displaystyle j}$.

Then the LCP array ${\displaystyle H[1,n]}$ is an integer array of size ${\displaystyle n}$ such that ${\displaystyle H[1]}$ is undefined and ${\displaystyle H[i]=\operatorname {lcp} (S[A[i-1],n],S[A[i],n])}$ for every ${\displaystyle 1. Thus ${\displaystyle H[i]}$ stores the length of longest common prefix of the lexicographically ${\displaystyle i}$'th smallest suffix and its predecessor in the suffix array.

## Example

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

 i S[i] 0 1 2 3 4 5 6 b a n a n a $and its corresponding sorted suffix array ${\displaystyle A}$ :  i A[i] 0 1 2 3 4 5 6 6 5 3 1 0 4 2 Complete suffix array with sorted suffixes itself: i A[i] 1 2 3 4 5 0 1 2 3 4 5 6 6 5 3 1 0 4 2$ a a a b n n $n n a a a a a n$ n $n a a a n$ $a$

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

 i H[i] 0 1 2 3 4 5 6 ⊥ 0 1 3 0 0 2

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

## Difference between suffix array and LCP 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.

## LCP array usage in finding the number of occurrences of a pattern

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

• We use binary search against the suffix array of T to find the starting and end position of all occurrences of 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) 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).

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

At any point during the binary search algorithm, we consider, as usual, a range (L,...,R) of the suffix array and its central point M, and decide whether we continue our search in the left sub-range (L,...,M) or in the right sub-range (M,...,R). In order to make the decision, we compare P to the string at M. If P is identical to M, search is complete. But if not, we have already compared the first k characters of P and then decided whether P is lexicographically smaller or larger than M. Let's assume the outcome is that P is larger than M. So, in the next step, we consider (M,...,R) and a new central point 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)-lookup tells us the longest common prefix of M and M', lcp(M,M').

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

• Case 1: k < lcp(M,M'), i.e. 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 > lcp(M,M'), i.e. P has more prefix characters in common with M than M has in common with M'. Consequently, if we were to compare P to M', the common prefix would be smaller than k, and M' would be lexicographically larger than P, so, without actually making the comparison, we continue in the left half (M,...,M').
• Case 3: k == lcp(M,M'). So M and M' are both identical with P in the first k characters. To decide whether we continue in the left or right half, it suffices to compare P to M' starting from the (k+1)-th character.
• We continue recursively.

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

We still need to precompute LCP-LR so it is able to tell us in 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. lcp(i-1,i) for any i. However, M and M' in the description above are not necessarily consecutive entries.

The key to this is to realize that only certain ranges (L,...,R) will ever occur during the binary search: It always starts with (0,...,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) that can possibly play a role during binary search, and it suffices to precompute lcp(L,M) and lcp(M,R) for those N possible ranges. So that is 2*N distinct precomputed values, hence LCP-LR is O(N) in size.

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

To sum up:

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

## 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 ${\displaystyle O(n\log n)}$ time. Kärkkäinen & Sanders (2003) show that it is also possible to modify their ${\displaystyle O(n)}$ time algorithm such that it computes the LCP array as well. Kasai et al. (2001) present the first ${\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 ${\displaystyle 13n}$ bytes, while the original output (text, suffix array, LCP array) only occupies ${\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 ${\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 (${\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 by Nong, Zhang & Chan (2009).

## 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.[2]

Deciding if a pattern ${\displaystyle P}$ of length ${\displaystyle m}$ is a substring of a string ${\displaystyle S}$ of length ${\displaystyle n}$ takes ${\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 ${\displaystyle O(m+\log n)}$ time.[3] Abouelhoda, Kurtz & Ohlebusch (2004) show how to improve this running time even further to achieve optimal ${\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.[5][6] Furthermore, it can be used together with the suffix array to compute the Lempel-Ziv LZ77 factorization in ${\displaystyle O(n)}$ time.[2][7][8][9]

The longest repeated substring problem for a string ${\displaystyle S}$ of length ${\displaystyle n}$ can be solved in ${\displaystyle \Theta (n)}$ time using both the suffix array ${\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 ${\displaystyle v_{max}}$ and the corresponding index ${\displaystyle i}$ where ${\displaystyle v_{max}}$ is stored. The longest substring that occurs at least twice is then given by ${\displaystyle S[A[i],A[i]+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.

### Suffix tree construction

Given the suffix array ${\displaystyle A}$ and the LCP array ${\displaystyle H}$ of a string ${\displaystyle S=s_{1},s_{2},\ldots s_{n}\}$ of length ${\displaystyle n+1}$, its suffix tree ${\displaystyle ST}$ can be constructed in ${\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 ${\displaystyle ST_{i}}$ be the partial suffix tree for ${\displaystyle 0\leq i\leq n}$. Further let ${\displaystyle d(v)}$ be the length of the concatenation of all path labels from the root of ${\displaystyle ST_{i}}$ to node ${\displaystyle v}$.

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

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

We need to distinguish two cases:

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

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

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

### LCP queries for arbitrary suffixes

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

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

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

## References

• Abouelhoda, Mohamed Ibrahim; Kurtz, Stefan; Ohlebusch, Enno (2004). "Replacing suffix trees with enhanced suffix arrays". Journal of Discrete Algorithms. 2: 53–86. doi:10.1016/S1570-8667(03)00065-0.
• Manber, Udi; Myers, Gene (1993). "Suffix Arrays: A New Method for On-Line String Searches". SIAM Journal on Computing. 22 (5): 935. CiteSeerX 10.1.1.105.6571. doi:10.1137/0222058.
• Kasai, T.; Lee, G.; Arimura, H.; Arikawa, S.; Park, K. (2001). Linear-Time Longest-Common-Prefix Computation in Suffix Arrays and Its Applications. Proceedings of the 12th Annual Symposium on Combinatorial Pattern Matching. Lecture Notes in Computer Science. 2089. pp. 181–192. doi:10.1007/3-540-48194-X_17. ISBN 978-3-540-42271-6.
• Ohlebusch, Enno; Fischer, Johannes; Gog, Simon (2010). CST++. String Processing and Information Retrieval. Lecture Notes in Computer Science. 6393. p. 322. doi:10.1007/978-3-642-16321-0_34. ISBN 978-3-642-16320-3.
• Kärkkäinen, Juha; Sanders, Peter (2003). Simple linear work suffix array construction. Proceedings of the 30th international conference on Automata, languages and programming. pp. 943–955. Retrieved 2012-08-28.
• Fischer, Johannes (2011). Inducing the LCP-Array. Algorithms and Data Structures. Lecture Notes in Computer Science. 6844. pp. 374–385. arXiv:1101.3448. doi:10.1007/978-3-642-22300-6_32. ISBN 978-3-642-22299-3.
• Manzini, Giovanni (2004). Two Space Saving Tricks for Linear Time LCP Array Computation. Algorithm Theory - SWAT 2004. Lecture Notes in Computer Science. 3111. p. 372. doi:10.1007/978-3-540-27810-8_32. ISBN 978-3-540-22339-9.
• Kärkkäinen, Juha; Manzini, Giovanni; Puglisi, Simon J. (2009). Permuted Longest-Common-Prefix Array. Combinatorial Pattern Matching. Lecture Notes in Computer Science. 5577. p. 181. doi:10.1007/978-3-642-02441-2_17. ISBN 978-3-642-02440-5.
• Puglisi, Simon J.; Turpin, Andrew (2008). Space-Time Tradeoffs for Longest-Common-Prefix Array Computation. Algorithms and Computation. Lecture Notes in Computer Science. 5369. p. 124. doi:10.1007/978-3-540-92182-0_14. ISBN 978-3-540-92181-3.
• Gog, Simon; Ohlebusch, Enno (2011). Fast and Lightweight LCP-Array Construction Algorithms (PDF). Proceedings of the Workshop on Algorithm Engineering and Experiments, ALENEX 2011. pp. 25–34. Retrieved 2012-08-28.
• Nong, Ge; Zhang, Sen; Chan, Wai Hong (2009). Linear Suffix Array Construction by Almost Pure Induced-Sorting. 2009 Data Compression Conference. p. 193. doi:10.1109/DCC.2009.42. ISBN 978-0-7695-3592-0.
• Fischer, Johannes; Heun, Volker (2007). A New Succinct Representation of RMQ-Information and Improvements in the Enhanced Suffix Array. Combinatorics, Algorithms, Probabilistic and Experimental Methodologies. Lecture Notes in Computer Science. 4614. p. 459. doi:10.1007/978-3-540-74450-4_41. ISBN 978-3-540-74449-8.
• Chen, G.; Puglisi, S. J.; Smyth, W. F. (2008). "Lempel–Ziv Factorization Using Less Time & Space". Mathematics in Computer Science. 1 (4): 605. doi:10.1007/s11786-007-0024-4.
• Crochemore, M.; Ilie, L. (2008). "Computing Longest Previous Factor in linear time and applications". Information Processing Letters. 106 (2): 75. CiteSeerX 10.1.1.70.5720. doi:10.1016/j.ipl.2007.10.006.
• Crochemore, M.; Ilie, L.; Smyth, W. F. (2008). A Simple Algorithm for Computing the Lempel Ziv Factorization. Data Compression Conference (dcc 2008). p. 482. doi:10.1109/DCC.2008.36. ISBN 978-0-7695-3121-2.
• Sadakane, K. (2007). "Compressed Suffix Trees with Full Functionality". Theory of Computing Systems. 41 (4): 589–607. CiteSeerX 10.1.1.224.4152. doi:10.1007/s00224-006-1198-x.
• Fischer, Johannes; Mäkinen, Veli; Navarro, Gonzalo (2009). "Faster entropy-bounded compressed suffix trees". Theoretical Computer Science. 410 (51): 5354. doi:10.1016/j.tcs.2009.09.012.