Generalized suffix array

In computer science, a generalized suffix array (GSA) is a suffix array containing all suffixes for a set of strings. Given the set of strings $$S = S_1, S_2, S_3, ..., S_k$$ of total length $$n$$, it is a lexicographically sorted array of all suffixes of each string in $$S$$. It is primarily used in bioinformatics and string processing.

Functionality
The functionality of a generalized suffix array is as follows:
 * For a collection or set of strings, $$S = S_1, S_2, S_3, ..., S_k$$.
 * It is a lexicographically sorted array of all suffixes of each string in the set $$S$$.
 * In the array, each suffix is represented by an integer pair $$(i, j)$$ which denotes the suffix starting from position $$j$$ in $$s_i$$.
 * In the case where different strings in $$S$$ have identical suffixes, in the generalized suffix array, those suffixes will occupy consecutive positions. However, for convenience, the exception can be made where repeats will not be listed.

A generalized suffix array can be generated for a generalized suffix tree. When compared to a generalized suffix tree, while the generalized suffix array will require more time to construct, it will use less space than the tree.

Construction Algorithms and Implementations
Algorithms and tools for constructing a generalized suffix array include:
 * Fei Shi's (1996) algorithm which runs in $$\mathcal{O}(N \log n)$$ worst case time and $$\mathcal{O}(N)$$ space, where $$N$$ is the sum of the lengths of all strings in $$S$$ and $$n$$ the length of the longest string in $$S$$. This includes sorting, searching and finding the longest common prefixes.
 * The external generalized enhanced suffix array, or eGSA, construction algorithm which specializes in external memory construction, is particularly useful when the size of the input collection or data structure is larger than the amount of available internal memory
 * gsufsort is an open-source, fast, portable and lightweight tool for the construction of generalized suffix arrays and related data structures like Burrows–Wheeler transform or LCP Array)
 * Mnemonist, a collection of data structures implemented in JavaScript contains an implementation for a generalized suffix tree and can be found publicly on npm and GitHub.

Solving the Pattern Matching Problem
Generalized suffix arrays can be used to solve the pattern matching problem:
 * Given a pattern $$P$$ and a text $$T$$, find all occurrences of $$P$$ in $$T$$.
 * Using the generalized suffix array $$G$$ of $$T$$, then first, the suffixes that have $$P$$ as a prefix need to be found.
 * Since $$G$$ is a lexicographically sorted array of the suffixes of $$T$$, then all such suffixes will appear in consecutive positions within $$G$$. Particularly important, since $$G$$ is sorted, it makes identification of suffixes possible and easy using binary search.
 * Using binary search, first find the smallest index $$i$$ in $$G$$ such that $$suff_G[i]$$ contains $$P$$ as a prefix, or determine that no such suffix is present. In the case where the suffix is not found, $$P$$ does not occur in $$T$$. Otherwise, find the largest index $$j(\geq i)$$ which contains $$P$$ as a prefix. The elements in the range $$G[i..j]$$ indicate the starting positions of the occurrences of $$P$$ in $$T$$.
 * Binary search on $$G$$ takes $$\Theta (log n)$$ comparisons. $$P$$ is compared with a suffix to determine their lexicographic order in each comparison that is done. Thus, this requires comparing at most $$|P| = m$$ characters. Note that a $$lcp$$ array is not required, but will offer the benefit of a lower running time.

The runtime of the algorithm is $$\Theta (m log n)$$. By comparison, solving this problem using suffix trees takes $$\Theta (m)$$ time. Note that with a generalized suffix array, the space required is smaller compared to a suffix tree, since the algorithm only requires space for $$n$$ words and the space to store the string. As mentioned above, by optionally keeping track of $$lcp$$ information which will use slightly more space, the running time of the algorithm can be improved to $$\Theta (m+log n)$$.

Other Applications

 * A generalized suffix array can be utilized to compute the longest common subsequence of all the strings in a set or collection. A naive implementation would compute the largest common subsequence of all the strings in the set in $$\Theta (n^2)$$.
 * A generalized suffix array can be utilized to find the longest previous factor array, a concept central to text compression techniques and in the detection of motifs and repeats