2080 3

CSC330 ยท 2080 ยท 10 marks

What do you mean by Huffman coding? Explain the Huffman coding process with example.

What do you mean by Huffman coding? Explain the Huffman coding process with example.[10]

Multimedia ComputingChapter 5: Data CompressionSource, Entropy and Hybrid Coding

Quick answer

Huffman coding is a lossless data compression technique and a type of entropy coding that assigns variable-length binary codes to input characters based on their frequency of occurrence. Characters that occur more frequently are assigned shorter codes, while characters that occur less frequently are assigned longer codes. This minimizes the total number of bits required to represent the data.

Huffman Coding

Definition

: "Run-length coding, Huffman coding, Arithmetic coding are entropy coding" and in JPEG entropy encoding, "Huffman coding is preferred as it is free."


Key Characteristics

  • It is a lossless (hassless) compression method -- decompression regenerates data completely.
  • It is based on the frequency/probability of characters in the data.
  • It produces an optimal prefix code -- no code is a prefix of another code.
  • It uses a binary tree structure called the Huffman Tree.

Huffman Coding Process (Algorithm)

The steps to build a Huffman code are:

Step 1: Count the frequency of each character in the input data.

Step 2: Create a leaf node for each character and insert all nodes into a min-priority queue (ordered by frequency, lowest first).

Step 3: While there is more than one node in the queue:

  • Remove the two nodes with the lowest frequency (call them Left and Right).
  • Create a new internal node with frequency = Left frequency + Right frequency.
  • Assign the Left node as the left child and the Right node as the right child.
  • Insert the new internal node back into the queue.

Step 4: The remaining single node is the root of the Huffman Tree.

Step 5: Assign binary codes by traversing the tree:

  • Going Left = assign bit 0
  • Going Right = assign bit 1

Step 6: The code for each character is the sequence of bits from the root to that leaf node.


Worked Example

Given Input String:

"AABBBCCDDDDEE"

Step 1: Calculate Frequencies

CharacterFrequency
A2
B3
C2
D4
E2

Total characters = 13


Step 2: Build Min-Priority Queue (sorted by frequency)

A(2), C(2), E(2), B(3), D(4)

Step 3: Build the Huffman Tree

Iteration 1: Pick two lowest: A(2) and C(2)

  • Create internal node AC(4)
  • Queue: E(2), B(3), AC(4), D(4)
    AC(4)
   /     \
 A(2)   C(2)

Iteration 2: Pick two lowest: E(2) and B(3)

  • Create internal node EB(5)
  • Queue: AC(4), D(4), EB(5)
    EB(5)
   /     \
 E(2)   B(3)

Iteration 3: Pick two lowest: AC(4) and D(4)

  • Create internal node ACD(8)
  • Queue: EB(5), ACD(8)
     ACD(8)
    /      \
  AC(4)   D(4)
  /   \
A(2) C(2)

Iteration 4: Pick two lowest: EB(5) and ACD(8)

  • Create root node EBACD(13)
  • Queue: EBACD(13) -- only one node remains, this is the root
          EBACD(13)
         /          \
       EB(5)        ACD(8)
      /    \        /    \
    E(2)  B(3)   AC(4)  D(4)
                 /    \
               A(2)  C(2)

Step 4 and 5: Assign Codes (Left = 0, Right = 1)

Traverse from root to each leaf:

CharacterPath from RootHuffman CodeCode Length
ELeft, Left002 bits
BLeft, Right012 bits
ARight, Left, Left1003 bits
CRight, Left, Right1013 bits
DRight, Right112 bits

Step 6: Calculate Compression

Total bits with Huffman coding:

CharacterFrequencyCodeBits Used
A21002 x 3 = 6
B3013 x 2 = 6
C21012 x 3 = 6
D4114 x 2 = 8
E2002 x 2 = 4
Total1330 bits

Without Huffman coding (fixed 3-bit code for 5 characters):

  • 13 characters x 3 bits = 39 bits

Compression achieved: 39 - 30 = 9 bits saved (approximately 23% compression)


Advantages of Huffman Coding

  1. It is lossless -- original data is perfectly recovered.
  2. It produces optimal prefix-free codes.
  3. It is widely used in JPEG, MPEG, ZIP and other multimedia compression standards.
  4. Frequently occurring symbols get shorter codes, reducing overall data size.

Summary

Huffman coding is a powerful entropy coding technique that exploits the statistical redundancy in the data by assigning shorter binary codes to more frequent characters and longer codes to rarer ones, while the prefix-free property of the resulting codes guarantees that the encoded bit stream can always be decoded unambiguously using the same binary tree that was built during encoding, without needing any separators between codes.

Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.