The Wayback Machine - https://web.archive.org/web/20240624014727/https://www.geeksforgeeks.org/burrows-wheeler-data-transform-algorithm/
Open In App

Burrows – Wheeler Data Transform Algorithm

Last Updated : 08 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

What is the Burrows-Wheeler Transform?

The BWT is a data transformation algorithm that restructures data in such a way that the transformed message is more compressible. Technically, it is a lexicographical reversible permutation of the characters of a string. It is first of the three steps to be performed in succession while implementing the Burrows-Wheeler Data Compression algorithm that forms the basis of the Unix compression utility bzip2.

 Why BWT? The main idea behind it. 

The most important application of BWT is found in biological sciences where genomes(long strings written in A, C, T, G alphabets) don’t have many runs but they do have many repeats. The idea of the BWT is to build an array whose rows are all cyclic shifts of the input string in dictionary order and return the last column of the array that tends to have long runs of identical characters. The benefit of this is that once the characters have been clustered together, they effectively have an ordering, which can make our string more compressible for other algorithms like run-length encoding and Huffman Coding. The remarkable thing about BWT is that this particular transform is reversible with minimal data overhead. 

Steps involved in BWT algorithm 

Let’s take the word “banana$” as an example.

  • Step 1: Form all cyclic rotations of the given text.
                                     banana$ 
$ b $banana
a a a$banan
Cyclic rotations ----------> na$bana
n n ana$ban
a nana$ba
anana$b

  • Step 2: The next step is to sort the rotations lexicographically. The ‘$’ sign is viewed as first letter lexicographically, even before ‘a’.
banana$                    $banana
$banana a$banan
a$banan Sorting ana$ban
na$bana ----------> anana$b
ana$ban alphabetically banana$
nana$ba na$bana
anana$b nana$ba

  • Step 3: The last column is what we output as BWT.
BWT(banana$) = annb$aa

Examples:

Input: text = “banana$” Output: Burrows-Wheeler Transform = “annb$aa” Input: text = “abracadabra$” Output: Burrows-Wheeler Transform = “ard$rcaaaabb”

Why last column is considered BWT?

  1. The last column has a better symbol clustering than any other columns.
  2. If we only have BWT of our string, we can recover the rest of the cyclic rotations entirely. The rest of the columns don’t possess this characteristic which is highly important while computing the inverse of BWT.

Why ‘$’ sign is embedded in the text? We can compute BWT even if our text is not concatenated with any EOF character (‘$’ here). The implication of ‘$’ sign comes while computing the inverse of BWT. Way of implementation

  1. Let’s instantiate “banana$” as our input_text and instantiate character array bwt_arr for our output.
  2. Let’s get all the suffixes of “banana$” and compute it’s suffix_arr to store index of each suffix.
0 banana$                6 $   
1 anana$ 5 a$
2 nana$ Sorting 3 ana$
3 ana$ ----------> 1 anana$
4 na$ alphabetically 0 banana$
5 a$ 4 na$
6 $ 2 nana$

  1. Iterating over the suffix_arr, let’s now add to our output array bwt_arr, the last character of each rotation.
  2. The last character of each rotation of input_text starting at the position denoted by the current value in the suffix array can be calculated with input_text[(suffix_arr[i] – 1 + n ) % n], where n is the number of elements in the suffix_arr.
bwt_arr[0] 
= input_text[(suffix_arr[0] - 1 + 7) % 7]
= input_text[5]
= a
bwt_arr[1]
= input_text[(suffix_arr[1] - 1 + 7) % 7]
= input_text[4]
= n

Following is the code for the way of implementation explained above 

C++

// CPP program to find Burrows Wheeler transform
// of a given text
#include <bits/stdc++.h>
using namespace std;
 
// Structure to store data of a rotation
struct rotation {
    int index;
    char* suffix;
};
 
// Compares the rotations and
// sorts the rotations alphabetically
int cmpfunc(const void* x, const void* y)
{
    struct rotation* rx = (struct rotation*)x;
    struct rotation* ry = (struct rotation*)y;
    return strcmp(rx->suffix, ry->suffix);
}
 
