Docsity

Huffman Encoding Algorithm - Design and Analysis - Study Notes, Study notes of Digital Systems Design

Toggle sidebar
bg1
Lecture No. 24
7.2 Greedy Algorithm: Huffman Encoding
The Huffman codes provide a method of encoding data efficiently. Normally, when
characters are coded using standard codes like ASCII. Each character is represented by a
fixed-length codeword of bits, e.g., 8 bits per character. Fixed-length codes are popular
because it is very easy to break up a string into its individual characters, and to access
individual characters and substrings by direct indexing. However, fixed-length codes may
not be he most efficient from the perspective of minimizing the total quantity of data.
Consider the string “ abacdaacac”. if the string is coded with ASCII codes, the message
length would be 10 × 8 = 80 bits. We will see shortly that the same string encoded with
a variable length Huffman encoding scheme will produce a shorter message.
7.2.1 Huffman Encoding Algorithm
Here is how the Huffman encoding algorithm works. Given a message string, determine
the frequency of occurrence (relative probability) of each character in the message. This
can be done by parsing the message and counting how many time each character (or
symbol) appears. The probability is the number of occurrence of a character divided by
the total characters in the message. The frequencies and probabilities for the example
string “ abacdaacac” are
character
a
b
C
d
frequency
5
1
3
1
probability
0.5
0.1
0.3
0.1
Next, create binary tree (leaf) node for each symbol (character) that occurs with nonzero
frequency Set node weight equal to the frequency of the symbol. Now comes the greedy
part: Find two nodes with smallest frequency. Create a new node with these two nodes as
children, and with weight equal to the sum of the weights of the two children. Continue
until we have a single tree.
Finding two nodes with the smallest frequency can be done efficiently by placing the
nodes in a heap-based priority queue. The min-heap is maintained using the frequencies.
When a new node is created by combining two nodes, the new node is placed in the
priority queue. Here is the Huffman tree building algorithm.
Docsity.com
pf3

Partial preview of the text

Download Huffman Encoding Algorithm - Design and Analysis - Study Notes and more Study notes Digital Systems Design in PDF only on Docsity!

Lecture No. 24

7.2 Greedy Algorithm: Huffman Encoding

The Huffman codes provide a method of encoding data efficiently. Normally, when characters are coded using standard codes like ASCII. Each character is represented by a fixed-length codeword of bits, e.g., 8 bits per character. Fixed-length codes are popular because it is very easy to break up a string into its individual characters, and to access individual characters and substrings by direct indexing. However, fixed-length codes may not be he most efficient from the perspective of minimizing the total quantity of data. Consider the string “ abacdaacac”. if the string is coded with ASCII codes, the message length would be 10 × 8 = 80 bits. We will see shortly that the same string encoded with a variable length Huffman encoding scheme will produce a shorter message.

7.2.1 Huffman Encoding Algorithm

Here is how the Huffman encoding algorithm works. Given a message string, determine the frequency of occurrence (relative probability) of each character in the message. This can be done by parsing the message and counting how many time each character (or symbol) appears. The probability is the number of occurrence of a character divided by the total characters in the message. The frequencies and probabilities for the example string “ abacdaacac” are

character a b C d frequency 5 1 3 1 probability 0.5 0.1 0.3 0.

Next, create binary tree (leaf) node for each symbol (character) that occurs with nonzero frequency Set node weight equal to the frequency of the symbol. Now comes the greedy part: Find two nodes with smallest frequency. Create a new node with these two nodes as children, and with weight equal to the sum of the weights of the two children. Continue until we have a single tree.

Finding two nodes with the smallest frequency can be done efficiently by placing the nodes in a heap-based priority queue. The min-heap is maintained using the frequencies. When a new node is created by combining two nodes, the new node is placed in the priority queue. Here is the Huffman tree building algorithm.

Figure 7.1 shows the tree built for the example message “abacdaacac”

Figure 7.1: Huffman binary tree for the string “abacdaacac”

Prefix Property: The codewords assigned to characters by the Huffman algorithm have the property that no codeword is a prefix of any other:

character a b C d frequency 5 1 3 1 probability 0.5 0.1 0.3 0. codeword 0 110 10 111