
CPS 100 8.1
Text Compression: Examples
00101101100100d
0101001100011c
1010001100101e
1100101100010b
00000001100001a
Var.
length
Fixed
length
ASCIISymbol
“abcde” in the different formats
ASCII:
01100001011000100110001101100100…
Fixed:
000001010011100
Var:
000110100110
0
0
0
0
0
0
0
1
1 1
1
a b c d e
ad
bc e
0
0
0
0 1
1
1
1
Encodings
ASCII: 8 bits/character
Unicode: 16 bits/charac ter
CPS 100 8.2
Huffman coding: go go gophers
Encoding uses tree:
0 left/1 right
How many bits? 37!!
Savings? Worth it?
ASCII 3 bits Huffman
g 103 1100111 000 00
o 111 1101111 001 01
p 112 1110000 010 1100
h 104 1101000 011 1101
e 101 1100101 100 1110
r 114 1110010 101 1111
s 115 1110011 110 101
sp. 32 1000000 111 101
3
s
1
*
2
2
p
1
h
1
2
e
1
r
1
4g
3
o
3
6
3
2
p
1
h
1
2
e
1
r
1
4
s
1
*
2
7
g
3
o
3
6
13
CPS 100 8.3
Huffman Coding
D.A Huffman in earl y 1950’s
Before compressing d ata, analyze the input stream
Represent data using variable length codes
Variable length code s though Prefix codes
Each letter is assigne d a codeword
Codeword is for a giv en letter is produced by traversing
the Huffman tree
Property: No codewo rd produced is the prefix of another
Letters appearing fre quently have short codewords,
while those that appe ar rarely have longer ones
Huffman coding is o ptimal per-character coding m ethod
CPS 100 8.4
Building a Huffman tree
Begin with a forest o f single-node trees (leaves)
Each node/tree/leaf is weighted with character count
Node stores two valu es: character and count
There are n node s in forest, n is size of alphabet?
Repeat until there is only one node left: root of tree
Remove two minima lly weighted trees from forest
Create new tree with minimal trees as children,
•New tree root's weight: sum of children (chara cter ignored)
Does this process term inate? How do we get minimal trees?
Remove minimal tre es, hummm……