// Takes text to be transformed and its length as
// arguments and returns the corresponding suffix array
int* computeSuffixArray(char* input_text, int len_text)
{
    // Array of structures to store rotations and
    // their indexes
    struct rotation suff[len_text];
 
    // Structure is needed to maintain old indexes of
    // rotations after sorting them
    for (int i = 0; i < len_text; i++) {
        suff[i].index = i;
        suff[i].suffix = (input_text + i);
    }
 
    // Sorts rotations using comparison
    // function defined above
    qsort(suff, len_text, sizeof(struct rotation), cmpfunc);
 
    // Stores the indexes of sorted rotations
    int* suffix_arr = (int*)malloc(len_text * sizeof(int));
    for (int i = 0; i < len_text; i++)
        suffix_arr[i] = suff[i].index;
 
    // Returns the computed suffix array
    return suffix_arr;
}
 
// Takes suffix array and its size
// as arguments and returns the
// Burrows - Wheeler Transform of given text
char* findLastChar(char* input_text, int* suffix_arr, int n)
{
    // Iterates over the suffix array to find
    // the last char of each cyclic rotation
    char* bwt_arr = (char*)malloc(n * sizeof(char));
    int i;
    for (i = 0; i < n; i++) {
        // Computes the last char which is given by
        // input_text[(suffix_arr[i] + n - 1) % n]
        int j = suffix_arr[i] - 1;
        if (j < 0)
            j = j + n;
 
        bwt_arr[i] = input_text[j];
    }
 
    bwt_arr[i] = '\0';
 
    // Returns the computed Burrows - Wheeler Transform
    return bwt_arr;
}
 
// Driver program to test functions above
int main()
{
    char input_text[] = "banana$";
    int len_text = strlen(input_text);
 
    // Computes the suffix array of our text
    int* suffix_arr
        = computeSuffixArray(input_text, len_text);
 
    // Adds to the output array the last char of each
    // rotation
    char* bwt_arr
        = findLastChar(input_text, suffix_arr, len_text);
 
    cout << "Input text : " << input_text << endl;
    cout << "Burrows - Wheeler Transform : " << bwt_arr
         << endl;
    return 0;
}
 
// This code is contributed by Susobhan Akhuli

                    

C

// C program to find Burrows Wheeler transform
// of a given text
 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
// Structure to store data of a rotation
struct rotation {
    int index;
    char* suffix;
};
 
// Compares the rotations and
// sorts the rotations alphabetically
int cmpfunc(const void* x, const void* y)
{
    struct rotation* rx = (struct rotation*)x;
    struct rotation* ry = (struct rotation*)y;
    return strcmp(rx->suffix, ry->suffix);
}
 
// Takes text to be transformed and its length as
// arguments and returns the corresponding suffix array
int* computeSuffixArray(char* input_text, int len_text)
{
    // Array of structures to store rotations and
    // their indexes
    struct rotation suff[len_text];
 
    // Structure is needed to maintain old indexes of
    // rotations after sorting them
    for (int i = 0; i < len_text; i++) {
        suff[i].index = i;
        suff[i].suffix = (input_text + i);
    }
 
    // Sorts rotations using comparison
    // function defined above
    qsort(suff, len_text, sizeof(struct rotation),
        cmpfunc);
 
    // Stores the indexes of sorted rotations
    int* suffix_arr
        = (int*)malloc(len_text * sizeof(int));
    for (int i = 0; i < len_text; i++)
        suffix_arr[i] = suff[i].index;
 
    // Returns the computed suffix array
    return suffix_arr;
}
 
// Takes suffix array and its size
// as arguments and returns the
// Burrows - Wheeler Transform of given text
char* findLastChar(char* input_text,
                int* suffix_arr, int n)
{
    // Iterates over the suffix array to find
    // the last char of each cyclic rotation
    char* bwt_arr = (char*)malloc(n * sizeof(char));
    int i;
    for (i = 0; i < n; i++) {
        // Computes the last char which is given by
        // input_text[(suffix_arr[i] + n - 1) % n]
        int j = suffix_arr[i] - 1;
        if (j < 0)
            j = j + n;
 
        bwt_arr[i] = input_text[j];
    }
 
    bwt_arr[i] = '\0';
 
    // Returns the computed Burrows - Wheeler Transform
    return bwt_arr;
}
 
// Driver program to test functions above
int main()
{
    char input_text[] = "banana$";
    int len_text = strlen(input_text);
 
    // Computes the suffix array of our text
    int* suffix_arr
        = computeSuffixArray(input_text, len_text);
 
    // Adds to the output array the last char
    // of each rotation
    char* bwt_arr
        = findLastChar(input_text, suffix_arr, len_text);
 
    printf("Input text : %s\n", input_text);
    printf("Burrows - Wheeler Transform : %s\n",
        bwt_arr);
    return 0;
}

                    

