Chapter 4 Variable–Length and Huffman Codes. Unique Decodability We must always be able to determine where one code word ends and the next one begins.

Slides:



Advertisements
Similar presentations
Introduction to Algorithms
Advertisements

Convolutional Codes Mohammad Hanaysheh Mahdi Barhoush.
Computational Complexity
Question (from exercises 2) Are the following sources likely to be stationary and ergodic? (i)Binary source, typical sequence aaaabaabbabbbababbbabbbbbabbbbaabbbbbba......
Greedy Technique Constructs a solution to an optimization problem piece by piece through a sequence of choices that are: feasible, i.e. satisfying the.
Chapter 9 Greedy Technique. Constructs a solution to an optimization problem piece by piece through a sequence of choices that are: b feasible - b feasible.
A New Recombination Lower Bound and The Minimum Perfect Phylogenetic Forest Problem Yufeng Wu and Dan Gusfield UC Davis COCOON07 July 16, 2007.
Binomial Heaps. Heap Under most circumstances you would use a “normal” binary heap Except some algorithms that may use heaps might require a “Union” operation.
Finite-state Recognizers
Synthesis For Finite State Machines. FSM (Finite State Machine) Optimization State tables State minimization State assignment Combinational logic optimization.
Finite State Machines Finite state machines with output
Synchronization of Huffman codes Marek Biskup Warsaw University Phd-Open,
Introduction to Computer Science 2 Lecture 7: Extended binary trees
Lecture 3: Source Coding Theory TSBK01 Image Coding and Data Compression Jörgen Ahlberg Div. of Sensor Technology Swedish Defence Research Agency (FOI)
Algorithm Design Techniques: Greedy Algorithms. Introduction Algorithm Design Techniques –Design of algorithms –Algorithms commonly used to solve problems.
Michael Alves, Patrick Dugan, Robert Daniels, Carlos Vicuna
Greedy Algorithms Amihood Amir Bar-Ilan University.
Greedy Algorithms Greed is good. (Some of the time)
Recursive Definitions and Structural Induction
22C:19 Discrete Structures Trees Spring 2014 Sukumar Ghosh.
Source Coding Data Compression A.J. Han Vinck. DATA COMPRESSION NO LOSS of information and exact reproduction (low compression ratio 1:4) general problem.
Bounds on Code Length Theorem: Let l ∗ 1, l ∗ 2,..., l ∗ m be optimal codeword lengths for a source distribution p and a D-ary alphabet, and let L ∗ be.
Huffman Encoding Dr. Bernard Chen Ph.D. University of Central Arkansas.
Data Compressor---Huffman Encoding and Decoding. Huffman Encoding Compression Typically, in files and messages, Each character requires 1 byte or 8 bits.
Data Compression.
Entropy and Shannon’s First Theorem
1 Huffman Codes. 2 Introduction Huffman codes are a very effective technique for compressing data; savings of 20% to 90% are typical, depending on the.
Lecture 6: Huffman Code Thinh Nguyen Oregon State University.
Optimal Merging Of Runs
CPSC 411, Fall 2008: Set 4 1 CPSC 411 Design and Analysis of Algorithms Set 4: Greedy Algorithms Prof. Jennifer Welch Fall 2008.
DL Compression – Beeri/Feitelson1 Compression דחיסה Introduction Information theory Text compression IL compression.
Information Theory Eighteenth Meeting. A Communication Model Messages are produced by a source transmitted over a channel to the destination. encoded.
Data Structures – LECTURE 10 Huffman coding
Lecture Recursive Definitions. Fractals fractals are examples of images where the same elements is being recursively.
4.8 Huffman Codes These lecture slides are supplied by Mathijs de Weerd.
Variable-Length Codes: Huffman Codes
Greedy Algorithms Huffman Coding
CSI Uncertainty in A.I. Lecture 201 Basic Information Theory Review Measuring the uncertainty of an event Measuring the uncertainty in a probability.
CPSC 411, Fall 2008: Set 4 1 CPSC 411 Design and Analysis of Algorithms Set 4: Greedy Algorithms Prof. Jennifer Welch Fall 2008.
CS420 lecture eight Greedy Algorithms. Going from A to G Starting with a full tank, we can drive 350 miles before we need to gas up, minimize the number.
Huffman Codes Message consisting of five characters: a, b, c, d,e
Richard W. Hamming Learning to Learn The Art of Doing Science and Engineering Session 10: Coding Theory I Learning to Learn The Art of Doing Science and.
4.8 Huffman Codes These lecture slides are supplied by Mathijs de Weerd.
Data Structures and Algorithms Lecture (BinaryTrees) Instructor: Quratulain.
Lossless Compression CIS 465 Multimedia. Compression Compression: the process of coding that will effectively reduce the total number of bits needed to.
Introduction to Algorithms Chapter 16: Greedy Algorithms.
Huffman coding Content 1 Encoding and decoding messages Fixed-length coding Variable-length coding 2 Huffman coding.
Huffman Code and Data Decomposition Pranav Shah CS157B.
Agenda Review: –Planar Graphs Lecture Content:  Concepts of Trees  Spanning Trees  Binary Trees Exercise.
Huffman Codes Juan A. Rodriguez CS 326 5/13/2003.
Foundation of Computing Systems
Bahareh Sarrafzadeh 6111 Fall 2009
Trees (Ch. 9.2) Longin Jan Latecki Temple University based on slides by Simon Langley and Shang-Hua Teng.
Lossless Decomposition and Huffman Codes Sophia Soohoo CS 157B.
Huffman Codes. Overview  Huffman codes: compressing data (savings of 20% to 90%)  Huffman’s greedy algorithm uses a table of the frequencies of occurrence.
CompSci 102 Discrete Math for Computer Science March 13, 2012 Prof. Rodger Slides modified from Rosen.
1 Data Compression Hae-sun Jung CS146 Dr. Sin-Min Lee Spring 2004.
1 Huffman Codes. 2 ASCII use same size encoding for all characters. Variable length codes can produce shorter messages than fixed length codes Huffman.
Chapter 11. Chapter Summary  Introduction to trees (11.1)  Application of trees (11.2)  Tree traversal (11.3)  Spanning trees (11.4)
ENTROPY Entropy measures the uncertainty in a random experiment. Let X be a discrete random variable with range S X = { 1,2,3,... k} and pmf p k = P X.
4.8 Huffman Codes These lecture slides are supplied by Mathijs de Weerd.
Proving the Correctness of Huffman’s Algorithm
Optimal Merging Of Runs
Optimal Merging Of Runs
Advanced Algorithms Analysis and Design
Trees Addenda.
Huffman Coding Greedy Algorithm
Proving the Correctness of Huffman’s Algorithm
Analysis of Algorithms CS 477/677
Presentation transcript:

