Академический Документы
Профессиональный Документы
Культура Документы
Huffman Coding |
Greedy Algo-3
E cient Huffman
Coding for Sorted Input
| Greedy Algo-4
Number of pairs in an
array with the sum
greater than 0
https://www.geeksforgeeks.org/huffman-coding-greedy-algo-3/ 1/38
7/30/2020 Huffman Coding | Greedy Algo-3 - GeeksforGeeks
Maximum pro t by
buying and selling a
share at most K times |
Greedy Approach
Find lexicographically
smallest string in at
most one swaps
Shortest path in a
directed graph by
Dijkstra’s algorithm
Maximum size of
square such that all
submatrices of that ▲
size have sum less than
K
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood
Got It !
our Cookie Policy & Privacy Policy
https://www.geeksforgeeks.org/huffman-coding-greedy-algo-3/ 2/38
7/30/2020 Huffman Coding | Greedy Algo-3 - GeeksforGeeks
Maximum Possible
Rating of a Coding
Contest
https://www.geeksforgeeks.org/huffman-coding-greedy-algo-3/ 3/38
7/30/2020 Huffman Coding | Greedy Algo-3 - GeeksforGeeks
Lexicographical
smallest number after
at most K consecutive
swaps
Minimum number of
colors required to color
a Circular Array
https://www.geeksforgeeks.org/huffman-coding-greedy-algo-3/ 4/38
7/30/2020 Huffman Coding | Greedy Algo-3 - GeeksforGeeks
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood
Got It !
our Cookie Policy & Privacy Policy
https://www.geeksforgeeks.org/huffman-coding-greedy-algo-3/ 5/38
7/30/2020 Huffman Coding | Greedy Algo-3 - GeeksforGeeks
codes be 00, 01, 0 and 1. This coding leads to ambiguity because code assigned to c is the pre x 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”.
See this for applications of Huffman Coding.
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 eld 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. use
We Create a new
cookies internal
to ensure node
you have withbrowsing
the best a frequency equal
experience towebsite.
on our the sum of the
By using our two nodes
site, you frequencies.
acknowledge that you Make the
have read and rst extracted
understood node as its left child
Got It !
our Cookie Policy & Privacy Policy
and the other extracted node as its right child. Add this node to the min heap.
https://www.geeksforgeeks.org/huffman-coding-greedy-algo-3/ 7/38
7/30/2020 Huffman Coding | Greedy Algo-3 - GeeksforGeeks
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.
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.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood
Got It !
our Cookie Policy & Privacy Policy
https://www.geeksforgeeks.org/huffman-coding-greedy-algo-3/ 8/38
7/30/2020 Huffman Coding | Greedy Algo-3 - GeeksforGeeks
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
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
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood Got It !
f 45 our Cookie Policy & Privacy Policy
https://www.geeksforgeeks.org/huffman-coding-greedy-algo-3/ 9/38
7/30/2020 Huffman Coding | Greedy Algo-3 - GeeksforGeeks
Step 5: Extract two minimum frequency nodes. Add a new internal node with frequency 25 + 30 = 55
character Frequency
f 45
Internal Node 55
Step 6: Extract two minimum frequency nodes. Add a new internal node with frequency 45 + 55 = 100
character Frequency
WeInternal Node
use cookies to ensure you100
have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood Got It !
our Cookie Policy & Privacy Policy
https://www.geeksforgeeks.org/huffman-coding-greedy-algo-3/ 11/38
7/30/2020 Huffman Coding | Greedy Algo-3 - GeeksforGeeks
Since the heap contains only one node, the algorithm stops here.
character code-word
f 0
c 100
d 101
a 1100
b 1101
e 111
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood
C our Cookie Policy & Privacy Policy
Got It !
https://www.geeksforgeeks.org/huffman-coding-greedy-algo-3/ 12/38
7/30/2020 Huffman Coding | Greedy Algo-3 - GeeksforGeeks
return temp;
}
// current size is 0
minHeap->size = 0;
minHeap->capacity = capacity;
minHeap->array
= (struct MinHeapNode**)malloc(minHeap->
capacity * sizeof(struct MinHeapNode*));
return minHeap;
}
// A utility function to
// swap two min heap nodes
void swapMinHeapNode(struct MinHeapNode** a,
struct MinHeapNode** b) ▲
{
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood
struct MinHeapNode* t = *a; Got It !
our Cookie Policy & Privacy Policy
*a = *b;
https://www.geeksforgeeks.org/huffman-coding-greedy-algo-3/ 14/38
7/30/2020 Huffman Coding | Greedy Algo-3 - GeeksforGeeks
*b = t;
}
if (smallest != idx) {
swapMinHeapNode(&minHeap->array[smallest],
&minHeap->array[idx]);
minHeapify(minHeap, smallest);
}
}
--minHeap->size;
minHeapify(minHeap, 0);
return temp;
}
++minHeap->size;
int i = minHeap->size - 1;
minHeap->array[i] = minHeapNode;
}
{
▲
int n = minHeap->size - 1;
int i;
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood
for (i = (n - 1) / 2; i >= 0; --i) Got It !
our Cookie Policy & Privacy Policy
minHeapify(minHeap, i);
https://www.geeksforgeeks.org/huffman-coding-greedy-algo-3/ 16/38
7/30/2020 Huffman Coding | Greedy Algo-3 - GeeksforGeeks
printf("\n");
}
minHeap->size = size;
buildMinHeap(minHeap); ▲
return minHeap;
}
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood
Got It !
our Cookie Policy & Privacy Policy
// The main function that builds Huffman tree
https://www.geeksforgeeks.org/huffman-coding-greedy-algo-3/ 17/38
7/30/2020 Huffman Coding | Greedy Algo-3 - GeeksforGeeks
{
struct MinHeapNode *left, *right, *top;
top->left = left;
top->right = right;
insertMinHeap(minHeap, top);
}
arr[top] = 0;
printCodes(root->left, arr, top + 1);
}
arr[top] = 1;
printCodes(root->right, arr, top + 1);
}
{ ▲
// Construct Huffman Tree
struct MinHeapNode* root
= buildHuffmanTree(data, freq, size);
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood
Got It !
our Cookie Policy & Privacy Policy
// Print Huffman codes using
https://www.geeksforgeeks.org/huffman-coding-greedy-algo-3/ 19/38
7/30/2020 Huffman Coding | Greedy Algo-3 - GeeksforGeeks
___
https://www.geeksforgeeks.org/huffman-coding-greedy-algo-3/ 6/38
7/30/2020 Huffman Coding | Greedy Algo-3 - GeeksforGeeks
return 0;
}
C++
https://www.geeksforgeeks.org/huffman-coding-greedy-algo-3/ 20/38
7/30/2020 Huffman Coding | Greedy Algo-3 - GeeksforGeeks
Step 4: Extract two minimum frequency nodes. Add a new internal node with frequency 14 + 16 = 30
Ad
7 Advantages of a Managed DB
We've identified the top benefits of switching to a managed database
character Frequency ▲
Internal Node 25
Internal Node 30
f to ensure you have the
We use cookies 45best browsing experience on our website. By using our site, you acknowledge that you have read and understood
Got It !
our Cookie Policy & Privacy Policy
https://www.geeksforgeeks.org/huffman-coding-greedy-algo-3/ 10/38
7/30/2020 Huffman Coding | Greedy Algo-3 - GeeksforGeeks
return temp; ▲
}
// current size is 0
minHeap->size = 0;
minHeap->capacity = capacity;
minHeap->array
= (struct MinHeapNode**)malloc(minHeap->
capacity * sizeof(struct MinHeapNode*));
return minHeap;
}
// A utility function to
// swap two min heap nodes
void swapMinHeapNode(struct MinHeapNode** a,
struct MinHeapNode** b)
smallest = left;
if (smallest != idx) {
swapMinHeapNode(&minHeap->array[smallest],
&minHeap->array[idx]);
minHeapify(minHeap, smallest);
}
}
--minHeap->size;
minHeapify(minHeap, 0);
return temp; ▲
}
++minHeap->size;
int i = minHeap->size - 1;
minHeap->array[i] = minHeapNode;
}
int n = minHeap->size - 1;
int i;
minHeap->size = size;
buildMinHeap(minHeap);
return minHeap;
}
{
struct MinHeapNode *left, *right, *top;
top->left = left;
top->right = right;
insertMinHeap(minHeap, top);
}
arr[top] = 0;
printCodes(root->left, arr, top + 1); ▲
}
https://www.geeksforgeeks.org/huffman-coding-greedy-algo-3/ 26/38
7/30/2020 Huffman Coding | Greedy Algo-3 - GeeksforGeeks
arr[top] = 1;
printCodes(root->right, arr, top + 1);
}
{
// Construct Huffman Tree
struct MinHeapNode* root
= buildHuffmanTree(data, freq, size);
return 0;
}
// For comparison of
// two
We use cookies heap you
to ensure nodes (needed
have the in min
best browsing heap)
experience on our website. By using our site, you acknowledge that you have read and understood Got It !
struct compare { our Cookie Policy & Privacy Policy
https://www.geeksforgeeks.org/huffman-coding-greedy-algo-3/ 28/38
7/30/2020 Huffman Coding | Greedy Algo-3 - GeeksforGeeks
{
return (l->freq > r->freq);
}
};
if (!root)
return;
if (root->data != '$')
cout << root->data << ": " << str << "\n";
left = minHeap.top();
minHeap.pop();
right = minHeap.top();
minHeap.pop();
top->left = left;
top->right = right;
minHeap.push(top);
}
return 0;
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood Got It !
}
our Cookie Policy & Privacy Policy
https://www.geeksforgeeks.org/huffman-coding-greedy-algo-3/ 30/38
7/30/2020 Huffman Coding | Greedy Algo-3 - GeeksforGeeks
Java
import java.util.PriorityQueue;
import java.util.Scanner;
import java.util.Comparator;
int data;
char c;
HuffmanNode left;
HuffmanNode right;
}
https://www.geeksforgeeks.org/huffman-coding-greedy-algo-3/ 31/38
7/30/2020 Huffman Coding | Greedy Algo-3 - GeeksforGeeks
return;
}
// main function
public static void main(String[] args)
{
// number of characters.
int n = 6;
char[] charArray = { 'a', 'b', 'c', 'd', 'e', 'f' }; ▲
int[] charfreq = { 5, 9, 12, 13, 16, 45 };
hn.c = charArray[i];
hn.data = charfreq[i];
hn.left = null;
hn.right = null;
f: 0
c: 100
d: 101
a: 1100
b: 1101
e: 111
▲
Time complexity: O(nlogn) where n is the number of unique characters. If there are n nodes, extractMin() is called 2*(n – 1) times.
extractMin() takes O(logn) time as it calles minHeapify(). So, overall complexity is O(nlogn).
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood
Got It !
our Cookie Policy & Privacy Policy
If the input array is sorted, there exists a linear time algorithm. We will soon be discussing in our next post.
https://www.geeksforgeeks.org/huffman-coding-greedy-algo-3/ 34/38
7/30/2020 Huffman Coding | Greedy Algo-3 - GeeksforGeeks
Recommended Posts:
E cient Huffman Coding for Sorted Input | Greedy Algo-4
Canonical Huffman Coding
Image Compression using Huffman Coding
Huffman Coding using Priority Queue
Huffman Decoding
Practice Questions on Huffman Encoding
Correctness of Greedy Algorithms
Greedy approach vs Dynamic programming
Graph Coloring | Set 2 (Greedy Algorithm)
Greedy Algorithm for Egyptian Fraction
Boruvka's algorithm | Greedy Algo-9
Top 20 Greedy Algorithms Interview Questions
Coin game of two corners (Greedy Approach)
Set Cover Problem | Set 1 (Greedy Approximate Algorithm) ▲
K Centers Problem | Set 1 (Greedy Approximate Algorithm)
Greedy Algorithms (General Structure and Applications)
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood Got It !
Activity Selection Problem | Greedy Algo-1 our Cookie Policy & Privacy Policy
https://www.geeksforgeeks.org/huffman-coding-greedy-algo-3/ 36/38
7/30/2020 Huffman Coding | Greedy Algo-3 - GeeksforGeeks
Reference:
http://en.wikipedia.org/wiki/Huffman_coding
This article is compiled by Aashish Barnwal and reviewed by GeeksforGeeks team. Please write comments if you nd anything incorrect, or
you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly
price and become industry ready.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood
Got It !
our Cookie Policy & Privacy Policy
https://www.geeksforgeeks.org/huffman-coding-greedy-algo-3/ 35/38
7/30/2020 Huffman Coding | Greedy Algo-3 - GeeksforGeeks
Article Tags : Greedy Heap Amazon encoding-decoding Huffman Coding Morgan Stanley priority-queue Samsung United Health Group
Practice Tags : Morgan Stanley Amazon Samsung United Health Group Greedy Heap priority-queue
64
4.1
To-do Done
Based on 192 vote(s)
Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.
Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.
https://www.geeksforgeeks.org/huffman-coding-greedy-algo-3/ 37/38
7/30/2020 Huffman Coding | Greedy Algo-3 - GeeksforGeeks
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood
Got It !
our Cookie Policy & Privacy Policy
https://www.geeksforgeeks.org/huffman-coding-greedy-algo-3/ 38/38