Java

import java.util.Arrays;
 
class Rotation implements Comparable<Rotation> {
    int index;
    String suffix;
 
    public Rotation(int index, String suffix)
    {
        this.index = index;
        this.suffix = suffix;
    }
 
    @Override public int compareTo(Rotation o)
    {
        return this.suffix.compareTo(o.suffix);
    }
}
 
public class BurrowsWheelerTransform {
 
    public static int[] computeSuffixArray(String inputText)
    {
        int lenText = inputText.length();
 
        Rotation[] suff = new Rotation[lenText];
 
        for (int i = 0; i < lenText; i++) {
            suff[i]
                = new Rotation(i, inputText.substring(i));
        }
 
        Arrays.sort(suff);
 
        int[] suffixArr = new int[lenText];
        for (int i = 0; i < lenText; i++) {
            suffixArr[i] = suff[i].index;
        }
 
        return suffixArr;
    }
 
    public static String findLastChar(String inputText,
                                      int[] suffixArr)
    {
        int n = inputText.length();
 
        StringBuilder bwtArr = new StringBuilder();
        for (int i = 0; i < n; i++) {
            int j = suffixArr[i] - 1;
            if (j < 0) {
                j = j + n;
            }
            bwtArr.append(inputText.charAt(j));
        }
 
        return bwtArr.toString();
    }
 
    public static void main(String[] args)
    {
        String inputText = "banana$";
 
        int[] suffixArr = computeSuffixArray(inputText);
 
        String bwtArr = findLastChar(inputText, suffixArr);
 
        System.out.println("Input text : " + inputText);
        System.out.println("Burrows - Wheeler Transform : "
                           + bwtArr);
    }
}

                    

Python3

# Python program to find Burrows-Wheeler Transform of a given text
# Compares the rotations and sorts the rotations alphabetically
 
 
def cmp_func(x, y):
    return (x[1] > y[1]) - (x[1] < y[1])
 
# Takes text to be transformed and its length as arguments
# and returns the corresponding suffix array
 
 
def compute_suffix_array(input_text, len_text):
    # Array of structures to store rotations and their indexes
    suff = [(i, input_text[i:]) for i in range(len_text)]
 
    # Sorts rotations using comparison function defined above
    suff.sort(key=lambda x: x[1])
 
    # Stores the indexes of sorted rotations
    suffix_arr = [i for i, _ in suff]
 
    # Returns the computed suffix array
    return suffix_arr
 
# Takes suffix array and its size as arguments
# and returns the Burrows-Wheeler Transform of given text
 
 
def find_last_char(input_text, suffix_arr, n):
    # Iterates over the suffix array to
    # find the last char of each cyclic rotation
    bwt_arr = ""
    for i in range(n):
        # Computes the last char which is given by
        # input_text[(suffix_arr[i] + n - 1) % n]
        j = suffix_arr[i] - 1
        if j < 0:
            j = j + n
        bwt_arr += input_text[j]
 
    # Returns the computed Burrows-Wheeler Transform
    return bwt_arr
 
 
# Driver program to test functions above
input_text = "banana$"
len_text = len(input_text)
 
# Computes the suffix array of our text
suffix_arr = compute_suffix_array(input_text, len_text)
 
# Adds to the output array the last char of each rotation
bwt_arr = find_last_char(input_text, suffix_arr, len_text)
 
print("Input text :", input_text)
print("Burrows - Wheeler Transform :", bwt_arr)
 
# This code is contributed by Susobhan Akhuli

                    

C#

using System;
using System.Collections.Generic;
 
public class BurrowsWheelerTransform
{
  // Structure to store data of a rotation
  public struct Rotation
  {
    public int Index;
    public string Suffix;
  }
 
  // Compares the rotations and sorts them alphabetically
  private static int CompareRotations(Rotation x, Rotation y)
  {
    return string.Compare(x.Suffix, y.Suffix);
  }
 
  // Takes text to be transformed and its length as
  // arguments and returns the corresponding suffix array
  private static int[] ComputeSuffixArray(string inputText)
  {
    int lenText = inputText.Length;
 
    // Array of structures to store rotations and their indexes
    Rotation[] suff = new Rotation[lenText];
 
    // Structure is needed to maintain old indexes of rotations
    // after sorting them
    for (int i = 0; i < lenText; i++)
    {
      suff[i].Index = i;
      suff[i].Suffix = inputText.Substring(i);
    }
 
    // Sorts rotations using comparison function defined above
    Array.Sort(suff, CompareRotations);
 
    // Stores the indexes of sorted rotations
    int[] suffixArr = new int[lenText];
    for (int i = 0; i < lenText; i++)
    {
      suffixArr[i] = suff[i].Index;
    }
 
    // Returns the computed suffix array
    return suffixArr;
  }
 
