How the Burrows-Wheeler transform sorts rotations

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 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 O(n log n) with a suffix array, or linear with a linear suffix-array construction.


What the problem actually asks

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.

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.


What the algorithm guarantees

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.

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.


The working steps

  1. Append a sentinel that is smaller than every character and appears once. Build the suffix array of the result.
  2. 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.
  3. 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.
  4. 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.
  5. Strip the sentinel. The emitted characters are the original string.

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²) memory and is only a teaching picture.


Approaches that look simpler and fail

Approach Why it fails when the last-to-first map ignores stability
Pair the first c in L with an arbitrary c in F The occurrence order is the matching. The second c in L belongs with the second c in F. Crossing them reconstructs a different string.
Drop the original index The last column’s cycle can be walked from the wrong row. The index selects the row that begins the original string.
Store every rotation as its own string The picture is correct and the memory is quadratic. The suffix array with a sentinel is the linear representation.
Expect the transform alone to shrink the file It reorders bytes. A later entropy coder is what spends fewer bits on the runs.

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.


A working example

This Python fragment takes the last column of the sorted rotations, which is the Burrows–Wheeler transform, and remembers the row of the original string. The inverse is a stable walk from that row. banana with a sentinel transforms and comes back. It prints banana.

def bwt(text):
    s = text + "$"
    rotations = sorted(s[i:] + s[:i] for i in range(len(s)))
    last = "".join(row[-1] for row in rotations)
    row = rotations.index(s)
    return last, row

def inverse(last, row):
    tally = {}
    ranks = []
    for ch in last:
        tally[ch] = tally.get(ch, 0) + 1
        ranks.append(tally[ch])
    starts, acc = {}, 0
    for ch in sorted(tally):
        starts[ch] = acc
        acc += tally[ch]
    lf = [starts[ch] + rank - 1 for ch, rank in zip(last, ranks)]
    chars = [""] * len(last)
    i = row
    for k in range(len(last) - 1, -1, -1):
        chars[k] = last[i]
        i = lf[i]
    return "".join(chars).rstrip("$")

last, row = bwt("banana")
assert inverse(last, row) == "banana"
print(inverse(last, row))

Where it is used

bzip2 sorts rotations with the Burrows-Wheeler transform, and the FM-index behind Bowtie and *BWA* 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.


When you can drop this algorithm

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.


Why this stays published

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. 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.

本文由 HoHo 與 AI 協作整理,最後更新於 2026 年 11 月 13 日。

Leave a Reply

Your email address will not be published. Required fields are marked *