CSE 589 Applied Algorithms Spring 1999

Slides:



Advertisements
Similar presentations
Introduction to Computer Science 2 Lecture 7: Extended binary trees
Advertisements

Lecture 4 (week 2) Source Coding and Compression
Applied Algorithmics - week7
Michael Alves, Patrick Dugan, Robert Daniels, Carlos Vicuna
Greedy Algorithms Amihood Amir Bar-Ilan University.
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.
Lecture04 Data Compression.
Compression & Huffman Codes
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
A Data Compression Algorithm: Huffman Compression
DL Compression – Beeri/Feitelson1 Compression דחיסה Introduction Information theory Text compression IL compression.
Data Structures – LECTURE 10 Huffman coding
Chapter 9: Huffman Codes
4.8 Huffman Codes These lecture slides are supplied by Mathijs de Weerd.
CSE 326 Huffman coding Richard Anderson. Coding theory Conversion, Encryption, Compression Binary coding Variable length coding A B C D E F.
CPSC 411, Fall 2008: Set 4 1 CPSC 411 Design and Analysis of Algorithms Set 4: Greedy Algorithms Prof. Jennifer Welch Fall 2008.
Huffman Codes Message consisting of five characters: a, b, c, d,e
Page 110/6/2015 CSE 40373/60373: Multimedia Systems So far  Audio (scalar values with time), image (2-D data) and video (2-D with time)  Higher fidelity.
Lecture Objectives  To learn how to use a Huffman tree to encode characters using fewer bytes than ASCII or Unicode, resulting in smaller files and reduced.
4.8 Huffman Codes These lecture slides are supplied by Mathijs de Weerd.
Compression.  Compression ratio: how much is the size reduced?  Symmetric/asymmetric: time difference to compress, decompress?  Lossless; lossy: any.
Lossless Compression CIS 465 Multimedia. Compression Compression: the process of coding that will effectively reduce the total number of bits needed to.
COMPRESSION. Compression in General: Why Compress? So Many Bits, So Little Time (Space) CD audio rate: 2 * 2 * 8 * = 1,411,200 bps CD audio storage:
Introduction to Algorithms Chapter 16: Greedy Algorithms.
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part 8. 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.
Data Compression Meeting October 25, 2002 Arithmetic Coding.
Huffman Encodings Section 9.4. Data Compression: Array Representation Σ denotes an alphabet used for all strings Each element in Σ is called a character.
Huffman Codes Juan A. Rodriguez CS 326 5/13/2003.
CS654: Digital Image Analysis Lecture 34: Different Coding Techniques.
Huffman’s Algorithm 11/02/ Weighted 2-tree A weighted 2-tree T is an extended binary tree with n external nodes and each of the external nodes is.
Foundation of Computing Systems
Bahareh Sarrafzadeh 6111 Fall 2009
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.
1 Data Compression Hae-sun Jung CS146 Dr. Sin-Min Lee Spring 2004.
Chapter 7 Lossless Compression Algorithms 7.1 Introduction 7.2 Basics of Information Theory 7.3 Run-Length Coding 7.4 Variable-Length Coding (VLC) 7.5.
Huffman encoding.
Prof. Paolo Ferragina, Algoritmi per "Information Retrieval" Basics
Compression and Huffman Coding. Compression Reducing the memory required to store some information. Lossless compression vs lossy compression Lossless.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 18.
Lossless Compression-Statistical Model Lossless Compression One important to note about entropy is that, unlike the thermodynamic measure of entropy,
Information theory Data compression perspective Pasi Fränti
Lecture on Data Structures(Trees). Prepared by, Jesmin Akhter, Lecturer, IIT,JU 2 Properties of Heaps ◈ Heaps are binary trees that are ordered.
CSE 589 Applied Algorithms Spring 1999
HUFFMAN CODES.
4.8 Huffman Codes These lecture slides are supplied by Mathijs de Weerd.
Chapter 5 : Trees.
Applied Algorithmics - week7
The Greedy Method and Text Compression
Proving the Correctness of Huffman’s Algorithm
The Greedy Method and Text Compression
Chapter 8 – Binary Search Tree
Chapter 9: Huffman Codes
Analysis & Design of Algorithms (CSCE 321)
Chapter 11 Data Compression
CSE 589 Applied Algorithms Spring 1999
Trees Addenda.
Data Structure and Algorithms
Podcast Ch23d Title: Huffman Compression
Huffman codes Binary character code: each character is represented by a unique binary string. A data file can be coded in two ways: a b c d e f frequency(%)
Algorithms CSCI 235, Spring 2019 Lecture 31 Huffman Codes
Proving the Correctness of Huffman’s Algorithm
Analysis of Algorithms CS 477/677
Presentation transcript:

