Table of Contents

4.8 Huffman Codes and Data Compression

The Problem


Variable-Length Encoding Schemes:


Prefix codes:


Optimal Prefix Codes :



Designing the Algorithm



Representing Prefix Codes using Binary Trees

Algorithm to Construct an Optimal Prefix Code

To construct a prefix code for an alphabet S, with given frequencies:
If S has 2 letters:
Encode one letter using 0 and the other using 1

Else:

Let y* and z* be the 2 lowest-frequency letters

Form a new alphabet S' by deleting y* and z* and replacing them with
a new letter ω of frequency fy*+ fz*

Recursively Construct a prefix code y' for S', with tree T'

Define a prefix code for S as follows:

Start with T'

Take the leaf labeled ω and add two children below it labeled y* and z*

End if




Analyzing the Algorithm

Implementation and Running Time

Create a leaf node for each symbol, labeled by its frequency, and add to a
queue(PQ).Takes O(nlogn)

While there is more than one node in the queue: Takes O(n)
Remove the two nodes of lowest frequency. Takes O(logn)

Create a new internal node with these two nodes as children and
with frequency equal to the sum of the two nodes' probabilities. Takes O(1) time.

Add the new node to the queue Takes O(logn)

The remaining node is the tree’s root node.


So overall takes O(nlogn).

Very intriguing section although its proofs were long. I give it an 8/10.