Short answer: A suffix array is the starting positions of all suffixes of a string, sorted in lexicographic order. Prefix doubling sorts those suffixes by the first character, then by the first 2, 4, 8, … characters, using the ranks from the previous round as a pair key. Each round is a radix sort of pairs, so the construction is O(n log n). The array answers pattern search by binary search in O(m log n) comparisons, and with the LCP array it answers many string queries.
What the problem actually asks
A suffix tree stores the same information as a compacted trie of every suffix and is harder to implement. The suffix array stores only the sorted order of the starting indices. Building it by sorting the suffixes with a general string sort can be O(n² log n) character comparisons. Prefix doubling never compares two long suffixes directly. It reuses the order computed for half the length.
After the ranks of all substrings of length 2^k are known, the order of length 2^{k+1} is the order of the pairs (rank at i, rank at i+2^k). Positions near the end pad the missing half with a sentinel rank that is smaller than every real rank.
What the algorithm guarantees
The finished ranks are the lexicographic order of the suffixes, and the suffix array SA lists starting positions in that order. Time is O(n log n) with a linear radix sort of the pairs at each of O(log n) rounds. Extra memory is a handful of integer arrays of length n. Pattern search binary-searches SA, comparing the pattern with the suffix at the midpoint, in O(m log n) character time. The LCP array, built afterward by Kasai’s algorithm, drops many of those comparisons and supports longest common extension.
Linear-time constructions exist: SA-IS and DC3/skew. They are more code and they are the right citation when the bound must be O(n). Prefix doubling is the construction whose invariant is easy to check. A suffix array is not a permutation of characters. It is a permutation of starting indices.
The working steps
- Rank the suffixes by their first character. Equal characters share a rank. Store the inverse: the rank of the suffix that starts at i.
- For k = 1, 2, 4, … while k < n, sort starting positions by the pair (rank[i], rank[i+k] if i+k < n else a sentinel).
- Reassign dense ranks from that sorted order. Two positions that received the same pair receive the same rank.
- Stop when every rank is unique, or when k reaches n. SA is the list of positions in rank order.
- To find a pattern, binary-search SA. At each midpoint, compare the pattern with s[SA[mid]..]. The lower and upper bounds of equal comparisons are the match range.
The O(n log n) bound uses radix sort of integer pairs, not comparison sort of strings. DC3 and SA-IS are the linear alternatives. The binary search finds exact pattern occurrences. Approximate search and longest common substrings want the LCP array next to SA. Kasai’s algorithm builds that array in linear time from the finished suffix array.
Approaches that look simpler and fail
| Approach | Why it fails when a round sorts raw substrings instead of ranks |
|---|---|
| Compare suffixes by scanning characters in every round | The result can be correct and the cost returns to quadratic character work. The pair of old ranks is the constant-time key. |
| Forget the sentinel when i+k runs off the end | A short suffix can sort after a longer one that shares its prefix. The missing half has to rank below every real character. |
| Binary-search the text instead of SA | The text is not in suffix order. The midpoint has to be an index into SA, and the string compared is the suffix that starts at SA[mid]. |
| Quote O(n) for prefix doubling | Each of the log n rounds is linear. The O(n) constructions are SA-IS and DC3, which use a different recursion. |
The longest-common-prefix array next to this one is Kasai’s algorithm. A compacted trie of the same suffixes is a suffix tree, which Ukkonen’s algorithm builds online. Prefix doubling is the simple suffix-array construction.
A working example
This Python fragment builds a suffix array by prefix doubling. Each pass radix-sorts pairs of ranks and doubles the compared length. A sentinel rank sits past the end of the text. For banana it prints the suffix starts 5 3 1 0 4 2.
def suffix_array(text):
n = len(text)
rank = [ord(ch) for ch in text]
sa = list(range(n))
k = 1
while k < n:
sa.sort(key=lambda i: (rank[i], rank[i + k] if i + k < n else -1))
fresh = [0] * n
for a, b in zip(sa, sa[1:]):
fresh[b] = fresh[a] + ((rank[a], rank[a + k] if a + k < n else -1) < (rank[b], rank[b + k] if b + k < n else -1))
rank = fresh
k *= 2
if rank[sa[-1]] == n - 1:
break
return sa
text = "banana"
sa = suffix_array(text)
assert sa == sorted(range(len(text)), key=lambda i: text[i:])
print(*sa)
Where it is used
Prefix doubling, the Manber-Myers method, builds a suffix array by sorting prefixes whose length doubles each pass. Genome indexes used it before induced sorting (SA-IS) became the usual construction. The suffix array is the same table either way.
When you can drop this algorithm
Drop prefix doubling when a library suffix array or a linear construction is already available and audited. Keep it when an O(n log n) build with an obvious invariant is what you want to implement and prove.
Why this stays published
We publish this so a search for prefix doubling returns the rank-pair sort and the O(n log n) bound that comes from O(log n) radix sorts. Prefix doubling builds a suffix array in O(n log n) time by radix-sorting suffixes on pairs of ranks of half the length.
本文由 HoHo 與 AI 協作整理,最後更新於 2026 年 11 月 13 日。