  // Takes suffix array and its size as arguments
  // and returns the Burrows-Wheeler Transform of given text
  private static string FindLastChar(string inputText, int[] suffixArr)
  {
    int n = suffixArr.Length;
 
    // Iterates over the suffix array to find the last char of each cyclic rotation
    char[] bwtArr = new char[n];
    for (int i = 0; i < n; i++)
    {
      // Computes the last char which is given by inputText[(suffixArr[i] + n - 1) % n]
      int j = suffixArr[i] - 1;
      if (j < 0)
      {
        j += n;
      }
 
      bwtArr[i] = inputText[j];
    }
 
    // Returns the computed Burrows-Wheeler Transform
    return new string(bwtArr);
  }
 
  // Driver program to test functions above
  public static void Main(string[] args)
  {
    string inputText = "banana$";
    int lenText = inputText.Length;
 
    // Computes the suffix array of our text
    int[] suffixArr = ComputeSuffixArray(inputText);
 
    // Adds to the output array the last char of each rotation
    string bwtArr = FindLastChar(inputText, suffixArr);
 
    Console.WriteLine($"Input text : {inputText}");
    Console.WriteLine($"Burrows-Wheeler Transform : {bwtArr}");
  }
}
// This code is contributed by divyansh2212

                    

Javascript

// Compares the rotations and sorts the rotations alphabetically
 
function cmp_func(x, y) {
    return (x[1] > y[1]) - (x[1] < y[1]);
}
//Takes text to be transformed and its length as arguments
//and returns the corresponding suffix array
 
function compute_suffix_array(input_text, len_text) {
    let suff = [];
    for (let i = 0; i < len_text; i++) {
        suff.push([i, input_text.slice(i)]);
    }
    suff.sort(cmp_func);
    let suffix_arr = suff.map(item => item[0]);
    return suffix_arr;
}
// Takes suffix array and its size as arguments
// and returns the Burrows-Wheeler Transform of given text
function find_last_char(input_text, suffix_arr, n) {
    let bwt_arr = "";
    for (let i = 0; i < n; i++) {
        let j = suffix_arr[i] - 1;
        if (j < 0) {
            j = j + n;
        }
        bwt_arr += input_text[j];
    }
    return bwt_arr;
}
// Driver program to test functions above
 
let input_text = "banana$";
let len_text = input_text.length;
let suffix_arr = compute_suffix_array(input_text, len_text);
let bwt_arr = find_last_char(input_text, suffix_arr, len_text);
 
console.log("Input text: " + input_text);
console.log("Burrows-Wheeler Transform: " + bwt_arr);

                    

PHP

<?php
// Comparison function for sorting rotations
function cmp_func($x, $y) {
    return ($x[1] > $y[1]) - ($x[1] < $y[1]);
}
 
// Function to compute the suffix array
function compute_suffix_array($input_text, $len_text) {
    $suff = [];
    // Array of structures to store rotations and their indexes
    for ($i = 0; $i < $len_text; $i++) {
        $suff[] = [$i, substr($input_text, $i)];
    }
 
    // Sort rotations using comparison function
    usort($suff, function($a, $b) {
        return strcmp($a[1], $b[1]);
    });
 
    // Store the indexes of sorted rotations
    $suffix_arr = array_map(function($x) { return $x[0]; }, $suff);
 
    // Return the computed suffix array
    return $suffix_arr;
}
 
// Function to find the last char of each cyclic rotation
function find_last_char($input_text, $suffix_arr, $n) {
    $bwt_arr = "";
    // Iterate over the suffix array to find the last char of each cyclic rotation
    for ($i = 0; $i < $n; $i++) {
        // Compute the last char
        $j = $suffix_arr[$i] - 1;
        if ($j < 0) {
            $j = $j + $n;
        }
        $bwt_arr .= $input_text[$j];
    }
 
    // Return the computed Burrows-Wheeler Transform
    return $bwt_arr;
}
 
// Driver code to test functions above
$input_text = "banana$";
$len_text = strlen($input_text);
 
