10.07.2015 Views

Information Theory, Inference, and Learning ... - Inference Group

Information Theory, Inference, and Learning ... - Inference Group

Information Theory, Inference, and Learning ... - Inference Group

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Copyright Cambridge University Press 2003. On-screen viewing permitted. Printing not permitted. http://www.cambridge.org/0521642981You can buy this book for 30 pounds or $50. See http://www.inference.phy.cam.ac.uk/mackay/itila/ for links.5.5: Optimal source coding with symbol codes: Huffman coding 99The Huffman coding algorithmWe now present a beautifully simple algorithm for finding an optimal prefixcode. The trick is to construct the code backwards starting from the tails ofthe codewords; we build the binary tree from its leaves.1. Take the two least probable symbols in the alphabet. These twosymbols will be given the longest codewords, which will have equallength, <strong>and</strong> differ only in the last digit.Algorithm 5.4. Huffman codingalgorithm.2. Combine these two symbols into a single symbol, <strong>and</strong> repeat.Since each step reduces the size of the alphabet by one, this algorithm willhave assigned strings to all the symbols after |A X | − 1 steps.Example 5.15. Let A X = { a, b, c, d, e }<strong>and</strong> P X = { 0.25, 0.25, 0.2, 0.15, 0.15 }.x step 1 step 2 step 3 step 40 0a 0.25 0.25 0.25 0.55 1.00 ✂ b 0.25 0.25 0.45 ✂ 0.45 1 ✂c 0.2 0.2 1 ✂0✂d 0.15 0.3 0.31e 0.15 1The codewords are then obtained by concatenating the binary digits inreverse order: C = {00, 10, 11, 010, 011}. The codelengths selectedby the Huffman algorithm (column 4 of table 5.5) are in some caseslonger <strong>and</strong> in some cases shorter than the ideal codelengths, the Shannoninformation contents log 21/ pi (column 3). The expected length of thecode is L = 2.30 bits, whereas the entropy is H = 2.2855 bits. ✷a i p i h(p i ) l i c(a i )a 0.25 2.0 2 00b 0.25 2.0 2 10c 0.2 2.3 2 11d 0.15 2.7 3 010e 0.15 2.7 3 011Table 5.5. Code created by theHuffman algorithm.If at any point there is more than one way of selecting the two least probablesymbols then the choice may be made in any manner – the expected length ofthe code will not depend on the choice.Exercise 5.16. [3, p.105] Prove that there is no better symbol code for a sourcethan the Huffman code.Example 5.17. We can make a Huffman code for the probability distributionover the alphabet introduced in figure 2.1. The result is shown in figure5.6. This code has an expected length of 4.15 bits; the entropy ofthe ensemble is 4.11 bits. Observe the disparities between the assignedcodelengths <strong>and</strong> the ideal codelengths log 21/ pi.Constructing a binary tree top-down is suboptimalIn previous chapters we studied weighing problems in which we built ternaryor binary trees. We noticed that balanced trees – ones in which, at every step,the two possible outcomes were as close as possible to equiprobable – appearedto describe the most efficient experiments. This gave an intuitive motivationfor entropy as a measure of information content.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!