<s>
In	O
computer	B-General_Concept
science	I-General_Concept
and	O
information	O
theory	O
,	O
a	O
Huffman	B-General_Concept
code	I-General_Concept
is	O
a	O
particular	O
type	O
of	O
optimal	O
prefix	B-Algorithm
code	I-Algorithm
that	O
is	O
commonly	O
used	O
for	O
lossless	B-Algorithm
data	I-Algorithm
compression	I-Algorithm
.	O
</s>
<s>
The	O
process	O
of	O
finding	O
or	O
using	O
such	O
a	O
code	O
proceeds	O
by	O
means	O
of	O
Huffman	B-General_Concept
coding	I-General_Concept
,	O
an	O
algorithm	O
developed	O
by	O
David	O
A	O
.	O
Huffman	O
while	O
he	O
was	O
a	O
Sc.D.	O
</s>
<s>
The	O
output	O
from	O
Huffman	B-General_Concept
's	I-General_Concept
algorithm	I-General_Concept
can	O
be	O
viewed	O
as	O
a	O
variable-length	B-Algorithm
code	I-Algorithm
table	O
for	O
encoding	O
a	O
source	O
symbol	O
(	O
such	O
as	O
a	O
character	O
in	O
a	O
file	O
)	O
.	O
</s>
<s>
As	O
in	O
other	O
entropy	B-Algorithm
encoding	I-Algorithm
methods	O
,	O
more	O
common	O
symbols	O
are	O
generally	O
represented	O
using	O
fewer	O
bits	O
than	O
less	O
common	O
symbols	O
.	O
</s>
<s>
However	O
,	O
although	O
optimal	O
among	O
methods	O
encoding	O
symbols	O
separately	O
,	O
Huffman	B-General_Concept
coding	I-General_Concept
is	O
not	O
always	O
optimal	O
among	O
all	O
compression	O
methods	O
-	O
it	O
is	O
replaced	O
with	O
arithmetic	B-Algorithm
coding	I-Algorithm
or	O
asymmetric	B-General_Concept
numeral	I-General_Concept
systems	I-General_Concept
if	O
a	O
better	O
compression	O
ratio	O
is	O
required	O
.	O
</s>
<s>
Building	O
the	O
tree	O
from	O
the	O
bottom	O
up	O
guaranteed	O
optimality	O
,	O
unlike	O
the	O
top-down	O
approach	O
of	O
Shannon	B-Algorithm
–	I-Algorithm
Fano	I-Algorithm
coding	I-Algorithm
.	O
</s>
<s>
Huffman	B-General_Concept
coding	I-General_Concept
uses	O
a	O
specific	O
method	O
for	O
choosing	O
the	O
representation	O
for	O
each	O
symbol	O
,	O
resulting	O
in	O
a	O
prefix	B-Algorithm
code	I-Algorithm
(	O
sometimes	O
called	O
"	O
prefix-free	B-Algorithm
codes	I-Algorithm
"	O
,	O
that	O
is	O
,	O
the	O
bit	O
string	O
representing	O
some	O
particular	O
symbol	O
is	O
never	O
a	O
prefix	O
of	O
the	O
bit	O
string	O
representing	O
any	O
other	O
symbol	O
)	O
.	O
</s>
<s>
Huffman	B-General_Concept
coding	I-General_Concept
is	O
such	O
a	O
widespread	O
method	O
for	O
creating	O
prefix	B-Algorithm
codes	I-Algorithm
that	O
the	O
term	O
"	O
Huffman	B-General_Concept
code	I-General_Concept
"	O
is	O
widely	O
used	O
as	O
a	O
synonym	O
for	O
"	O
prefix	B-Algorithm
code	I-Algorithm
"	O
even	O
when	O
such	O
a	O
code	O
is	O
not	O
produced	O
by	O
Huffman	B-General_Concept
's	I-General_Concept
algorithm	I-General_Concept
.	O
</s>
<s>
Find	O
A	O
prefix-free	B-Algorithm
binary	I-Algorithm
code	I-Algorithm
(	O
a	O
set	O
of	O
codewords	O
)	O
with	O
minimum	O
expected	O
codeword	O
length	O
(	O
equivalently	O
,	O
a	O
tree	O
with	O
minimum	O
weighted	O
path	O
length	O
from	O
the	O
root	O
)	O
.	O
</s>
<s>
We	O
give	O
an	O
example	O
of	O
the	O
result	O
of	O
Huffman	B-General_Concept
coding	I-General_Concept
for	O
a	O
code	O
with	O
five	O
characters	O
and	O
given	O
weights	O
.	O
</s>
<s>
As	O
a	O
consequence	O
of	O
Shannon	B-Protocol
's	I-Protocol
source	I-Protocol
coding	I-Protocol
theorem	I-Protocol
,	O
the	O
entropy	O
is	O
a	O
measure	O
of	O
the	O
smallest	O
codeword	O
length	O
that	O
is	O
theoretically	O
possible	O
for	O
the	O
given	O
alphabet	O
with	O
associated	O
weights	O
.	O
</s>
<s>
In	O
general	O
,	O
a	O
Huffman	B-General_Concept
code	I-General_Concept
need	O
not	O
be	O
unique	O
.	O
</s>
<s>
Thus	O
the	O
set	O
of	O
Huffman	B-General_Concept
codes	I-General_Concept
for	O
a	O
given	O
probability	O
distribution	O
is	O
a	O
non-empty	O
subset	O
of	O
the	O
codes	O
minimizing	O
for	O
that	O
probability	O
distribution	O
.	O
</s>
<s>
(	O
However	O
,	O
for	O
each	O
minimizing	O
codeword	O
length	O
assignment	O
,	O
there	O
exists	O
at	O
least	O
one	O
Huffman	B-General_Concept
code	I-General_Concept
with	O
those	O
lengths	O
.	O
)	O
</s>
<s>
A	O
node	O
can	O
be	O
either	O
a	O
leaf	B-Data_Structure
node	I-Data_Structure
or	O
an	O
internal	O
node	O
.	O
</s>
<s>
Initially	O
,	O
all	O
nodes	O
are	O
leaf	B-Data_Structure
nodes	I-Data_Structure
,	O
which	O
contain	O
the	O
symbol	O
itself	O
,	O
the	O
weight	O
(	O
frequency	O
of	O
appearance	O
)	O
of	O
the	O
symbol	O
and	O
optionally	O
,	O
a	O
link	O
to	O
a	O
parent	O
node	O
which	O
makes	O
it	O
easy	O
to	O
read	O
the	O
code	O
(	O
in	O
reverse	O
)	O
starting	O
from	O
a	O
leaf	B-Data_Structure
node	I-Data_Structure
.	O
</s>
<s>
A	O
finished	O
tree	O
has	O
up	O
to	O
leaf	B-Data_Structure
nodes	I-Data_Structure
and	O
internal	O
nodes	O
.	O
</s>
<s>
A	O
Huffman	B-General_Concept
tree	I-General_Concept
that	O
omits	O
unused	O
symbols	O
produces	O
the	O
most	O
optimal	O
code	O
lengths	O
.	O
</s>
<s>
The	O
process	O
begins	O
with	O
the	O
leaf	B-Data_Structure
nodes	I-Data_Structure
containing	O
the	O
probabilities	O
of	O
the	O
symbol	O
they	O
represent	O
.	O
</s>
<s>
We	O
then	O
apply	O
the	O
process	O
again	O
,	O
on	O
the	O
new	O
internal	O
node	O
and	O
on	O
the	O
remaining	O
nodes	O
(	O
i.e.	O
,	O
we	O
exclude	O
the	O
two	O
leaf	B-Data_Structure
nodes	I-Data_Structure
)	O
,	O
we	O
repeat	O
this	O
process	O
until	O
only	O
one	O
node	O
remains	O
,	O
which	O
is	O
the	O
root	O
of	O
the	O
Huffman	B-General_Concept
tree	I-General_Concept
.	O
</s>
<s>
The	O
simplest	O
construction	O
algorithm	O
uses	O
a	O
priority	B-Application
queue	I-Application
where	O
the	O
node	O
with	O
lowest	O
probability	O
is	O
given	O
highest	O
priority	O
:	O
</s>
<s>
Create	O
a	O
leaf	B-Data_Structure
node	I-Data_Structure
for	O
each	O
symbol	O
and	O
add	O
it	O
to	O
the	O
priority	B-Application
queue	I-Application
.	O
</s>
<s>
While	O
there	O
is	O
more	O
than	O
one	O
node	O
in	O
the	O
queue	B-Application
:	O
</s>
<s>
Add	O
the	O
new	O
node	O
to	O
the	O
queue	B-Application
.	O
</s>
<s>
Since	O
efficient	O
priority	B-Application
queue	I-Application
data	O
structures	O
require	O
O(log n )	O
time	O
per	O
insertion	O
,	O
and	O
a	O
tree	O
with	O
n	O
leaves	O
has	O
2n−1	O
nodes	O
,	O
this	O
algorithm	O
operates	O
in	O
O(n log n )	O
time	O
,	O
where	O
n	O
is	O
the	O
number	O
of	O
symbols	O
.	O
</s>
<s>
If	O
the	O
symbols	O
are	O
sorted	O
by	O
probability	O
,	O
there	O
is	O
a	O
linear-time	O
(O(n )	O
)	O
method	O
to	O
create	O
a	O
Huffman	B-General_Concept
tree	I-General_Concept
using	O
two	O
queues	B-Application
,	O
the	O
first	O
one	O
containing	O
the	O
initial	O
weights	O
(	O
along	O
with	O
pointers	O
to	O
the	O
associated	O
leaves	O
)	O
,	O
and	O
combined	O
weights	O
(	O
along	O
with	O
pointers	O
to	O
the	O
trees	O
)	O
being	O
put	O
in	O
the	O
back	O
of	O
the	O
second	O
queue	B-Application
.	O
</s>
<s>
This	O
assures	O
that	O
the	O
lowest	O
weight	O
is	O
always	O
kept	O
at	O
the	O
front	O
of	O
one	O
of	O
the	O
two	O
queues	B-Application
:	O
</s>
<s>
Enqueue	O
all	O
leaf	B-Data_Structure
nodes	I-Data_Structure
into	O
the	O
first	O
queue	B-Application
(	O
by	O
probability	O
in	O
increasing	O
order	O
so	O
that	O
the	O
least	O
likely	O
item	O
is	O
in	O
the	O
head	O
of	O
the	O
queue	B-Application
)	O
.	O
</s>
<s>
While	O
there	O
is	O
more	O
than	O
one	O
node	O
in	O
the	O
queues	B-Application
:	O
</s>
<s>
Dequeue	O
the	O
two	O
nodes	O
with	O
the	O
lowest	O
weight	O
by	O
examining	O
the	O
fronts	O
of	O
both	O
queues	B-Application
.	O
</s>
<s>
Enqueue	O
the	O
new	O
node	O
into	O
the	O
rear	O
of	O
the	O
second	O
queue	B-Application
.	O
</s>
<s>
Once	O
the	O
Huffman	B-General_Concept
tree	I-General_Concept
has	O
been	O
generated	O
,	O
it	O
is	O
traversed	O
to	O
generate	O
a	O
dictionary	O
which	O
maps	O
the	O
symbols	O
to	O
binary	O
codes	O
as	O
follows	O
:	O
</s>
<s>
If	O
node	O
is	O
not	O
a	O
leaf	B-Data_Structure
node	I-Data_Structure
,	O
label	O
the	O
edge	O
to	O
the	O
left	O
child	O
as	O
0	O
and	O
the	O
edge	O
to	O
the	O
right	O
child	O
as	O
1	O
.	O
</s>
<s>
To	O
minimize	O
variance	O
,	O
simply	O
break	O
ties	O
between	O
queues	B-Application
by	O
choosing	O
the	O
item	O
in	O
the	O
first	O
queue	B-Application
.	O
</s>
<s>
This	O
modification	O
will	O
retain	O
the	O
mathematical	O
optimality	O
of	O
the	O
Huffman	B-General_Concept
coding	I-General_Concept
while	O
both	O
minimizing	O
variance	O
and	O
minimizing	O
the	O
length	O
of	O
the	O
longest	O
character	O
code	O
.	O
</s>
<s>
Generally	O
speaking	O
,	O
the	O
process	O
of	O
decompression	O
is	O
simply	O
a	O
matter	O
of	O
translating	O
the	O
stream	O
of	O
prefix	B-Algorithm
codes	I-Algorithm
to	O
individual	O
byte	O
values	O
,	O
usually	O
by	O
traversing	O
the	O
Huffman	B-General_Concept
tree	I-General_Concept
node	O
by	O
node	O
as	O
each	O
bit	O
is	O
read	O
from	O
the	O
input	O
stream	O
(	O
reaching	O
a	O
leaf	B-Data_Structure
node	I-Data_Structure
necessarily	O
terminates	O
the	O
search	O
for	O
that	O
particular	O
byte	O
value	O
)	O
.	O
</s>
<s>
Before	O
this	O
can	O
take	O
place	O
,	O
however	O
,	O
the	O
Huffman	B-General_Concept
tree	I-General_Concept
must	O
be	O
somehow	O
reconstructed	O
.	O
</s>
<s>
If	O
the	O
data	O
is	O
compressed	O
using	O
canonical	B-Algorithm
encoding	I-Algorithm
,	O
the	O
compression	O
model	O
can	O
be	O
precisely	O
reconstructed	O
with	O
just	O
bits	O
of	O
information	O
(	O
where	O
is	O
the	O
number	O
of	O
bits	O
per	O
symbol	O
)	O
.	O
</s>
<s>
Another	O
method	O
is	O
to	O
simply	O
prepend	O
the	O
Huffman	B-General_Concept
tree	I-General_Concept
,	O
bit	O
by	O
bit	O
,	O
to	O
the	O
output	O
stream	O
.	O
</s>
<s>
For	O
example	O
,	O
assuming	O
that	O
the	O
value	O
of	O
0	O
represents	O
a	O
parent	O
node	O
and	O
1	O
a	O
leaf	B-Data_Structure
node	I-Data_Structure
,	O
whenever	O
the	O
latter	O
is	O
encountered	O
the	O
tree	O
building	O
routine	O
simply	O
reads	O
the	O
next	O
8	O
bits	O
to	O
determine	O
the	O
character	O
value	O
of	O
that	O
particular	O
leaf	O
.	O
</s>
<s>
The	O
process	O
continues	O
recursively	O
until	O
the	O
last	O
leaf	B-Data_Structure
node	I-Data_Structure
is	O
reached	O
;	O
at	O
that	O
point	O
,	O
the	O
Huffman	B-General_Concept
tree	I-General_Concept
will	O
thus	O
be	O
faithfully	O
reconstructed	O
.	O
</s>
<s>
Other	O
methods	O
such	O
as	O
arithmetic	B-Algorithm
coding	I-Algorithm
often	O
have	O
better	O
compression	O
capability	O
.	O
</s>
<s>
Although	O
both	O
aforementioned	O
methods	O
can	O
combine	O
an	O
arbitrary	O
number	O
of	O
symbols	O
for	O
more	O
efficient	O
coding	O
and	O
generally	O
adapt	O
to	O
the	O
actual	O
input	O
statistics	O
,	O
arithmetic	B-Algorithm
coding	I-Algorithm
does	O
so	O
without	O
significantly	O
increasing	O
its	O
computational	O
or	O
algorithmic	O
complexities	O
(	O
though	O
the	O
simplest	O
version	O
is	O
slower	O
and	O
more	O
complex	O
than	O
Huffman	B-General_Concept
coding	I-General_Concept
)	O
.	O
</s>
<s>
However	O
,	O
Huffman	B-General_Concept
coding	I-General_Concept
is	O
usually	O
faster	O
and	O
arithmetic	B-Algorithm
coding	I-Algorithm
was	O
historically	O
a	O
subject	O
of	O
some	O
concern	O
over	O
patent	O
issues	O
.	O
</s>
<s>
Thus	O
many	O
technologies	O
have	O
historically	O
avoided	O
arithmetic	B-Algorithm
coding	I-Algorithm
in	O
favor	O
of	O
Huffman	O
and	O
other	O
prefix	O
coding	O
techniques	O
.	O
</s>
<s>
As	O
of	O
mid-2010	O
,	O
the	O
most	O
commonly	O
used	O
techniques	O
for	O
this	O
alternative	O
to	O
Huffman	B-General_Concept
coding	I-General_Concept
have	O
passed	O
into	O
the	O
public	O
domain	O
as	O
the	O
early	O
patents	O
have	O
expired	O
.	O
</s>
<s>
For	O
a	O
set	O
of	O
symbols	O
with	O
a	O
uniform	O
probability	O
distribution	O
and	O
a	O
number	O
of	O
members	O
which	O
is	O
a	O
power	O
of	O
two	O
,	O
Huffman	B-General_Concept
coding	I-General_Concept
is	O
equivalent	O
to	O
simple	O
binary	O
block	O
encoding	O
,	O
e.g.	O
,	O
ASCII	B-Protocol
coding	O
.	O
</s>
<s>
Huffman	B-General_Concept
coding	I-General_Concept
is	O
optimal	O
among	O
all	O
methods	O
in	O
any	O
case	O
where	O
each	O
input	O
symbol	O
is	O
a	O
known	O
independent	O
and	O
identically	O
distributed	O
random	O
variable	O
having	O
a	O
probability	O
that	O
is	O
dyadic	O
.	O
</s>
<s>
Prefix	B-Algorithm
codes	I-Algorithm
,	O
and	O
thus	O
Huffman	B-General_Concept
coding	I-General_Concept
in	O
particular	O
,	O
tend	O
to	O
have	O
inefficiency	O
on	O
small	O
alphabets	O
,	O
where	O
probabilities	O
often	O
fall	O
between	O
these	O
optimal	O
(	O
dyadic	O
)	O
points	O
.	O
</s>
<s>
The	O
worst	O
case	O
for	O
Huffman	B-General_Concept
coding	I-General_Concept
can	O
happen	O
when	O
the	O
probability	O
of	O
the	O
most	O
likely	O
symbol	O
far	O
exceeds	O
2−1	O
=	O
0.5	O
,	O
making	O
the	O
upper	O
limit	O
of	O
inefficiency	O
unbounded	O
.	O
</s>
<s>
There	O
are	O
two	O
related	O
approaches	O
for	O
getting	O
around	O
this	O
particular	O
inefficiency	O
while	O
still	O
using	O
Huffman	B-General_Concept
coding	I-General_Concept
.	O
</s>
<s>
As	O
the	O
size	O
of	O
the	O
block	O
approaches	O
infinity	O
,	O
Huffman	B-General_Concept
coding	I-General_Concept
theoretically	O
approaches	O
the	O
entropy	O
limit	O
,	O
i.e.	O
,	O
optimal	O
compression	O
.	O
</s>
<s>
However	O
,	O
blocking	O
arbitrarily	O
large	O
groups	O
of	O
symbols	O
is	O
impractical	O
,	O
as	O
the	O
complexity	O
of	O
a	O
Huffman	B-General_Concept
code	I-General_Concept
is	O
linear	O
in	O
the	O
number	O
of	O
possibilities	O
to	O
be	O
encoded	O
,	O
a	O
number	O
that	O
is	O
exponential	O
in	O
the	O
size	O
of	O
a	O
block	O
.	O
</s>
<s>
A	O
practical	O
alternative	O
,	O
in	O
widespread	O
use	O
,	O
is	O
run-length	B-Algorithm
encoding	I-Algorithm
.	O
</s>
<s>
This	O
technique	O
adds	O
one	O
step	O
in	O
advance	O
of	O
entropy	B-Algorithm
coding	I-Algorithm
,	O
specifically	O
counting	O
(	O
runs	O
)	O
of	O
repeated	O
symbols	O
,	O
which	O
are	O
then	O
encoded	O
.	O
</s>
<s>
For	O
the	O
simple	O
case	O
of	O
Bernoulli	O
processes	O
,	O
Golomb	B-Algorithm
coding	I-Algorithm
is	O
optimal	O
among	O
prefix	B-Algorithm
codes	I-Algorithm
for	O
coding	O
run	B-Algorithm
length	I-Algorithm
,	O
a	O
fact	O
proved	O
via	O
the	O
techniques	O
of	O
Huffman	B-General_Concept
coding	I-General_Concept
.	O
</s>
<s>
A	O
similar	O
approach	O
is	O
taken	O
by	O
fax	O
machines	O
using	O
modified	B-Algorithm
Huffman	I-Algorithm
coding	I-Algorithm
.	O
</s>
<s>
However	O
,	O
run-length	B-Algorithm
coding	I-Algorithm
is	O
not	O
as	O
adaptable	O
to	O
as	O
many	O
input	O
types	O
as	O
other	O
compression	O
technologies	O
.	O
</s>
<s>
Many	O
variations	O
of	O
Huffman	B-General_Concept
coding	I-General_Concept
exist	O
,	O
some	O
of	O
which	O
use	O
a	O
Huffman-like	O
algorithm	O
,	O
and	O
others	O
of	O
which	O
find	O
optimal	O
prefix	B-Algorithm
codes	I-Algorithm
(	O
while	O
,	O
for	O
example	O
,	O
putting	O
different	O
restrictions	O
on	O
the	O
output	O
)	O
.	O
</s>
<s>
The	O
n-ary	O
Huffman	B-General_Concept
algorithm	I-General_Concept
uses	O
the	O
{	O
0	O
,	O
1	O
,...,	O
n	O
−	O
1}	O
alphabet	O
to	O
encode	O
message	O
and	O
build	O
an	O
n-ary	O
tree	O
.	O
</s>
<s>
Note	O
that	O
for	O
n	O
greater	O
than	O
2	O
,	O
not	O
all	O
sets	O
of	O
source	O
words	O
can	O
properly	O
form	O
an	O
n-ary	O
tree	O
for	O
Huffman	B-General_Concept
coding	I-General_Concept
.	O
</s>
<s>
If	O
the	O
number	O
of	O
source	O
words	O
is	O
congruent	O
to	O
1	O
modulo	O
n1	O
,	O
then	O
the	O
set	O
of	O
source	O
words	O
will	O
form	O
a	O
proper	O
Huffman	B-General_Concept
tree	I-General_Concept
.	O
</s>
<s>
A	O
variation	O
called	O
adaptive	B-Algorithm
Huffman	I-Algorithm
coding	I-Algorithm
involves	O
calculating	O
the	O
probabilities	O
dynamically	O
based	O
on	O
recent	O
actual	O
frequencies	O
in	O
the	O
sequence	O
of	O
source	O
symbols	O
,	O
and	O
changing	O
the	O
coding	B-General_Concept
tree	I-General_Concept
structure	O
to	O
match	O
the	O
updated	O
probability	O
estimates	O
.	O
</s>
<s>
It	O
is	O
used	O
rarely	O
in	O
practice	O
,	O
since	O
the	O
cost	O
of	O
updating	O
the	O
tree	O
makes	O
it	O
slower	O
than	O
optimized	O
adaptive	O
arithmetic	B-Algorithm
coding	I-Algorithm
,	O
which	O
is	O
more	O
flexible	O
and	O
has	O
better	O
compression	O
.	O
</s>
<s>
Most	O
often	O
,	O
the	O
weights	O
used	O
in	O
implementations	O
of	O
Huffman	B-General_Concept
coding	I-General_Concept
represent	O
numeric	O
probabilities	O
,	O
but	O
the	O
algorithm	O
given	O
above	O
does	O
not	O
require	O
this	O
;	O
it	O
requires	O
only	O
that	O
the	O
weights	O
form	O
a	O
totally	O
ordered	O
commutative	O
monoid	O
,	O
meaning	O
a	O
way	O
to	O
order	O
weights	O
and	O
to	O
add	O
them	O
.	O
</s>
<s>
Length-limited	O
Huffman	B-General_Concept
coding	I-General_Concept
is	O
a	O
variant	O
where	O
the	O
goal	O
is	O
still	O
to	O
achieve	O
a	O
minimum	O
weighted	O
path	O
length	O
,	O
but	O
there	O
is	O
an	O
additional	O
restriction	O
that	O
the	O
length	O
of	O
each	O
codeword	O
must	O
be	O
less	O
than	O
a	O
given	O
constant	O
.	O
</s>
<s>
The	O
package-merge	B-Algorithm
algorithm	I-Algorithm
solves	O
this	O
problem	O
with	O
a	O
simple	O
greedy	B-Algorithm
approach	O
very	O
similar	O
to	O
that	O
used	O
by	O
Huffman	B-General_Concept
's	I-General_Concept
algorithm	I-General_Concept
.	O
</s>
<s>
In	O
the	O
standard	O
Huffman	B-General_Concept
coding	I-General_Concept
problem	O
,	O
it	O
is	O
assumed	O
that	O
each	O
symbol	O
in	O
the	O
set	O
that	O
the	O
code	O
words	O
are	O
constructed	O
from	O
has	O
an	O
equal	O
cost	O
to	O
transmit	O
:	O
a	O
code	O
word	O
whose	O
length	O
is	O
N	O
digits	O
will	O
always	O
have	O
a	O
cost	O
of	O
N	O
,	O
no	O
matter	O
how	O
many	O
of	O
those	O
digits	O
are	O
0s	O
,	O
how	O
many	O
are	O
1s	O
,	O
etc	O
.	O
</s>
<s>
Huffman	B-General_Concept
coding	I-General_Concept
with	O
unequal	O
letter	O
costs	O
is	O
the	O
generalization	O
without	O
this	O
assumption	O
:	O
the	O
letters	O
of	O
the	O
encoding	O
alphabet	O
may	O
have	O
non-uniform	O
lengths	O
,	O
due	O
to	O
characteristics	O
of	O
the	O
transmission	O
medium	O
.	O
</s>
<s>
No	O
algorithm	O
is	O
known	O
to	O
solve	O
this	O
in	O
the	O
same	O
manner	O
or	O
with	O
the	O
same	O
efficiency	O
as	O
conventional	O
Huffman	B-General_Concept
coding	I-General_Concept
,	O
though	O
it	O
has	O
been	O
solved	O
by	O
whose	O
solution	O
has	O
been	O
refined	O
for	O
the	O
case	O
of	O
integer	O
costs	O
by	O
.	O
</s>
<s>
In	O
the	O
standard	O
Huffman	B-General_Concept
coding	I-General_Concept
problem	O
,	O
it	O
is	O
assumed	O
that	O
any	O
codeword	O
can	O
correspond	O
to	O
any	O
input	O
symbol	O
.	O
</s>
<s>
This	O
is	O
also	O
known	O
as	O
the	O
Hu	O
–	O
Tucker	O
problem	O
,	O
after	O
T	O
.	O
C	O
.	O
Hu	O
and	O
Alan	O
Tucker	O
,	O
the	O
authors	O
of	O
the	O
paper	O
presenting	O
the	O
first	O
-time	O
solution	O
to	O
this	O
optimal	O
binary	O
alphabetic	O
problem	O
,	O
which	O
has	O
some	O
similarities	O
to	O
Huffman	B-General_Concept
algorithm	I-General_Concept
,	O
but	O
is	O
not	O
a	O
variation	O
of	O
this	O
algorithm	O
.	O
</s>
<s>
A	O
later	O
method	O
,	O
the	O
Garsia	B-Data_Structure
–	I-Data_Structure
Wachs	I-Data_Structure
algorithm	I-Data_Structure
of	O
Adriano	O
Garsia	O
and	O
Michelle	O
L	O
.	O
Wachs	O
(	O
1977	O
)	O
,	O
uses	O
simpler	O
logic	O
to	O
perform	O
the	O
same	O
comparisons	O
in	O
the	O
same	O
total	O
time	O
bound	O
.	O
</s>
<s>
These	O
optimal	O
alphabetic	O
binary	O
trees	O
are	O
often	O
used	O
as	O
binary	B-Language
search	I-Language
trees	I-Language
.	O
</s>
<s>
If	O
weights	O
corresponding	O
to	O
the	O
alphabetically	O
ordered	O
inputs	O
are	O
in	O
numerical	O
order	O
,	O
the	O
Huffman	B-General_Concept
code	I-General_Concept
has	O
the	O
same	O
lengths	O
as	O
the	O
optimal	O
alphabetic	O
code	O
,	O
which	O
can	O
be	O
found	O
from	O
calculating	O
these	O
lengths	O
,	O
rendering	O
Hu	O
–	O
Tucker	O
coding	O
unnecessary	O
.	O
</s>
<s>
The	O
code	O
resulting	O
from	O
numerically	O
(	O
re	O
-	O
)	O
ordered	O
input	O
is	O
sometimes	O
called	O
the	O
canonical	B-Algorithm
Huffman	I-Algorithm
code	I-Algorithm
and	O
is	O
often	O
the	O
code	O
used	O
in	O
practice	O
,	O
due	O
to	O
ease	O
of	O
encoding/decoding	O
.	O
</s>
<s>
The	O
technique	O
for	O
finding	O
this	O
code	O
is	O
sometimes	O
called	O
Huffman	O
–	O
Shannon	B-Algorithm
–	I-Algorithm
Fano	I-Algorithm
coding	I-Algorithm
,	O
since	O
it	O
is	O
optimal	O
like	O
Huffman	B-General_Concept
coding	I-General_Concept
,	O
but	O
alphabetic	O
in	O
weight	O
probability	O
,	O
like	O
Shannon	B-Algorithm
–	I-Algorithm
Fano	I-Algorithm
coding	I-Algorithm
.	O
</s>
<s>
But	O
in	O
canonical	B-Algorithm
Huffman	I-Algorithm
code	I-Algorithm
,	O
the	O
result	O
is	O
.	O
</s>
<s>
Arithmetic	B-Algorithm
coding	I-Algorithm
and	O
Huffman	B-General_Concept
coding	I-General_Concept
produce	O
equivalent	O
results	O
achieving	O
entropy	O
when	O
every	O
symbol	O
has	O
a	O
probability	O
of	O
the	O
form	O
1/2k	O
.	O
</s>
<s>
In	O
other	O
circumstances	O
,	O
arithmetic	B-Algorithm
coding	I-Algorithm
can	O
offer	O
better	O
compression	O
than	O
Huffman	B-General_Concept
coding	I-General_Concept
because	O
intuitively	O
its	O
"	O
code	O
words	O
"	O
can	O
have	O
effectively	O
non-integer	O
bit	O
lengths	O
,	O
whereas	O
code	O
words	O
in	O
prefix	B-Algorithm
codes	I-Algorithm
such	O
as	O
Huffman	B-General_Concept
codes	I-General_Concept
can	O
only	O
have	O
an	O
integer	O
number	O
of	O
bits	O
.	O
</s>
<s>
Therefore	O
,	O
a	O
code	O
word	O
of	O
length	O
k	O
only	O
optimally	O
matches	O
a	O
symbol	O
of	O
probability	O
1/2k	O
and	O
other	O
probabilities	O
are	O
not	O
represented	O
optimally	O
;	O
whereas	O
the	O
code	O
word	O
length	O
in	O
arithmetic	B-Algorithm
coding	I-Algorithm
can	O
be	O
made	O
to	O
exactly	O
match	O
the	O
true	O
probability	O
of	O
the	O
symbol	O
.	O
</s>
<s>
Prefix	B-Algorithm
codes	I-Algorithm
nevertheless	O
remain	O
in	O
wide	O
use	O
because	O
of	O
their	O
simplicity	O
,	O
high	O
speed	O
,	O
and	O
lack	O
of	O
patent	O
coverage	O
.	O
</s>
<s>
Deflate	B-Algorithm
(	O
PKZIP	B-Application
's	O
algorithm	O
)	O
and	O
multimedia	B-General_Concept
codecs	I-General_Concept
such	O
as	O
JPEG	O
and	O
MP3	B-Application
have	O
a	O
front-end	O
model	O
and	O
quantization	B-Algorithm
followed	O
by	O
the	O
use	O
of	O
prefix	B-Algorithm
codes	I-Algorithm
;	O
these	O
are	O
often	O
called	O
"	O
Huffman	B-General_Concept
codes	I-General_Concept
"	O
even	O
though	O
most	O
applications	O
use	O
pre-defined	O
variable-length	B-Algorithm
codes	I-Algorithm
rather	O
than	O
codes	O
designed	O
using	O
Huffman	B-General_Concept
's	I-General_Concept
algorithm	I-General_Concept
.	O
</s>
