We strongly recommend to read following post on suffix trees as a pre-requisite for this post.

Pattern Searching | Set 8 (Suffix Tree Introduction)

A suffix array is a sorted array of all suffixes of a given string. The definition is similar to Suffix Tree which is compressed trie of all suffixes of the given text. Any suffix tree based algorithm can be replaced with an algorithm that uses a suffix array enhanced with additional information and solves the same problem in the same time complexity (Source Wiki).
A suffix array can be constructed from Suffix tree by doing a DFS traversal of the suffix tree. In fact Suffix array and suffix tree both can be constructed from each other in linear time.
Advantages of suffix arrays over suffix trees include improved space requirements, simpler linear time construction algorithms (e.g., compared to Ukkonen’s algorithm) and improved cache locality.

Example:

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

So the suffix array for "banana" is {5, 3, 1, 0, 4, 2}
[ad type=”banner”]

Naive method to build Suffix Array
A simple method to construct suffix array is to make an array of all suffixes and then sort the array. Following is implementation of simple method.

[pastacode lang=”c” manual=”%2F%2F%20Naive%20algorithm%20for%20building%20suffix%20array%20of%20a%20given%20text%0A%23include%20%3Ciostream%3E%0A%23include%20%3Ccstring%3E%0A%23include%20%3Calgorithm%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%0A%20%20%20%20char%20*suff%3B%0A%7D%3B%0A%20%0A%2F%2F%20A%20comparison%20function%20used%20by%20sort()%20to%20compare%20two%20suffixes%0Aint%20cmp(struct%20suffix%20a%2C%20struct%20suffix%20b)%0A%7B%0A%20%20%20%20return%20strcmp(a.suff%2C%20b.suff)%20%3C%200%3F%201%20%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%0Aint%20*buildSuffixArray(char%20*txt%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.suff%20%3D%20(txt%2Bi)%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%20Store%20indexes%20of%20all%20sorted%20suffixes%20in%20the%20suffix%20array%0A%20%20%20%20int%20*suffixArr%20%3D%20new%20int%5Bn%5D%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%5Bi%5D%20%3D%20suffixes%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%2F%20A%20utility%20function%20to%20print%20an%20array%20of%20given%20size%0Avoid%20printArr(int%20arr%5B%5D%2C%20int%20n)%0A%7B%0A%20%20%20%20for(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%20to%20test%20above%20functions%0Aint%20main()%0A%7B%0A%20%20%20%20char%20txt%5B%5D%20%3D%20%22banana%22%3B%0A%20%20%20%20int%20n%20%3D%20strlen(txt)%3B%0A%20%20%20%20int%20*suffixArr%20%3D%20buildSuffixArray(txt%2C%20%20n)%3B%0A%20%20%20%20cout%20%3C%3C%20%22Following%20is%20suffix%20array%20for%20%22%20%3C%3C%20txt%20%3C%3C%20endl%3B%0A%20%20%20%20printArr(suffixArr%2C%20n)%3B%0A%20%20%20%20return%200%3B%0A%7D” message=”C” highlight=”” provider=”manual”/]

Output:

Following is suffix array for banana
5 3 1 0 4 2

The time complexity of above method to build suffix array is O(n2Logn) if we consider a O(nLogn) algorithm used for sorting. The sorting step itself takes O(n2Logn) time as every comparison is a comparison of two strings and the comparison takes O(n) time.
There are many efficient algorithms to build suffix array. We will soon be covering them as separate posts.

[ad type=”banner”]

Search a pattern using the built Suffix Array
To search a pattern in a text, we preprocess the text and build a suffix array of the text. Since we have a sorted array of all suffixes, Binary Search can be used to search. Following is the search function. Note that the function doesn’t report all occurrences of pattern, it only report one of them.

[pastacode lang=”c” manual=”%2F%2F%20This%20code%20only%20contains%20search()%20and%20main.%20To%20make%20it%20a%20complete%20running%0A%2F%2F%20above%20code%20or%20see%20http%3A%2F%2Fcode.geeksforgeeks.org%2FoY7OkD%0A%20%0A%2F%2F%20A%20suffix%20array%20based%20search%20function%20to%20search%20a%20given%20pattern%0A%2F%2F%20’pat’%20in%20given%20text%20’txt’%20using%20suffix%20array%20suffArr%5B%5D%0Avoid%20search(char%20*pat%2C%20char%20*txt%2C%20int%20*suffArr%2C%20int%20n)%0A%7B%0A%20%20%20%20int%20m%20%3D%20strlen(pat)%3B%20%20%2F%2F%20get%20length%20of%20pattern%2C%20needed%20for%20strncmp()%0A%20%0A%20%20%20%20%2F%2F%20Do%20simple%20binary%20search%20for%20the%20pat%20in%20txt%20using%20the%0A%20%20%20%20%2F%2F%20built%20suffix%20array%0A%20%20%20%20int%20l%20%3D%200%2C%20r%20%3D%20n-1%3B%20%20%2F%2F%20Initilize%20left%20and%20right%20indexes%0A%20%20%20%20while%20(l%20%3C%3D%20r)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%2F%2F%20See%20if%20’pat’%20is%20prefix%20of%20middle%20suffix%20in%20suffix%20array%0A%20%20%20%20%20%20%20%20int%20mid%20%3D%20l%20%2B%20(r%20-%20l)%2F2%3B%0A%20%20%20%20%20%20%20%20int%20res%20%3D%20strncmp(pat%2C%20txt%2BsuffArr%5Bmid%5D%2C%20m)%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20If%20match%20found%20at%20the%20middle%2C%20print%20it%20and%20return%0A%20%20%20%20%20%20%20%20if%20(res%20%3D%3D%200)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20cout%20%3C%3C%20%22Pattern%20found%20at%20index%20%22%20%3C%3C%20suffArr%5Bmid%5D%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20return%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20Move%20to%20left%20half%20if%20pattern%20is%20alphabtically%20less%20than%0A%20%20%20%20%20%20%20%20%2F%2F%20the%20mid%20suffix%0A%20%20%20%20%20%20%20%20if%20(res%20%3C%200)%20r%20%3D%20mid%20-%201%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20Otherwise%20move%20to%20right%20half%0A%20%20%20%20%20%20%20%20else%20l%20%3D%20mid%20%2B%201%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F%2F%20We%20reach%20here%20if%20return%20statement%20in%20loop%20is%20not%20executed%0A%20%20%20%20cout%20%3C%3C%20%22Pattern%20not%20found%22%3B%0A%7D%0A%20%0A%2F%2F%20Driver%20program%20to%20test%20above%20function%0Aint%20main()%0A%7B%0A%20%20%20%20char%20txt%5B%5D%20%3D%20%22banana%22%3B%20%20%2F%2F%20text%0A%20%20%20%20char%20pat%5B%5D%20%3D%20%22nan%22%3B%20%20%20%2F%2F%20pattern%20to%20be%20searched%20in%20text%0A%20%0A%20%20%20%20%2F%2F%20Build%20suffix%20array%0A%20%20%20%20int%20n%20%3D%20strlen(txt)%3B%0A%20%20%20%20int%20*suffArr%20%3D%20buildSuffixArray(txt%2C%20n)%3B%0A%20%0A%20%20%20%20%2F%2F%20search%20pat%20in%20txt%20using%20the%20built%20suffix%20array%0A%20%20%20%20search(pat%2C%20txt%2C%20suffArr%2C%20n)%3B%0A%20%0A%20%20%20%20return%200%3B%0A%7D%0A” message=”C” highlight=”” provider=”manual”/]

Output:

Pattern found at index 2
The time complexity of the above search function is O(mLogn). There are more efficient algorithms to search pattern once the suffix array is built. In fact there is a O(m) suffix array based algorithm to search a pattern. We will soon be discussing efficient algorithm for search.

[ad type=”banner”]

Applications of Suffix Array
Suffix array is an extremely useful data structure, it can be used for a wide range of problems. Following are some famous problems where Suffix array can be used.
1) Pattern Searching
2) Finding the longest repeated substring
3) Finding the longest common substring
4) Finding the longest palindrome in a string

See this for more problems where Suffix arrays can be used.

This post is a simple introduction. There is a lot to cover in Suffix arrays. We have discussed a O(nLogn) algorithm for Suffix Array construction here. We will soon be discussing more efficient suffix array algorithms.

[ad type=”banner”]