Greedy Algorithms

Huffman codes

Data Compression

The problem of data compression

Fixed-length binary codes

Variable-length binary codes

Prefix-free codes

Finding an optimal code to compress text

Understanding the pseudocode

Huffman's strategy

Pseudo code for Huffman's algorithm

Another example

Analyzing the Huffman tree

  • It will take O(n) time to initialize the heap for the priority queue
  • Every time an item is dequeued or enqued, it takes O(log n) time
  • Since we go through the loop n-1 times, it takes n + 3(log n)* (n-1) time
  • The Big O time complexity is then O(n log n) to build the tree to get the code to compress a text file.