Løsningsforslag, Ukeoppgaver nr 9 INF2310, våren 2007. Assignment 1: Arithmetic Coding

Løsningsforslag, Ukeoppgaver nr 9 INF2310,
våren 2007.
Assignment 1: Arithmetic Coding
Huffman coding and the like use an integer number (k) of bits for each symbol, hence k is never
less than 1. Sometimes, e.g., when sending a 1-bit image, compression becomes impossible.
Suppose that the alphabet is X and Y (alias 0 and 1), and that the normalized probability of X is
known. Show how you can generate codewords of unequal length for all the possible 2-symbol
messages, and find the bit-pattern for each codeword, given that p(X) =2/3.
If p(x) = 2/3, then p(Y) =1-p(X) = 1/3
If we are only concerned with encoding length 2 messages, then we can map all possible
messages of length 2 onto intervals in the range [0..1] as shown below
To encode any one of the four possible length 2 message, just send enough bits of a binary
fraction that uniquely specifies the proper interval. A solution is as follows:
Similarly, map all possible length 3 messages to intervals in the range [0..1], and find the
codewords for all possible messages of length 3.
How would you encode X Y X X Y X ? Do you get any compression?
Pairwise compression gives 7 bits instead of 6. Triplets give 6 bits. So we need longer sequences
to gain anything.
Is there a relation between the width of the intervals and the number of bits used to encode them?
In general, you need – log(p) bits to represent interval of size p. So you will approach optimal
encoding as message length approaches infinity.
Assignment 2:
Write a java-program that implements Huffman coding of an image, given the normalized
You are NOT expected to implement the actual storage of code words of different lengths.
You ARE expected to generate the code words for the pixel values of the image (the code book),
so that you can compute the average number of bits per pixel, as well as the entropy.
At http://folk.uio.no/inf1040/java/huffman.html you will find a JAVA-program to encode and
decode text. This may be useful in order to check that your own implementation produces the
proper result.