
















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!

















These lecture slides are supplied by Mathijs de Weerd
Q. Given a text that uses 32 symbols (26 different letters, space, and some punctuation characters), how can we encode this text in bits? Q. Some symbols (e, t, a, o, i, n) are used far more often than others. How can we use this to reduce our encoding? Q. How do we know when the next symbol begins? Ex. c(a) = 01 What is 0101? c(b) = 010 c(e) = 1
Definition. A prefix code for a set S is a function c that maps each x∈ S to 1s and 0s in such a way that for x,y∈S, x≠ y, c(x) is not a prefix of c(y). Ex. c(a) = 11 c(e) = 01 c(k) = 001 c(l) = 10 c(u) = 000 Q. What is the meaning of 1001000001? Suppose frequencies are known in a text of 1G: fa=0.4, fe=0.2, fk=0.2, fl=0.1, fu=0. Q. What is the size of the encoded text?
Definition. A prefix code for a set S is a function c that maps each x∈ S to 1s and 0s in such a way that for x,y∈S, x≠ y, c(x) is not a prefix of c(y). Ex. c(a) = 11 c(e) = 01 c(k) = 001 c(l) = 10 c(u) = 000 Q. What is the meaning of 1001000001? A. “leuk” Suppose frequencies are known in a text of 1G: fa=0.4, fe=0.2, fk=0.2, fl=0.1, fu=0. Q. What is the size of the encoded text? A. 2fa + 2fe + 3fk + 2fl + 4*fu = 2.4G
Ex. c(a) = 11 c(e) = 01 c(k) = 001 c(l) = 10 c(u) = 000 Q. How does the tree of a prefix code look? l u a e k 0 0 0 0 1 1 1 1
Ex. c(a) = 11 c(e) = 01 c(k) = 001 c(l) = 10 c(u) = 000 Q. How does the tree of a prefix code look? A. Only the leaves have a label. Pf. An encoding of x is a prefix of an encoding of y if and only if the path of x is a prefix of the path of y. l u a e k 0 0 0 0 1 1 1 1
Q. What is the meaning of 111010001111101000? A. “simpel” Q. How can this prefix code be made more efficient? l e m 0 0 0 0 1 1 1 1 p 1 i 0 s 1
"
x S x T
Q. What is the meaning of 111010001111101000? A. “simpel” Q. How can this prefix code be made more efficient? A. Change encoding of p and s to a shorter one. This tree is now full. l e m 0 0 0 0 1 1 1 1 p 1 i 0 s 1 s 0
"
x S x T
Definition. A tree is full if every node that is not a leaf has two children. Claim. The binary tree corresponding to the optimal prefix code is full. Pf. (by contradiction) Suppose T is binary tree of optimal prefix code and is not full. This means there is a node u with only one child v. Case 1: u is the root; delete u and use v as the root Case 2: u is not the root
v w u
Q. Where in the tree of an optimal prefix code should letters be placed with a high frequency?
Observation. Lowest frequency items should be at the lowest level in tree of optimal prefix code. Observation. For n > 1, the lowest level always contains at least two leaves. Observation. The order in which items appear in a level does not matter. Claim. There is an optimal prefix code with tree T* where the two lowest-frequency letters are assigned to leaves that are siblings in T*. Greedy template. [Huffman, 1952] Create tree bottom-up. Make two leaves for two lowest-frequency letters y and z. Recursively build tree for the rest using a meta-letter for yz.
Q. What is the time complexity? Huffman(S) { if |S|=2 { return tree with root and 2 leaves } else { let y and z be lowest-frequency letters in S S’ = S remove y and z from S’ insert new letter ω in S’ with f ω =fy+fz T’ = Huffman(S’) T = add two children y and z to leaf ω from T’ return T } }
Claim. Huffman code for S achieves the minimum ABL of any prefix code. Pf. by induction, based on optimality of T’ (y and z removed, ω added) (see next page) Claim. ABL(T’)=ABL(T)-fω Pf.
20
Claim. Huffman code for S achieves the minimum ABL of any prefix code. Pf. by induction, based on optimality of T’ (y and z removed, ω added) (see next page) Claim. ABL(T’)=ABL(T)-fω Pf. ! ABL( T ) = fx " depth T ( x ) x # S
= fy " depth T ( y ) + fz " depth T ( z ) + fx " depth T ( x ) x # S , x % y , z
= (^) ( fy + fz ) " (^) ( 1 + depth T ( &)) + fx " depth T ( x ) x # S , x % y , z
= f & " (^) ( 1 + depth T ( &)) + fx " depth T ( x ) x # S , x % y , z
= f & + fx " depth T ' ( x ) x # S '
= f & + ABL( T ' )