The Burrows–Wheeler transform and its inversion by the LF-mapping #
The Burrows–Wheeler transform of a word w over a linearly ordered alphabet sorts all rotations
of w lexicographically (the matrix M), and outputs the last column L of M together with
the index I of the row equal to w ([BW94], Section 2, Algorithm C). It is
inverted by following the LF-mapping T: if L[r] is the k-th occurrence of a symbol c
in L, then T r is the position of the k-th occurrence of c in the first column F
(Algorithm D, step D2). Row T r of M, rotated left by one place, is row r, so the word is
read off backwards from I as w[n - 1 - i] = L[T^[i] I] (step D3).
The LF-mapping is computed from L alone as T r = C[L[r]] + P[r], where C[c] counts the
symbols of L smaller than c, and P[r] counts the occurrences of L[r] in L[0..r-1]
(Section 4.2). The occurrence correspondence holds because F is sorted, and the rows that
start with c, rotated left by one place, are the rows that end in c in the same relative
order. Lexicographic order on equal-length rows is preserved by moving a common first symbol to
the end.
Conventions #
- The alphabet
αis an arbitraryLinearOrder; rows are compared by Mathlib's lexicographic order onList α. No end-of-word sentinel is used. bwtIndex wis the first row of the sorted matrix equal tow, numbered from zero, as in [BW94], Algorithm C, step C1. Periodic words (w = u ^ p) have repeated rows. The transform still determines them, although the sequenceT^[i] (bwtIndex w),i < |w|, then repeats and is not a permutation of the rows (the remark after step D3).- The empty word has the matrix
[[]], the last column[]and the index0.
Main definitions #
bwtMatrix w: the rotations ofw, sorted lexicographically.bwtLast w,bwtFirst w: the last and first columns ofbwtMatrix w.bwtIndex w: the index of the first row ofbwtMatrix wequal tow.bwt w = (bwtLast w, bwtIndex w): the Burrows–Wheeler transform.lfMap L r: the LF-mappingL.countP (· < L[r]) + (L.take r).count L[r], computed from the last column alone.lfDecode L k r acc:kLF steps from rowr, prepending the symbols read.inverseBwt (L, I):L.lengthLF steps from rowI.
Main results #
bwtFirst_eq_mergeSort_bwtLast:Fis the sortedL(step D1).rotate_one_getElem_bwtMatrix_lfMap: rowlfMap (bwtLast w) r, rotated left by one place, is rowr(the LF-mapping, row form).getElem?_bwtFirst_lfMap,count_take_bwtFirst_lfMap:F[lfMap L r] = L[r], and both are the same occurrence of that symbol (the LF-mapping, step D2).lfMap_bwtLast_injOn,map_lfMap_range_perm: the LF-mapping permutes the rows (step D2).getElem?_bwtLast_iterate_lfMap:L[T^[i] I] = w[n - 1 - i](step D3).inverseBwt_bwt:inverseBwt (bwt w) = w, hencebwt_injective.
References #
Order-free facts about rotations #
The transform #
The Burrows–Wheeler matrix: the rotations of w, sorted lexicographically
([BW94], Section 2, step C1).
The last column L of the Burrows–Wheeler matrix ([BW94], Section 2,
step C2).
The first column F of the Burrows–Wheeler matrix ([BW94], Section 2,
step D1).
The index I of the first row of the Burrows–Wheeler matrix equal to w, numbered from
zero ([BW94], Section 2, step C1).
The Burrows–Wheeler transform (L, I) ([BW94], Section 2, Algorithm C).
The LF-mapping T of [BW94], Section 2, step D2, computed from the last
column as in Section 4.2: T r = C[L[r]] + P[r], where C[c] counts the symbols of L
smaller than c, and P[r] counts the occurrences of L[r] before position r. It is the
identity out of range.
The inverse Burrows–Wheeler transform: L.length LF steps from row I
([BW94], Section 2, Algorithm D).
Basic facts #
The rows starting with c, each rotated left by one place, are exactly the rows ending in
c, in the same order.
The first column is a permutation of the word ([BW94], Section 2, step D1).
The last column is a permutation of the word ([BW94], Section 2, step D1).
The LF-mapping #
LF-mapping, row form ([BW94], Section 2, step D2): row
lfMap (bwtLast w) r, rotated left by one place, is row r.
LF-mapping, occurrence form ([BW94], Section 2, step D2): if entry r of
the last column is the k-th occurrence of c there, then entry lfMap (bwtLast w) r of the
first column is the k-th occurrence of c there.
The LF-mapping is one-to-one on the rows ([BW94], Section 2, step D2).
The LF-mapping permutes the rows ([BW94], Section 2, step D2).
One decoding step: if row r holds w.rotate (k + 1), then entry r of the last column is
w[k], and the LF-mapping moves to a row holding w.rotate k.
After i ≤ |w| LF steps from bwtIndex w, the current row holds w.rotate (|w| - i).
Decoding formula ([BW94], Section 2, step D3): w[n - 1 - i] is the
last-column symbol of the row reached by i LF steps from bwtIndex w.
Inversion #
Decoding invariant: from a row holding the rotation w.rotate k, k LF steps prepend
w[0], …, w[k-1] to the accumulator.
Invertibility ([BW94], Section 2, Algorithm D): the LF-mapping decoder recovers the word from its transform.
The Burrows–Wheeler transform, with its index, is injective.