{"id":271,"date":"2026-11-13T12:36:32","date_gmt":"2026-11-13T04:36:32","guid":{"rendered":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-the-burrows-wheeler-transform-sorts-rotations\/"},"modified":"2026-11-13T21:54:08","modified_gmt":"2026-11-13T13:54:08","slug":"how-the-burrows-wheeler-transform-sorts-rotations","status":"publish","type":"post","link":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-the-burrows-wheeler-transform-sorts-rotations\/","title":{"rendered":"How the Burrows-Wheeler transform sorts rotations"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>Short answer:<\/strong> The <strong>Burrows-Wheeler transform<\/strong> sorts every rotation of a string and emits the last character of each rotation, plus the index of the original string in that sorted list. The output is a permutation that tends to group identical characters, which is why it sits in front of a move-to-front coder and a compressor. The inverse recovers the string from the last column alone, using the stable relationship between the last column and the first column. The transform is <strong>O(n log n)<\/strong> with a suffix array, or linear with a linear suffix-array construction.<\/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\">Sorting rotations looks like an expensive way to permute a string. The reason to do it is the context. The last character of a rotation is the character that precedes the first character. In the sorted list, rotations that share a long prefix are adjacent, so the characters that precede that shared context sit together in the last column. Runs of identical characters are what a later coder compresses.<\/p>\n\n\n<p class=\"wp-block-paragraph\">Invertibility does not require storing the whole sorted matrix. The first column is the last column sorted. A stable sort pairs them. The original string is recovered by jumping from last column to first column, following the stable matching, for n steps.<\/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 pair (last column, original index) determines the string. Construction via a suffix array of the string plus a sentinel is the practical method: the sorted rotations are the sorted suffixes once a sentinel makes every rotation a unique suffix. Time is the cost of the suffix array. Extra memory is that array. The inverse is O(n), building a next-pointer from the stable ranks of the last column and walking n steps from the original index.<\/p>\n\n\n<p class=\"wp-block-paragraph\">The transform itself does not compress. It permutes. Bzip2-style stacks add run-length or move-to-front and a Huffman or arithmetic coder. Quoting a compression ratio for the bare transform confuses the permutation with the coder. FM-indexes use the same last-to-first mapping as a compressed searchable form of the suffix array.<\/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>Append a sentinel that is smaller than every character and appears once. Build the suffix array of the result.<\/li>\n<li>The last column L at rank i is the character just before the suffix SA[i], wrapping around to the sentinel side. The original index is the rank whose suffix starts at position 0.<\/li>\n<li>To invert, compute the first column F by sorting L. Because the sort is stable, the r-th occurrence of character c in L corresponds to the r-th occurrence of c in F.<\/li>\n<li>Build a next array from each L position to the F position of the same occurrence. Start at the original index and emit F characters, or equivalently walk the cycle, for n steps.<\/li>\n<li>Strip the sentinel. The emitted characters are the original string.<\/li>\n<\/ol>\n\n\n<p class=\"wp-block-paragraph\">The permutation is invertible given the index. Without the index, the cycle walk does not know where to start, and several strings can share a last column. Compression ratios belong to the coder placed after the transform. The suffix-array construction assumes the sentinel. Building the matrix of rotations explicitly is O(n\u00b2) memory and is only a teaching picture.<\/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 the last-to-first map ignores stability<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Pair the first c in L with an arbitrary c in F<\/td>\n<td>The occurrence order is the matching. The second c in L belongs with the second c in F. Crossing them reconstructs a different string.<\/td>\n<\/tr>\n<tr>\n<td>Drop the original index<\/td>\n<td>The last column&#8217;s cycle can be walked from the wrong row. The index selects the row that begins the original string.<\/td>\n<\/tr>\n<tr>\n<td>Store every rotation as its own string<\/td>\n<td>The picture is correct and the memory is quadratic. The suffix array with a sentinel is the linear representation.<\/td>\n<\/tr>\n<tr>\n<td>Expect the transform alone to shrink the file<\/td>\n<td>It reorders bytes. A later entropy coder is what spends fewer bits on the runs.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n\n\n<p class=\"wp-block-paragraph\">The suffix array that builds the transform is prefix doubling or SA-IS. The searchable compressed form of the same mapping is an FM-index. The Burrows-Wheeler transform is the last-column permutation and its stable inverse.<\/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 takes the last column of the sorted rotations, which is the Burrows\u2013Wheeler transform, and remembers the row of the original string. The inverse is a stable walk from that row. <code>banana<\/code> with a sentinel transforms and comes back. It prints <code>banana<\/code>.<\/p>\n\n<pre><code class=\"language-python\">def bwt(text):\n    s = text + &quot;$&quot;\n    rotations = sorted(s[i:] + s[:i] for i in range(len(s)))\n    last = &quot;&quot;.join(row[-1] for row in rotations)\n    row = rotations.index(s)\n    return last, row\n\ndef inverse(last, row):\n    tally = {}\n    ranks = []\n    for ch in last:\n        tally[ch] = tally.get(ch, 0) + 1\n        ranks.append(tally[ch])\n    starts, acc = {}, 0\n    for ch in sorted(tally):\n        starts[ch] = acc\n        acc += tally[ch]\n    lf = [starts[ch] + rank - 1 for ch, rank in zip(last, ranks)]\n    chars = [&quot;&quot;] * len(last)\n    i = row\n    for k in range(len(last) - 1, -1, -1):\n        chars[k] = last[i]\n        i = lf[i]\n    return &quot;&quot;.join(chars).rstrip(&quot;$&quot;)\n\nlast, row = bwt(&quot;banana&quot;)\nassert inverse(last, row) == &quot;banana&quot;\nprint(inverse(last, row))\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\"><strong>bzip2<\/strong> sorts rotations with the Burrows-Wheeler transform, and the FM-index behind <strong>Bowtie<\/strong> and *<em>BWA*<\/em> is that transform plus rank counts. Short-read aligners search the index instead of the genome. A suffix array is the related table of the same order.<\/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 an explicit rotation matrix for any string long enough to matter. Build the transform from a suffix array. Keep the transform when a compressor or an index wants the last-to-first permutation, and always store the original row index with the last column.<\/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 the Burrows-Wheeler transform returns the last column of the sorted rotations and the stable last-to-first map that inverts it. <strong>The Burrows-Wheeler transform emits the last character of each sorted rotation, and the original string is recovered by a stable walk from that column back to the first.<\/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: The Burrows-Wheeler transform sorts every rotation of a string and emits the last character of each rotation, plus the index of the original string in that sorted list. The output is a permutation that tends to group identical characters, which is why it sits in front of&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-271","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\/271","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=271"}],"version-history":[{"count":2,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/271\/revisions"}],"predecessor-version":[{"id":670,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/271\/revisions\/670"}],"wp:attachment":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/media?parent=271"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/categories?post=271"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/tags?post=271"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}