10.2 Arithmetic coding 187
Consider the basic example of a source X =
{
a, b, c
}
with probabilities p
X
(x
i
) =
{
0.4, 0.4, 0.2
}
. We assume that symbol c is exclusively used to signal the end of message.
The interval containing all real values p such that 0 ≤ p < 1 is noted [0, 1). Our encoder’s
program then proceeds as follows (Fig. 10.1):
r
Step 1: The interval [0, 1) is first divided into three subintervals [0.0, 0.4), [0.4, 0.8),
and [0.8, 1.0), corresponding to the symbol events a, b, and c, respectively. The interval
[0,1) is also divided into two equal regions, labeled in binary with prefixes 0 and 1,
as shown in the right-hand side. We observe from the figure that the prefix 1 so far
corresponds to either a or b, and the prefix 0 to either b or c.
r
Step 2: Each of the previous subintervals, except the last one [0.8, 1.0), corresponding
to event c, is divided into three subintervals, the widths of which correspond to
the joint probabilities p(x
1
, x
2
) = p(x
2
|x
1
)p(x
1
) of either joint events aa, ab, ac,or
ba, bb, bc. The regions corresponding to labels 0 and 1 are also divided into two
equal parts, which are labeled with prefixes 00, 01, 10, and 11. We observe from the
figure that:
◦
The prefix 11 corresponds to either aa or ab;
◦
The prefix 10 corresponds to either ab, ac,orba;
◦
The prefix 01 corresponds to either ba, bb,orbc;
◦
The prefix 00 corresponds to either bc or c;
r
Step 3: Each of the previous subintervals, except the last ones corresponding to final
events c, is divided again into three subintervals, the widths of which correspond to
the joint probabilities p(x
1
, x
2
, x
3
) = p(x
3
|x
1
, x
2
)p(x
3
) of joint events aaa, aab, aac,
aba, abb, abc, baa, bab, bac, bba, bbb, bbc. The region corresponding to prefixes
00, 01, 10, and 11 are also divided into two equal parts, which are labeled 000 to
111 and correspond to different joint-event possibilities, except for 000 which is
exclusively attached to event c.
The encoder is capable of executing the above steps an arbitrary number of times in
order to find the prefix attached to a string of any length and ending in c. To clarify
this point, assume that the string (or joint event) to encode is bc. We observe from
Fig. 10.1(a) that the unique subinterval which is fully contained in the region defined
by string bc has the prefix 00111. We can then use this prefix as the unique codeword
for string bc. The same observation applies, for instance, to the string aac, which
gets 11001 as a unique prefix and codeword. Assume next that the machine must
encode the string babc. The magnification in Fig. 10.1(b) shows that the subinterval
corresponding to prefix 1000001 is the only one to be fully contained in the region
defined by string babc, and, therefore, it should be used as the unique codeword. In
summary, the encoder assigns a unique codeword to any symbol string (or joint event)
x
1
x
2
,...,x
n
c by slicing down the interval [0, 1) into as many subintervals of widths
p(x
1
, x
2
,...,x
n
, c) = p(c|x
1
, x
2
,...,x
n
)p(c). The algorithm to perform this operation
is described in detail in Appendix H. Our description represents a simplification of the
more general algorithm described in MacKay (2003).
7
7
D. J. C. MacKay, A Short Course in Information Theory (Cambridge, UK: Cambridge University Press,
2003).