BREAKING NEWS
Suffix array

## Summary

In computer science, a suffix array is a sorted array of all suffixes of a string. It is a data structure used in, among others, full text indices, data compression algorithms, and the field of bibliometrics.

Suffix array
TypeArray
Invented byManber & Myers (1990)
Time complexity
in big O notation
Average Worst case
Space ${\displaystyle {\mathcal {O}}(n)}$ ${\displaystyle {\mathcal {O}}(n)}$
Construction ${\displaystyle {\mathcal {O}}(n)}$ ${\displaystyle {\mathcal {O}}(n)}$

Suffix arrays were introduced by Manber & Myers (1990) as a simple, space efficient alternative to suffix trees. They had independently been discovered by Gaston Gonnet in 1987 under the name PAT array (Gonnet, Baeza-Yates & Snider 1992).

Li, Li & Huo (2016) gave the first in-place ${\displaystyle {\mathcal {O}}(n)}$ time suffix array construction algorithm that is optimal both in time and space, where in-place means that the algorithm only needs ${\displaystyle {\mathcal {O}}(1)}$ additional space beyond the input string and the output suffix array.

Enhanced suffix arrays (ESAs) are suffix arrays with additional tables that reproduce the full functionality of suffix trees preserving the same time and memory complexity.[1] The suffix array for a subset of all suffixes of a string is called sparse suffix array.[2] Multiple probabilistic algorithms have been developed to minimize the additional memory usage including an optimal time and memory algorithm.[3]

## Definition

Let ${\displaystyle S=S[1]S[2]...S[n]}$  be an ${\textstyle n}$ -string and let ${\displaystyle S[i,j]}$  denote the substring of ${\displaystyle S}$  ranging from ${\displaystyle i}$  to ${\displaystyle j}$  inclusive.

