Presentation is loading. Please wait.

Presentation is loading. Please wait.

Huffman Coding Dr. Ying Lu RAIK 283 Data Structures & Algorithms.

Similar presentations


Presentation on theme: "Huffman Coding Dr. Ying Lu RAIK 283 Data Structures & Algorithms."— Presentation transcript:

1 Huffman Coding Dr. Ying Lu ylu@cse.unl.edu RAIK 283 Data Structures & Algorithms

2 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 RAIK 283 Data Structures & Algorithms

3 Coding theory ASCII coding Conversion, Encryption, Compression Binary coding A B C D E F For fixed-length binary coding of a 6-character alphabet, how many bits are needed?

4 Coding theory (cont.) ASCII coding Conversion, Encryption, Compression Binary coding A000 B001 C010 D011 E100 F101

5 Coding theory (cont.) ASCII coding Conversion, Encryption, Compression Binary coding Variable length coding A000000.3 B0010100.1 C0100110.1 D0111000.1 E100110.3 F101 0.1 Probability Average bits/character = ? Compression Ratio = ?

6 Decode the following E0 T11 N100 I1010 S1011 11010010010101011 E0 T10 N100 I0111 S1010 100100101010

7 Prefix(-free) codes No prefix of a codeword is a codeword Uniquely decodable A001 B0100110 C01100111 D1000001 E110000111000 F101000001101

8 Prefix codes and binary trees Tree representation of prefix codes A00 B010 C0110 D0111 E10 F11 A 0 B 0 0 0 0 1 1 1 1 1 C D E F

9 Minimum length code A1/4 B1/8 C1/16 D E1/2 Probability How to code so that average bits/character is minimized?

10 Minimum length code (cont.) Huffman tree – prefix codes tree with minimum weighted path length C(T) – weighted path length

11 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

12 Huffman code H = new minHeap() for each w i T = new Tree(w i ) H.Insert(T) while H.Size() > 1 T 1 = H.DeleteMin() T 2 = H.DeleteMin() T 3 = Merge(T 1, T 2 ) H.Insert(T 3 )

13 Example characterABCD_ probability0.350.10.2 0.15

14 In-class exercises P332 Exercises 9.4.1

15 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.


Download ppt "Huffman Coding Dr. Ying Lu RAIK 283 Data Structures & Algorithms."

Similar presentations


Ads by Google