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.
1. In many cases, the output array gives frequently repeated characters’ lower indexes which is useful in data compression algorithms.
2. It is first of the three steps to be performed in succession while implementing Burrows – Wheeler Data Compression algorithm that forms the basis of the Unix compression utility bzip2.
The main idea behind MTF:
1. The primary idea behind MTF is to maintain an ordered list of legal symbols (a to z, in our example).
2. Read one character at a time from input string .
3. Print out the position at which that character appears in the list.
4. Move that character to front of the list and repeat the process until indexes for all input characters are obtained.
Illustration for "panama". List initially contains English alphabets in order. We one by one characters of input to front. input_str chars output_arr list p 15 abcdefghijklmnopqrstuvwxyz a 15 1 pabcdefghijklmnoqrstuvwxyz n 15 1 14 apbcdefghijklmnoqrstuvwxyz a 15 1 14 1 napbcdefghijklmoqrstuvwxyz m 15 1 14 1 14 anpbcdefghijklmoqrstuvwxyz a 15 1 14 1 14 manpbcdefghijkloqrstuvwxyz
1. If the letters occur many times in the input, then many of the output values will be small integers such as 0, 1, 2 etc.
2. Thus, encoding extremely high frequency of these letters makes an ideal scenario for Huffman Coding.
Input : panama Output : 15 1 14 1 14 1 Input : geeksforgeeks Output : 6 5 0 10 18 8 15 18 6 6 0 6 6
Following is the code for idea explained above:
Input text: panama Move to Front Transform: 15 1 14 1 14 1
Time Complexity: O(n^2)
Exercise: Implement Inverse of Move to Front Transform.