The suffix array ${\displaystyle A}$  of ${\displaystyle S}$  is now defined to be an array of integers providing the starting positions of suffixes of ${\displaystyle S}$  in lexicographical order. This means, an entry ${\displaystyle A[i]}$  contains the starting position of the ${\displaystyle i}$ -th smallest suffix in ${\displaystyle S}$  and thus for all ${\displaystyle 1\leq i\leq n}$ : ${\displaystyle S[A[i-1],n] .

Each suffix of ${\displaystyle S}$  shows up in ${\displaystyle A}$  exactly once. Suffixes are simple strings. These strings are sorted (as in a paper dictionary), before their starting positions (integer indices) are saved in ${\displaystyle A}$ .

## Example

Consider the text ${\displaystyle S}$ =banana$ to be indexed:  i ${\displaystyle S[i]}$ 1 2 3 4 5 6 7 b a n a n a$

The text ends with the special sentinel letter $ that is unique and lexicographically smaller than any other character. The text has the following suffixes: Suffix i banana$ 1
anana$2 nana$ 3
ana$4 na$ 5
a$6$ 7

These suffixes can be sorted in ascending order:

Suffix i
$7 a$ 6
ana$4 anana$ 2
banana$1 na$ 5
nana$3 The suffix array ${\displaystyle A}$ contains the starting positions of these sorted suffixes:  i = ${\displaystyle A[i]}$ = 1 2 3 4 5 6 7 7 6 4 2 1 5 3 The suffix array with the suffixes written out vertically underneath for clarity: i = ${\displaystyle A[i]}$ = 1 2 3 4 5 1 2 3 4 5 6 7 7 6 4 2 1 5 3$ a a a b n n $n n a a a a a n$ n $n a a a n$ $a$

So for example, ${\displaystyle A[3]}$  contains the value 4, and therefore refers to the suffix starting at position 4 within ${\displaystyle S}$ , which is the suffix ana$. ## Correspondence to suffix trees Suffix arrays are closely related to suffix trees: • Suffix arrays can be constructed by performing a depth-first traversal of a suffix tree. The suffix array corresponds to the leaf-labels given in the order in which these are visited during the traversal, if edges are visited in the lexicographical order of their first character. • A suffix tree can be constructed in linear time by using a combination of suffix array and LCP array. For a description of the algorithm, see the corresponding section in the LCP array article. It has been shown that every suffix tree algorithm can be systematically replaced with an algorithm that uses a suffix array enhanced with additional information (such as the LCP array) and solves the same problem in the same time complexity.[1] 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.[4] ## Space efficiency Suffix arrays were introduced by Manber & Myers (1990) in order to improve over the space requirements of suffix trees: Suffix arrays store ${\displaystyle n}$ integers. Assuming an integer requires ${\displaystyle 4}$ bytes, a suffix array requires ${\displaystyle 4n}$ bytes in total. This is significantly less than the ${\displaystyle 20n}$ bytes which are required by a careful suffix tree implementation.[5] However, in certain applications, the space requirements of suffix arrays may still be prohibitive. Analyzed in bits, a suffix array requires ${\displaystyle {\mathcal {O}}(n\log n)}$ space, whereas the original text over an alphabet of size ${\displaystyle \sigma }$ only requires ${\displaystyle {\mathcal {O}}(n\log \sigma )}$ bits. For a human genome with ${\displaystyle \sigma =4}$ and ${\displaystyle n=3.4\times 10^{9}}$ the suffix array would therefore occupy about 16 times more memory than the genome itself. Such discrepancies motivated a trend towards compressed suffix arrays and BWT-based compressed full-text indices such as the FM-index. These data structures require only space within the size of the text or even less. ## Construction algorithms A suffix tree can be built in ${\displaystyle {\mathcal {O}}(n)}$ and can be converted into a suffix array by traversing the tree depth-first also in ${\displaystyle {\mathcal {O}}(n)}$ , so there exist algorithms that can build a suffix array in ${\displaystyle {\mathcal {O}}(n)}$ . A naive approach to construct a suffix array is to use a comparison-based sorting algorithm. These algorithms require ${\displaystyle {\mathcal {O}}(n\log n)}$ suffix comparisons, but a suffix comparison runs in ${\displaystyle {\mathcal {O}}(n)}$ time, so the overall runtime of this approach is ${\displaystyle {\mathcal {O}}(n^{2}\log n)}$ . More advanced algorithms take advantage of the fact that the suffixes to be sorted are not arbitrary strings but related to each other. These algorithms strive to achieve the following goals:[6] • minimal asymptotic complexity ${\displaystyle \Theta (n)}$ • lightweight in space, meaning little or no working memory beside the text and the suffix array itself is needed • fast in practice One of the first algorithms to achieve all goals is the SA-IS algorithm of Nong, Zhang & Chan (2009). The algorithm is also rather simple (< 100 LOC) and can be enhanced to simultaneously construct the LCP array.[7] The SA-IS algorithm is one of the fastest known suffix array construction algorithms. A careful implementation by Yuta Mori outperforms most other linear or super-linear construction approaches. Beside time and space requirements, suffix array construction algorithms are also differentiated by their supported alphabet: constant alphabets where the alphabet size is bound by a constant, integer alphabets where characters are integers in a range depending on ${\displaystyle n}$ and general alphabets where only character comparisons are allowed.[8] Most suffix array construction algorithms are based on one of the following approaches:[6] • Prefix doubling algorithms are based on a strategy of Karp, Miller & Rosenberg (1972). The idea is to find prefixes that honor the lexicographic ordering of suffixes. The assessed prefix length doubles in each iteration of the algorithm until a prefix is unique and provides the rank of the associated suffix. • Recursive algorithms follow the approach of the suffix tree construction algorithm by Farach (1997) to recursively sort a subset of suffixes. This subset is then used to infer a suffix array of the remaining suffixes. Both of these suffix arrays are then merged to compute the final suffix array. • Induced copying algorithms are similar to recursive algorithms in the sense that they use an already sorted subset to induce a fast sort of the remaining suffixes. The difference is that these algorithms favor iteration over recursion to sort the selected suffix subset. A survey of this diverse group of algorithms has been put together by Puglisi, Smyth & Turpin (2007). A well-known recursive algorithm for integer alphabets is the DC3 / skew algorithm of Kärkkäinen & Sanders (2003). It runs in linear time and has successfully been used as the basis for parallel[9] and external memory[10] suffix array construction algorithms. Recent work by Salson et al. (2010) proposes an algorithm for updating the suffix array of a text that has been edited instead of rebuilding a new suffix array from scratch. Even if the theoretical worst-case time complexity is ${\displaystyle {\mathcal {O}}(n\log n)}$ , it appears to perform well in practice: experimental results from the authors showed that their implementation of dynamic suffix arrays is generally more efficient than rebuilding when considering the insertion of a reasonable number of letters in the original text. In practical open source work, a commonly used routine for suffix array construction was qsufsort, based on the 1999 Larsson-Sadakane algorithm.[11] This routine has been superseded by Yuta Mori's DivSufSort, "the fastest known suffix sorting algorithm in main memory" as of 2017. It too can be modified to compute an LCP array. It uses a induced copying combined with Itoh-Tanaka.[12] In 2021 a faster implementation of the algorithm was presented by Ilya Grebnov [13] which in average showed 65% performance improvement over DivSufSort implementation on Silesia Corpus.[14] ## Generalized Suffix Array The concept of a suffix array can be extended to more than one string. This is called a generalized suffix array (or GSA), a suffix array that contains all suffixes for a set of strings (for example, ${\displaystyle S=S_{1},S_{2},S_{3},...,S_{k}}$ and is lexicographically sorted with all suffixes of each string.[15] ## Applications The suffix array of a string can be used as an index to quickly locate every occurrence of a substring pattern ${\displaystyle P}$ within the string ${\displaystyle S}$ . Finding every occurrence of the pattern is equivalent to finding every suffix that begins with the substring. Thanks to the lexicographical ordering, these suffixes will be grouped together in the suffix array and can be found efficiently with two binary searches. The first search locates the starting position of the interval, and the second one determines the end position:[citation needed] n = len(S) def search(P: str) -> Tuple[int, int]: """ Return indices (s, r) such that the interval A[s:r] (including the end index) represents all suffixes of S that start with the pattern P. """ # Find starting position of interval l = 0 # in Python, arrays are indexed starting at 0 r = n while l < r: mid = (l + r) // 2 # division rounding down to nearest integer # suffixAt(A[i]) is the ith smallest suffix if P > suffixAt(A[mid]): l = mid + 1 else: r = mid s = l # Find ending position of interval r = n while l < r: mid = (l + r) // 2 if suffixAt(A[mid]).startswith(P): l = mid + 1 else: r = mid return (s, r)  Finding the substring pattern ${\displaystyle P}$ of length ${\displaystyle m}$ in the string ${\displaystyle S}$ of length ${\displaystyle n}$ takes ${\displaystyle {\mathcal {O}}(m\log n)}$ time, given that a single suffix comparison needs to compare ${\displaystyle m}$ characters. Manber & Myers (1990) describe how this bound can be improved to ${\displaystyle {\mathcal {O}}(m+\log n)}$ time using LCP information. The idea is that a pattern comparison does not need to re-compare certain characters, when it is already known that these are part of the longest common prefix of the pattern and the current search interval. Abouelhoda, Kurtz & Ohlebusch (2004) improve the bound even further and achieve a search time of ${\displaystyle {\mathcal {O}}(m)}$ as known from suffix trees. Suffix sorting algorithms can be used to compute the Burrows–Wheeler transform (BWT). The BWT requires sorting of all cyclic permutations of a string. If this string ends in a special end-of-string character that is lexicographically smaller than all other character (i.e.,$), then the order of the sorted rotated BWT matrix corresponds to the order of suffixes in a suffix array. The BWT can therefore be computed in linear time by first constructing a suffix array of the text and then deducing the BWT string: ${\displaystyle BWT[i]=S[A[i]-1]}$ .

Suffix arrays can also be used to look up substrings in example-based machine translation, demanding much less storage than a full phrase table as used in Statistical machine translation.

Many additional applications of the suffix array require the LCP array. Some of these are detailed in the application section of the latter.

## Enhanced Suffix Arrays

Suffix trees are powerful data structures that have wide application in areas of pattern and string matching, indexing and textual statistics. However, it occupies a significant amount of space and thus has a drawback in many real-time applications that requires processing a considerably large amount of data like genome analysis. To overcome this drawback, Enhanced Suffix Arrays were developed that are data structures consisting of suffix arrays and an additional table called the child table that contains the information about the parent-child relationship between the nodes in the suffix tree. The node branching data structure for this tree is a linked list. Enhanced suffix trees are superior in terms of both space efficiency and time complexity and are easy to implement. Moreover, they can be applied to any algorithm that uses a suffix tree by using an abstract concept lcp-interval trees. The time complexity for searching a pattern in an enhanced suffix array is O(m|Σ|).

The suffix array of the string is an array of n integers in the range of 0 to n that represents the n+1 suffixes of the string including the special character #.

The suffix array is composed of two arrays:

1. pos array pos[1,...n]: It represents a sorted list of all S suffixes. Only the initial positions of the suffixes are stored in the array to reduce the space complexity since the suffixes are too large.
2. lcp array lcp[1,...n]: It is an array of n integers that maintains the lengths of the longest common prefix of two consecutive suffixes stored in the pos array.

## Constructing the lcp-interval

For a suffix array of S, the lcp-interval associated with the corresponding node of suffix tree of S can be defined as:

Interval [i,..j], 0 ≤ i ≤ j ≤ n is an lcp-interval of lcp-value, if

1. lcptab[i] < l,

2. lcptab[k] ≥ l for all i + 1 ≤ k ≤ j,

3. lcptab[k] = l for some i + 1 ≤ k ≤ j if i ≠ j and l = n − i + 1 if i = j,

4. lcptab[j + 1] < l.

The length of the longest common prefix of pos[i − 1] and pos[i] is stored in lcp[i],where 2 ≤ i ≤ n. The lcp-interval portrays the same parent-child relationship as that among the associated nodes in the suffix tree of S.This shows that if the corresponding node of [i..j] is a child of the corresponding node of [k..l], a lcp-interval [i..j] is a child interval of another lcp-interval [k..l]. If [k..l] is a child interval of [i..j], a lcp-interval [i..j] is the parent interval of a lcp-interval [k..l].

## Constructing a Child Table

The child table cldtab is composed of three n arrays, up, down and nextlIndex.The information about the edges of the corresponding suffix tree is stored in maintained by the up and down array. The nextlIndexarray stores the links in the linked list used for node branching the suffix tree.

The up, down and nextlIndex array are defined as follows:

1. The element up[i]records the starting index of the longest lcp-second interval’s child interval, which ends at index i-1.
2. The initial index of the second child interval of the longest lcp-interval, starting at index i is stored in the element down[i].
3. If and only if the interval is neither the first child nor the final child of its parent, the element nextlIndex[i] contains the first index of the next sibling interval of the longest lcp-interval, starting at index i.

By performing a bottom-up traversal of the lcp-interval of the tree, the child table can be constructed in linear time. The up/down values and the nextlIndex values can be computed separately by using two distinct algorithms.

## Constructing a Suffix Link Table

The suffix links for an enhanced suffix array can be computed by generating the suffix link interval [1,..,r] for each [i,..j] interval during the preprocessing. The left and right elements l and r of the interval are maintained in the first index of [i,..,j]. The table for this interval ranges from 0 to n. The suffix link table is constructed by the left-to-right breadth-first traversal of the lcp-interval tree. Every time an l-interval is computed, it is added to the list of l-intervals, which is referred to as the l-list. When the lcp-value > 0, for every l-interval[i,..,j] in the list, link[i] is calculated. The interval [l,..,r] is computed by a binary search in(l-1)-list, where l is the largest left boundary amongst all the l-1 intervals. The suffix link interval of [i,..j] is represented by this interval[l,..,r]. The values l and r are ultimately stored in the first index of [i,..,j].

## Notes

1. ^ a b
2. ^
3. ^
4. ^
5. ^
6. ^ a b
7. ^
8. ^
9. ^
10. ^
11. ^ Larsson, N. Jesper; Sadakane, Kunihiko (22 November 2007). "Faster suffix sorting". Theoretical Computer Science. 387 (3): 258–272. doi:10.1016/j.tcs.2007.07.017. ISSN 0304-3975.
12. ^ Fischer, Johannes; Kurpicz, Florian (5 October 2017). "Dismantling DivSufSort". Proceedings of the Prague Stringology Conference 2017. arXiv:1710.01896.
13. ^ "New saca and bwt library (libsais)". encode.su. Retrieved 2021-10-03.
14. ^ Grebnov, Ilya (2021-09-22), libsais, retrieved 2021-10-02
15. ^

## References

• Manber, Udi; Myers, Gene (1990). Suffix arrays: a new method for on-line string searches. First Annual ACM-SIAM Symposium on Discrete Algorithms. pp. 319–327.
• Manber, Udi; Myers, Gene (1993). "Suffix arrays: a new method for on-line string searches". SIAM Journal on Computing. 22 (5): 935–948. doi:10.1137/0222058. S2CID 5074629.
• Gawrychowski, Paweł; Kociumaka, Tomasz (January 2017). "Sparse Suffix Tree Construction in Optimal Time and Space". Proceedings of the Twenty-Eighth Annual ACM-SIAM Symposium on Discrete Algorithms. Philadelphia, PA: Society for Industrial and Applied Mathematics: 425–439. arXiv:1608.00865. doi:10.1137/1.9781611974782.27. ISBN 9781611974782. S2CID 6608776.
• Li, Zhize; Li, Jian; Huo, Hongwei (2016). Optimal In-Place Suffix Sorting. Proceedings of the 25th International Symposium on String Processing and Information Retrieval (SPIRE). Lecture Notes in Computer Science. Vol. 11147. Springer. pp. 268–284. arXiv:1610.08305. doi:10.1007/978-3-030-00479-8_22. ISBN 978-3-030-00478-1.
• Shi, Fei (1996). Suffix arrays for multiple strings: A method for on-line multiple string searches. Lecture Notes in Computer Science. Vol. 1179. Springer Berlin Heidelberg. pp. 11–22. doi:10.1007/BFb0027775. ISBN 978-3-540-62031-0.
• Abouelhoda, Mohamed Ibrahim; Kurtz, Stefan; Ohlebusch, Enno (2002). The Enhanced Suffix Array and Its Applications to Genome Analysis. Algorithms in Bioinformatics. Lecture Notes in Computer Science. Vol. 2452. doi:10.1007/3-540-45784-4_35. ISBN 978-3-540-44211-0.
• Abouelhoda, Mohamed Ibrahim; Kurtz, Stefan; Ohlebusch, Enno (March 2004). "Replacing suffix trees with enhanced suffix arrays". Journal of Discrete Algorithms. 2 (1): 53–86. doi:10.1016/S1570-8667(03)00065-0. ISSN 1570-8667.
• Gonnet, G.H; Baeza-Yates, R.A; Snider, T (1992). "New indices for text: PAT trees and PAT arrays". Information Retrieval: Data Structures and Algorithms.
• Kurtz, S (1999). "Reducing the space requirement of suffix trees". Software: Practice and Experience. 29 (13): 1149–1171. doi:10.1002/(SICI)1097-024X(199911)29:13<1149::AID-SPE274>3.0.CO;2-O. hdl:10338.dmlcz/135448.
• Puglisi, Simon J.; Smyth, W. F.; Turpin, Andrew H. (2007). "A taxonomy of suffix array construction algorithms". ACM Computing Surveys. 39 (2): 4. doi:10.1145/1242471.1242472. S2CID 2653529.
• Nong, Ge; Zhang, Sen; Chan, Wai Hong (2009). Linear Suffix Array Construction by Almost Pure Induced-Sorting. 2009 Data Compression Conference. p. 193. doi:10.1109/DCC.2009.42. ISBN 978-0-7695-3592-0.
• Fischer, Johannes (2011). Inducing the LCP-Array. Algorithms and Data Structures. Lecture Notes in Computer Science. Vol. 6844. p. 374. arXiv:1101.3448. doi:10.1007/978-3-642-22300-6_32. ISBN 978-3-642-22299-3.
• Salson, M.; Lecroq, T.; Léonard, M.; Mouchard, L. (2010). "Dynamic extended suffix arrays". Journal of Discrete Algorithms. 8 (2): 241. doi:10.1016/j.jda.2009.02.007.
• Burkhardt, Stefan; Kärkkäinen, Juha (2003). Fast Lightweight Suffix Array Construction and Checking. Combinatorial Pattern Matching. Lecture Notes in Computer Science. Vol. 2676. p. 55. doi:10.1007/3-540-44888-8_5. ISBN 978-3-540-40311-1.
• Karp, Richard M.; Miller, Raymond E.; Rosenberg, Arnold L. (1972). Rapid identification of repeated patterns in strings, trees and arrays. Proceedings of the fourth annual ACM symposium on Theory of computing - STOC '72. p. 125. doi:10.1145/800152.804905.
• Farach, M. (1997). Optimal suffix tree construction with large alphabets. Proceedings 38th Annual Symposium on Foundations of Computer Science. doi:10.1109/SFCS.1997.646102. ISBN 0-8186-8197-7.
• I, Tomohiro; Kärkkäinen, Juha; Kempa, Dominik (2014). Faster Sparse Suffix Sorting. Leibniz International Proceedings in Informatics (LIPIcs). Vol. 25. Schloss Dagstuhl – Leibniz-Zentrum fuer Informatik. pp. 386–396. doi:10.4230/LIPIcs.STACS.2014.386. ISBN 978-3-939897-65-1.
• Kärkkäinen, Juha; Sanders, Peter (2003). Simple Linear Work Suffix Array Construction. Automata, Languages and Programming. Lecture Notes in Computer Science. Vol. 2719. doi:10.1007/3-540-45061-0_73. ISBN 978-3-540-40493-4.
• Dementiev, Roman; Kärkkäinen, Juha; Mehnert, Jens; Sanders, Peter (2008). "Better external memory suffix array construction". Journal of Experimental Algorithmics. 12: 1–24. doi:10.1145/1227161.1402296. S2CID 12296500.
• Kulla, Fabian; Sanders, Peter (2007). "Scalable parallel suffix array construction". Parallel Computing. 33 (9). doi:10.1016/j.parco.2007.06.004.
• Mohamed Ibrahim Abouelhoda, Stefan Kurtz, and Enno Ohlebusch. "Replacing suffix trees with enhanced suffix arrays." Journal of Discrete Algorithms, 2(1):53–86, 2004.
• Dong Kyue Kim, Jeong Eun Jeon, and Heejin Park. "An efficient index data structure with the capabilities of suffix trees and suffix arrays for alphabets of non-negligible size." String Processing and Information Retrieval Lecture Notes in Computer Science, page138–149, 2004.