Huffman Coding PDF
Huffman Coding PDF
Huffman Coding
The Huffman encoding algorithm starts by constructing a list of all the alphabet symbols in
descending order of their probabilities. It then constructs, from the bottom up, a
binary tree with a symbol at every leaf. This is done in steps, where at each step two
symbols with the smallest probabilities are selected, added to the top of the partial tree,
deleted from the list, and replaced with an auxiliary symbol representing the two original
symbols. When the list is reduced to just one auxiliary symbol (representing the entire
alphabet), the tree is complete. The tree is then traversed to determine the code words
of the symbols.
Huffman’s Algorithm:
1. Create a terminal node for each ai with probability p(ai) and let S = the set of terminal
nodes.
2. Select nodes x and y in S with the two smallest probabilities.
3. Replace x and y in S by a node with probability p(x) + p(y). Also, create a node in the tree
which is the parent of x and y.
4. Repeat (2)-(3) until |S| = 1.
Matlab Code:
%%%%%%%%%%%%%%%%%%%%%%% Huffman Coding %%%%%%%%%%%%%%%%%%%%%%%%%%%%
fprintf('\nRemember !\n')
disp('Allocate probabilities such that')
disp('sum of all probabilities must be 1')
for i=1:length(String)
fprintf('\nProbability of\n')
disp(String(i))
Prob(i)=input('is \n')
end
total_prob=sum(Prob)
num_bits = ceil(log2(length(Prob)))
%% Coreresponding Probabilities
disp('Character Probability:');
2
for i = 1:length(Prob)
display(strcat(String(i),' --> ',num2str(Prob(i))));
end
for i = 1:length(String)
Str_in_cell{i} = String(i);
end
sorted_prob = Prob;
counter = 1;
while (length(sorted_prob) > 1)
% Sort probs
[sorted_prob,indeces] = sort(sorted_prob,'ascend');
tree = [newq_str,init_str];
tree_prob = [newq_prob, init_prob];
parent= 0;
3
num_children = 2;
for i = 2:length(sorted_tree)
% Extract my symbol
me = sorted_tree{i};
treeplot(parent);
title(strcat('Huffman Coding Tree - "',String,'"'));
display(sorted_tree)
display(sorted_tree_prob)
[xs,ys,h,s] = treelayout(parent);
%% Label Tree Nodes
text(xs,ys,sorted_tree);
for i = 2:length(sorted_tree)
% Get my coordinate
my_x = xs(i);
my_y = ys(i);
text(mid_x,mid_y,num2str(weight(i)));
end
for i = 1:length(sorted_tree)
% Initialize code
code{i} = '';
%% Huffman Codebook
Huffman Tree:
0.9
dacbe
0.8
0
0.7
dacb
0.6 0
0.5 dac 1
0.4 1 1
0 ac
0.3
0 1
0.2
d a c b e
0.1
0
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
height = 4
Figure 1. Huffman tree plot for input "abcde" with user defined probabilities