Activity Selection Problem + Huffman encoding tree
Activity Selection Problem + Huffman encoding tree
ActivitySelection(activities)
for i = 1 to n - 1
selected.append(activities[i])
last_selected = i
return selected
What is Huffman Encoding?
Huffman encoding is a Greedy Algorithm used for data compression. It assigns variable-
length binary codes to characters such that:
1. Create a Min-Heap:
o Start with leaf nodes for each character and their frequencies.
2. Create a new internal node with a frequency equal to the sum of the
two nodes' frequencies.
3. Make the two nodes the left and right children of the new node.
3. Generate Codes:
o Traverse the Huffman tree from the root to each leaf node.
4. Encode/Decode:
HuffmanEncoding(frequencies)
- A character
- Its frequency
Traverse(root, code)
If node is a leaf:
Otherwise: