LCP array
LCP array | ||
---|---|---|
Type | Array | |
Invented by | Template:Harvtxt | |
Time complexity and space complexity in big O notation | ||
Average | Worst case | |
Space | ||
Construction |
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 between pairs of consecutive suffixes in the sorted suffix array.
Augmenting the suffix array with the LCP array allows to efficiently simulate top-down and bottom-up traversals of the suffix tree,Template:SfnTemplate:Sfn speeds up pattern matching on the suffix arrayTemplate:Sfn and is a prerequisite for compressed suffix trees.Template:Sfn
History
The LCP array was introduced by Udi Manber and Gene Myers alongside the suffix array in order to improve the running time of their string search algorithm.Template:Sfn
Definition
Let be the suffix array of the string and let denote the length of the longest common prefix between two strings and . Let further denote the substring of ranging from to .
Then the LCP array is an integer array of size such that is undefined and for every . Thus stores the length of longest common prefix of the lexicographically 'th smallest suffix and its predecessor in the suffix array.
Example
Template:Left header | i | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
---|---|---|---|---|---|---|---|
Template:Left header | S[i] | b | a | n | a | n | a | $ |
and its corresponding suffix array :
Template:Left header | i | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
---|---|---|---|---|---|---|---|
Template:Left header | A[i] | 7 | 6 | 4 | 2 | 1 | 5 | 3 |
Complete suffix array with suffixes itself :
Template:Left header | i | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
---|---|---|---|---|---|---|---|
Template:Left header | A[i] | 7 | 6 | 4 | 2 | 1 | 5 | 3 |
Template:Left header | 1 | $ | a | a | a | b | n | n |
Template:Left header | 2 | $ | n | n | a | a | a | |
Template:Left header | 3 | a | a | n | $ | n | ||
Template:Left header | 4 | $ | n | a | a | |||
Template:Left header | 5 | a | n | $ | ||||
Template:Left header | 6 | $ | a | |||||
Template:Left header | 7 | $ |
Then the LCP array is constructed by comparing lexicographically consecutive suffixes to determine their longest common prefix:
Template:Left header | i | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
---|---|---|---|---|---|---|---|
Template:Left header | H[i] | 0 | 1 | 3 | 0 | 0 | 2 |
So, for example, is the length of the longest common prefix shared by the suffixes and . Note that , 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.
Template:Harvtxt provide an algorithm to compute the LCP array alongside the suffix array in time. Template:Harvtxt show that it is also possible to modify their time algorithm such that it computes the LCP array as well. Template:Harvtxt present the first 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 bytes, while the original output (text, suffix array, LCP array) only occupies bytes. Therefore Template:Harvtxt created a refined version of the algorithm of Template:Harvtxt (lcp9) and reduced the space occupancy to bytes. Template:Harvtxt provide another refinement of Kasai's algorithm (-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.
Template:Harvtxt provide two algorithms that although being theoretically slow () were faster than the above mentioned algorithms in practice.
As of 2012, the currently fastest linear-time LCP array construction algorithm is due to Template:Harvtxt, which in turn is based on one of the fastest suffix array construction algorithms by Template:Harvtxt.
Applications
As noted by Template:Harvtxt 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.
Template:Harvtxt show how to simulate a bottom-up traversal of the suffix tree using only the suffix array and LCP array. Template:Harvtxt 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. Template:Harvtxt 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.Template:Sfn
Deciding if a pattern of length is a substring of a string of length takes time if only the suffix array is used. By additionally using the LCP information, this bound can be improved to time.Template:Sfn Template:Harvtxt show how to improve this running time even further to achieve optimal 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.Template:SfnTemplate:Sfn Furthermore it can be used together with the suffix array to compute the Lempel-Ziv LZ77 factorization in time. Template:SfnTemplate:SfnTemplate:SfnTemplate:Sfn
The longest repeated substring problem for a string of length can be solved in time using both the suffix array and the LCP array. It is sufficient to perform a linear scan through the LCP array in order to find its maximum value and the corresponding index where is stored. The longest substring that occurs at least twice is then given by .
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 and the LCP array of a string of length , its suffix tree can be constructed in 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 be the partial suffix tree for . Further let be the length of the concatenation of all path labels from the root of to node .
Start with , the tree consisting only of the root. To insert into , walk up the rightmost path beginning at the recently inserted leaf to the root, until the deepest node with is reached.
We need to distinguish two cases:
- : This means that the concatenation of the labels on the root-to- path equals the longest common prefix of suffixes and .
In this case, insert as a new leaf of node and label the edge with . Thus the edge label consists of the remaining characters of suffix that are not already represented by the concatenation of the labels of the root-to- path.
This creates the partial suffix tree .Case 2 (): In order to add suffix , the edge to the previously inserted suffix has to be split up. The new edge to the new internal node is labeled with the longest common prefix of the suffixes and . The edges connecting the two leafs are labeled with the remaining suffix characters that are not part of the prefix.
- : This means that the concatenation of the labels on the root-to- path displays less characters than the longest common prefix of suffixes and and the missing characters are contained in the edge label of 's rightmost edge. Therefore we have to split up that edge as follows:
Let be the child of on 's rightmost path.
- Delete the edge .
- Add a new internal node and a new edge with label . The new label consists of the missing characters of the longest common prefix of and . Thus, the concatenation of the labels of the root-to- path now displays the longest common prefix of and .
- Connect to the newly created internal node by an edge that is labeled . The new label consists of the remaining characters of the deleted edge that were not used as the label of edge .
- Add as a new leaf and connect it to the new internal node by an edge that is labeled . Thus the edge label consists of the remaining characters of suffix that are not already represented by the concatenation of the labels of the root-to- path.
- This creates the partial suffix tree .
A simple amortization argument shows that the running time of this algorithm is bounded by :
The nodes that are traversed in step by walking up the rightmost path of (apart from the last node ) are removed from the rightmost path, when is added to the tree as a new leaf. These nodes will never be traversed again for all subsequent steps . Therefore, at most nodes will be traversed in total.
LCP queries for arbitrary suffixes
The LCP array only contains the length of the longest common prefix of every pair of consecutive suffixes in the suffix array . However, with the help of the inverse suffix array (, i.e. the suffix that starts at position in is stored in position in ) and constant-time range minimum queries on , it is possible to determine the length of the longest common prefix of arbitrary suffixes in time.
Because of the lexicographic order of the suffix array, every common prefix of the suffixes and has to be a common prefix of all suffixes between 's position in the suffix array and 's position in the suffix array . Therefore the length of the longest prefix that is shared by all of these suffixes is the minimum value in the interval . This value can be found in constant time if is preprocessed for range minimum queries.
Thus given a string of length and two arbitrary positions in the string with , the length of the longest common prefix of the suffixes and can be computed as follows: .
Notes
References
- {{#invoke:Citation/CS1|citation
|CitationClass=journal }}
- {{#invoke:Citation/CS1|citation
|CitationClass=journal }}
- {{#invoke:Citation/CS1|citation
|CitationClass=journal }}
- {{#invoke:Citation/CS1|citation
|CitationClass=journal }}
- {{#invoke:Citation/CS1|citation
|CitationClass=journal }}
- {{#invoke:Citation/CS1|citation
|CitationClass=journal }}
- {{#invoke:Citation/CS1|citation
|CitationClass=journal }}
- {{#invoke:Citation/CS1|citation
|CitationClass=journal }}
- {{#invoke:Citation/CS1|citation
|CitationClass=journal }}
- {{#invoke:Citation/CS1|citation
|CitationClass=journal }}
- {{#invoke:Citation/CS1|citation
|CitationClass=journal }}
- {{#invoke:Citation/CS1|citation
|CitationClass=journal }}
- {{#invoke:Citation/CS1|citation
|CitationClass=journal }}
- {{#invoke:Citation/CS1|citation
|CitationClass=journal }}
- {{#invoke:Citation/CS1|citation
|CitationClass=journal }}
- {{#invoke:Citation/CS1|citation
|CitationClass=journal }}
- {{#invoke:Citation/CS1|citation
|CitationClass=journal }}
External links
- Mirror of the ad-hoc-implementation of the code described in Template:Harvtxt
- SDSL: Succinct Data Structure Library - Provides various LCP array implementations, Range Minimum Query (RMQ) support structures and many more succinct data structures
- Bottom-up suffix tree traversal emulated using suffix array and LCP array (Java)
- Text-Indexing project (linear-time construction of suffix trees, suffix arrays, LCP array and Burrows-Wheeler Transform)