160 likes | 334 Views
RAIK 283 Data Structures & Algorithms. Huffman Coding Dr. Ying Lu ylu@cse.unl.edu. RAIK 283 Data Structures & Algorithms. Giving credit where credit is due: Most of slides for this lecture are based on slides created by Dr. Richard Anderson, University of Washington.
E N D
RAIK 283 Data Structures & Algorithms Huffman Coding Dr. Ying Lu ylu@cse.unl.edu
RAIK 283 Data Structures & Algorithms • Giving credit where credit is due: • Most of slides for this lecture are based on slides created by Dr. Richard Anderson, University of Washington. • I have modified them and added new slides
Coding theory Code examples 000,001,010,011,100,101 1,01,001,0001,00001,000001 00,010,011,100,11,101 • ASCII coding • Conversion, Encryption, Compression • Binary coding For fixed-length binary coding of a 6-character alphabet, how many bits are needed?
Coding theory (cont.) Code examples 000,001,010,011,100,101 1,01,001,0001,00001,000001 00,010,011,100,11,101 • ASCII coding • Conversion, Encryption, Compression • Binary coding
Coding theory (cont.) Code examples 000,001,010,011,100,101 1,01,001,0001,00001,000001 00,010,011,100,11,101 Probability • ASCII coding • Conversion, Encryption, Compression • Binary coding • Variable length coding Average bits/character = ? Compression Ratio = ?
Decode the following 11010010010101011 100100101010 Prefix code Ambiguous
Prefix(-free) codes • No prefix of a codeword is a codeword • Uniquely decodable
Prefix codes and binary trees • Tree representation of prefix codes 0 1 0 1 1 0 F A 0 1 E B 0 1 D C
Minimum length code How to code so that average bits/character is minimized? Probability
Minimum length code (cont.) • Huffman tree – prefix codes tree with minimum weighted path length • C(T) – weighted path length
Huffman code algorithm • Derivation • Two rarest items will have the longest codewords • Codewords for rarest items differ only in the last bit • Idea: suppose the weights are with and the smallest weights • Start with an optimal code for and • Extend the codeword for to get codewords for and
Huffman code H = new minHeap() for each wi T = new Tree(wi) H.Insert(T) while H.Size() > 1 T1 = H.DeleteMin() T2 = H.DeleteMin() T3 = Merge(T1, T2) H.Insert(T3)
Example 21 14 11 6 7 4 5
In-class exercises • P332 Exercises 9.4.1
In-class exercises • 9.4.3 What is the maximal length of a codeword possible in a Huffman encoding of an alphabet of n characters? • 9.4.5 Show that a Huffman tree can be constructed in linear time if the alphabet’s characters are given in a sorted order of their frequencies.