Docsity

Understanding Compression and Huffman Codes: A Detailed Guide, Study notes of Computer Science

Toggle sidebar
bg1
1
1
CMSC 132:
Object-Oriented Programming II
Compression & Huffman Codes
Department of Computer Science
University of Maryland, College Park
2
Overview
Compression
Examples
Sources
Types
Effectiveness
Huffman Code
Properties
Huffman tree (encoding)
Decoding
3
Compression
Definition
Reduce size of data
(number of bits needed to represent data)
Benefits
Reduce storage needed
Reduce transmission cost / latency / bandwidth
4
Compression Examples
Formats
General
.zip, .rar
Images
.jpg, .gif
Audio
.mp3, .wmv
Video
.mpg, .mov
5
Sources of Compressibility
Redundancy
Recognize repeating patterns
Exploit using
Dictionary
Variable length encoding
Human perception
Less sensitive to some information
Can discard less important data
6
Types of Compression
Lossless
Preserves all information
Exploits redundancy in data
Applied to general data
Some lossless audio formats (e.g., FLAC)
Lossy
May lose some information
Exploits redundancy &human perception
Applied to audio, image, video, multimedia
pf3
pf4
pf5

Partial preview of the text

Download Understanding Compression and Huffman Codes: A Detailed Guide and more Study notes Computer Science in PDF only on Docsity!

1

CMSC 132:

Object-Oriented Programming II

Compression & Huffman Codes

Department of Computer Science

University of Maryland, College Park

2

Overview

Compression Examples

Sources Types Effectiveness

Huffman Code Properties

Huffman tree (encoding) Decoding

3

Compression

Definition Reduce size of data

(number of bits needed to represent data)

Benefits Reduce storage needed

Reduce transmission cost / latency / bandwidth 4

Compression Examples

Formats General

.zip, .rar Images .jpg, .gif Audio .mp3, .wmv Video .mpg, .mov

5

Sources of Compressibility

Redundancy Recognize repeating patterns

Exploit using Dictionary Variable length encoding

Human perception Less sensitive to some information

Can discard less important data 6

Types of Compression

Lossless Preserves all information

Exploits redundancy in data Applied to general data Some lossless audio formats (e.g., FLAC)

Lossy May lose some information

Exploits redundancy & human perception Applied to audio, image, video, multimedia

7

Effectiveness of Compression

Metrics Bits per byte (8 bits)

2 bits / byte 8 bits / byte โ‡’โ‡’ ยผ original sizeno compression Percentage 75% compression โ‡’ ยผ original size

8

Effectiveness of Compression

Depends on data Random data โ‡’ hard

Organized data^ Example: 1001110100 โ‡’ easy^ โ‡’^? Example: 1111111111 โ‡’ 1 ร— 10

Corollary No universally best compression algorithm

9

Effectiveness of Compression

Lossless Compression is not guaranteed Pigeonhole principle

Reduce size 1 bit Example โ‡’ can only store ยฝ of data If compression is always possible (alternative view)^ 000, 001, 010, 011, 100, 101, 110, 111^ โ‡’^ 00, 01, 10, 11 Compress file (reduce size by 1 bit) Recompress output Repeat (until we can store data with 0 bits)

10

Lossless Compression Techniques

LZW (Lempel-Ziv-Welch) compression Build pattern dictionary

Replace patterns with index into dictionary

Run length encoding Find & compress repetitive sequences

Huffman code Use variable length codes based on frequency

11

Huffman Code

Approach Variable length encoding of symbols

Exploit statistical frequency of symbols Efficient when symbol probabilities vary widely

Principle Use fewer bits to represent frequent symbols

Use more bits to represent infrequent symbols A A B A A A B A 12

Huffman Code Example

Expected size Original โ‡’ 1/8ร—2 + 1/4ร—2 + 1/2ร—2 + 1/8ร—2 = 2 bits / symbol

Huffman โ‡’ 1/8ร—3 + 1/4ร—2 + 1/2ร—1 + 1/8ร—3 = 1.75 bits / symbol

Symbol

EncodingHuffman 3 bits^110 2 bits^10 1 bit^0 3 bits^111

(^0001) 2 bits 10 11

Frequency 1/8 1/4 Bird1/2 1/ EncodingOriginal 2 bits 2 bits 2 bits

Dog Cat Fish

19

Huffman Tree Construction 4

3 5

A

C

H E I

2 trees withlowest frequency (^20)

Huffman Tree Construction 5

3 5 8

A

C E

H

I

EI == (^0100) CA == (^10111) H = 110 Huffmancode for each leaf

21

Huffman Coding Example

Huffman code

Input ACE

Output (111)(10)(01) = 1111001

EI == (^0100) CA == (^10111) H = 110

22

Huffman Code Algorithm Overview

1.^ Decoding Read compressed file & binary tree

  1. Use binary tree to decode file Follow path from root to leaf

23

Huffman Decoding 1

3 5 8

A

C E

H

I

1111001

24

Huffman Decoding 2

3 5 8

A

C E

H

I

1111001

25

Huffman Decoding 3

3 5 8

A

C E

H

I

1111001 A

26

Huffman Decoding 4

3 5 8

A

C E

H

I

1111001 A

27

Huffman Decoding 5

3 5 8

A

C E

H

I

1111001 AC

28

Huffman Decoding 6

3 5 8

A

C E

H

I

1111001 AC

29

Huffman Decoding 7

3 5 8

A

C E

H

I

1111001 ACE

30

Huffman Code Properties

Prefix code No code is a prefix of another code

Example Huffman(โ€œdogโ€) โ‡’ 01 Can stop as soon as complete code found^ Huffman(โ€œcatโ€)^ โ‡’^011 // not legal prefix code No need for end-of-code marker

Nondeterministic Multiple Huffman coding possible for same input

If more than two trees with same minimal weight