Prim’s Huffmans Coding Algorithm
Prim’s Huffmans Coding Algorithm
(MST)
A group of edges that connects two sets of vertices in a graph is called cut in graph
theory. So, at every step of Prim’s algorithm, find a cut, pick the minimum weight
edge from the cut, and include this vertex in MST Set (the set that contains already
included vertices).
Consider the following graph as an example for which we need to find the Minimum
Spanning Tree (MST).
○
Example of a graph
Step 1: Firstly, we select an arbitrary vertex that acts as the starting vertex of the
Minimum Spanning Tree. Here we have selected vertex 0 as the starting vertex.
○
0 is selected as starting vertex
Step 2: All the edges connecting the incomplete MST and other vertices are the
edges {0, 1} and {0, 7}. Between these two the edge with minimum weight is {0, 1}.
So include the edge and vertex 1 in the MST.
○
Structure of the alternate MST if we had selected edge {1, 2} in the MST
How to implement Prim’s Algorithm?
Follow the given steps to utilize the Prim’s Algorithm mentioned above for finding
MST of a graph:
● Create a set mstSet that keeps track of vertices already included in MST.
● Assign a key value to all vertices in the input graph. Initialize all key values as
INFINITE. Assign the key value as 0 for the first vertex so that it is picked first.
● While mstSet doesn’t include all vertices
○ Pick a vertex u that is not there in mstSet and has a minimum key
value.
○ Include u in the mstSet.
○ Update the key value of all adjacent vertices of u. To update the key
values, iterate through all adjacent vertices.
○ For every adjacent vertex v, if the weight of edge u-v is less than
the previous key value of v, update the key value as the weight
of u-v.
○
3. Create a new internal node with a frequency equal to the sum of the two
nodes frequencies. Make the first extracted node as its left child and the other
extracted node as its right child. Add this node to the min heap.
4. Repeat steps#2 and #3 until the heap contains only one node. The remaining
node is the root node and the tree is complete.
Let us understand the algorithm with an example:
character Frequency
a 5
b 9
c 12
d 13
e 16
f 45
Step 1. Build a min heap that contains 6 nodes where each node represents root of a
tree with single node.
Step 2 Extract two minimum frequency nodes from min heap. Add a new internal
node with frequency 5 + 9 = 14.
Illustration of step 2
Now min heap contains 5 nodes where 4 nodes are roots of trees with single
element each, and one heap node is root of tree with 3 elements
character Frequency
c 12
d 13
Internal Node 14
e 16
f 45
Step 3: Extract two minimum frequency nodes from heap. Add a new internal node
with frequency 12 + 13 = 25
Illustration of step 3
Now min heap contains 4 nodes where 2 nodes are roots of trees with single
element each, and two heap nodes are root of tree with more than one nodes
character Frequency
Internal Node 14
e 16
Internal Node 25
f 45
Step 4: Extract two minimum frequency nodes. Add a new internal node with
frequency 14 + 16 = 30
Illustration of step 4
Now min heap contains 3 nodes.
character Frequency
Internal Node 25
Internal Node 30
f 45
Step 5: Extract two minimum frequency nodes. Add a new internal node with
frequency 25 + 30 = 55
Illustration of step 5
Now min heap contains 2 nodes.
character Frequency
f 45
Internal Node 55
Step 6: Extract two minimum frequency nodes. Add a new internal node with
frequency 45 + 55 = 100
Illustration of step 6
Now min heap contains only one node.
character Frequency
Internal Node 100
Since the heap contains only one node, the algorithm stops here.
Steps to print codes from Huffman Tree:
Traverse the tree formed starting from the root. Maintain an auxiliary array. While
moving to the left child, write 0 to the array. While moving to the right child, write 1 to
the array. Print the array when a leaf node is encountered.
Steps to print code from HuffmanTree
The codes are as follows:
character code-word
f 0
c 100
d 101
a 1100
b 1101
e 111