CSE 589 Applied Algorithms Spring 1999 Huffman Coding Arithmetic Coding

Huffman Coding Huffman (1951) Uses frequencies of symbols in a string to build a variable rate prefix code. Each symbol is mapped to a binary string. More frequent symbols have shorter codes. No code is a prefix of another. Example: a 0 b 100 c 101 d 11 CSE 589 - Lecture 11 - Spring 1999

Variable Rate Code Example Example: a 0, b 100, c 101, d 11 Coding: aabddcaa = 16 bits 0 0 100 11 11 101 0 0= 14 bits Prefix code ensures unique decodability. 00100111110100 a a b d d c a a Morse Code an example of variable rate code. E = . and Z = _ _ . . CSE 589 - Lecture 11 - Spring 1999

Huffman Tree for a Prefix Code Example: a 0, b 100, c 101, d 11 Leaves are labeled with symbols. The code of a symbol is the sequence of labels on the path from the root to the symbol. 1 a 1 d 1 b c CSE 589 - Lecture 11 - Spring 1999

Encoding and Decoding Encoding: send the code, then the encoded data x = aabddcaa c(x) = a0,b100,c101,d11,0010011111010 0 Decoding: Build the Huffman tree, then use it to decode. code of x Huffman code repeat start at root of tree if node is not a leaf if read bit = 1 then go right else go left report leaf 1 a 1 d 1 b c CSE 589 - Lecture 11 - Spring 1999

Cost of a Huffman Tree Let p1, p2, ... , pm be the probabilities for the symbols a1, a2, ... ,am, respectively. Define the cost of the Huffman tree T to be where ri is the length of the path from the root to ai. C(T) is the expected length of the code of a symbol coded by the tree T. CSE 589 - Lecture 11 - Spring 1999

Example of Cost Example: a 1/2, b 1/8, c 1/8, d 1/4 T 1 a 1 d 1 b c 1 a 1 d 1 b c C(T) = 1 x 1/2 + 3 x 1/8 + 3 x 1/8 + 2 x 1/4 = 1.75 a b c d CSE 589 - Lecture 11 - Spring 1999

Optimal Huffman Tree Input: Probabilities p1, p2, ... , pm for symbols a1, a2, ... ,am, respectively. Output: A Huffman tree that minimizes the average number of bits to code a symbol. That is, minimizes where ri is the length of the path from the root to ai. CSE 589 - Lecture 11 - Spring 1999

Optimality Principle 1 In an optimal Huffman tree a lowest probability symbol has maximum distance from the root. If not exchanging a lowest probability symbol with one at maximum distance will lower the cost. T p smallest p < q k < h T’ k h p q q p C(T’) = C(T) + hp - hq + kq - kp = C(T) - (h-k)(q-p) < C(T) CSE 589 - Lecture 11 - Spring 1999

Optimality Principle 2 The second lowest probability is a sibling of the the smallest in some optimal Huffman tree. If not, we can move it there not raising the cost. T p smallest q 2nd smallest q < r k < h T’ k h q r r p q p C(T’) = C(T) + hq - hr + kr - kq = C(T) - (h-k)(r-q) < C(T) CSE 589 - Lecture 11 - Spring 1999

Optimality Principle 3 Assuming we have an optimal Huffman tree T whose two lowest probability symbols are siblings at maximum depth, they can be replaced by a new symbol whose probability is the sum of their probabilities. The resulting tree is optimal for the new symbol set. T T’ p smallest q 2nd smallest h q+p q p C(T’) = C(T) + (h-1)(p+q) - hp -hq = C(T) - (p+q) CSE 589 - Lecture 11 - Spring 1999

Optimality Principle 3 (cont’) If T’ were not optimal then we could find a lower cost tree T’’. This will lead to a lower cost tree T’’’ for the original alphabet. T’’’ T’ T’’ q+p q+p q p C(T’’’) = C(T’’) + p + q < C(T’) + p + q = C(T) which is a contradiction CSE 589 - Lecture 11 - Spring 1999

Huffman Tree Algorithm form a node for each symbol ai with weight pi; insert the nodes in a min priority queue ordered by probability; while the priority queue has more than one element do min1 := delete-min; min2 := delete-min; create a new node n; n.weight := min1.weight + min2.weight; n.left := min1; n.right := min2; insert(n) return the last node in the priority queue. CSE 589 - Lecture 11 - Spring 1999

