The Burrows-Wheeler Transform and the FM-Index
A suffix array answers pattern queries fast but takes bits, many times the size of the text. The FM-index (Ferragina and Manzini, 2000) is a compressed full-text index: it takes space close to the compressed size of the text, and it can count the occurrences of a pattern of length with rank queries, without the text or the suffix array. It also replaces the text: any substring can be extracted from it. Read aligners in genomics (BWA, Bowtie) index human genomes this way in a few gigabytes.
The Burrows-Wheeler transform
Burrows and Wheeler (1994) defined a reversible permutation of the text. Write all rotations of , sort them, and take the last column . Equivalently, using the suffix array, : the character before each suffix in sorted order (with before the suffix starting at 0).
Sorting groups suffixes that start with the same context, and the characters that precede a common context are often the same ("_th" is usually preceded by space or "e", in "the"), so has long runs and compresses well with move-to-front and entropy coding: that is how bzip2 works.
Reversibility: the LF mapping. Let be the first column (the sorted characters). The -th occurrence of a character in and the -th occurrence of in are the same character of the text: both orders sort the occurrences of by the suffix that follows them. So
maps row to the row of the suffix that starts one position earlier, where is the number of characters smaller than in and counts the 's in . Starting at the row of "" and applying LF repeatedly reads the text backwards.
def suffix_array(s):
n = len(s)
rank, sa, k = [ord(c) for c in s], list(range(n)), 1
while True:
key = lambda i: (rank[i], rank[i + k] if i + k < n else -1)
sa.sort(key=key)
new = [0] * n
for j in range(1, n):
new[sa[j]] = new[sa[j - 1]] + (key(sa[j]) != key(sa[j - 1]))
rank = new
if rank[sa[-1]] == n - 1: return sa
k *= 2
def bwt(s):
sa = suffix_array(s)
return "".join(s[i - 1] for i in sa), sa # s[-1] is the sentinel for i = 0
def inverse_bwt(L):
"""Rebuild the text from its BWT with the LF mapping: LF(j) = C[L[j]] + rank of L[j] in L[0..j-1]."""
C, total = {}, 0
for c in sorted(set(L)):
C[c] = total; total += L.count(c)
seen, LF = {}, []
for c in L:
LF.append(C[c] + seen.get(c, 0)); seen[c] = seen.get(c, 0) + 1
j, out = 0, [] # row 0 is the suffix "quot;
for _ in range(len(L) - 1):
out.append(L[j]); j = LF[j] # L[j] is the character before row j's suffix
return "".join(reversed(out)) + "quot;
s = "bananaquot;
L, sa = bwt(s)
print("sorted rotations (the BWT is the last column):")
for j, i in enumerate(sa):
rot = s[i:] + s[:i]
print(f" {rot} SA[{j}] = {i}")
print("BWT:", L, " inverse:", inverse_bwt(L))
for text in ["abracadabraquot;, "the_theme_of_the_thesis_is_these_themesquot;]:
L, _ = bwt(text)
runs = 1 + sum(L[i] != L[i - 1] for i in range(1, len(L)))
print(f"{text!r}: BWT {L!r}, {runs} runs for {len(text)} characters, inverts: {inverse_bwt(L) == text}")sorted rotations (the BWT is the last column):
$banana SA[0] = 6
a$banan SA[1] = 5
ana$ban SA[2] = 3
anana$b SA[3] = 1
banana$ SA[4] = 0
na$bana SA[5] = 4
nana$ba SA[6] = 2
BWT: annb$aa inverse: banana$
'abracadabra