Suffix Array | Set 1 (Introduction)
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 (Source: Wiki)
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}
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.
CPP
// Naive algorithm for building suffix array of a given text #include <iostream> #include <cstring> #include <algorithm> using namespace std; // Structure to store information of a suffix struct suffix { int index; char *suff; }; // A comparison function used by sort() to compare two suffixes int cmp( struct suffix a, struct suffix b) { return strcmp (a.suff, b.suff) < 0? 1 : 0; } // This is the main function that takes a string 'txt' of size n as an // argument, builds and return the suffix array for the given string int *buildSuffixArray( char *txt, int n) { // A structure to store suffixes and their indexes struct suffix suffixes[n]; // Store suffixes and their indexes in an array of structures. // The structure is needed to sort the suffixes alphabetically // and maintain their old indexes while sorting for ( int i = 0; i < n; i++) { suffixes[i].index = i; suffixes[i].suff = (txt+i); } // Sort the suffixes using the comparison function // defined above. sort(suffixes, suffixes+n, cmp); // Store indexes of all sorted suffixes in the suffix array int *suffixArr = new int [n]; for ( int i = 0; i < n; i++) suffixArr[i] = suffixes[i].index; // Return the suffix array return suffixArr; } // A utility function to print an array of given size void printArr( int arr[], int n) { for ( int i = 0; i < n; i++) cout << arr[i] << " " ; cout << endl; } // Driver program to test above functions int main() { char txt[] = "banana" ; int n = strlen (txt); int *suffixArr = buildSuffixArray(txt, n); cout << "Following is suffix array for " << txt << endl; printArr(suffixArr, n); return 0; } |
Java
//importing the required packages import java.util.ArrayList; import java.util.Arrays; class suffix_array { public static void main(String[] args) throws Exception { String word = "banana" ; String arr1[] = new String[word.length()]; String arr2[] = new String[word.length()]; ArrayList<Integer> suffix_index = new ArrayList<Integer>(); int suffix_array[] = new int [word.length()]; for ( int i = 0 ; i < word.length(); i++) { arr1[i] = word.substring(i); } arr2 = arr1.clone(); Arrays.sort(arr1); for (String i : arr1) { String piece = i; int index = new suffix_array().index(arr2, piece); suffix_index.add(index); } for ( int i = 0 ; i < suffix_array.length; i++) { suffix_array[i] = suffix_index.get(i); } System.out.println( "following is the suffix array for banana" ); for ( int i : suffix_array) { System.out.print(i + " " ); } } //simple function to return the index of item from array arr[] int index(String arr[], String item) { for ( int i = 0 ; i < arr.length; i++) { if (item == arr[i]) return i; } return - 1 ; } } |
Output:
Following is suffix array for banana 5 3 1 0 4 2
Time Complexity: O(n*k*Logn). if we consider a O(nLogn)) algorithm used for sorting. The sorting step itself takes O(n*k*Logn) time as every comparison is a comparison of two strings and the comparison takes O(K) time where K is max length of string in given array.
Auxiliary Space: O(n)