Docsity

Compression & Huffman Codes: Techniques & Algorithms for Data Compression - Prof. Nelson P, Study notes of Computer Science

Partial preview of the text

Download Compression & Huffman Codes: Techniques & Algorithms for Data Compression - Prof. Nelson P and more Study notes Computer Science in PDF only on Docsity!

CMSC 132:

Object-Oriented Programming II

Compression & Huffman Codes

Department of Computer Science

University of Maryland, College Park

Overview

Compression

Examples Sources Types Effectiveness

Huffman Code

Properties Huffman tree (encoding) Decoding

Compression Examples

Tools

winzip, pkzip, compress, gzip

Formats

Images

.jpg, .gif

Audio

.wav (CD), .mp3, .wma, .aac

Video

mpeg1 (LD,VCD), mpeg2 (DVD), mpeg4 (Divx)

General

.zip, .gz

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

Effectiveness of Compression

Metrics

Bits per byte (8 bits)

2 bits / byte

¼ original size

8 bits / byte

no compression

Percentage

75% compression

¼ original size

Effectiveness of Compression

Depends on data

Random data

hard

Example: 1001110100

Organized data

easy

Example: 1111111111

×

Corollary

No universally best compression algorithm

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

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

A

B

Huffman Code Data Structures

Binary (Huffman) tree

Represents Huffman code Edge

code (0 or 1)

Leaf

symbol

Path to leaf

encoding

Example

A = “11”, H = “10”, C = “0”

Priority queue

To efficiently build binary tree

A

H

C 0

Huffman Code Algorithm Overview

Encoding

Calculate frequency of symbols in file Create binary tree representing “best” encoding Use binary tree to encode compressed file

For each symbol, output path from root to leaf Size of encoding = length of path

Save binary tree

Huffman Tree Construction 1

C

E

I

H

A

Huffman Tree Construction 2

C

E

I

A

H

Huffman Tree Construction 4

E

I

A

H

C

Huffman Tree Construction 5

A

H

E

=

01

I

=

00

C

=

10

A

=

111

H

=

110

C

E

I