# Burrows-Wheeler transform

The Burrows-Wheeler transform (BWT, also called block-sorting compression), is an algorithm used in data compression techniques such as bzip2. It was invented by Michael Burrows and David Wheeler.

When a character string is transformed by the BWT, none of its characters change. It just rearranges the order of the characters. If the original string had several substrings that occurred often, then the transformed string will have several places where a single character is repeated multiple times in a row. This is useful for compression, since it tends to be easy to compress a string that has runs of repeated characters by techniques such as run-length encoding.

For example, the string:

```SIX.MIXED.PIXIES.SIFT.SIXTY.PIXIE.DUST.BOXES
```

transforms into this* string, which is easier to compress because it has many repeated characters:

``` TEXYDST.E.XIIXIXXSMPPSS.B...S.EEUSFXDIOIIIIT
```

The transform is done by sorting all rotations of the text, then taking the last column. For example, the text ".BANANA." is transformed into "BNN.AA.A" through these steps:

Input All
Rotations
Sort the
Lines
Output
`.BANANA.`
```.BANANA.
..BANANA
A..BANAN
NA..BANA
ANA..BAN
NANA..BA
ANANA..B
BANANA..```
```ANANA..B
ANA..BAN
A..BANAN
BANANA..
NANA..BA
NA..BANA
.BANANA.
..BANANA
```
`BNN.AA.A`

See Burrows-Wheeler transform demonstration (http://wikisource.org/wiki/Burrows-Wheeler_transform_demonstration) at Wikisource for a longer example.

The following pseudocode gives a simple, but inefficient, way to calculate the BWT and its inverse. It assumes that there is a special character 'EOF' which is the last character of the text, occurs nowhere else in the text, and is ignored during sorting.

``` function BWT (string s)
create a list of all possible rotations of s
let each rotation be one row in a large, square table
sort the rows of the table alphabetically, treating each row as a string
return the last (rightmost) column of the table

function inverseBWT (string s)
create an empty table with no rows or columns
repeat length(s) times
insert s as a new column down the left side of the table
sort the rows of the table alphabetically
return the row that ends with the 'EOF' character
```

The remarkable thing about the BWT is not that it generates a more easily encoded output - any number of trivial operations would do that - but that it is reversible, allowing the original document to be re-generated from the last column data.

The inverse can be understood this way. Take the final table in the BWT algorithm, and erase all but the last column. Given only this information, you can easily reconstruct the first column. The last column tells you all the characters in the text, so just sort these characters to get the first column. Then, the first and last columns together give you all pairs of characters in the document. Sorting the list of pairs gives the first and second columns. Continuing in this manner, you can reconstruct the entire list. Then, the row with the "end of file" character at the end is the original text.

A number of optimizations can make these algorithms run more efficiently without changing the output. In BWT, there is no need to actually store the table. Each row of the table can be represented by a single pointer into the strings. In inverse BWT there is no need to store the table or to do the multiple sorts. It is sufficient to sort s once with a stable sort, and remember where each character moved. This gives a single-cycle permutation, whose cycle is the output. A "character" in the algorithm can be a byte, or a bit, or any other convenient size.

There is no need to have an actual 'EOF' character. Instead, a pointer can be used that remembers where in a string the 'EOF' would be if it existed. In this approach, the output of the BWT must include both the transformed string, and the final value of the pointer. That means the BWT does expand its input slightly. The inverse transform then shrinks it back down to the original size: it is given a string and a pointer, and returns just a string.

A complete description of the algorithms can be found in Burrows and Wheeler's paper, or in a number of online sources.

Sample implementation (in C, with English comments) is in the Polish Wikipedia article (http://pl.wikipedia.org/wiki/Transformata_Burrowsa-Wheelera).

#### Note on sorting convention

If you sort with Posix collating, you get the slightly different string

```TEXYDST.E.IXIXIXXSSMPPS.B..E.S.EUSFXDIIOIIIT
```

```TEXYDST.E.XIIXIXXSMPPSS.B...S.EEUSFXDIOIIIIT
```

ISO 8859 has complex collating rules, but in this case, periods are ignored. Posix collating treats periods as characters.

## References

• M. Burrows and D. Wheeler. A block sorting lossless data compression algorithm. Technical Report 124, Digital Equipment Corporation, 1994.

• Art and Cultures
• Countries of the World (http://www.academickids.com/encyclopedia/index.php/Countries)
• Space and Astronomy