Chapter 4 Variable–Length and Huffman Codes

Unique Decodability We must always be able to determine where one code word ends and the next one begins. Counterexample: Suppose: s 1 = 0; s 2 = 1; s 3 = 11; s 4 = = s 4 s 3 or s 1 s 1 s 3 Unique decodability means that any two distinct sequences of symbols (of possibly differing lengths) result in distinct code words. 4.1, 2

Instantaneous Codes No code word is the prefix of another. By reading a continuous sequence of code words, one can instantaneously determine the end of each code word. Consider the reverse: s 1 = 0; s 2 = 01; s 3 = 011; s 4 = ……111 is uniquely decodable, but the first symbol cannot be decoded without reading all the way to the end. s1s1 s2s2 s3s decoding tree s4s4 4.3 s 1 = 0 s 2 = 10 s 3 = 110 s 4 = 111

Constructing Instantaneous Codes comma code:s 1 = 0 s 2 = 10 s 3 = 110 s 4 = 1110 s 5 = 1111 modification:s 1 = 00 s 2 = 01 s 3 =10 s 4 = 110 s 5 = 111 s 1 = 00s 2 = 01s 3 = 10 s 4 = 110s 5 = Decoding tree Notice that every code word is located on the leaves 4.4

Kraft Inequality Basis: n = 1 s1s1 0,1 s1s1 0 s2s2 1 or Induction: n > 1 01 <n T0T0 T1T1 Prefixing one symbol at top of tree increases all the lengths by one, so Theorem:There exists an instantaneous code for S where each symbol s  S is encoded in radix r with length |s| if and only if Proof: (  ) By induction on the height (maximal length path) of the decoding tree, max{|s|: s  S}. For simplicity, pick r = 2 (the binary case). By IH, the leaves of T 0, T 1 satisfy the Kraft inequality. 4.5 Could use n = 0 here!

Same argument for radix r: Basis: n = 1 s1s1 0 s≤rs≤r ≤ r  1 …… ………… at most r Induction: n > 1 0 ≤ r  1 T0T0 T ≤r-1 at most r subtrees IH so adding at most r of these together gives ≤ 1  Inequality in the binary case implies that not all internal nodes have degree 2, but if a node has degree 1, then clearly that edge can be removed by contraction. 4.5 ……

