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 {\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[i,j]} denote the substring of S {\displaystyle S} ranging from i {\displaystyle i} to j {\displaystyle j} . Thus, S [ A [ i ] , n ] {\displaystyle S[A[i],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[1,n]} is an integer array of size n {\displaystyle n} such that H [ 1 ] {\displaystyle H[1]} is undefined and H [ i ] = lcp ⁡ ( S [ A [ i − 1 ] , n ] , S [ A [ i ] , n ] ) {\displaystyle H[i]=\operatorname {lcp} (S[A[i-1],n],S[A[i],n])} for every 1 < i ≤ n {\displaystyle 1<i\leq n} . Thus H [ i ] {\displaystyle H[i]} 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\$}}} :

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

Suffix array with suffixes written out underneath vertically:

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

So, for example, H [ 4 ] = 3 {\displaystyle H[4]=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[3]=S[4,7]={\textrm {ana\$}}} and A [ 4 ] = S [ 2 , 7 ] = anana$ {\displaystyle A[4]=S[2,7]={\textrm {anana\$}}} . Note that H [ 1 ] {\displaystyle H[1]} is undefined, since there is no lexicographically smaller suffix.

… excerpt ends here. Continue reading the full article.

Illustrations

LCP array: Case 2 (
  
    
      
        d
        (
        v
        )
        <
        H
        [
        i
        +
        1
        ]
      
    
    {\displaystyle d(v)<H[i+1]}
  
): 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.
Case 2 ( d ( v ) < H [ i + 1 ] {\displaystyle d(v)<H[i+1]} ): 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.

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.

Affiliate

Preply — study more efficiently by working with a personal tutor. 50% off.

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