CSC 310, Spring 2004 - Assignment #1 Solutions
CSC 310, Spring 2004 - Assignment #1 Solutions
CSC 310, Spring 2004 - Assignment #1 Solutions
a) Explain why the code with codewords {0, 0010, 0001100} is uniquely decodable. A
convincing informal explanation is OK.
This code can be decoded by looking for ocurrences of the substrings 010 and 0110. The
first can occur only as part of the codeword 0010, the second only as part of the codeword
0001100. After finding these substrings, mark the bits that make up the corresponding
codewords. (Note that the occurrences of 010 and 0110 cannot overlap, since clearly two
consecutive 1s will be separated by at least three 0s in a string produced by this code.)
The remaining unmarked bits will all be 0s, and each of those bits is an instance of the
first codeword, 0.
c) Show that the code with codewords {0, 0010, 000100} is not uniquely decodable, by
giving an example of a string of bits that can be decoded in more than one way.
The string 000100 can be decoded as the single codeword 000100 or as the three codewords
0, 0010, and 0.
a1 : 11
a2 : 000
a3 : 01
a4 : 001
a5 : 100
a6 : 101
c) Compute the expected codeword length for the Huffman code you found in part (b).
0.27 × 2 + 0.09 × 3 + 0.23 × 2 + 0.11 × 3 + 0.15 × 3 + 0.15 × 3 = 2.5
d) Find an optimal instantaneous code for this source that is not a Huffman code. (Your
code should not just be different from the Huffman code you found in part (b); it must
also be different from any other Huffman code that could be obtained by changing the
way arbitrary choices are made in the Huffman code algorithm.)
We can get another optimal code from this Huffman code by swapping the assignment of
codewords to symbols for two symbols whose codewords are of equal length. For example,
swapping the codewords for a4 and a5 produces the following code, which is uniquely
decodable and also has minimum expected codeword length:
a1 : 11
a2 : 000
a3 : 01
a4 : 100
a5 : 001
a6 : 101
It is uniquely decodable because the set of codewords is identical to the Huffman code
above. It is optimal because every symbol has a codeword of the same length as the
Huffman code. But it cannot be a Huffman code, because the two symbols with smallest
probability, a2 and a4 , have codewords that differ in more than the last bit (whereas in
any Huffman code, these codewords will be “siblings” in the tree).
Question 3 (30 marks): Let C be a uniquely-decodable binary code for a source with
symbol probabilities p1 , . . . , pI in which the codewords for these symbols have lengths l1 , . . . , lI .
Suppose that for some distinct i, j, and k, li = lj = lk . Prove that if C is optimal (ie, has
minimal expected codeword length), then pi ≤ pj +pk . Caution: C is not necessarily a Huffman
code; it might not even be instantaneous.
First proof: We will suppose that pi > pj +pk and derive a contradiction. Since C is uniquely
decodable, it satisfies the Kraft-McMillan inequality, which can be written as
1 1 1 X 1
l
+ lj + l + ≤ 1
2i 2 2k h∈{i,j,k}
/
2lh
The last inequality comes about because pi − pj − pk is positive, since we have assumed that
pi > pj + pk . The right side of this inequality is the expected codeword length for the original
code, C. But if C 0 has smaller expected codeword length than C, C cannot be an optimal code.
So we see that assuming pi > pj + pk leads to a contradiction. This assumption must therefore
be wrong, and hence pi ≤ pj + pk .
Second proof: We will suppose that pi > pj + pk and derive a contradiction. Since C is
uniquely decodable, the lengths of its codewords must satisfy the Kraft-McMillan inequality.
Since these lengths satisfy the Kraft-McMillan inequality, there exists an instantaneous code
with these same codeword lengths. Let C 0 be such an instantaneous code.
Since C 0 is instantaneous, it can be represented as a tree, with codewords at the leaves. Consider
the leaves corresponding to the codewords for symbols ai , aj , and ak , all of which will be at the
same level in the tree. If the codewords for aj and ak are not siblings, swap the codeword for
aj and the subtree that is the sibling of ak to produce another instantaneous code, C 00 , with the
same codeword lengths as C 0 (and C), but in which the codewords for aj and ak are siblings.
The codeword for ai in C 00 can be written as xb, where x is some string of bits and b is a single
bit. The codewords for aj and ak will have the forms y0 and y1, where y is some string of bits,
which will be the same length as x. We now create a new code, C 000 , in which the codeword
for ai is y, and the codewords for aj and ak are xb0 and xb1, with the codewords for all other
symbols being the same as in C 00 . (This change is most easily visualized by drawing the code
trees.) It is easy to see that C 000 is an instantaneous code. Since C 000 encodes a1 using one less
bit than C 00 , and a2 and a3 using one more bit than C 00 , the difference of the expected codeword
length of C 000 and that of C 00 is −p1 + pj + pk . If p1 > pj + pk , this difference is negative,
which is impossible if C 00 is optimal. Since assuming pi > pj + pk leads to a contradiction, this
assumption must be wrong, and hence pi ≤ pj + pk .
Question 4 (30 marks total): Suppose a source produces independent symbols from the
alphabet {a1 , a2 , a3 }, with probabilities p1 = 0.4999999, p2 = 0.4999999, and p3 = 0.0000002.
a1 : 0
a2 : 10
a3 : 11
c) Find an optimal code for the second extension of this source (ie, for blocks of two sym-
bols), and compute its expected codeword length, and the expected codeword length
divided by two.
Here is one optimal code:
(a1 , a1 ) : 00
(a1 , a2 ) : 01
(a2 , a1 ) : 10
(a2 , a2 ) : 110
(a1 , a3 ) : 11100
(a2 , a3 ) : 11101
(a3 , a1 ) : 11110
(a3 , a2 ) : 111110
(a3 , a3 ) : 111111
Its expected codeword length is approximately 2.2500012, which divided by two is approx-
imately 1.1250006.
d) Prove (without any tedious calculations) that in order to compress to within 1% of the
entropy by encoding blocks of size N from this source, N will have to be at least 5.
When N is less than 5, the 2N blocks in which all symbols are either a1 or a2 will be much
more probable than all other blocks. An optimal code for the N -th extension will assign
codewords of length N to all but one of these high-probability blocks, and a codeword of
length N + 1 to the remaining high-probability block. (Other blocks will have codewords
of length greater than N + 1. The expected codeword length for such a code will be greater
than