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 can be studied from here. The naive approach is speed and memory intensive and requires us to store |text| cyclic rotations of the string |text|.
3. Let’s discuss a faster algorithm where we have with us only two things:
- bwt_arr[] which is the last column of sorted rotations list given as “annb$aa”.
- ‘x’ which is the row index at which our original string “banana$” appears in the sorted rotations list. We can see that ‘x’ is 4 in the example below.
Row Index Original Rotations Sorted Rotations
~~~~~~~~~ ~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~
0 banana$ $banana
1 anana$b a$banan
2 nana$ba ana$ban
3 ana$ban anana$b
*4 na$bana banana$
5 a$banan na$bana
6 $banana nana$ba
4. An important observation: If the jth original rotation (which is original rotation shifted j characters to the left) is the ith row in the sorted order, then l_shift[i] records in the sorted order where (j+1)st original rotation appears. For example, the 0th original rotation “banana$” is row 4 of sorted order, and since l_shift[4] is 3, the next original rotation “anana$b” is row 3 of the sorted order.
Row Index Original Rotations Sorted Rotations l_shift
~~~~~~~~~ ~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~ ~~~~~~~
0 banana$ $banana 4
1 anana$b a$banan 0
2 nana$ba ana$ban 5
3 ana$ban anana$b 6
*4 na$bana banana$ 3
5 a$banan na$bana 1
6 $banana nana$ba 2
5. Our job is to deduce l_shift[] from the information available to us which is bwt_arr[] and ‘x’ and with its help compute the inverse of BWT. How to compute l_shift[] ? 1. We know BWT which is “annb$aa”. This implies that we know all the characters of our original string, even though they’re permuted in wrong order. 2. By sorting bwt_arr[], we can reconstruct first column of sorted rotations list and we call it sorted_bwt[].
Row Index Sorted Rotations bwt_arr l_shift
~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~
0 $ ? ? ? ? ? a 4
1 a ? ? ? ? ? n
2 a ? ? ? ? ? n
3 a ? ? ? ? ? b
*4 b ? ? ? ? ? $ 3
5 n ? ? ? ? ? a
6 n ? ? ? ? ? a
3. Since ‘$’ occurs only once in the string ‘sorted_bwt[]’ and rotations are formed using cyclic wrap around, we can deduce that l_shift[0] = 4. Similarly, ‘b’ occurs once, so we can deduce that l_shift[4] = 3.
4. But, because ‘n’ appears twice, it seems ambiguous whether l_shift[5] = 1 and l_shift[6] = 2 or whether l_shift[5] = 2 and l_shift[6] = 1.
5. Rule to solve this ambiguity is that if rows i and j both start with the same letter and i<j, then l_shift[i] < l_shift[j]. This implies l_shift[5] = 1 and l_shift[6] =2. Continuing in a similar fashion, l_shift[] gets computed to the following.
Row Index Sorted Rotations bwt_arr l_shift
~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~
0 $ ? ? ? ? ? a 4
1 a ? ? ? ? ? n 0
2 a ? ? ? ? ? n 5
3 a ? ? ? ? ? b 6
*4 b ? ? ? ? ? $ 3
5 n ? ? ? ? ? a 1
6 n ? ? ? ? ? a 2
Why is the ambiguity resolving rule valid?
- The rotations are sorted in such a way that row 5 is lexicographically less than row 6.
- Thus, the five unknown characters in row 5 must be less than the five unknown characters in row 6 (as both start with ‘n’).
- We also know that between the two rows than end with ‘n’, row 1 is lower than row 2.
- But, the five unknown characters in rows 5 and 6 are precisely the first five characters in rows 1 and 2 or this would contradict the fact that rotations were sorted.
- Thus, l_shift[5] = 1 and l_shift[6] = 2.
Way of implementation:
1. Sort BWT: Using qsort(), we arrange characters of bwt_arr[] in sorted order and store it in sorted_arr[].
2. Compute l_shift[]:
i. We take an array of pointers struct node *arr[], each of which points to a linked list.
ii. Making each distinct character of bwt_arr[] a head node of a linked list, we append nodes to the linked list whose data part contains index at which that character occurs in bwt_arr[].
i *arr[128] Linked Lists
~~~~~~~~~ ~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~
37 $ -----> 4 -> NULL
97 a -----> 0 -> 5 -> 6 -> NULL
110 n -----> 1 -> 2 -> NULL
98 b -----> 3 -> NULL
iii. Making distinct characters of sorted_bwt[] heads of linked lists, we traverse linked lists and get corresponding l_shift[] values.
int[] l_shift = { 4, 0, 5, 6, 3, 1, 2 };
3. Iterating string length times, we decode BWT with x = l_shift[x] and output bwt_arr[x].
x = l_shift[4]
x = 3
bwt_arr[3] = 'b'
x = l_shift[3]
x = 6
bwt_arr[6] = 'a'
Examples:
Input : annb$aa // Burrows - Wheeler Transform
4 // Row index at which original message
// appears in sorted rotations list
Output : banana$
Input : ard$rcaaaabb
3
Output : abracadabra$
Following is the C code for way of implementation explained above:
C
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct node {
int data;
struct node* next;
};
int cmpfunc( const void * a, const void * b)
{
const char * ia = ( const char *)a;
const char * ib = ( const char *)b;
return strcmp (ia, ib);
}
struct node* getNode( int i)
{
struct node* nn =
( struct node*) malloc ( sizeof ( struct node));
nn->data = i;
nn->next = NULL;
return nn;
}
void addAtLast( struct node** head, struct node* nn)
{
if (*head == NULL) {
*head = nn;
return ;
}
struct node* temp = *head;
while (temp->next != NULL)
temp = temp->next;
temp->next = nn;
}
void * computeLShift( struct node** head, int index,
int * l_shift)
{
l_shift[index] = (*head)->data;
(*head) = (*head)->next;
}
void invert( char bwt_arr[])
{
int i,len_bwt = strlen (bwt_arr);
char * sorted_bwt = ( char *) malloc (len_bwt * sizeof ( char ));
strcpy (sorted_bwt, bwt_arr);
int * l_shift = ( int *) malloc (len_bwt * sizeof ( int ));
int x = 4;
qsort (sorted_bwt, len_bwt, sizeof ( char ), cmpfunc);
struct node* arr[128] = { NULL };
for (i = 0; i < len_bwt; i++) {
struct node* nn = getNode(i);
addAtLast(&arr[bwt_arr[i]], nn);
}
for (i = 0; i < len_bwt; i++)
computeLShift(&arr[sorted_bwt[i]], i, l_shift);
printf ( "Burrows - Wheeler Transform: %s\n" , bwt_arr);
printf ( "Inverse of Burrows - Wheeler Transform: " );
for (i = 0; i < len_bwt; i++) {
x = l_shift[x];
printf ( "%c" , bwt_arr[x]);
}
}
int main()
{
char bwt_arr[] = "annb$aa" ;
invert(bwt_arr);
return 0;
}
|
C++
#include <bits/stdc++.h>
using namespace std;
class Node {
public :
char Data;
Node* Next;
Node( char data) {
Data = data;
Next = NULL;
}
};
class InvertBWT {
public :
static void invert(string bwtArr) {
int lenBwt = bwtArr.length();
string sortedBwt = bwtArr;
sort(sortedBwt.begin(), sortedBwt.end());
int * lShift = new int [lenBwt];
int x = 4;
vector< int >* arr = new vector< int >[128];
for ( int i = 0; i < lenBwt; i++) {
arr[bwtArr[i]].push_back(i);
}
for ( int i = 0; i < lenBwt; i++) {
lShift[i] = arr[sortedBwt[i]][0];
arr[sortedBwt[i]].erase(arr[sortedBwt[i]].begin());
}
char * decoded = new char [lenBwt];
for ( int i = 0; i < lenBwt; i++) {
x = lShift[x];
decoded[lenBwt-1-i] = bwtArr[x];
}
string decodedStr(decoded, lenBwt);
cout << "Burrows - Wheeler Transform: " << bwtArr << endl;
cout << "Inverse of Burrows - Wheeler Transform: " << string(decodedStr.rbegin(), decodedStr.rend()) << endl;
}
};
int main() {
string bwtArr = "annb$aa" ;
InvertBWT::invert(bwtArr);
return 0;
}
|
Java
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
class Node {
public char Data;
public Node Next;
public Node( char data) {
Data = data;
Next = null ;
}
}
class InvertBWT {
static void invert(String bwtArr) {
int lenBwt = bwtArr.length();
String sortedBwt = new String(bwtArr.chars().sorted().toArray(), 0 , lenBwt);
int [] lShift = new int [lenBwt];
int x = 4 ;
List<Integer>[] arr = new ArrayList[ 128 ];
for ( int i = 0 ; i < arr.length; i++) {
arr[i] = new ArrayList<Integer>();
}
for ( int i = 0 ; i < lenBwt; i++) {
arr[bwtArr.charAt(i)].add(i);
}
for ( int i = 0 ; i < lenBwt; i++) {
lShift[i] = arr[sortedBwt.charAt(i)].get( 0 );
arr[sortedBwt.charAt(i)].remove( 0 );
}
char [] decoded = new char [lenBwt];
for ( int i = 0 ; i < lenBwt; i++) {
x = lShift[x];
decoded[lenBwt- 1 -i] = bwtArr.charAt(x);
}
String decodedStr = new String(decoded);
System.out.printf( "Burrows - Wheeler Transform: %s\n" , bwtArr);
System.out.printf( "Inverse of Burrows - Wheeler Transform: %s\n" , new StringBuilder(decodedStr).reverse().toString());
}
public static void main(String[] args) {
String bwtArr = "annb$aa" ;
invert(bwtArr);
}
}
|
Python3
import string
class Node:
def __init__( self , data):
self .data = data
self . next = None
def addAtLast(head, nn):
if head is None :
head = nn
return head
temp = head
while temp. next is not None :
temp = temp. next
temp. next = nn
return head
def computeLShift(head, index, l_shift):
l_shift[index] = head.data
head = head. next
def cmpfunc(a, b):
return ord (a) - ord (b)
def invert(bwt_arr):
len_bwt = len (bwt_arr)
sorted_bwt = sorted (bwt_arr)
l_shift = [ 0 ] * len_bwt
x = 4
arr = [[] for i in range ( 128 )]
for i in range (len_bwt):
arr[ ord (bwt_arr[i])].append(i)
for i in range (len_bwt):
l_shift[i] = arr[ ord (sorted_bwt[i])].pop( 0 )
decoded = [''] * len_bwt
for i in range (len_bwt):
x = l_shift[x]
decoded[len_bwt - 1 - i] = bwt_arr[x]
decoded_str = ''.join(decoded)
print ( "Burrows - Wheeler Transform:" , bwt_arr)
print ( "Inverse of Burrows - Wheeler Transform:" , decoded_str[:: - 1 ])
if __name__ = = "__main__" :
bwt_arr = "annb$aa"
invert(bwt_arr)
|
C#
using System;
using System.Collections.Generic;
using System.Linq;
public class Node {
public char Data { get ; set ; }
public Node Next { get ; set ; }
public Node( char data) {
Data = data;
Next = null ;
}
}
public class InvertBWT {
public static void Main( string [] args) {
string bwtArr = "annb$aa" ;
Invert(bwtArr);
}
public static void Invert( string bwtArr) {
int lenBwt = bwtArr.Length;
string sortedBwt = new string (bwtArr.ToCharArray().OrderBy(c => c).ToArray());
int [] lShift = new int [lenBwt];
int x = 4;
List< int >[] arr = new List< int >[128];
for ( int i = 0; i < arr.Length; i++) {
arr[i] = new List< int >();
}
for ( int i = 0; i < lenBwt; i++) {
arr[bwtArr[i]].Add(i);
}
for ( int i = 0; i < lenBwt; i++) {
lShift[i] = arr[sortedBwt[i]][0];
arr[sortedBwt[i]].RemoveAt(0);
}
char [] decoded = new char [lenBwt];
for ( int i = 0; i < lenBwt; i++) {
x = lShift[x];
decoded[lenBwt-1-i] = bwtArr[x];
}
string decodedStr = new string (decoded);
Console.WriteLine( "Burrows - Wheeler Transform: {0}" , bwtArr);
Console.WriteLine( "Inverse of Burrows - Wheeler Transform: {0}" , new string (decodedStr.ToCharArray().Reverse().ToArray()));
}
}
|
Javascript
class Node {
constructor(data) {
this .data = data;
this .next = null ;
}
}
function addAtLast(head, nn) {
if (head === null ) {
head = nn;
return head;
}
let temp = head;
while (temp.next !== null ) {
temp = temp.next;
}
temp.next = nn;
return head;
}
function computeLShift(head, index, l_shift) {
l_shift[index] = head.data;
head = head.next;
}
function cmpfunc(a, b) {
return a.charCodeAt() - b.charCodeAt();
}
function invert(bwt_arr) {
const len_bwt = bwt_arr.length;
const sorted_bwt = bwt_arr.split( '' ).sort().join( '' );
const l_shift = Array(len_bwt).fill(0);
let x = 4;
const arr = Array(128).fill().map(() => []);
for (let i = 0; i < len_bwt; i++) {
arr[bwt_arr.charCodeAt(i)].push(i);
}
for (let i = 0; i < len_bwt; i++) {
l_shift[i] = arr[sorted_bwt.charCodeAt(i)].shift();
}
const decoded = Array(len_bwt).fill( '' );
for (let i = 0; i < len_bwt; i++) {
x = l_shift[x];
decoded[len_bwt-1-i] = bwt_arr.charAt(x);
}
const decoded_str = decoded.join( '' );
console.log( "Burrows - Wheeler Transform:" , bwt_arr);
console.log( "Inverse of Burrows - Wheeler Transform:" , decoded_str.split( '' ).reverse().join( '' ));
}
const bwt_arr = "annb$aa" ;
invert(bwt_arr);
|
OutputBurrows - Wheeler Transform: annb$aa
Inverse of Burrows - Wheeler Transform: banana$
Time Complexity: O(nLogn) as qsort() takes O(nLogn) time.
Exercise: Implement inverse of Inverse of Burrows – Wheeler Transform in O(n) time.
If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.