Documentation

CslibFrontier.Algorithms.Lean.BurrowsWheeler.Basic

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 #

Main definitions #

Main results #

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.

def Cslib.Algorithms.Lean.BurrowsWheeler.lfDecode {α : Type u_1} [LinearOrder α] (L : List α) :
ℕ → ℕ → List α → List α

lfDecode L k r acc takes k LF steps from row r, prepending the last-column symbol of each visited row to acc ([BW94], Section 2, step D3). It stops early out of range.

The inverse Burrows–Wheeler transform: L.length LF steps from row I ([BW94], Section 2, Algorithm D).

@[simp]
@[simp]

Basic facts #

theorem Cslib.Algorithms.Lean.BurrowsWheeler.pairwise_bwtMatrix {α : Type u_1} [LinearOrder α] (w : List α) :
List.Pairwise (fun (x1 x2 : List α) => x1 ≤ x2) (bwtMatrix w)

Rotating every row of the matrix left by one place permutes its rows.

theorem Cslib.Algorithms.Lean.BurrowsWheeler.filter_head_map_rotate_one {α : Type u_1} [LinearOrder α] (w : List α) (c : α) :
List.map (fun (x : List α) => x.rotate 1) (List.filter (fun (x : List α) => decide (x.head? = some c)) (bwtMatrix w)) = List.filter (fun (x : List α) => decide (x.getLast? = some c)) (bwtMatrix w)

The rows starting with c, each rotated left by one place, are exactly the rows ending in c, in the same order.

Entry r of the last column is the last symbol of row r.

Entry r of the first column is the first symbol of row r.

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

theorem Cslib.Algorithms.Lean.BurrowsWheeler.pairwise_bwtFirst {α : Type u_1} [LinearOrder α] (w : List α) :
List.Pairwise (fun (x1 x2 : α) => x1 ≤ x2) (bwtFirst w)

The first column is the sorted last column ([BW94], Section 2, step D1).

The LF-mapping #

The LF-mapping stays inside the matrix.

LF-mapping, row form ([BW94], Section 2, step D2): row lfMap (bwtLast w) r, rotated left by one place, is row r.

LF-mapping, symbol form: the first symbol of row lfMap (bwtLast w) r is the last symbol of 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.

theorem Cslib.Algorithms.Lean.BurrowsWheeler.lfMap_bwtLast_injOn {α : Type u_1} [LinearOrder α] (w : List α) {r r' : ℕ} (hr : r < (bwtMatrix w).length) (hr' : r' < (bwtMatrix w).length) (h : lfMap (bwtLast w) r = lfMap (bwtLast w) r') :
r = r'

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

theorem Cslib.Algorithms.Lean.BurrowsWheeler.bwtLast_lfMap_step {α : Type u_1} [LinearOrder α] (w : List α) {k r : ℕ} (hk : k < w.length) (hr : r < (bwtMatrix w).length) (hrow : (bwtMatrix w)[r] = w.rotate (k + 1)) :

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 #

theorem Cslib.Algorithms.Lean.BurrowsWheeler.lfDecode_bwtLast {α : Type u_1} [LinearOrder α] (w : List α) (k : ℕ) :
k ≤ w.length → ∀ (r : ℕ) (hr : r < (bwtMatrix w).length), (bwtMatrix w)[r] = w.rotate k → lfDecode (bwtLast w) k r (List.drop k w) = w

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.