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

61 Practical 06

The document describes the Huffman coding algorithm which assigns variable length codes to characters based on their frequency. It provides an example and outlines the steps of the greedy Huffman algorithm. These include calculating character frequencies, creating a min heap of nodes, repeatedly merging the two least frequent nodes until a single root node remains, and assigning codes to characters based on their position in the Huffman tree.

Uploaded by

Vedant Gawade
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

61 Practical 06

The document describes the Huffman coding algorithm which assigns variable length codes to characters based on their frequency. It provides an example and outlines the steps of the greedy Huffman algorithm. These include calculating character frequencies, creating a min heap of nodes, repeatedly merging the two least frequent nodes until a single root node remains, and assigning codes to characters based on their position in the Huffman tree.

Uploaded by

Vedant Gawade
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

PRACTICAL -06

NAME : Vedant Gawade


PRN : 202101040090 ROLL NO : 61
BATCH : A-3
SUBJECT : DAA[LAB]
PROBLEM STATEMENT :
Design Implement Huffman Algorithm using a Greedy Approach. Calculate the time complexity of
the algorithm.

Theory:
The Huffman coding algorithm is a method used for lossless data compression. It assigns variable-length
codes to input characters, with shorter codes assigned to more frequent characters. This allows for more
efficient encoding of the input data. Huffman coding is typically implemented using a greedy approach,
where at each step, the two least frequent symbols are merged into a single node until only one node
remains, which becomes the root of the Huffman tree.

Example :
Consider the following example where we want to encode the string "ABBCCCDDDEEEEE":
Calculate the frequency of each character:
• A: 1 B: 2 C: 3 D: 3 E: 5

Assign codes to each character


A: 100 B: 101 C: 00 D: 01 E: 11
So, the encoded string for "ABBCCCDDDEEEEE" would be "100101101000001111111111".
Algorithm ;
The Huffman algorithm using a greedy approach can be summarized as follows:
1. Calculate the frequency of each character in the input.
2. Create a priority queue (min-heap) containing all the characters and their frequencies.
3. While there is more than one node in the queue:
4. Remove the two nodes with the lowest frequency from the queue.
5. Create a new node whose frequency is the sum of the frequencies of the two nodes removed.
6. Make the two removed nodes children of the new node.
7. Add the new node to the queue.
8. The remaining node in the queue is the root of the Huffman tree.
CODE :
#include <stdio.h>
#include <stdlib.h>
#define MAX_TREE_HT 100
struct MinHeapNode {
char data;
unsigned freq;
struct MinHeapNode *left, *right;
};
struct MinHeap
{
unsigned size;
unsigned capacity;
struct MinHeapNode** array;
};
struct MinHeapNode* newNode(char data, unsigned freq)
{
struct MinHeapNode* temp = (struct MinHeapNode*)malloc( sizeof(struct MinHeapNode));
temp->left = temp->right = NULL;
temp->data = data;
temp->freq = freq;
return temp;
}
struct MinHeap* createMinHeap(unsigned capacity)
{
struct MinHeap* minHeap = (struct MinHeap*)malloc(sizeof(struct MinHeap));
minHeap->size = 0;
minHeap->capacity = capacity;
minHeap->array = (struct MinHeapNode**)malloc(minHeap->capacity * sizeof(struct
MinHeapNode*));
return minHeap;
}
void swapMinHeapNode(struct MinHeapNode** a, struct MinHeapNode** b)

{
struct MinHeapNode* t = *a;
*a = *b;
*b = t;
}
void minHeapify(struct MinHeap* minHeap, int idx)
{
int smallest = idx;
int left = 2 * idx + 1;
int right = 2 * idx + 2;
if (left < minHeap->size && minHeap->array[left]->freq < minHeap->array[smallest]-
>freq)
smallest = left;

if (right < minHeap->size && minHeap->array[right]->freq < minHeap->array[smallest]-


>freq)
smallest = right;
if (smallest != idx) {
swapMinHeapNode(&minHeap->array[smallest], &minHeap->array[idx]);
minHeapify(minHeap, smallest);
}
}

int isSizeOne(struct MinHeap* minHeap)


{
return (minHeap->size == 1);
}
struct MinHeapNode* extractMin(struct MinHeap* minHeap)
{
struct MinHeapNode* temp = minHeap->array[0];
minHeap->array[0] = minHeap->array[minHeap->size - 1];
--minHeap->size;
minHeapify(minHeap, 0);
return temp;
}
void insertMinHeap(struct MinHeap* minHeap, struct MinHeapNode* minHeapNode)

{
++minHeap->size;
int i = minHeap->size - 1;
while (i && minHeapNode->freq < minHeap->array[(i - 1) / 2]->freq) {

minHeap->array[i] = minHeap->array[(i - 1) / 2];


i = (i - 1) / 2;
}
minHeap->array[i] = minHeapNode;
}
void buildMinHeap(struct MinHeap* minHeap)
{
int n = minHeap->size - 1;
int i;
for (i = (n - 1) / 2; i >= 0; --i)
minHeapify(minHeap, i);
}
void printArr(int arr[], int n)
{
int i;
for (i = 0; i < n; ++i)
printf("%d", arr[i]);
printf("\n");
}
int isLeaf(struct MinHeapNode* root)
{

return !(root->left) && !(root->right);


}
struct MinHeap* createAndBuildMinHeap(char data[], int freq[], int size)
{

struct MinHeap* minHeap = createMinHeap(size);


for (int i = 0; i < size; ++i)
minHeap->array[i] = newNode(data[i], freq[i]);
minHeap->size = size;
buildMinHeap(minHeap);
return minHeap;
}
struct MinHeapNode* buildHuffmanTree(char data[], int freq[], int size)

{
struct MinHeapNode *left, *right, *top;
struct MinHeap* minHeap = createAndBuildMinHeap(data, freq, size);
while (!isSizeOne(minHeap)) {
left = extractMin(minHeap);
right = extractMin(minHeap);
top = newNode('$', left->freq + right->freq);
top->left = left;
top->right = right;
insertMinHeap(minHeap, top);
}
return extractMin(minHeap);
}
void printCodes(struct MinHeapNode* root, int arr[], int top)
{
if (root->left) {

arr[top] = 0;
printCodes(root->left, arr, top + 1);
}
if (root->right) {

arr[top] = 1;
printCodes(root->right, arr, top + 1);
}
if (isLeaf(root)) {

printf("%c: ", root->data);


printArr(arr, top);
}
}
void HuffmanCodes(char data[], int freq[], int size)

{
struct MinHeapNode* root = buildHuffmanTree(data, freq, size);
int arr[MAX_TREE_HT], top = 0;
printCodes(root, arr, top);
}
int main()
{
char arr[] = { 'a', 'b', 'c', 'd', 'e', 'f' };
int freq[] = { 5, 9, 12, 13, 16, 45 };
int size = sizeof(arr) / sizeof(arr[0]);
HuffmanCodes(arr, freq, size);
return 0;
}

OUTPUT:

Time Complexity :
O(nlogn),where n is the number of unique characters.
Time Complexity is O(nlogn) because we are extracting minimum nodes 2*(n-1) times and each time
whenever a node is being extracted from the heap then a function called heapify() is being called to
rearrange the element according to their priority.This function heapify() takes O(logn) time.
Space Complexity: O(N)

You might also like