All Exams Test series for 1 year @ ₹349 only
Question

A text is made up of the characters A, B, C, D, E each occurring with the probability 0.08, 0.40, 0.25, 0.15 and 0.12 respectively. The optimal coding technique will have the average length of:

The correct answer is

2.15

Understanding Optimal Coding for Data Compression

The question asks about the average length of an optimal coding technique for a given set of characters and their probabilities. An optimal coding technique aims to minimize the average code length required to represent the characters, based on their frequencies or probabilities. One of the most well-known methods for achieving optimal prefix coding is Huffman coding.

Applying Huffman Coding for Average Length

Huffman coding is an algorithm used for lossless data compression. It builds a variable-length prefix code table based on the estimated probability of occurrence for each source symbol. The key idea is to assign shorter codes to more frequent symbols and longer codes to less frequent symbols, resulting in the minimum possible average code length for that set of probabilities.

Given the characters and their probabilities:

  • A: 0.08
  • B: 0.40
  • C: 0.25
  • D: 0.15
  • E: 0.12

To find the optimal coding using Huffman algorithm, we follow these steps:

  1. Arrange the characters (or their probabilities) in ascending order: A (0.08), E (0.12), D (0.15), C (0.25), B (0.40).
  2. Combine the two smallest probabilities and treat them as a single node with a probability equal to their sum. Repeat this process until only one node remains.
    • Combine A (0.08) and E (0.12) → Node AE (0.20). Remaining: D (0.15), AE (0.20), C (0.25), B (0.40).
    • Sort and combine the smallest: D (0.15) and AE (0.20) → Node DAE (0.35). Remaining: C (0.25), DAE (0.35), B (0.40).
    • Sort and combine the smallest: C (0.25) and DAE (0.35) → Node CDAE (0.60). Remaining: B (0.40), CDAE (0.60).
    • Sort and combine the smallest: B (0.40) and CDAE (0.60) → Node BCDAE (1.00). This is the root.
  3. Assign binary codes (0s and 1s) to the branches of the tree from the root down to the leaves (characters). Typically, one branch is assigned '0' and the other '1'. Let's assign '0' to the left branch (usually representing the smaller probability) and '1' to the right branch at each node.
    • From root (1.00): Go to B (0.40) with '1', Go to CDAE (0.60) with '0'.
    • From CDAE (0.60): Go to C (0.25) with '00', Go to DAE (0.35) with '01'.
    • From DAE (0.35): Go to D (0.15) with '010', Go to AE (0.20) with '011'.
    • From AE (0.20): Go to A (0.08) with '0110', Go to E (0.12) with '0111'.

The generated Huffman codes and their lengths are:

Character Probability Huffman Code Code Length (\(L_i\))
A 0.08 0110 4
B 0.40 1 1
C 0.25 00 2
D 0.15 010 3
E 0.12 0111 4

Calculating the Average Code Length

The average length of the code is calculated by summing the product of each character's probability (\(p_i\)) and its corresponding code length (\(L_i\)). The formula is:

\[ Average\ Length = \sum_{i=1}^{n} p_i \times L_i \]

Using the probabilities and code lengths from the table:

Average Length = (0.08 \times 4) + (0.40 \times 1) + (0.25 \times 2) + (0.15 \times 3) + (0.12 \times 4)

Average Length = 0.32 + 0.40 + 0.50 + 0.45 + 0.48

Average Length = 2.15

The calculated average length for this optimal coding technique is 2.15.

Revision Table: Optimal Coding Key Concepts

Concept Description
Optimal Coding A method that minimizes the average code length for a set of symbols based on their probabilities.
Huffman Coding A greedy algorithm for optimal prefix coding. Assigns variable-length codes.
Prefix Code No code is a prefix of another code. This allows unambiguous decoding.
Average Length Expected code length, calculated as the sum of (probability * code length) for all symbols.
Data Compression Reducing the amount of data needed to represent information. Huffman coding is lossless compression.

Additional Information: Entropy and Coding Efficiency

The theoretical minimum average code length for a source is given by its entropy (\(H\)). Entropy is a measure of the uncertainty or randomness in the source. For a discrete source with probabilities \(p_1, p_2, \dots, p_n\), the entropy is calculated as:

\[ H = -\sum_{i=1}^{n} p_i \log_2(p_i) \]

Let's calculate the entropy for the given probabilities:

  • \(p_A \log_2(p_A) = 0.08 \times \log_2(0.08) \approx 0.08 \times -3.643 \approx -0.291\)
  • \(p_B \log_2(p_B) = 0.40 \times \log_2(0.40) \approx 0.40 \times -1.322 \approx -0.529\)
  • \(p_C \log_2(p_C) = 0.25 \times \log_2(0.25) = 0.25 \times -2.000 = -0.500\)
  • \(p_D \log_2(p_D) = 0.15 \times \log_2(0.15) \approx 0.15 \times -2.737 \approx -0.411\)
  • \(p_E \log_2(p_E) = 0.12 \times \log_2(0.12) \approx 0.12 \times -3.059 \approx -0.367\)

Entropy \(H \approx -(-0.291 - 0.529 - 0.500 - 0.411 - 0.367) = -(-2.098) = 2.098\) bits per character.

The average length of the Huffman code (2.15 bits) is very close to the entropy (2.098 bits), which demonstrates the efficiency of Huffman coding as an optimal coding technique. Huffman coding is guaranteed to achieve an average length within 1 bit of the entropy.

Was this answer helpful?

Important Questions from Greedy Algorithms - Teaching

  1. Match List-I with List-II:

    List-I

    List-II

    (a) Greedy best-first

    (i) Minimal cost (p)+h(p)

    (b) Lowest cost-first

    (ii) Minimal h(p)

    (c) A* algorithm

    (iii) Minimal cost (p)

    Choose the correct option from those given below:

  2. Which among the following statement(s) is(are) FALSE?

    A. Greedy best‐first search is not optimal but is often efficient.

    B. A* is complete and optimal provided h(n) is admissible or consistent.

    C. Recursive best‐first search is efficient in terms of time complexity but poor in terms of space complexity.

    D. h(n) = 0 is an admissible heuristic for the 8‐puzzle.

  3. Suppose there are six files F1, F2, F3, F4, F5, F6 with corresponding sizes 150 KB, 225 KB, 75 KB, 60 KB, 275 KB and 65 KB respectively. The files are to be stored on a sequential device in such a way that optimizes access time. In what order should the files be stored?

  4. A text is made up of the characters a, b, c, d, e each occurring with the probability 0.11, 0.40, 0.16, 0.09 and 0.24 respectively. The optimal Huffman coding technique will have the average length of :

  5. If b is the branching factor and m is the maximum depth of the search tree, what is the space complexity of greedy search ?  

Need Expert Advice?

Start Your Preparation with Prepp Mobile App

Download the app from Google Play & App Store
Download the app from Google Play & App Store
Prepp Mobile App