Professional Documents
Culture Documents
Huffman Codes Huffman's Algorithm: Improved Implementation (Cont.)
Huffman Codes Huffman's Algorithm: Improved Implementation (Cont.)
If (n==2) then {0,1} Else combine 2 smallest probabilities Pn,Pn-1 solve for P1,P2,...,P n-2,P n- 1 +P n if Pn- 1 +P n is represented by then Pn- 1 will be represented by 0 Pn will be represented by 1
2. Klein S. T. and Wiseman Y.
Huffman Codes
Let A1,...,An be a set of items. Let P1,...,P n be their probabilities. We want to find a set of lengths L1,...,L n n that will produce a minimal Li Pi i =1
2.
Implementation
Sorting is O(n log n). Finding where to insert a new item is O(n).
There are n-2 items to find, so the total order is O(n2).
1 1 0
C
2. Klein S. T. and Wiseman Y. 2. Klein S. T. and Wiseman Y.
Improved implementation
Two queues.
true false
a1 >b2
true
a4 a3 a2 a1
2.
b4 b3 b2 b1
b1 >a2
false
b1 ,b2
a1 ,a2
a1 ,b1
Huffman is optimal
An optimal tree is a tree for which
Results of Huffman
Examples for text files:
4500000 4000000 3500000 3000000 2500000 2000000 1500000 1000000 500000 0 hebrew bible
2.
is minimal
n L i Pi i =1
original compressed
The tree is full so at least 2 nodes on lowest level. 2 nodes with lowest weights are on lowest level. 2 lowest weights can be on brother nodes.
Klein S. T. and Wiseman Y.
english bible
Voltaire in French
2.
Theorem (cont.)
T1 T2
Theorem
Let T 1 be an optimal tree with weights W1,...,Wn. The two lowest weights W n,Wn-1 are on lowest level and they are brothers. Let be the father of W n,Wn-1. Let T 2 be a same tree as T1,, without Wn,Wn-1 but with having weight W n+Wn-1 Theorem: T2 is an optimal tree.
2. Klein S. T. and Wiseman Y.
Wn Wn-1
Wn+Wn-1
2.
Theorem - proof
n Let us denote M1 = Li Wi. i =1
M1=W1 L1+...+W n-1Ln-1+W nL n-1
Brothers have same level
2.
T 2 is an optimal tree.
Wn
Wn-1
2.
2.
Theorem - Proof
Induction on number of leaves. for n=2 there is just one option so the trees are equivalent.
Theorem
Let T be an optimal tree for any n leaves. T is equivalent to a Huffman tree with same leaves. i.e. they both have same
n Li Wi. i =1
W1
W2
2.
2.
Optimal trees
Huffman trees are optimal as proved but there are other trees which are optimal but not Huffman. Example:
A 20%, B 10%, C 10%, D 30%, E 30% E
1 1 0 0 1 1 0 0
C
2. Klein S. T. and Wiseman Y.