Docsity

Text Compression: Understanding Huffman Coding, Lecture notes of Data Structures and Algorithms

Toggle sidebar
bg1
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……
pf3
pf4
pf5
pf8

Partial preview of the text

Download Text Compression: Understanding Huffman Coding and more Lecture notes Data Structures and Algorithms in PDF only on Docsity!

CPS 100 8. 1

Text Compression: Examples

d 01100100 011 001

c 01100011 010 01

e 01100101 100 10

b 01100010 001 11

a 01100001 000 000

Var.

length

Fixed

length

Symbol ASCII

“abcde” in the different formats

ASCII:

Fixed:

Var:

a b c d e

a d

c e b

Encodings

ASCII: 8 bits/character

Unicode: 16 bits/character

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

s

p

h

e

r

g

o

p

h

e

r

s

g

o

Huffman Coding

 D.A Huffman in early 1950’s

 Before compressing data, analyze the input stream

 Represent data using variable length codes

 Variable length codes though Prefix codes

 Each letter is assigned a codeword

 Codeword is for a given letter is produced by traversing

the Huffman tree

 Property: No codeword produced is the prefix of another

 Letters appearing frequently have short codewords,

while those that appear rarely have longer ones

 Huffman coding is optimal per-character coding method

Building a Huffman tree

 Begin with a forest of single-node trees (leaves)

 Each node/tree/leaf is weighted with character count

 Node stores two values: character and count

 There are n nodes in forest, n is size of alphabet?

 Repeat until there is only one node left: root of tree

 Remove two minimally weighted trees from forest

 Create new tree with minimal trees as children,

  • New tree root's weight: sum of children (character ignored)

 Does this process terminate? How do we get minimal trees?

 Remove minimal trees, hummm……

CPS 100 8. 5

Building a tree

“A SIMPLE STRING TO BE ENCODED USING A MINIMAL NUMBER OF BITS”

I
E
N
C
F
P
U
R
L
D
G
T
O
B
A
M
S

CPS 100 8. 6

Building a tree

“A SIMPLE STRING TO BE ENCODED USING A MINIMAL NUMBER OF BITS”

I
E
N
C
F
P
U
R
L
D
G
T
O
B
A
M
S

CPS 100 8. 7

Building a tree

“A SIMPLE STRING TO BE ENCODED USING A MINIMAL NUMBER OF BITS”

I
E
N
F
C
P
U
R
L
D
G
T
O
B
A
M
S

CPS 100 8. 8

Building a tree

“A SIMPLE STRING TO BE ENCODED USING A MINIMAL NUMBER OF BITS”

I
E
N
F
C
P
U
R
L
D
G
T
O
B
A
M
S

CPS 100 8. 13

Huffman Complexities

 How do we measure? Size of input file, size of alphabet

 Which is typically bigger?

 Accumulating character counts: ______

 How can we do this in O(1) time, though not really

 Building the heap/priority queue from counts ____

 Initializing heap guaranteed

 Building Huffman tree ____

 Why?

 Create table of encodings from tree ____

 Why?

 Write tree and compressed file _____

CPS 100 8. 14

Writing code out to file

 How do we go from characters to encodings?

 Build Huffman tree

 Root-to-leaf path generates encoding

 Need way of writing bits out to file

 Platform dependent?

 Complicated to write bits and read in same ordering

 See BitInputStream and BitOutputStream classes

 Depend on each other, bit ordering preserved

 How do we know bits come from compressed file?

 Store a magic number

Decoding a message

I
N
E
F
C
P
U
R
L
D
G
O
T
B
A
M
S

0 1100000100001001101

Decoding a message

I
N
E
F
C
P
U
R
L
D
G
O
T
B
A
M
S

1100000100001001101

CPS 100 8. 17

Decoding a message

I
N
E
F
C
P
U
R
L
D
G
O
T
B
A
M
S

CPS 100 8. 18

Decoding a message

I
N
E
F
C
P
U
R
L
D
G
O
T
B
A
M
S

CPS 100 8. 19

Decoding a message

I
N
E
F
C
P
U
R
L
D
G
O
T
B
A
M
S

G

CPS 100 8. 20

Decoding a message

I
N
E
F
C
P
U
R
L
D
G
O
T
B
A
M
S

GOOD

CPS 100 8. 25

Huffman Tree 2

 “A SIMPLE STRING TO BE ENCODED USING A

MINIMAL NUMBER OF BITS”

 E.g. “ A SIMPLE” ⇔

CPS 100 8. 26

Huffman Tree 2

 “A SIMPLE STRING TO BE ENCODED USING A

MINIMAL NUMBER OF BITS”

 E.g. “ A SIMPLE” ⇔

Huffman Tree 2

 “A SIMPLE STRING TO BE ENCODED USING A

MINIMAL NUMBER OF BITS”

 E.g. “ A SIMPLE” ⇔

Huffman Tree 2

 “A SIMPLE STRING TO BE ENCODED USING A

MINIMAL NUMBER OF BITS”

 E.g. “ A SIMPLE” ⇔

CPS 100 8. 29

Huffman Tree 2

 “A SIMPLE STRING TO BE ENCODED USING A

MINIMAL NUMBER OF BITS”

 E.g. “ A SIMPLE” ⇔

CPS 100 8. 30

Huffman Tree 2

 “A SIMPLE STRING TO BE ENCODED USING A

MINIMAL NUMBER OF BITS”

 E.g. “ A SIMPLE” ⇔

Huffman Tree 2

 “A SIMPLE STRING TO BE ENCODED USING A

MINIMAL NUMBER OF BITS”

 E.g. “ A SIMPLE” ⇔

Other methods

 Adaptive Huffman coding

 Lempel-Ziv algorithms

 Build the coding table on the fly while reading

document

 Coding table changes dynamically

 Protocol between encoder and decoder so that everyone

is always using the right coding scheme

 Works well in practice (compress, gzip, etc.)

 More complicated methods

 Burrows-Wheeler (bunzip2)

 PPM statistical methods