Suffix Array : A suffix array is a sorted array of all suffixes of a given string.
Let the given string be “banana”.

0 banana                          5 a
1 anana     Sort the Suffixes     3 ana
2 nana      ---------------->     1 anana  
3 ana        alphabetically       0 banana  
4 na                              4 na   
5 a                               2 nana

The suffix array for “banana” :

suffix[] = {5, 3, 1, 0, 4, 2}

We have discussed Suffix Array and it O(nLogn) construction .

Once Suffix array is built, we can use it to efficiently search a pattern in a text. For example, we can use Binary Search to find a pattern (Complete code for the same is discussed here)

[ad type=”banner”]

LCP Array

The Binary Search based solution discussed here takes O(m*Logn) time where m is length of the pattern to be searched and n is length of the text. With the help of LCP array, we can search a pattern in O(m + Log n) time. For example, if our task is to search “ana” in “banana”, m = 3, n = 5.
LCP Array is an array of size n (like Suffix Array). A value lcp[i] indicates length of the longest common prefix of the suffixes inexed by suffix[i] and suffix[i+1]. suffix[n-1] is not defined as there is no suffix after it.

txt[0..n-1] = “banana”
suffix[] = {5, 3, 1, 0, 4, 2|
lcp[] = {1, 3, 0, 0, 2, 0}

Suffixes represented by suffix array in order are:
{“a”, “ana”, “anana”, “banana”, “na”, “nana”}
lcp[0] = Longest Common Prefix of “a” and “ana” = 1
lcp[1] = Longest Common Prefix of “ana” and “anana” = 3
lcp[2] = Longest Common Prefix of “anana” and “banana” = 0
lcp[3] = Longest Common Prefix of “banana” and “na” = 0
lcp[4] = Longest Common Prefix of “na” and “nana” = 2
lcp[5] = Longest Common Prefix of “nana” and None = 0
How to construct LCP array?
LCP array construction is done two ways:
1) Compute the LCP array as a byproduct to the suffix array (Manber & Myers Algorithm)
2) Use an already constructed suffix array in order to compute the LCP values. (Kasai Algorithm).

There exist algorithms that can construct Suffix Array in O(n) time and therefore we can always construct LCP array in O(n) time. But in the below implementation, a O(n Log n) algorithm is discussed.

[ad type=”banner”]

kasai’s Algorithm

