Inverting the Move to Front Transform
Prerequisite:Move To Front Data Transform Algorithm
The main idea behind inverse of MTF Transform:
- To compute inverse of MTF Transform is to undo the MTF Transform and recover the original string. We have with us “input_arr” which is the MTF transform and “n” which is the number of elements in “input_arr”.
- Our task is to maintain an ordered list of characters (a to z, in our example) and read in “ith”element from “input_arr” one at a time.
- Then, taking that element as index j, print “jth” character in the list.
Illustration for "[15 1 14 1 14 1]"
List initially contains English alphabets in order.
We move characters at indexes depicted by input
to front of the list one by one.
input arr chars output str list
15 p abcdefghijklmnopqrstuvwxyz
1 pa pabcdefghijklmnoqrstuvwxyz
14 pan apbcdefghijklmnoqrstuvwxyz
1 pana napbcdefghijklmoqrstuvwxyz
14 panam anpbcdefghijklmoqrstuvwxyz
1 panama manpbcdefghijkloqrstuvwxyz
Examples:
Input : arr[] = {15, 1, 14, 1, 14, 1}
Output : panama
Input : arr[] = {6, 5, 0, 10, 18, 8, 15, 18,
6, 6, 0, 6, 6};
Output : geeksforgeeks
Following is the code for idea explained above:
C++
#include<bits/stdc++.h>
using namespace std;
void moveToFront( int index, string &list)
{
char record[27];
int i = 0;
for (; i < list.size(); i++) record[i] = list[i];
i = 1;
for (; i <= index; i++) list[i] = record[i-1];
list[0] = record[index];
}
void mtfDecode(vector< int > arr, int n)
{
string list = "abcdefghijklmnopqrstuvwxyz" ;
int i;
cout << "\nInverse of Move to Front Transform: " ;
for (i = 0; i < n; i++)
{
cout << list[arr[i]];
moveToFront(arr[i], list);
}
}
int main()
{
vector< int > arr = {15, 1, 14, 1, 14, 1};
int n = arr.size();
mtfDecode(arr, n);
return 0;
}
|
C
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void moveToFront( int index, char *list)
{
char record[27];
strcpy (record, list);
strncpy (list+1, record, index);
list[0] = record[index];
}
void mtfDecode( int arr[], int n)
{
char list[] = "abcdefghijklmnopqrstuvwxyz" ;
int i;
printf ( "\nInverse of Move to Front Transform: " );
for (i = 0; i < n; i++)
{
printf ( "%c" , list[arr[i]]);
moveToFront(arr[i], list);
}
}
int main()
{
int arr[] = {15, 1, 14, 1, 14, 1};
int n = sizeof (arr)/ sizeof (arr[0]);
mtfDecode(arr, n);
return 0;
}
|
Java
import java.util.*;
class Main {
static void moveToFront( int index, StringBuilder list) {
char [] record = new char [list.length()];
list.getChars( 0 , list.length(), record, 0 );
for ( int i = index; i > 0 ; i--) {
list.setCharAt(i, record[i - 1 ]);
}
list.setCharAt( 0 , record[index]);
}
static void mtfDecode(List<Integer> arr, int n) {
StringBuilder list = new StringBuilder( "abcdefghijklmnopqrstuvwxyz" );
System.out.print( "\nInverse of Move to Front Transform: " );
for ( int i = 0 ; i < n; i++) {
System.out.print(list.charAt(arr.get(i)));
moveToFront(arr.get(i), list);
}
}
public static void main(String[] args) {
List<Integer> arr = Arrays.asList( 15 , 1 , 14 , 1 , 14 , 1 );
int n = arr.size();
mtfDecode(arr, n);
}
}
|
Python3
def move_to_front(index, lst):
record = lst.copy()
lst[ 1 :index + 1 ] = record[:index]
lst[ 0 ] = record[index]
def mtf_decode(arr):
lst = list ( "abcdefghijklmnopqrstuvwxyz" )
result = []
for i in arr:
result.append(lst[i])
move_to_front(i, lst)
return ''.join(result)
arr = [ 15 , 1 , 14 , 1 , 14 , 1 ]
print ( "Inverse of Move to Front Transform:" , mtf_decode(arr))
|
C#
using System;
using System.Collections.Generic;
using System.Text;
class MainClass {
static void moveToFront( int index, StringBuilder list) {
char [] record = list.ToString().ToCharArray();
for ( int i = index; i > 0; i--) {
list[i] = record[i - 1];
}
list[0] = record[index];
}
static void mtfDecode(List< int > arr, int n) {
StringBuilder list = new StringBuilder( "abcdefghijklmnopqrstuvwxyz" );
Console.Write( "\nInverse of Move to Front Transform: " );
for ( int i = 0; i < n; i++) {
Console.Write(list[arr[i]]);
moveToFront(arr[i], list);
}
}
public static void Main( string [] args) {
List< int > arr = new List< int > {15, 1, 14, 1, 14, 1};
int n = arr.Count;
mtfDecode(arr, n);
}
}
|
Javascript
function move_to_front(index, lst) {
const record = lst.slice();
lst.splice(1, index, ...record.slice(0, index));
lst[0] = record[index];
}
function mtf_decode(arr) {
const lst = "abcdefghijklmnopqrstuvwxyz" .split( "" );
const result = [];
for (let i = 0; i < arr.length; i++) {
const charIndex = arr[i];
result.push(lst[charIndex]);
move_to_front(charIndex, lst);
}
return result.join( "" );
}
const arr = [15, 1, 14, 1, 14, 1];
console.log( "Inverse of Move to Front Transform:" , mtf_decode(arr));
|
Output
Inverse of Move to Front Transform: panama
Time Complexity: O(n^2)
Auxiliary Space: O(n), size of the given array.
Exercise: Implement MTF encoding and decoding together in one program and check if the original message is recovered.
Last Updated :
31 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...