0% found this document useful (0 votes)
6 views

Prim’s Huffmans Coding Algorithm

Uploaded by

KIM Denzel
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Prim’s Huffmans Coding Algorithm

Uploaded by

KIM Denzel
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Prim’s Algorithm for Minimum Spanning Tree

(MST)

Introduction to Prim’s algorithm:


We have discussed Kruskal’s algorithm for Minimum Spanning Tree. Like Kruskal’s
algorithm, Prim’s algorithm is also a Greedy algorithm. This algorithm always starts
with a single node and moves through several adjacent nodes, in order to explore all
of the connected edges along the way.
The algorithm starts with an empty spanning tree. The idea is to maintain two sets of
vertices. The first set contains the vertices already included in the MST, and the
other set contains the vertices not yet included. At every step, it considers all the
edges that connect the two sets and picks the minimum weight edge from these
edges. After picking the edge, it moves the other endpoint of the edge to the set
containing 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).

How does Prim’s Algorithm Work?


The working of Prim’s algorithm can be described by using the following steps:
Step 1: Determine an arbitrary vertex as the starting vertex of the MST.
Step 2: Follow steps 3 to 5 till there are vertices that are not included in the MST
(known as fringe vertex).
Step 3: Find edges connecting any tree vertex with the fringe vertices.
Step 4: Find the minimum among these edges.
Step 5: Add the chosen edge to the MST if it does not form any cycle.
Step 6: Return the MST and exit

Note: For determining a cycle, we can divide the vertices into two sets [one set
contains the vertices included in MST and the other contains the fringe vertices.]

Illustration of Prim’s Algorithm:

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.

1 is added to the MST


Step 3: The edges connecting the incomplete MST to other vertices are {0, 7}, {1, 7}
and {1, 2}. Among these edges the minimum weight is 8 which is of the edges {0, 7}
and {1, 2}. Let us here include the edge {0, 7} and the vertex 7 in the MST. [We could
have also included edge {1, 2} and vertex 2 in the MST].

7 is added in the MST


Step 4: The edges that connect the incomplete MST with the fringe vertices are {1,
2}, {7, 6} and {7, 8}. Add the edge {7, 6} and the vertex 6 in the MST as it has the
least weight (i.e., 1).

6 is added in the MST
Step 5: The connecting edges now are {7, 8}, {1, 2}, {6, 8} and {6, 5}. Include edge
{6, 5} and vertex 5 in the MST as the edge has the minimum weight (i.e., 2) among
them.

Include vertex 5 in the MST


Step 6: Among the current connecting edges, the edge {5, 2} has the minimum
weight. So include that edge and the vertex 2 in the MST.

Include vertex 2 in the MST


Step 7: The connecting edges between the incomplete MST and the other edges are
{2, 8}, {2, 3}, {5, 3} and {5, 4}. The edge with minimum weight is edge {2, 8} which
has weight 2. So include this edge and the vertex 8 in the MST.

Add vertex 8 in the MST
Step 8: See here that the edges {7, 8} and {2, 3} both have same weight which are
minimum. But 7 is already part of MST. So we will consider the edge {2, 3} and
include that edge and vertex 3 in the MST.

Include vertex 3 in MST


Step 9: Only the vertex 4 remains to be included. The minimum weighted edge from
the incomplete MST to 4 is {3, 4}.

Include vertex 4 in the MST


The final structure of the MST is as follows and the weight of the edges of the MST is
(4 + 8 + 1 + 2 + 4 + 2 + 7 + 9) = 37.

The structure of the MST formed using the above method
Note: If we had selected the edge {1, 2} in the third step then the MST would look
like the following.

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.

Huffman Coding | Greedy Algo-3

Huffman coding is a lossless data compression algorithm. The idea is to assign


variable-length codes to input characters, lengths of the assigned codes are based
on the frequencies of corresponding characters.
The variable-length codes assigned to input characters are Prefix Codes, means the
codes (bit sequences) are assigned in such a way that the code assigned to one
character is not the prefix of code assigned to any other character. This is how
Huffman Coding makes sure that there is no ambiguity when decoding the generated
bitstream.
Let us understand prefix codes with a counter example. Let there be four characters
a, b, c and d, and their corresponding variable length codes be 00, 01, 0 and 1. This
coding leads to ambiguity because code assigned to c is the prefix of codes
assigned to a and b. If the compressed bit stream is 0001, the de-compressed output
may be “cccd” or “ccb” or “acd” or “ab”.
Build a Huffman Tree from input characters.

Traverse the Huffman Tree and assign codes to characters.


Algorithm: The method which is used to construct optimal prefix code is called
Huffman coding. This algorithm builds a tree in bottom up manner. We can denote
this tree by T

Steps to build Huffman Tree


Input is an array of unique characters along with their frequency of occurrences and
output is Huffman Tree.
1. Create a leaf node for each unique character and build a min heap of all leaf
nodes (Min Heap is used as a priority queue. The value of frequency field is
used to compare two nodes in min heap. Initially, the least frequent character
is at root)
2. Extract two nodes with the minimum frequency from the min heap.

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

You might also like