ArticleslgStudy

computer science

LCP array

LCP array is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand LCP array rather than just read about it. In short: 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.

LCP array — main illustration
LCP array — illustration

Key takeaways

  • LCP array belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect LCP array to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of LCP array from memory before moving on to harder problems.

Reference excerpt

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, 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 be the suffix array of the string S=s1,s2,…sn−1$ of length n, where $ is a sentinel letter that is unique and lexicographically smaller than any other character. Let S[i,j] denote the substring of S ranging from i to j. Thus, S[A[i],n] is the ith smallest suffix of S. Let lcp⁡(v,w) denote the length of the longest common prefix between two strings v and w. Then the LCP array H[1,n] is an integer array of size n such that H[1] is undefined and H[i]=lcp⁡(S[A[i−1],n],S[A[i],n]) for every 1<i≤n. Thus H[i] stores the length of longest common prefix of the lexicographically ith 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$:

and its corresponding sorted suffix array A :

Suffix array with suffixes written out underneath vertically:

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

So, for example, H[4]=3 is the length of the longest common prefix ana shared by the suffixes A[3]=S[4,7]=ana$ and A[4]=S[2,7]=anana$. Note that H[1] 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(nlog⁡n) time. Kärkkäinen & Sanders (2003) show that it is also possible to modify their O(n) time algorithm such that it computes the LCP array as well. Kasai et al. (2001) present the first 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 13n bytes, while the original output (text, suffix array, LCP array) only occupies 9n bytes. Therefore, Manzini (2004) created a refined version of the algorithm of Kasai et al. (2001) (lcp9) and reduced the space occupancy to 9n bytes. Kärkkäinen, Manzini & Puglisi (2009) 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. Gog & Ohlebusch (2011) provide two algorithms that although being theoretically slow (O(n2)) 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 of length m is a substring of a string S of length n takes O(mlog⁡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) time. Abouelhoda, Kurtz & Ohlebusch (2004) show how to improve this running time even further to achieve optimal 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) time. The longest repeated substring problem for a string S of length n can be solved in Θ(n) time using both the suffix array A and the LCP array. It is sufficient to perform a linear scan through the LCP array in order to find its maximum value vmax and the corresponding index i where vmax is stored. The longest substring that occurs at least twice is then given by S[A[i],A[i]+vmax−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

… excerpt ends here. Continue reading the full article.

Illustrations

LCP array: Case 2 (d(v)<H[i+1]): In order to add suffix nana$, the edge to the previously inserted suffix na$ has to be split up. The new edge to the new internal node is labeled with the longest common prefix of the suffixes na$ and nana$. The edges connecting the two leaves are labeled with the remaining suffix characters that are not part of the prefix.
Case 2 (d(v)<H[i+1]): In order to add suffix nana$, the edge to the previously inserted suffix na$ has to be split up. The new edge to the new internal node is labeled with the longest common prefix of the suffixes na$ and nana$. The edges connecting the two leaves are labeled with the remaining suffix characters that are not part of the prefix.

Worked examples

Example 1 — a first encounter with LCP array

Start with the simplest possible case. Write down what LCP array claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to LCP array before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about LCP array ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of LCP array

In research
LCP array appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses LCP array in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
LCP array is common in secondary-school and first-year university syllabi. It links to neighbouring topics Arrays, String data structures, Substring indices, so understanding it makes those chapters shorter.
In everyday life
Look for LCP array outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “LCP array” →

How to study LCP array in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what LCP array means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain LCP array out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is LCP array in simple terms?

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.

Why does LCP array matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study LCP array?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on LCP array.

Tags

  • Arrays
  • String data structures
  • Substring indices

Keep exploring