Explanation of BWT (Burrows-wheeler transformation) and Java implementation

Source: Internet
Author: User

BWT (Burrows-wheeler transformation)

1. What is BWT

The main way to work with compression is to find repetitive patterns that are tightly coded.

BWT (Burrows–wheeler_transform) converts the original text to a similar text, converting it so that the same character position is contiguous or contiguous, and can then use other techniques such as:Move-to-front transform and Run-length encoding for text compression.

2.BWT principle

2.1 BWT Encoding

(1) First, bwt the text blocks that need to be converted, loop right, one at a time. A text block of length n can be known and repeated n times, so that the N-length string is seen. As in the "Rotate Right" column. (where ' # ' is used as an identifier, not in the character set of the text block, so that the string of n loops shifts is the same.) and define ' # ' less than any character in the character set.

(2) The n strings after the cyclic shift are sorted in dictionary order. such as the "Sorted (M)" column in.

(3) record the last character of each string in the "Sorted (M)" column, which makes up the "L" column. (Where the "F" column is the prefix for each string in the "Sorted (M)" column)

In this way, the original string "banana#" is converted to "Annb#aa". In some cases, compression with the L column will have a better effect. The "L" column is the result of the encoding.

2.2 BWT decoding

Because it's a cyclic shift , and it's a circular left shift , note the following properties:

1.the first element of L is the last element in text2.for each row in m (except for the first row) The first element is the next element of the last element. That is , for a text block, F is the next element of L in the same row, and L is the previous element of F. Thus, it is necessary (1) to find the characters in front of him by the elements in the "F" column, that is, the corresponding line "L" column, and (2) to find the corresponding character position in the "F" column by the element in the "L" column. But "L" has 3 characters a, how to correspond to the 3 a in F? Because L is the previous element of F, multiple strings with the same prefix are sorted, and the relative order is not changed after the common prefix is removed. All encounters multiple identical characters, relative position unchanged; (3) go to (1) until the end.

The sorted string BWT the transformed string


The dollar sign "$" is found in the string after the bwt transform , and the letter on the same line is B, we'll write it down ,$B find B, we'll find it in the right column . B, which corresponds to a,

Then write down $ba,

now A on the left is the third occurrence, so in the right column to find a, the same is the third occurrence, and the corresponding is N, and then note $ban, left n

It was the second time, so I found it in the right column. N is also the second occurrence, corresponding to a,

then write down $BANA, now a on the left is the second occurrence, so on the right, a column to find a is also the second occurrence, corresponding to the N, and then write down $BANAN

left N is the first time, so in the right column to find N is also the first occurrence,

corresponding to it is A, then write down $banaNA, now a on the left is the first occurrence, so in the right column to find a is also the first occurrence, with the corresponding is "$", theend.

 Packagecn.genekang.io.test;Importjava.util.Arrays; Public classBwttest { Public Static voidMain (string[] args) {String str= "Banana"; System.out.println ("The input string is:" +str); String Encodestr=EnCode (str); System.out.println ("The encoded string is:" +encodestr); System.out.println ("The decoded string is:" +DeCode (ENCODESTR)); }    //BWT Encoding     Public Staticstring EnCode (String line) {string str= line + "&"; intLen =str.length (); //1. Rotation        Char[] Chararray =Str.tochararray (); Char[] ch =New Char[Len][len];  for(inti = 0; i < Len; i++) {            Char[] c_tmp =Chararray.clone ();  for(intj = 0; J < Len; J + +) {Ch[i][j]=C_tmp[j]; if(J <= Len-2) Chararray[j+ 1] =C_tmp[j]; } chararray[0] = c_tmp[len-1]; }        //2. Sorting, in dictionary orderString[] Strings =NewString[len];  for(inti = 0; i < Len; i++) {StringBuffer chline=NewStringBuffer ();  for(CharC:ch[i])            {chline.append (c); } Strings[i]=chline.tostring ();        } arrays.sort (strings); //3. Take the last lineStringBuffer Sbuffer =NewStringBuffer ();  for(String s:strings) {sbuffer.append (s.substring (Len-1, Len)); }        returnsbuffer.tostring (); }    //BWT decoding     Public Staticstring DeCode (String str) {intLen =str.length (); String[] Strarr=NewString[len];  for(inti = 0; i < Len; i++) {Strarr[i]= Str.substring (I, i + 1) + ":" +i;        } arrays.sort (Strarr); //For (String String:strarr)//System.out.println (string);StringBuffer SB =NewStringBuffer (); intnum = 0; intCorr = Integer.parseint (Strarr[0].split (":") [1]);  while(Num < len-1) {sb.append (Strarr[corr].split (":") [0]); Corr=integer.parseint (Strarr[corr].split (":") [1]); Num++; }        returnsb.tostring (); }}

Explanation of BWT (Burrows-wheeler transformation) and Java implementation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.