{"id":237,"date":"2026-11-13T12:35:31","date_gmt":"2026-11-13T04:35:31","guid":{"rendered":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-prefix-doubling-builds-a-suffix-array\/"},"modified":"2026-11-13T21:53:25","modified_gmt":"2026-11-13T13:53:25","slug":"how-prefix-doubling-builds-a-suffix-array","status":"publish","type":"post","link":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-prefix-doubling-builds-a-suffix-array\/","title":{"rendered":"How prefix doubling builds a suffix array"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>Short answer:<\/strong> A <strong>suffix array<\/strong> is the starting positions of all suffixes of a string, sorted in lexicographic order. <strong>Prefix doubling<\/strong> sorts those suffixes by the first character, then by the first 2, 4, 8, \u2026 characters, using the ranks from the previous round as a pair key. Each round is a radix sort of pairs, so the construction is <strong>O(n log n)<\/strong>. The array answers pattern search by binary search in <strong>O(m log n)<\/strong> comparisons, and with the LCP array it answers many string queries.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>What the problem actually asks<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">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\u00b2 log n) character comparisons. Prefix doubling never compares two long suffixes directly. It reuses the order computed for half the length.<\/p>\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>What the algorithm guarantees<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">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&#8217;s algorithm, drops many of those comparisons and supports longest common extension.<\/p>\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>The working steps<\/strong><\/h4>\n\n<ol class=\"wp-block-list\">\n<li>Rank the suffixes by their first character. Equal characters share a rank. Store the inverse: the rank of the suffix that starts at i.<\/li>\n<li>For k = 1, 2, 4, \u2026 while k &lt; n, sort starting positions by the pair (rank[i], rank[i+k] if i+k &lt; n else a sentinel).<\/li>\n<li>Reassign dense ranks from that sorted order. Two positions that received the same pair receive the same rank.<\/li>\n<li>Stop when every rank is unique, or when k reaches n. SA is the list of positions in rank order.<\/li>\n<li>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.<\/li>\n<\/ol>\n\n\n<p class=\"wp-block-paragraph\">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&#8217;s algorithm builds that array in linear time from the finished suffix array.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Approaches that look simpler and fail<\/strong><\/h4>\n\n<figure class=\"wp-block-table\">\n<table>\n<thead>\n<tr>\n<th>Approach<\/th>\n<th>Why it fails when a round sorts raw substrings instead of ranks<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Compare suffixes by scanning characters in every round<\/td>\n<td>The result can be correct and the cost returns to quadratic character work. The pair of old ranks is the constant-time key.<\/td>\n<\/tr>\n<tr>\n<td>Forget the sentinel when i+k runs off the end<\/td>\n<td>A short suffix can sort after a longer one that shares its prefix. The missing half has to rank below every real character.<\/td>\n<\/tr>\n<tr>\n<td>Binary-search the text instead of SA<\/td>\n<td>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].<\/td>\n<\/tr>\n<tr>\n<td>Quote O(n) for prefix doubling<\/td>\n<td>Each of the log n rounds is linear. The O(n) constructions are SA-IS and DC3, which use a different recursion.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n\n\n<p class=\"wp-block-paragraph\">The longest-common-prefix array next to this one is Kasai&#8217;s algorithm. A compacted trie of the same suffixes is a suffix tree, which Ukkonen&#8217;s algorithm builds online. Prefix doubling is the simple suffix-array construction.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>A working example<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">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 <code>banana<\/code> it prints the suffix starts <code>5 3 1 0 4 2<\/code>.<\/p>\n\n<pre><code class=\"language-python\">def suffix_array(text):\n    n = len(text)\n    rank = [ord(ch) for ch in text]\n    sa = list(range(n))\n    k = 1\n    while k &lt; n:\n        sa.sort(key=lambda i: (rank[i], rank[i + k] if i + k &lt; n else -1))\n        fresh = [0] * n\n        for a, b in zip(sa, sa[1:]):\n            fresh[b] = fresh[a] + ((rank[a], rank[a + k] if a + k &lt; n else -1) &lt; (rank[b], rank[b + k] if b + k &lt; n else -1))\n        rank = fresh\n        k *= 2\n        if rank[sa[-1]] == n - 1:\n            break\n    return sa\n\ntext = &quot;banana&quot;\nsa = suffix_array(text)\nassert sa == sorted(range(len(text)), key=lambda i: text[i:])\nprint(*sa)\n<\/code><\/pre>\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Where it is used<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">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 (<strong>SA-IS<\/strong>) became the usual construction. The suffix array is the same table either way.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>When you can drop this algorithm<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Why this stays published<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">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. <strong>Prefix doubling builds a suffix array in O(n log n) time by radix-sorting suffixes on pairs of ranks of half the length.<\/strong><\/p>\n\n\n<p class=\"wp-block-paragraph\">\u672c\u6587\u7531 HoHo \u8207 AI \u5354\u4f5c\u6574\u7406\uff0c\u6700\u5f8c\u66f4\u65b0\u65bc 2026 \u5e74 11 \u6708 13 \u65e5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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, \u2026 characters, using the ranks from the previous round as a pair key. Each&hellip;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[30,38],"tags":[],"class_list":["post-237","post","type-post","status-publish","format-standard","hentry","category-algorithms","category-strings"],"_links":{"self":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/237","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/comments?post=237"}],"version-history":[{"count":2,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/237\/revisions"}],"predecessor-version":[{"id":636,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/237\/revisions\/636"}],"wp:attachment":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/media?parent=237"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/categories?post=237"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/tags?post=237"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}