Download Understanding Compression and Huffman Codes: A Guide to Data Compression Techniques - Prof and more Study notes Computer Science in PDF only on Docsity! 1 CMSC 132: Object-Oriented Programming II Compression & Huffman Codes Department of Computer Science University of Maryland, College Park 2 Overview Compression Examples Sources Types Effectiveness Huffman Code Properties Huffman tree (encoding) Decoding 5 Sources of Compressibility Redundancy Recognize repeating patterns Exploit using Dictionary Variable length encoding Human perception Less sensitive to some information Can discard less important data 6 Types of Compression Lossless Preserves all information Exploits redundancy in data Applied to general data Some lossless audio formats (e.g., FLAC) Lossy May lose some information Exploits redundancy & human perception Applied to audio, image, video, multimedia 7 Effectiveness of Compression Metrics Bits per byte (8 bits) 2 bits / byte ¼ original size 8 bits / byte no compression Percentage 75% compression ¼ original size 10 Lossless Compression Techniques LZW (Lempel-Ziv-Welch) compression Build pattern dictionary Replace patterns with index into dictionary Run length encoding Find & compress repetitive sequences Huffman code Use variable length codes based on frequency 11 Huffman Code Approach Variable length encoding of symbols Exploit statistical frequency of symbols Efficient when symbol probabilities vary widely Principle Use fewer bits to represent frequent symbols Use more bits to represent infrequent symbols A A B A A AA B 12 Huffman Code Example Expected size Original 1/82 + 1/42 + 1/22 + 1/82 = 2 bits / symbol Huffman 1/83 + 1/42 + 1/21 + 1/83 = 1.75 bits / symbol Symbol Dog Cat Bird Fish Frequency 1/8 1/4 1/2 1/8 Original Encoding 00 01 10 11 2 bits 2 bits 2 bits 2 bits Huffman Encoding 110 10 0 111 3 bits 2 bits 1 bit 3 bits 15 Huffman Code – Creating Tree Algorithm 1. Place each symbol in leaf Weight of leaf = symbol frequency 2. Select two trees L and R (initially leafs) Such that L, R have lowest frequencies in tree 3. Create new (internal) node Left child L Right child R New frequency frequency( L ) + frequency( R ) 4. Repeat until all nodes merged into one tree 16 Huffman Tree Construction 1 3 5 8 2 7 A C E H I 2 trees with lowest frequency 17 Huffman Tree Construction 2 3 5 82 7 5 A C EH I 2 trees with lowest frequency 20 Huffman Tree Construction 5 3 5 8 2 75 10 15 25 1 1 1 1 0 0 0 0 A C E H I E = 01 I = 00 C = 10 A = 111 H = 110 Huffman code for each leaf 21 Huffman Coding Example Huffman code Input ACE Output (111)(10)(01) = 1111001 E = 01 I = 00 C = 10 A = 111 H = 110 22 Huffman Code Algorithm Overview Decoding 1. Read compressed file & binary tree 2. Use binary tree to decode file Follow path from root to leaf 25 Huffman Decoding 3 3 5 8 2 75 10 15 25 1 1 1 1 0 0 0 0 A C E H I 1111001 A 26 Huffman Decoding 4 3 5 8 2 75 10 15 25 1 1 1 1 0 0 0 0 A C E H I 1111001 A 27 Huffman Decoding 5 3 5 8 2 75 10 15 25 1 1 1 1 0 0 0 0 A C E H I 1111001 AC 30 Huffman Code Properties Prefix code No code is a prefix of another code Example Huffman(“dog”) 01 Huffman(“cat”) 011 // not legal prefix code Can stop as soon as complete code found No need for end-of-code marker Nondeterministic Multiple Huffman coding possible for same input If more than two trees with same minimal weight 31 Huffman Code Properties Greedy algorithm Chooses best local solution at each step Combines 2 trees with lowest frequency Still yields overall best solution Optimal prefix code Based on statistical frequency Better compression possible (depends on data) Using other approaches (e.g., pattern dictionary)