📜  门| GATE-CS-2002 |第 32 题(1)

📅  最后修改于: 2023-12-03 15:12:39.811000             🧑  作者: Mango

GATE-CS-2002 - Problem 32

This question is about Huffman coding and Prefix coding.

Part 1

For the given set of characters and their corresponding frequencies:

| Character | Frequency | |-----------|-----------| | A | 10 | | B | 15 | | C | 12 | | D | 3 | | E | 4 | | F | 13 | | G | 1 | | H | 2 |

We need to:

  1. Construct an optimal binary prefix code for these characters.
  2. Draw the optimal binary prefix tree.

To construct the optimal binary prefix code, we use the Huffman coding algorithm, which involves the following steps:

  1. Sort the characters in increasing order of frequency.
  2. Take the two characters with the lowest frequency and create a new internal node with them as children. The frequency of the new node is the sum of the frequencies of the children.
  3. Remove the two children from consideration and insert the new node into the set of characters.
  4. Repeat steps 2 and 3 until there is only one node left.
  5. Assign binary code to each character using the following convention: assign 0 to the left child of each internal node and 1 to the right child. The code for each character is the concatenation of the digits on the path from the root to the leaf.

Using the above algorithm, we can construct the optimal binary prefix code for the given set of characters:

| Character | Frequency | Code | |-----------|-----------|------| | A | 10 | 110 | | B | 15 | 10 | | C | 12 | 111 | | D | 3 | 0010 | | E | 4 | 0011 | | F | 13 | 01 | | G | 1 | 0000 | | H | 2 | 0001 |

And we can draw the optimal binary prefix tree as follows:

           _______
          |       |
        __|__    _|_
       |     |  |   |
      _|_   _|_ |   |
     |   | |   ||   |
     D   E F   |_   |
                  |_|
                 _|_
                |   |
                G   H
Part 2

For a binary prefix code to be uniquely decodable, it must satisfy the prefix property, which states that no code is the prefix of another code. In other words, there should be no character code that is the beginning of another character code.

The given set of codes:

10
00
110
01
111

Is not a prefix code because the code for B (10) is the prefix of the code for C (110). Therefore, it is not uniquely decodable.

To check if a given prefix code is uniquely decodable, we can use the following algorithm:

  1. Create a set of codes containing the empty string.
  2. For each code in the prefix code: a. For each code in the set, concatenate the code with the current code and add it to the set. b. If any of the newly created codes are already in the set or are a prefix of another code in the set, then the prefix code is not uniquely decodable.
  3. If the algorithm reaches the end of the prefix code without finding any repeated or overlapping codes, then the prefix code is uniquely decodable.

Using the above algorithm, we can check that the given set of codes is not uniquely decodable:

Codes: 10, 00, 110, 01, 111

Iteration 1:
Set: {""}
Code: 10
New set: {"", "10"}

Iteration 2:
Set: {"", "10"}
Code: 00
New set: {"", "10", "00", "100"}

Iteration 3:
Set: {"", "10", "00", "100"}
Code: 110
New set: {"", "10", "00", "100", "110", "1010"}

Iteration 4:
Set: {"", "10", "00", "100", "110", "1010"}
Code: 01
New set: {"", "10", "00", "100", "110", "1010", "010", "1000"}

Iteration 5:
Set: {"", "10", "00", "100", "110", "1010", "010", "1000"}
Code: 111
New set: {"", "10", "00", "100", "110", "1010", "010", "1000", "111", "1011"}

The prefix code is not uniquely decodable because 10 is the prefix of 110.