Download Compression and Huffman Codes: Understanding Data Compression Techniques and more Study notes Computer Science in PDF only on Docsity! 1 Compression & Huffman Codes CMSC 132 Department of Computer Science University of Maryland, College Park 2 Overview Compression Compression Examples Types of Compression Effectiveness of Compression Huffman Code Huffman Code Properties 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 Lossy May lose some information Exploits redundancy & human perception Applied to audio, image, video 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/8×2 + 1/4×2 + 1/2×2 + 1/8×2 = 2 bits / symbol Huffman ⇒ 1/8×3 + 1/4×2 + 1/2×1 + 1/8×3 = 1.75 bits / symbol Symbol 3 bits1 bit2 bits3 bits 111010110Huffman Encoding 11100100 2 bits 1/2 Bird 1/81/41/8Frequency 2 bits2 bits2 bits Original Encoding FishCatDog 15 Huffman Code – Creating Tree Algorithm Place each symbol in leaf Weight of leaf = symbol frequency Select two trees L and R (initially leafs) Such that L, R have lowest frequencies in tree Create new (internal) node Left child ⇒ L Right child ⇒ R New frequency ⇒ frequency( L ) + frequency( R ) Repeat until all nodes merged into one tree 16 Huffman Tree Construction 1 3 5 8 2 7 A C E H I 17 Huffman Tree Construction 2 3 5 82 7 5 A C EH I 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 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 Read compressed file & binary tree 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)