Example of Huffman Tree Algorithm (1) P(a) =.4, P(b)=.1, P(c)=.3, P(d)=.1, P(e)=.1 .4 .1 .3 .1 .1 a b c d e .4 .3 .1 .2 a c d b e CSE 589 - Lecture 11 - Spring 1999

Example of Huffman Tree Algorithm (2) .3 .1 .4 .2 c d a b e .4 .3 .3 a c d b e CSE 589 - Lecture 11 - Spring 1999

Example of Huffman Tree Algorithm (3) .4 .3 .3 .4 .6 a c a c d b e d b e CSE 589 - Lecture 11 - Spring 1999

Example of Huffman Tree Algorithm (4) 1 .4 .6 a a c c d d b e b e CSE 589 - Lecture 11 - Spring 1999

Optimal Huffman Code 1 average number of bits per symbol is 1 average number of bits per symbol is .4 x 1 + .1 x 4 + .3 x 2 + .1 x 3 + .1 x 4 = 2.1 a 1 a 0 b 1110 c 10 d 110 e 1111 c 1 d 1 b e CSE 589 - Lecture 11 - Spring 1999

Huffman Code vs. Entropy P(a) =.4, P(b)=.1, P(c)=.3, P(d)=.1, P(e)=.1 Entropy H = -(.4 x log2(.4) + .1 x log2(.1) + .3 x log2(.3) + .1 x log2(.1) + .1 x log2(.1)) = 2.05 bits per symbol Huffman Code HC = .4 x 1 + .1 x 4 + .3 x 2 + .1 x 3 + .1 x 4 = 2.1 bits per symbol pretty good! CSE 589 - Lecture 11 - Spring 1999

Quality of the Huffman Code The Huffman code is within one bit of the entropy lower bound. Huffman code does not work well with a two symbol alphabet. Example: P(0) = 1/100, P(1) = 99/100 HC = 1 bits/symbol H = -((1/100)*log2(1/100) + (99/100)log2(99/100)) = .08 bits/symbol 1 1 CSE 589 - Lecture 11 - Spring 1999

Extending the Alphabet Assuming independence P(ab) = P(a)P(b), so we can lump symbols together. Example: P(0) = 1/100, P(1) = 99/100 P(00) = 1/10000, P(01) = P(10) = 99/10000, P(11) = 9801/10000. 1 HC = 1.03 bits/symbol (2 bit symbol) = .515 bits/bit Still not that close to H = .08 bits/bit 11 1 10 1 01 00 CSE 589 - Lecture 11 - Spring 1999

Including Context Suppose we add a one symbol context. That is in compressing a string x1x2...xn we want to take into account xk-1 when encoding xk. New model, so entropy based on just independent probabilities of the symbols doesn’t hold. The new entropy model (2nd order entropy) has for each symbol a probability for each other symbol following it. Example: {a,b,c} next a b c a .4 .2 .4 b .1 .9 0 c .1 .1 .8 prev CSE 589 - Lecture 11 - Spring 1999

Multiple Codes next Code for first symbol a 00 b 01 c 10 a b c prev b c a 1 1 1 b a c a .9 .1 .8 1 1 .4 a b b c a b b a c c .1 .2 .1 .4 00 00 0 1 01 0 CSE 589 - Lecture 11 - Spring 1999

Notes on Huffman Codes Time to design Huffman Code is O(n log n) where n is the number of symbols. Typically, for compressing a string the probabilities are chosen as the actual frequencies of the symbols in the string. Huffman works better for larger alphabets. There are adaptive (one pass) Huffman coding algorithms. No need to transmit code. Huffman is still popular. It is simple and it works. CSE 589 - Lecture 11 - Spring 1999

Arithmetic Coding Huffman coding works well for larger alphabets and gets to within one bit of the entropy lower bound. Can we do better. Yes Basic idea in arithmetic coding: represent each string x of length n by an interval A in [0,1). The width of the interval A represents the probability of x occurring. The interval A can itself be represented by any number, called a tag, within the half open interval. The significant bits of the tag is the code of x. CSE 589 - Lecture 11 - Spring 1999

Example of Arithmetic Coding (1) 1. tag must be in the half open interval. 2. tag can be chosen to be (l+r)/2. 3. code must be significant bits of tag. 1/3 a 15/27 .100011100... 2/3 bba b 19/27 .101101000... bb tag = 17/27 = .101000010... code = 101 1 CSE 589 - Lecture 11 - Spring 1999

Example of Arithmetic Coding (2) 1/3 a 11/27 .011010000... ba bab 15/27 .100011100... 2/3 b tag1 = 13/27 = .011110110... code1 = 0111 tag2 = 14/27 = .100001001... code2 = 1 1 CSE 589 - Lecture 11 - Spring 1999