|
| 1 | +--- |
| 2 | +id: huffman-coding |
| 3 | +title: Huffman Coding Solution |
| 4 | +sidebar_label: 0003 - Huffman Coding |
| 5 | +tags: [Huffman Coding, Data Compression, Algorithm, C++, Problem Solving] |
| 6 | +description: This is a solution to the Huffman Coding problem. |
| 7 | +--- |
| 8 | + |
| 9 | +## Problem Statement |
| 10 | + |
| 11 | +### Problem Description |
| 12 | + |
| 13 | +Huffman Coding assigns variable-length codes to input characters, with shorter codes assigned to more frequent characters, resulting in efficient compression. Given a set of characters and their frequencies, the goal is to build a Huffman Tree and determine the Huffman codes for each character. |
| 14 | + |
| 15 | +### Examples |
| 16 | + |
| 17 | +**Example 1:** |
| 18 | + |
| 19 | +```plaintext |
| 20 | +Input: Characters = {a, b, c, d, e, f}, Frequencies = {5, 9, 12, 13, 16, 45} |
| 21 | +Output: |
| 22 | +a: 1100 |
| 23 | +b: 1101 |
| 24 | +c: 100 |
| 25 | +d: 101 |
| 26 | +e: 111 |
| 27 | +f: 0 |
| 28 | +``` |
| 29 | + |
| 30 | +### Constraints |
| 31 | +The input should be a set of characters and their respective frequencies. |
| 32 | + |
| 33 | +## Solution of Given Problem |
| 34 | + |
| 35 | +### Intuition and Approach |
| 36 | + |
| 37 | +The Huffman Coding algorithm follows these steps: |
| 38 | + |
| 39 | +1. Create a leaf node for each character and build a min heap of all leaf nodes. |
| 40 | +2. Extract two nodes with the smallest frequency from the min heap. |
| 41 | +3. Create a new internal node with a frequency equal to the sum of the two nodes' frequencies. |
| 42 | +4. Add the new node to the min heap. |
| 43 | +5. Repeat steps 2-4 until the heap contains only one node, which becomes the root of the Huffman Tree. |
| 44 | +6. Assign binary codes to each character based on their position in the Huffman Tree. |
| 45 | + |
| 46 | +### Approaches |
| 47 | + |
| 48 | +#### Codes in Different Languages |
| 49 | + |
| 50 | +<Tabs> |
| 51 | + <TabItem value="cpp" label="C++" default> |
| 52 | + <SolutionAuthor name="sjain1909"/> |
| 53 | + ```cpp |
| 54 | + #include <bits/stdc++.h> |
| 55 | + using namespace std; |
| 56 | + struct MinHeapNode { |
| 57 | + char data; |
| 58 | + int freq; |
| 59 | + MinHeapNode *left, *right; |
| 60 | + |
| 61 | + MinHeapNode(char data, int freq) { |
| 62 | + left = right = nullptr; |
| 63 | + this->data = data; |
| 64 | + this->freq = freq; |
| 65 | + } |
| 66 | + }; |
| 67 | + |
| 68 | + struct compare { |
| 69 | + bool operator()(MinHeapNode* left, MinHeapNode* right) { |
| 70 | + return (left->freq > right->freq); |
| 71 | + } |
| 72 | + }; |
| 73 | + void printCodes(struct MinHeapNode* root, string str) { |
| 74 | + if (!root) |
| 75 | + return; |
| 76 | + |
| 77 | + if (root->data != '$') |
| 78 | + cout << root->data << ": " << str << "\n"; |
| 79 | + |
| 80 | + printCodes(root->left, str + "0"); |
| 81 | + printCodes(root->right, str + "1"); |
| 82 | + } |
| 83 | + |
| 84 | + void HuffmanCodes(char data[], int freq[], int size) { |
| 85 | + struct MinHeapNode *left, *right, *top; |
| 86 | + priority_queue<MinHeapNode*, vector<MinHeapNode*>, compare> minHeap; |
| 87 | + |
| 88 | + for (int i = 0; i < size; ++i) |
| 89 | + minHeap.push(new MinHeapNode(data[i], freq[i])); |
| 90 | + |
| 91 | + while (minHeap.size() != 1) { |
| 92 | + left = minHeap.top(); |
| 93 | + minHeap.pop(); |
| 94 | + |
| 95 | + right = minHeap.top(); |
| 96 | + minHeap.pop(); |
| 97 | + |
| 98 | + top = new MinHeapNode('$', left->freq + right->freq); |
| 99 | + |
| 100 | + top->left = left; |
| 101 | + top->right = right; |
| 102 | + |
| 103 | + minHeap.push(top); |
| 104 | + } |
| 105 | + printCodes(minHeap.top(), ""); |
| 106 | + } |
| 107 | + |
| 108 | + int main() { |
| 109 | + char arr[] = {'a', 'b', 'c', 'd', 'e', 'f'}; |
| 110 | + int freq[] = {5, 9, 12, 13, 16, 45}; |
| 111 | + int size = sizeof(arr) / sizeof(arr[0]); |
| 112 | + |
| 113 | + HuffmanCodes(arr, freq, size); |
| 114 | + |
| 115 | + return 0; |
| 116 | + } |
| 117 | + ``` |
| 118 | +
|
| 119 | + </TabItem> |
| 120 | +</Tabs> |
| 121 | +
|
| 122 | +### Complexity Analysis |
| 123 | +
|
| 124 | +- Time Complexity: $O(n \log n)$ |
| 125 | +- Space Complexity: $O(n)$ |
| 126 | +- Where `n` is the number of characters. |
| 127 | +- The time complexity is dominated by the operations on the min heap. |
| 128 | +- The space complexity is linear due to the storage of the Huffman Tree. |
| 129 | +
|
| 130 | +## Video Explanation of Given Problem |
| 131 | +
|
| 132 | + <LiteYouTubeEmbed |
| 133 | + id="uDS8AkTAcIU" |
| 134 | + params="autoplay=1&autohide=1&showinfo=0&rel=0" |
| 135 | + title="Problem Explanation | Solution | Approach" |
| 136 | + poster="maxresdefault" |
| 137 | + webp |
| 138 | + /> |
| 139 | +
|
| 140 | +--- |
| 141 | +
|
| 142 | +<h2>Authors:</h2> |
| 143 | +
|
| 144 | +<div style={{display: 'flex', flexWrap: 'wrap', justifyContent: 'space-between', gap: '10px'}}> |
| 145 | +{['sjain1909'].map(username => ( |
| 146 | + <Author key={username} username={username} /> |
| 147 | +))} |
| 148 | +</div> |
0 commit comments