A suffix array is a sorted array of all suffixes of a given string.
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.
Naive method to build Suffix Array
O(n2Logn) where n is the number of characters in the input string.
A simple method to construct suffix array is to make an array of all suffixes and then sort the array.
Search a pattern using the built Suffix Array: O(mLogn)
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.
Video: https://www.youtube.com/watch?v=HKPrVm5FWvg
https://courses.cs.ut.ee/MTAT.03.190/2012_fall/uploads/Main/karkkainen_sanders.pdf
http://algs4.cs.princeton.edu/63suffix/
http://web.stanford.edu/class/cs97si/suffix-array.pdf
Read full article from Suffix Array | Set 1 (Introduction) | GeeksforGeeks
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.
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
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
Naive method to build Suffix Array
O(n2Logn) where n is the number of characters in the input string.
A simple method to construct suffix array is to make an array of all suffixes and then sort the array.
// 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 alphabatically
// 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;
}
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.
// A suffix array based search function to search a given pattern
// 'pat' in given text 'txt' using suffix array suffArr[]
void
search(
char
*pat,
char
*txt,
int
*suffArr,
int
n)
{
int
m =
strlen
(pat);
// get length of pattern, needed for strncmp()
// Do simple binary search for the pat in txt using the
// built suffix array
int
l = 0, r = n-1;
// Initilize left and right indexes
while
(l <= r)
{
// See if 'pat' is prefix of middle suffix in suffix array
int
mid = l + (r - l)/2;
int
res =
strncmp
(pat, txt+suffArr[mid], m);
// If match found at the middle, print it and return
if
(res == 0)
{
cout <<
"Pattern found at index "
<< suffArr[mid];
return
;
}
// Move to left half if pattern is alphabtically less than
// the mid suffix
if
(res < 0) r = mid - 1;
// Otherwise move to right half
else
l = mid + 1;
}
// We reach here if return statement in loop is not executed
cout <<
"Pattern not found"
;
}
https://courses.cs.ut.ee/MTAT.03.190/2012_fall/uploads/Main/karkkainen_sanders.pdf
http://algs4.cs.princeton.edu/63suffix/
http://web.stanford.edu/class/cs97si/suffix-array.pdf
Read full article from Suffix Array | Set 1 (Introduction) | GeeksforGeeks