Kraft Inequality (  ) Construct a code via decoding trees. Number the symbols s 1, …, s q so that l 1 ≤ … ≤ l q and assume K ≤ 1. Greedy method: proceed left-to-right, systematically assigning leaves to code words, so that you never pass through or land on a previous one. The only way this method could fail is if it runs out of nodes (tree is over-full), but that would mean K > 1. Exs:r = 21, 3, 3, 3r = 21, 2, 3, 3r = 21, 2, 2, 3 not used ½ + ⅛ + ⅛ + ⅛ < 1½ + ¼ + ⅛ + ⅛ = 1 ½ + ¼ + ¼ + ⅛ > 1 4.5

Shortened Block Codes With exactly 2 m symbols, we can form a set of code words each of length m : b 1 …… b m b i  {0,1}. This is a complete binary decoding tree of depth m. With < 2 m symbols, we can chop off branches to get modified (shortened) block codes s1s1 s2s2 s3s3 s4s4 s5s s1s1 s2s2 s3s3 s4s4 s5s5 0 1 Ex 1 Ex 2

McMillan Inequality Idea: Uniquely decodable codes satisfy the same bounds as instantaneous codes. Theorem: Suppose we have a uniquely decodable code in radix r of lengths of l 1 ≤ … ≤ l q. Then their Kraft sum is ≤ 1. Use a multinomial expansion to see that N k = the number of ways n l‘s can add up to k, which is the same as the number of different ways n symbols can form a coded message of length k. Because of uniqueness, this must be ≤ r k, the number of codewords. Conclusion: WLOG we can use only instantaneous codes. 4.7

Average code length Our goal is to minimize the average coded length. If p n > p m then l n ≤ l m. For if p m < p n with l m < l n, then interchanging the encodings for s m and s n we get So we can assume that if p 1 ≥ … ≥ p q then l 1 ≤ … ≤ l q, because if p i = p i+1 with l i > l i+1, we can just switch s i and s i old new >

Start with S = {s 1, …, s q } the source alphabet. And consider B = {0, 1} as our code alphabet (binary). First, observe that l q  1 = l q, since the code is instantaneous, s l q  1 ) won’t hurt. Huffman algorithm: So, we can combine s q  1 and s q into a “combo-symbol” (s q  1 +s q ) with probability (p q  1 +p q ) and get a code for the reduced alphabet. For q = 1, assign s 1 = ε. For q > 1, let s q-1 = (s q-1 +s q ) 0 and s q = (s q-1 +s q ) Example: ε N. B. the case for q = 1 does not produce a valid code. 4.8

Huffman is always of shortest average length Assume p 1 ≥ … ≥ p q Huffman Alternative  L avg  L L ≥ trying to show We know l 1 ≤ … ≤ l q Example: p 1 = 0.7; p 2 = p 3 = p 4 = 0.1 Compare L avg = 1.5 to log 2 q = 2. Base Case: For q = 2, no shorter code exists. Induction Step: For q > 2 take any instantaneous code for s 1, …, s q with minimal average length. s1s1 10 s2s2 4.8

Claim that l q  1 = l q = l q  1, q + 1 because So its reduced code will always satisfy: By IH, L′ avg ≤ L′. But more importantly the reduced Huffman code shares the same properties so it also satisfies the same equation L′ avg + (p q  1 + p q ) = L avg, hence L avg ≤ L. 4.8 reduced code 01 sq1 + sqsq1 + sq combined symbol total height = l q s 1, ………sq1sq1 sqsq

Code Extensions Takep 1 = ⅔ and p 2 = ⅓Huffman code gives s 1 = 0 s 2 = 1 L avg = 1 Square the symbol alphabet to get: S 2 : s 1,1 = s 1 s 1 ;s 1,2 = s 1 s 2 ; s 2,1 = s 2 s 1 ; s 2,2 = s 2 s 2 ; p 1,1 = 4 ⁄ 9 p 1,2 = 2 ⁄ 9 p 2,1 = 2 ⁄ 9 p 2,2 = 1 ⁄ 9 Apply Huffman to S 2 : s 1,1 = 1; s 1,2 = 01; s 2,1 = 000; s 2,2 = 001 But we are sending two symbols at a time! 4.10

Huffman Codes in radix r At each stage down, we merge the last (least probable) r states into 1, reducing the # of states by r  1. Since we end with one state, we must begin with no. of states  1 mod (r  1). We pad out states with probability 0 to get this. Example: r = 4; k =  4.11 pads