<s>
The	O
move-to-front	B-Algorithm
(	O
MTF	O
)	O
transform	O
is	O
an	O
encoding	O
of	O
data	O
(	O
typically	O
a	O
stream	O
of	O
bytes	B-Application
)	O
designed	O
to	O
improve	O
the	O
performance	O
of	O
entropy	B-Algorithm
encoding	I-Algorithm
techniques	O
of	O
compression	B-General_Concept
.	O
</s>
<s>
When	O
efficiently	O
implemented	O
,	O
it	O
is	O
fast	O
enough	O
that	O
its	O
benefits	O
usually	O
justify	O
including	O
it	O
as	O
an	O
extra	O
step	O
in	O
data	B-General_Concept
compression	I-General_Concept
algorithm	I-General_Concept
.	O
</s>
<s>
Assume	O
for	O
simplicity	O
that	O
the	O
symbols	O
in	O
the	O
data	O
are	O
bytes	B-Application
.	O
</s>
<s>
Each	O
byte	B-Application
value	O
is	O
encoded	O
by	O
its	O
index	O
in	O
a	O
list	O
of	O
bytes	B-Application
,	O
which	O
changes	O
over	O
the	O
course	O
of	O
the	O
algorithm	O
.	O
</s>
<s>
The	O
list	O
is	O
initially	O
in	O
order	O
by	O
byte	B-Application
value	O
(	O
0	O
,	O
1	O
,	O
2	O
,	O
3	O
,	O
...	O
,	O
255	O
)	O
.	O
</s>
<s>
Therefore	O
,	O
the	O
first	O
byte	B-Application
is	O
always	O
encoded	O
by	O
its	O
own	O
value	O
.	O
</s>
<s>
However	O
,	O
after	O
encoding	O
a	O
byte	B-Application
,	O
that	O
value	O
is	O
moved	O
to	O
the	O
front	O
of	O
the	O
list	O
before	O
continuing	O
to	O
the	O
next	O
byte	B-Application
.	O
</s>
<s>
Imagine	O
instead	O
of	O
bytes	B-Application
,	O
we	O
are	O
encoding	O
values	O
in	O
a	O
–	O
z	O
.	O
</s>
<s>
For	O
encoding	O
,	O
no	O
clear	O
advantage	O
is	O
gained	O
by	O
using	O
a	O
linked	B-Data_Structure
list	I-Data_Structure
,	O
so	O
using	O
an	O
array	B-Data_Structure
to	O
store	O
the	O
list	O
is	O
acceptable	O
,	O
with	O
worst-case	O
performance	O
O(nk )	O
,	O
where	O
n	O
is	O
the	O
length	O
of	O
the	O
data	O
to	O
be	O
encoded	O
and	O
k	O
is	O
the	O
number	O
of	O
values	O
(	O
generally	O
a	O
constant	O
for	O
a	O
given	O
implementation	O
)	O
.	O
</s>
<s>
This	O
is	O
also	O
the	O
idea	O
behind	O
a	O
Move-to-front	B-Algorithm
self-organizing	O
list	O
.	O
</s>
<s>
This	O
is	O
a	O
possible	O
implementation	O
of	O
the	O
move-to-front	B-Algorithm
algorithm	O
in	O
Python	B-Language
.	O
</s>
<s>
The	O
common	O
dictionary	O
here	O
,	O
however	O
,	O
is	O
less	O
than	O
ideal	O
since	O
it	O
is	O
initialized	O
with	O
more	O
commonly	O
used	O
ASCII	B-Protocol
printable	O
characters	O
put	O
after	O
little-used	O
control	O
codes	O
,	O
against	O
the	O
MTF	O
code	O
's	O
design	O
intent	O
of	O
keeping	O
what	O
's	O
commonly	O
used	O
in	O
the	O
front	O
.	O
</s>
<s>
The	O
MTF	B-Algorithm
transform	I-Algorithm
takes	O
advantage	O
of	O
local	O
correlation	O
of	O
frequencies	O
to	O
reduce	O
the	O
entropy	O
of	O
a	O
message	O
.	O
</s>
<s>
However	O
,	O
not	O
all	O
data	O
exhibits	O
this	O
type	O
of	O
local	O
correlation	O
,	O
and	O
for	O
some	O
messages	O
,	O
the	O
MTF	B-Algorithm
transform	I-Algorithm
may	O
actually	O
increase	O
the	O
entropy	O
.	O
</s>
<s>
An	O
important	O
use	O
of	O
the	O
MTF	B-Algorithm
transform	I-Algorithm
is	O
in	O
Burrows	B-Algorithm
–	I-Algorithm
Wheeler	I-Algorithm
transform	I-Algorithm
based	O
compression	B-General_Concept
.	O
</s>
<s>
The	O
Burrows	B-Algorithm
–	I-Algorithm
Wheeler	I-Algorithm
transform	I-Algorithm
is	O
very	O
good	O
at	O
producing	O
a	O
sequence	O
that	O
exhibits	O
local	O
frequency	O
correlation	O
from	O
text	O
and	O
certain	O
other	O
special	O
classes	O
of	O
data	O
.	O
</s>
<s>
Compression	B-General_Concept
benefits	O
greatly	O
from	O
following	O
up	O
the	O
Burrows	B-Algorithm
–	I-Algorithm
Wheeler	I-Algorithm
transform	I-Algorithm
with	O
an	O
MTF	B-Algorithm
transform	I-Algorithm
before	O
the	O
final	O
entropy-encoding	O
step	O
.	O
</s>
<s>
Naively	O
,	O
we	O
might	O
try	O
to	O
apply	O
the	O
MTF	B-Algorithm
transform	I-Algorithm
directly	O
.	O
</s>
<s>
However	O
,	O
if	O
we	O
first	O
apply	O
the	O
Burrows	B-Algorithm
–	I-Algorithm
Wheeler	I-Algorithm
transform	I-Algorithm
,	O
and	O
then	O
the	O
MTF	B-Algorithm
transform	I-Algorithm
,	O
we	O
get	O
a	O
message	O
with	O
6187	O
bits	O
.	O
</s>
<s>
Note	O
that	O
the	O
Burrows	B-Algorithm
–	I-Algorithm
Wheeler	I-Algorithm
transform	I-Algorithm
does	O
not	O
decrease	O
the	O
entropy	O
of	O
the	O
message	O
;	O
it	O
only	O
reorders	O
the	O
bytes	B-Application
in	O
a	O
way	O
that	O
makes	O
the	O
MTF	B-Algorithm
transform	I-Algorithm
more	O
effective	O
.	O
</s>
<s>
One	O
problem	O
with	O
the	O
basic	O
MTF	B-Algorithm
transform	I-Algorithm
is	O
that	O
it	O
makes	O
the	O
same	O
changes	O
for	O
any	O
character	O
,	O
regardless	O
of	O
frequency	O
,	O
which	O
can	O
result	O
in	O
diminished	O
compression	B-General_Concept
as	O
characters	O
that	O
occur	O
rarely	O
may	O
push	O
frequent	O
characters	O
to	O
higher	O
values	O
.	O
</s>
<s>
The	O
term	O
Move	B-Algorithm
To	I-Algorithm
Front	I-Algorithm
(	O
MTF	O
)	O
is	O
also	O
used	O
in	O
a	O
slightly	O
different	O
context	O
,	O
as	O
a	O
type	O
of	O
a	O
dynamic	O
linked	B-Data_Structure
list	I-Data_Structure
.	O
</s>
