Data structures / Suffix arrays

Suffix arrays

How to find any word in a huge text in a few dozen steps: sort every ending of the text once, remember only where each one starts, then search by halving.

Intermediate7 steps~6 min
Text — the letters being indexed: banana
Start position — where a suffix begins in the text
Pattern — the word we are searching for
// THE SHORT VERSION

In 30 seconds

A suffix array is an index for searching inside one long text. Take every suffix (the text from some position to its end), sort them alphabetically, and write down just their starting positions.

Any word you look for is the beginning of some suffix. Because the suffixes are sorted, a binary search finds it in a handful of comparisons, and all its occurrences sit in neighbouring rows.

It stores one number per letter, far less than a suffix tree, which makes it practical for genomes and other giant texts.

Suffix arrays, step by step

STEPS01 / 07

Sizes assume 32-bit positions and a genome of about 3.1 billion letters. Real builders do not sort whole suffixes one by one: algorithms such as SA-IS build the array in linear time.

STEP 01 / 07 Find a word in a text
THE MATH
text = banana pattern = nan scan: try every position
  1. Step 1: Find a word in a text. Where does “nan” occur in “banana”? The plain way lines the pattern up at position 0, then 1, then 2, comparing letters each time. That is fine for six letters and painfully slow for a genome of three billion, searched millions of times.
  2. Step 2: List every suffix. A suffix is the text from some position to the end. “banana” has six, one per starting position. The key fact: any piece of the text is the beginning of one of its suffixes. “nan” is the start of suffix 2, so finding a pattern means finding a suffix that begins with it.
  3. Step 3: Sort the suffixes. Now put the six suffixes in dictionary order, each keeping the number of the position where it starts. A shorter word comes before a longer one that begins the same way, so “a” is first and “nana” last. Sorting is the one-time preparation; searching will reuse it.
  4. Step 4: Keep only the numbers. The suffix array is just that column of start positions. The suffixes themselves are not stored: whenever one is needed, it is read straight from the text, starting at the stored position. Six letters of text cost six numbers.
  5. Step 5: Search by halving. Because the rows are sorted, “nan” can be found like a word in a dictionary. Compare with the middle row: “anana” comes before “nan”, so discard the top half. Then “na” is still too small. The last row, “nana”, begins with “nan”, and its number says where: position 2.
  6. Step 6: All matches sit together. Suffixes that begin with the same letters are neighbours after sorting. Searching for “an” lands on a block of two rows, “ana” and “anana”, so the pattern occurs twice, at positions 3 and 1. Two binary searches find the top and bottom of the block; its height is the number of matches.
  7. Step 7: Small next to a suffix tree. A suffix tree answers the same questions, but stores nodes and pointers: typically 10 to 20 bytes per letter of text. A suffix array is one flat list of integers, 4 bytes each for texts up to 4 GB. The tree can be a little faster per search; the array fits in memory when the tree would not.
VALUES SO FAR
textbananainput
patternnanquery
suffixes····count
SA····index
nan····result
compares····cost
an····result
memory····size

Spotted a mistake on this page?