// Compute the suffix array of our text
$suffix_arr = compute_suffix_array($input_text, $len_text);
 
// Add to the output array the last char of each rotation
$bwt_arr = find_last_char($input_text, $suffix_arr, $len_text);
 
echo "Input text: ", $input_text, "\n";
echo "Burrows-Wheeler Transform: ", $bwt_arr, "\n";
?>

                    

Output
Input text : banana$
Burrows - Wheeler Transform : annb$aa

Time Complexity: O(n^2            Logn). This is because of the method used above to build suffix array which has O(n^2            Logn) time complexity, due to O(n) time for strings comparisons in O(nLogn) sorting algorithm. Exercise: 

  1. Compute suffix array in O(nLogn) time and then implement BWT.
  2. Implement Inverse of Burrows-Wheeler Transform.

 



Previous Article
Next Article

Similar Reads

Inverting the Burrows - Wheeler Transform
Prerequisite: Burrows - Wheeler Data Transform Algorithm Why inverse of BWT? The main idea behind it: 1. The remarkable thing about BWT algorithm is that this particular transform is invertible with minimal data overhead. 2. To compute inverse of BWT is to undo the BWT and recover the original string. The naive method of implementing this algorithm
13 min read
Move To Front Data Transform Algorithm
What is the MTF transform? The MTF (Move to Front) is a data transformation algorithm that restructures data in such a way that the transformed message is more compressible and therefore used as an extra step in compression. Technically, it is an invertible transform of a sequence of input characters to an array of output numbers. Why MTF? In many
9 min read
Discrete Cosine Transform (Algorithm and Program)
Image Compression : Image is stored or transmitted with having pixel value. It can be compressed by reducing the value its every pixel contains. Image compression is basically of two types : Lossless compression : In this type of compression, after recovering image is exactly become same as that was before applying compression techniques and so, it
11 min read
Transform a BST to greater sum tree
Given a BST, transform it into a greater sum tree where each node contains the sum of all nodes greater than that node. We strongly recommend minimizing the browser and try this yourself first. Method 1 (Naïve): This method doesn’t require the tree to be a BST. Following are the steps. Traverse node by node(Inorder, preorder, etc.) For each node fi
8 min read
Transform a string such that it has abcd..z as a subsequence
Given a string S of only small English letters. We need to transform the string by making some number of moves (any number of times) to get the string "abcdefghijklmnopqrstuvwxyz" as a subsequence in that string. In one move, you can replace any character of the string with the next character in alphabetical order i.e. 'a' can be replaced by 'b', '
8 min read
Maximum steps to transform 0 to X with bitwise AND
For an integer N, there are elements ranging from 0 to N-1. Certain elements can be transformed into other elements. Each transformation requires a certain effort which is equal to 1 unit, for each transformation. An element A can be transformed to element B, if and only if A != B and A &amp; B = A (where &amp; is the bitwise AND operator). We need
5 min read
Minimum operations required to transform a sequence of numbers to a sequence where a[i]=a[i+2]
Given a sequence of integers of even length 'n', the task is to find the minimum number of operations required to convert the sequence to follow the rule a[i]=a[i+2] where 'i' is the index. The operation here is to replace any element of the sequence with any element. Examples: Input : n=4 ; Array : 3, 1, 3, 2 Output : 1 If we change the last eleme
11 min read
Properties of Fourier Transform
Fourier Transform: Fourier transform is the input tool that is used to decompose an image into its sine and cosine components. Properties of Fourier Transform: Linearity: Addition of two functions corresponding to the addition of the two frequency spectrum is called the linearity. If we multiply a function by a constant, the Fourier transform of th
2 min read
Transform N to Minimum possible value
Given two numbers and N and D. Apply any of two below operations to N: add D to Nchange N to digitsum(N), where digitsum(N) is the sum of digits of N The task is to transform N to the minimum possible value. Print the minimum possible value of N and the number of times the given operations applied(any one of them). The number of operations must be
9 min read
Transform string str1 into str2 by taking characters from string str3
Given three strings str1, str2 &amp; str3. The task is to find whether string str1 can be transformed to string str2 by taking characters from str3. If yes then print “YES”, Else print “NO”.Examples: Input: str1 = "abyzf", str2 = "abgdeyzf", str3 = "poqgode". Output: YES Explanation: Remove 'g' from str3 and insert it after 'b' in str1, A = "abgyzf
9 min read
three90RightbarBannerImg