On this page
Search engines, plagiarism checkers and DNA aligners all face the same question: where does this pattern occur in a very long text? A suffix array answers it with nothing more than a sorted list of numbers, and you can build one by hand.
What is a suffix array?
A suffix array is a sorted array of all suffixes of a string, stored as the starting position of each suffix (Wikipedia: Suffix array). The same page says Udi Manber and Gene Myers introduced it in 1990 as a simple, space-efficient alternative to suffix trees, and that Gaston Gonnet had found the idea independently in 1987 under the name PAT array. Their 1993 journal paper reports that in practice suffix arrays use three to five times less space than suffix trees.
How do you build the suffix array of BANANA?
Number the letters from 0: B0 A1 N2 A3 N4 A5. Write out every suffix, then sort the suffixes alphabetically:
A (starts at 5)
ANA (starts at 3)
ANANA (starts at 1)
BANANA (starts at 0)
NA (starts at 4)
NANA (starts at 2)
Keep only the starting positions and you have the suffix array: [5, 3, 1, 0, 4, 2]. It matches the banana example on Wikipedia's suffix array page (that page adds an end marker $ and counts from 1). It's six integers, and the text itself is never copied.
How do you find a pattern with binary search?
Every occurrence of a pattern is the start of some suffix. Because the suffixes are sorted, all suffixes that begin with the pattern sit next to each other, and binary search finds that block.
Take the pattern NA. Compare it with the middle suffix, BANANA: NA comes later, so drop the top half. Both suffixes left, NA and NANA, start with NA, so NA occurs at positions 4 and 2.
Finding a pattern of length m in a text of length n this way takes O(m log n) time, because each of the log n comparisons can read m characters (Wikipedia). Manber and Myers cut this to O(m + log n) using longest-common-prefix information (Manber and Myers, 1993).
What does the LCP array tell you?
The LCP array stores the length of the longest common prefix of each pair of neighbouring suffixes in the sorted order (Wikipedia: LCP array). For BANANA:
A and ANA share A: 1
ANA and ANANA share ANA: 3
ANANA and BANANA share nothing: 0
BANANA and NA: 0
NA and NANA share NA: 2
So the LCP array is [–, 1, 3, 0, 0, 2]. A substring that appears twice is a prefix of two different suffixes, and sorting puts suffixes with long shared prefixes next to each other. So the largest LCP value gives the longest repeated substring: 3, which is ANA. The two copies overlap, at positions 1 and 3. The LCP array page says this takes one linear scan once the suffix and LCP arrays exist, and that Kasai and colleagues gave the first linear-time way to compute the LCP array (Kasai et al., 2001).
Why is naive construction O(n² log n), and how do you get O(n log n)?
The naive method passes all n suffixes to an ordinary sort. That sort needs O(n log n) comparisons (the reason is in our post on why comparison sorting needs n log n comparisons). But comparing two suffixes can read up to n letters, so the total cost is O(n² log n) (Wikipedia).
The faster idea, prefix doubling, sorts the suffixes by their first 1 letter, then their first 2, then 4, reusing each round's ranks. Here it is on BANANA:
Sort by 1 letter: A-suffixes {5, 1, 3}, then B {0}, then N {2, 4}.
Sort by 2 letters, using pairs of ranks: A on its own (5), then AN (1, 3, tied), BA (0), NA (2, 4, tied).
Sort by 4 letters: ANA beats ANAN, so 3 comes before 1, and NA beats NANA, so 4 comes before 2. No ties are left: [5, 3, 1, 0, 4, 2].
Each round sorts pairs of small integer ranks, which a radix sort does in linear time, and there are about log n rounds. That gives the O(n log n) construction Manber and Myers report (1993). Kärkkäinen, Sanders and Burkhardt later published a linear-time algorithm, DC3 (Journal of the ACM, 2006).
Hash tables and their load factor give fast exact-key lookups, but cannot list every place a substring occurs. If sorting or binary search feels shaky, the Fermi Concept Library gives every concept in mathematics and computing its own lesson.
How are suffix arrays used in genome search?
A DNA sequencer produces millions of short reads, and each has to be found in a reference genome. Wikipedia's suffix array page notes that for a human genome of about 3.4 billion letters, a plain suffix array would take about 16 times more memory than the genome itself. The page says this gap motivated a trend towards compressed indexes based on the Burrows–Wheeler transform, which can be computed from the suffix array.
Bowtie used Burrows–Wheeler indexing to align more than 25 million reads per CPU hour with about 1.3 GB of memory for the human genome (Langmead et al., Genome Biology, 2009). BWA used backward search on the transform for short-read alignment (Li and Durbin, Bioinformatics, 2009).
For how repeats relate to compression, read our piece on Kolmogorov complexity and incompressible strings. For worked problems with a step-by-step solver that explains the reasoning, try the Fermi Problem Library.



