Greedy Algorithms8 sections · 316 units
Open in Course

Problem - Huffman Coding

Optimal prefix codes

Huffman coding compresses data by assigning shorter codes to frequent characters.

Example: in "aaabbc", 'a' appears 33 times, 'b' twice, 'c' once.

Instead of fixed 22-bit codes, use variable: a= 00, b= 1010, c= 1111. Original: 66 chars * 22 bits = 1212 bits. Huffman: 3311 + 2222 + 11*22 = 99 bits. 2525% smaller! Constraint: the character set can have up to 10510^5 distinct characters, so the heap must handle large inputs efficiently. The compression ratio depends on frequency distribution. Uniform frequencies yield minimal compression.