In this article kasai’s Algorithm is discussed. The algorithm constructs LCP array from suffix array and input text in O(n) time. The idea is based on below fact:
Let lcp of suffix beginning at txt[i[ be k. If k is greater than 0, then lcp for suffix beginning at txt[i+1] will be at-least k-1. The reason is, relative order of characters remain same. If we delete the first character from both suffixes, we know that at least k characters will match. For example for substring “ana”, lcp is 3, so for string “na” lcp will be at-least 2. Refer this for proof.

Below is C++ implementation of Kasai’s algorithm.

[pastacode lang=”cpp” manual=”%2F%2F%20C%2B%2B%20program%20for%20building%20LCP%20array%20for%20given%20text%0A%23include%20%3Cbits%2Fstdc%2B%2B.h%3E%0Ausing%20namespace%20std%3B%0A%20%0A%2F%2F%20Structure%20to%20store%20information%20of%20a%20suffix%0Astruct%20suffix%0A%7B%0A%20%20%20%20int%20index%3B%20%20%2F%2F%20To%20store%20original%20index%0A%20%20%20%20int%20rank%5B2%5D%3B%20%2F%2F%20To%20store%20ranks%20and%20next%20rank%20pair%0A%7D%3B%0A%20%0A%2F%2F%20A%20comparison%20function%20used%20by%20sort()%20to%20compare%20two%20suffixes%0A%2F%2F%20Compares%20two%20pairs%2C%20returns%201%20if%20first%20pair%20is%20smaller%0Aint%20cmp(struct%20suffix%20a%2C%20struct%20suffix%20b)%0A%7B%0A%20%20%20%20return%20(a.rank%5B0%5D%20%3D%3D%20b.rank%5B0%5D)%3F%20(a.rank%5B1%5D%20%3C%20b.rank%5B1%5D%20%3F1%3A%200)%3A%0A%20%20%20%20%20%20%20%20%20%20%20(a.rank%5B0%5D%20%3C%20b.rank%5B0%5D%20%3F1%3A%200)%3B%0A%7D%0A%20%0A%2F%2F%20This%20is%20the%20main%20function%20that%20takes%20a%20string%20’txt’%20of%20size%20n%20as%20an%0A%2F%2F%20argument%2C%20builds%20and%20return%20the%20suffix%20array%20for%20the%20given%20string%0Avector%3Cint%3E%20buildSuffixArray(string%20txt%2C%20int%20n)%0A%7B%0A%20%20%20%20%2F%2F%20A%20structure%20to%20store%20suffixes%20and%20their%20indexes%0A%20%20%20%20struct%20suffix%20suffixes%5Bn%5D%3B%0A%20%0A%20%20%20%20%2F%2F%20Store%20suffixes%20and%20their%20indexes%20in%20an%20array%20of%20structures.%0A%20%20%20%20%2F%2F%20The%20structure%20is%20needed%20to%20sort%20the%20suffixes%20alphabatically%0A%20%20%20%20%2F%2F%20and%20maintain%20their%20old%20indexes%20while%20sorting%0A%20%20%20%20for%20(int%20i%20%3D%200%3B%20i%20%3C%20n%3B%20i%2B%2B)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20suffixes%5Bi%5D.index%20%3D%20i%3B%0A%20%20%20%20%20%20%20%20suffixes%5Bi%5D.rank%5B0%5D%20%3D%20txt%5Bi%5D%20-%20’a’%3B%0A%20%20%20%20%20%20%20%20suffixes%5Bi%5D.rank%5B1%5D%20%3D%20((i%2B1)%20%3C%20n)%3F%20(txt%5Bi%20%2B%201%5D%20-%20’a’)%3A%20-1%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F%2F%20Sort%20the%20suffixes%20using%20the%20comparison%20function%0A%20%20%20%20%2F%2F%20defined%20above.%0A%20%20%20%20sort(suffixes%2C%20suffixes%2Bn%2C%20cmp)%3B%0A%20%0A%20%20%20%20%2F%2F%20At%20his%20point%2C%20all%20suffixes%20are%20sorted%20according%20to%20first%0A%20%20%20%20%2F%2F%202%20characters.%20%20Let%20us%20sort%20suffixes%20according%20to%20first%204%0A%20%20%20%20%2F%2F%20characters%2C%20then%20first%208%20and%20so%20on%0A%20%20%20%20int%20ind%5Bn%5D%3B%20%20%2F%2F%20This%20array%20is%20needed%20to%20get%20the%20index%20in%20suffixes%5B%5D%0A%20%20%20%20%2F%2F%20from%20original%20index.%20%20This%20mapping%20is%20needed%20to%20get%0A%20%20%20%20%2F%2F%20next%20suffix.%0A%20%20%20%20for%20(int%20k%20%3D%204%3B%20k%20%3C%202*n%3B%20k%20%3D%20k*2)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%2F%2F%20Assigning%20rank%20and%20index%20values%20to%20first%20suffix%0A%20%20%20%20%20%20%20%20int%20rank%20%3D%200%3B%0A%20%20%20%20%20%20%20%20int%20prev_rank%20%3D%20suffixes%5B0%5D.rank%5B0%5D%3B%0A%20%20%20%20%20%20%20%20suffixes%5B0%5D.rank%5B0%5D%20%3D%20rank%3B%0A%20%20%20%20%20%20%20%20ind%5Bsuffixes%5B0%5D.index%5D%20%3D%200%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20Assigning%20rank%20to%20suffixes%0A%20%20%20%20%20%20%20%20for%20(int%20i%20%3D%201%3B%20i%20%3C%20n%3B%20i%2B%2B)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20If%20first%20rank%20and%20next%20ranks%20are%20same%20as%20that%20of%20previous%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20suffix%20in%20array%2C%20assign%20the%20same%20new%20rank%20to%20this%20suffix%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20(suffixes%5Bi%5D.rank%5B0%5D%20%3D%3D%20prev_rank%20%26%26%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20suffixes%5Bi%5D.rank%5B1%5D%20%3D%3D%20suffixes%5Bi-1%5D.rank%5B1%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20prev_rank%20%3D%20suffixes%5Bi%5D.rank%5B0%5D%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20suffixes%5Bi%5D.rank%5B0%5D%20%3D%20rank%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20else%20%2F%2F%20Otherwise%20increment%20rank%20and%20assign%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20prev_rank%20%3D%20suffixes%5Bi%5D.rank%5B0%5D%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20suffixes%5Bi%5D.rank%5B0%5D%20%3D%20%2B%2Brank%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20ind%5Bsuffixes%5Bi%5D.index%5D%20%3D%20i%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20Assign%20next%20rank%20to%20every%20suffix%0A%20%20%20%20%20%20%20%20for%20(int%20i%20%3D%200%3B%20i%20%3C%20n%3B%20i%2B%2B)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20int%20nextindex%20%3D%20suffixes%5Bi%5D.index%20%2B%20k%2F2%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20suffixes%5Bi%5D.rank%5B1%5D%20%3D%20(nextindex%20%3C%20n)%3F%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20suffixes%5Bind%5Bnextindex%5D%5D.rank%5B0%5D%3A%20-1%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20Sort%20the%20suffixes%20according%20to%20first%20k%20characters%0A%20%20%20%20%20%20%20%20sort(suffixes%2C%20suffixes%2Bn%2C%20cmp)%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F%2F%20Store%20indexes%20of%20all%20sorted%20suffixes%20in%20the%20suffix%20array%0A%20%20%20%20vector%3Cint%3EsuffixArr%3B%0A%20%20%20%20for%20(int%20i%20%3D%200%3B%20i%20%3C%20n%3B%20i%2B%2B)%0A%20%20%20%20%20%20%20%20suffixArr.push_back(suffixes%5Bi%5D.index)%3B%0A%20%0A%20%20%20%20%2F%2F%20Return%20the%20suffix%20array%0A%20%20%20%20return%20%20suffixArr%3B%0A%7D%0A%20%0A%2F*%20To%20construct%20and%20return%20LCP%20*%2F%0Avector%3Cint%3E%20kasai(string%20txt%2C%20vector%3Cint%3E%20suffixArr)%0A%7B%0A%20%20%20%20int%20n%20%3D%20suffixArr.size()%3B%0A%20%0A%20%20%20%20%2F%2F%20To%20store%20LCP%20array%0A%20%20%20%20vector%3Cint%3E%20lcp(n%2C%200)%3B%0A%20%0A%20%20%20%20%2F%2F%20An%20auxiliary%20array%20to%20store%20inverse%20of%20suffix%20array%0A%20%20%20%20%2F%2F%20elements.%20For%20example%20if%20suffixArr%5B0%5D%20is%205%2C%20the%0A%20%20%20%20%2F%2F%20invSuff%5B5%5D%20would%20store%200.%20%20This%20is%20used%20to%20get%20next%0A%20%20%20%20%2F%2F%20suffix%20string%20from%20suffix%20array.%0A%20%20%20%20vector%3Cint%3E%20invSuff(n%2C%200)%3B%0A%20%0A%20%20%20%20%2F%2F%20Fill%20values%20in%20invSuff%5B%5D%0A%20%20%20%20for%20(int%20i%3D0%3B%20i%20%3C%20n%3B%20i%2B%2B)%0A%20%20%20%20%20%20%20%20invSuff%5BsuffixArr%5Bi%5D%5D%20%3D%20i%3B%0A%20%0A%20%20%20%20%2F%2F%20Initialize%20length%20of%20previous%20LCP%0A%20%20%20%20int%20k%20%3D%200%3B%0A%20%0A%20%20%20%20%2F%2F%20Process%20all%20suffixes%20one%20by%20one%20starting%20from%0A%20%20%20%20%2F%2F%20first%20suffix%20in%20txt%5B%5D%0A%20%20%20%20for%20(int%20i%3D0%3B%20i%3Cn%3B%20i%2B%2B)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%2F*%20If%20the%20current%20suffix%20is%20at%20n-1%2C%20then%20we%20don%E2%80%99t%0A%20%20%20%20%20%20%20%20%20%20%20have%20next%20substring%20to%20consider.%20So%20lcp%20is%20not%0A%20%20%20%20%20%20%20%20%20%20%20defined%20for%20this%20substring%2C%20we%20put%20zero.%20*%2F%0A%20%20%20%20%20%20%20%20if%20(invSuff%5Bi%5D%20%3D%3D%20n-1)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20k%20%3D%200%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20continue%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%0A%20%20%20%20%20%20%20%20%2F*%20j%20contains%20index%20of%20the%20next%20substring%20to%0A%20%20%20%20%20%20%20%20%20%20%20be%20considered%20%20to%20compare%20with%20the%20present%0A%20%20%20%20%20%20%20%20%20%20%20substring%2C%20i.e.%2C%20next%20string%20in%20suffix%20array%20*%2F%0A%20%20%20%20%20%20%20%20int%20j%20%3D%20suffixArr%5BinvSuff%5Bi%5D%2B1%5D%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20Directly%20start%20matching%20from%20k’th%20index%20as%0A%20%20%20%20%20%20%20%20%2F%2F%20at-least%20k-1%20characters%20will%20match%0A%20%20%20%20%20%20%20%20while%20(i%2Bk%3Cn%20%26%26%20j%2Bk%3Cn%20%26%26%20txt%5Bi%2Bk%5D%3D%3Dtxt%5Bj%2Bk%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20k%2B%2B%3B%0A%20%0A%20%20%20%20%20%20%20%20lcp%5BinvSuff%5Bi%5D%5D%20%3D%20k%3B%20%2F%2F%20lcp%20for%20the%20present%20suffix.%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20Deleting%20the%20starting%20character%20from%20the%20string.%0A%20%20%20%20%20%20%20%20if%20(k%3E0)%0A%20%20%20%20%20%20%20%20%20%20%20%20k–%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F%2F%20return%20the%20constructed%20lcp%20array%0A%20%20%20%20return%20lcp%3B%0A%7D%0A%20%0A%2F%2F%20Utility%20function%20to%20print%20an%20array%0Avoid%20printArr(vector%3Cint%3Earr%2C%20int%20n)%0A%7B%0A%20%20%20%20for%20(int%20i%20%3D%200%3B%20i%20%3C%20n%3B%20i%2B%2B)%0A%20%20%20%20%20%20%20%20cout%20%3C%3C%20arr%5Bi%5D%20%3C%3C%20%22%20%22%3B%0A%20%20%20%20cout%20%3C%3C%20endl%3B%0A%7D%0A%20%0A%2F%2F%20Driver%20program%0Aint%20main()%0A%7B%0A%20%20%20%20string%20str%20%3D%20%22banana%22%3B%0A%20%0A%20%20%20%20vector%3Cint%3EsuffixArr%20%3D%20buildSuffixArray(str%2C%20str.length())%3B%0A%20%20%20%20int%20n%20%3D%20suffixArr.size()%3B%0A%20%0A%20%20%20%20cout%20%3C%3C%20%22Suffix%20Array%20%3A%20%5Cn%22%3B%0A%20%20%20%20printArr(suffixArr%2C%20n)%3B%0A%20%0A%20%20%20%20vector%3Cint%3Elcp%20%3D%20kasai(str%2C%20suffixArr)%3B%0A%20%0A%20%20%20%20cout%20%3C%3C%20%22%5CnLCP%20Array%20%3A%20%5Cn%22%3B%0A%20%20%20%20printArr(lcp%2C%20n)%3B%0A%20%20%20%20return%200%3B%0A%7D” message=”C++” highlight=”” provider=”manual”/]

 

Output:

Suffix Array : 
5 3 1 0 4 2 

LCP Array : 
1 3 0 0 2 0

Illustration:

txt[]     = "banana",  suffix[]  = {5, 3, 1, 0, 4, 2| 

Suffix array represents
{"a", "ana", "anana", "banana", "na", "nana"}

Inverse Suffix Array would be 
invSuff[] = {3, 2, 5, 1, 4, 0}

LCP values are evaluated in below order

We first compute LCP of first suffix in text which is “banana“. We need next suffx in suffix array to compute LCP (Remember lcp[i] is defined as Longest Common Prefix of suffix[i] and suffix[i+1]). To find the next suffix in suffixArr[], we use SuffInv[]. The next suffix is “na”. Since there is no common prefix between “banana” and “na”, the value of LCP for “banana” is 0 and it is at index 3 in suffix array, so we fill lcp[3] as 0.

Next we compute LCP of second suffix which “anana“. Next suffix of “anana” in suffix array is “banana”. Since there is no common prefix, the value of LCP for “anana” is 0 and it is at index 2 in suffix array, so we fill lcp[2] as 0.

[ad type=”banner”]

Next we compute LCP of third suffix which “nana“. Since there is no next suffix, the value of LCP for “nana” is not defined. We fill lcp[5] as 0.

Next suffix in text is “ana”. Next suffix of “ana” in suffix array is “anana”. Since there is a common prefix of length 3, the value of LCP for “ana” is 3. We fill lcp[1] as 3.

Now we lcp for next suffix in text which is “na“. This is where Kasai’s algorithm uses the trick that LCP value must be at least 2 because previous LCP value was 3. Since there is no character after “na”, final value of LCP is 2. We fill lcp[4] as 2.

Next suffix in text is “a“. LCP value must be at least 1 because previous value was 2. Since there is no character after “a”, final value of LCP is 1. We fill lcp[0] as 1.

We will soon be discussing implementation of search with the help of LCP array and how LCP array helps in reducing time complexity to O(m + Log n).

[ad type=”banner”]