6. Burrows-Wheeler Transform
A magical rearrangement
What if you could rearrange the characters in your data so that identical characters cluster together — and then perfectly undo that rearrangement to get back the original? That sounds impossible, but it's exactly what the Burrows-Wheeler Transform does.
Published by Michael Burrows and David Wheeler in 1994, the BWT doesn't compress data itself. It transforms data into a form that's dramatically easier to compress with simple techniques like RLE and Huffman coding. It's the core idea behind bzip2.
The Forward Transform — Step by Step
Let's transform the string BANANA$ (the $ is a special
end-of-string marker that sorts before all other characters).
-
Write out ALL rotations of the string
Take the string and rotate it one position at a time, wrapping characters from the end to the beginning:
Rotation 0: B A N A N A $ Rotation 1: A N A N A $ B Rotation 2: N A N A $ B A Rotation 3: A N A $ B A N Rotation 4: N A $ B A N A Rotation 5: A $ B A N A N Rotation 6: $ B A N A N A -
Sort all rotations alphabetically
(Remember:
$sorts before all letters)Row 0: $ B A N A N A Row 1: A $ B A N A N Row 2: A N A $ B A N Row 3: A N A N A $ B Row 4: B A N A N A $ ← original string (row 4) Row 5: N A $ B A N A Row 6: N A N A $ B A -
Take the LAST column
Reading down the last column of the sorted table:
Row 0: $ B A N A N [A] Row 1: A $ B A N A [N] Row 2: A N A $ B A [N] Row 3: A N A N A $ [B] Row 4: B A N A N A [$] Row 5: N A $ B A N [A] Row 6: N A N A $ B [A]Last column: A N N B $ A A
BWT("BANANA$") = "ANNB$AA"
Look at the clustering!
The original string has its characters spread out: B-A-N-A-N-A-$. But in the BWT output, the A's have clustered together ("AA" at the end) and the N's are adjacent ("NN"). This clustering is exactly what makes the output easy to compress — RLE can now encode those runs efficiently.
We also need to record that the original string was at row 4 in
the sorted table. This index (or equivalently, the position of the $
marker) is needed to reverse the transform.
Why Does Clustering Happen?
This isn't a coincidence — it's a deep structural property. Think about it this way:
- The sorted table groups rotations by what they start with
- Rotations that start with the same characters are adjacent rows
- The last column tells you what comes just before each rotation's starting point in the original string
- So if many positions in the text are preceded by the same character, those entries cluster in the last column
A concrete example with English
In English, the letter 'h' is very often preceded by 't' (in "the", "that", "this", "them", "then", "there", ...). In the sorted rotation table, all rotations starting with "h..." would be grouped together, and the last column for those rows would be mostly 't'. Similarly, 'u' is very often preceded by 'q', 'n' often by 'i' (in "ing", "tion", "ion"), and so on.
Natural language has very strong contextual patterns — certain characters consistently appear before certain others. BWT exposes and clusters these patterns.
The Inverse Transform — Getting the Original Back
Here's the remarkable part: we can reconstruct the original string from just the last column and the row index. We don't need the whole sorted table — just that one column.
The key property
We already know the first column too! It's just the last column sorted alphabetically. (The first column of the sorted rotation table is, by definition, sorted.)
-
We have the last column: A N N B $ A A
Sort it to get the first column:
First column: $ A A A B N N Last column: A N N B $ A A -
Pair them up — we know the first and last character of each row
Row 0: $ ... A Row 1: A ... N Row 2: A ... N Row 3: A ... B Row 4: B ... $ Row 5: N ... A Row 6: N ... A -
Use the LF mapping (Last-to-First mapping)
This is the crucial insight. The i-th occurrence of a character in the last column corresponds to the i-th occurrence of that same character in the first column. This is because sorting is stable — it preserves the relative order of equal elements.
Let's label each character with its occurrence number:
First column: $₁ A₁ A₂ A₃ B₁ N₁ N₂ Last column: A₁ N₁ N₂ B₁ $₁ A₂ A₃A₁ in the last column maps to A₁ in the first column (row 1).
N₁ in the last column maps to N₁ in the first column (row 5).
And so on. -
Follow the chain starting from the row containing $
The original string ends with $. Start from the row where $ is in the first column — row 0.
Start at row 0: first=$₁, last=A₁ → A₁ is in first column at row 1 Row 1: first=A₁, last=N₁ → N₁ is in first column at row 5 Row 5: first=N₁, last=A₂ → A₂ is in first column at row 2 Row 2: first=A₂, last=N₂ → N₂ is in first column at row 6 Row 6: first=N₂, last=A₃ → A₃ is in first column at row 3 Row 3: first=A₃, last=B₁ → B₁ is in first column at row 4 Row 4: first=B₁, last=$₁ → $₁ is in first column at row 0 — we're back to the start! -
Read off the first-column characters in chain order
$ → A → N → A → N → A → B → (back to $)Drop the leading $ and read: the chain gives us the string starting from $:
$ANANAB. Rotating to put $ at the end: BANANA$ ✓
BWT in bzip2 — The Full Pipeline
The bzip2 compressor, created by Julian Seward in 1996, uses BWT as the core of a beautiful four-stage pipeline:
Input → [BWT] → [Move-to-Front] → [RLE] → [Huffman] → Output
Each stage makes the data easier for the next stage:
-
BWT — cluster similar characters
Rearranges the data so repeated contexts produce runs of the same character.
-
Move-to-Front (MTF) — convert to small numbers
Maintain a list of all symbols (initially in order: A, B, C, ...). For each input character, output its position in the list, then move it to the front of the list.
After BWT, characters come in runs (AAAABBCC...). The first 'A' might output position 65 (its initial position), but then A is at the front, so the next three A's all output 0. When B appears, it outputs some small number, then it goes to the front, and the next B outputs 0.
Result: the MTF output is dominated by zeros and small numbers.
-
RLE — compress the runs of zeros
All those consecutive zeros from MTF get compressed by run-length encoding.
-
Huffman coding — encode the final result
The small numbers get short Huffman codes; the rare large numbers get longer codes.
Each stage is simple on its own, but together they achieve excellent compression — bzip2 typically compresses 10–20% better than gzip on text.
BWT vs LZ77 — Different Philosophies
| LZ77 (gzip) | BWT (bzip2) | |
|---|---|---|
| Approach | Find and replace repeated sequences | Transform to cluster, then compress |
| Memory | Streaming — bounded by window size | Block-based — entire block must fit in memory |
| Compression | Good | Better (especially on text) |
| Compress speed | Fast | Moderate |
| Decompress speed | Very fast | Moderate |
| Block size | 32 KB window | 100–900 KB blocks |
Connection to Suffix Arrays and Bioinformatics
The sorted rotations in BWT are closely related to suffix arrays — a fundamental data structure in string algorithms. In fact, BWT can be computed efficiently by first building a suffix array (which can be done in O(n) time).
This connection has a surprising application: the FM-index, which uses the BWT to create a compact, searchable index of a text. You can search for any pattern in O(m) time (where m is the pattern length) using a structure that's roughly the same size as the compressed text.
This makes BWT invaluable in bioinformatics: tools like BWA and Bowtie use BWT-based indices to align DNA sequencing reads against a reference genome — searching billions of base pairs in seconds.
Try It Yourself — Burrows-Wheeler Transform
Type a string to see the BWT in action. A $ sentinel is appended
automatically if you don't include one. Switch between the forward transform
(rotations, sorting, last column) and the inverse (LF-mapping chain that
recovers the original). Watch how the BWT output clusters repeated characters
compared to the original.
All Rotations
Sorted Rotation Table
BWT Output (Last Column)
Inverse Transform
Statistics
Key Takeaways
- BWT rearranges data so that characters appearing in similar contexts cluster together
- The transform is perfectly reversible using just the last column and the row index
- It doesn't compress by itself — it makes data dramatically more compressible
- Core of bzip2: BWT → Move-to-Front → RLE → Huffman
- Related to suffix arrays; enables the FM-index for fast string searching
- Used in bioinformatics for DNA sequence alignment
- Conceptually beautiful: sort all rotations, take the last column — and it's reversible
BWT improves compression by transforming data before encoding. But we've been living with a limitation in our entropy coding: Huffman coding assigns an integer number of bits per symbol. What if a symbol's ideal cost is a fraction of a bit? Arithmetic coding breaks through that barrier.