Docsity

Notes for Huffman Codes, Data Compression - Fundamental Algorithms | CS 473, Study notes of Algorithms and Programming

Partial preview of the text

Download Notes for Huffman Codes, Data Compression - Fundamental Algorithms | CS 473 and more Study notes Algorithms and Programming in PDF only on Docsity!

CS 473ug: Algorithms

Mahesh Viswanathan [email protected] 3232 Siebel Center

University of Illinois, Urbana-Champaign

Spring 2008

The ProblemCodes Towards a SolutionHuffman Codes

Part I

Huffman Codes

Towards a SolutionHuffman Codes Prefix Codes

Codes

Definition A code translates text into sequence of bits such that the text can be recovered from the sequence of bits.

Example Consider encoding the english alphabet, space, period, comma, question mark, exclamation point and apostrophe. Each of the 32 symbols can be encoded by a (different) 5 bit string. Given a string of 0s and 1s, decoding involves breaking up string into 5 symbol substrings, and decoding each one of these.

Towards a SolutionHuffman Codes Prefix Codes

Data Compression

Variable Length Codes Have different length encoding for each symbol Shorter encodings for more frequent symbols will reduce the average bits per symbol.

Towards a SolutionHuffman Codes Prefix Codes

Data Compression

Variable Length Codes Have different length encoding for each symbol Shorter encodings for more frequent symbols will reduce the average bits per symbol.

Example Morse code is a variable length encoding. Maps e to 0 (dot), t to 1 (dash), a to 01 (dot-dash),... What is the text for 0101? Could be etet, or aa or eta or aet! Ambiguity removed by adding pauses between letters.

Towards a SolutionHuffman Codes Prefix Codes

Data Compression

Variable Length Codes Have different length encoding for each symbol Shorter encodings for more frequent symbols will reduce the average bits per symbol.

Example Morse code is a variable length encoding. Maps e to 0 (dot), t to 1 (dash), a to 01 (dot-dash),... What is the text for 0101? Could be etet, or aa or eta or aet! Ambiguity removed by adding pauses between letters. But then encoding is not over 0,1 but over 0,1,2.

Towards a SolutionHuffman Codes Prefix Codes

Prefix Codes

Definition A prefix code for a set S is function γ such that (^1) For x ∈ S, γ(x) is a bit-string (^2) For distinct x and y , it is not the case that γ(x) is a prefix of γ(y ), or vice versa.

Example Consider S = {a, b, c, d, e} with encoding γ as follows:

γ(a) = 11 γ(b) = 01 γ(c) = 001 γ(d) = 10 γ(e) = 000

String “bad” encoded as 01 11 10

Towards a SolutionHuffman Codes Prefix Codes

Decoding Prefix Codes

Towards a SolutionHuffman Codes Prefix Codes

Decoding Prefix Codes

Algorithm (^1) Scan the bit sequence from left to right (^2) When a prefix matches code of some symbol, output the symbol Justified since no shorter prefix, nor longer extension could encode a symbol

Example S = {a, b, c, d, e}, with γ(a) = 11, γ(b) = 01, γ(c) = 001, γ(d) = 10, γ(e) = 000

0010000011101

Towards a SolutionHuffman Codes Prefix Codes

Decoding Prefix Codes

Algorithm (^1) Scan the bit sequence from left to right (^2) When a prefix matches code of some symbol, output the symbol Justified since no shorter prefix, nor longer extension could encode a symbol

Example S = {a, b, c, d, e}, with γ(a) = 11, γ(b) = 01, γ(c) = 001, γ(d) = 10, γ(e) = 000

0010000011101 c

Towards a SolutionHuffman Codes Prefix Codes

Decoding Prefix Codes

Algorithm (^1) Scan the bit sequence from left to right (^2) When a prefix matches code of some symbol, output the symbol Justified since no shorter prefix, nor longer extension could encode a symbol

Example S = {a, b, c, d, e}, with γ(a) = 11, γ(b) = 01, γ(c) = 001, γ(d) = 10, γ(e) = 000

0010000011101 c e c

Towards a SolutionHuffman Codes Prefix Codes

Decoding Prefix Codes

Algorithm (^1) Scan the bit sequence from left to right (^2) When a prefix matches code of some symbol, output the symbol Justified since no shorter prefix, nor longer extension could encode a symbol

Example S = {a, b, c, d, e}, with γ(a) = 11, γ(b) = 01, γ(c) = 001, γ(d) = 10, γ(e) = 000

0010000011101 c e c a

The ProblemCodes Towards a SolutionHuffman Codes

Average Encoding Length Problem Defintion

Average Bits per Letter

Observation For a text of n letters, with x occuring nfx times, the length of the encoded text is

x∈S nfx^ |γ(x)|^ =^ n^

x∈S fx^ |γ(x)|.

The ProblemCodes Towards a SolutionHuffman Codes

Average Encoding Length Problem Defintion

Average Bits per Letter

Observation For a text of n letters, with x occuring nfx times, the length of the encoded text is

x∈S nfx^ |γ(x)|^ =^ n^

x∈S fx^ |γ(x)|.

Definition For an alphabet S, with frequency fx for symbol x (

x∈S fx^ = 1), the average number of bits required per letter under the encoding γ (denoted ABL(γ)) is

x∈S fx^ |γ(x)|.