Data Structure & Algorithms Solutions
Data Structure & Algorithms Solutions
Data Structure & Algorithms Solutions
SOLUTIONS
1. Which of the following is not an application of Greedy approach?
(a) Only (ii), (iv) & (v) (b) Only (ii), (iii) & (v)
(c) Only (i) & (iii) (d) Only (i), (iii) & (iv)
Explanation:
(i) & (iii) uses dynamic programming approach.
2. Suppose the letters a, b, c, d, e has probability 1/2, 1/4, 1/8, 1/16, 1/32 respectively. Which one
of the following is the Huffman code for the letters a, b, c, d, e respectively?
(a) 11, 10, 011, 010, 001 (b) 0, 10, 110, 1110, 1111
(c) 1, 10, 01, 001, 0001 (d) 110, 100, 010, 000, 001
Explanation:
Given Probabilities occuring: a, b, c, d, e as 1/2, 1/4, 1/8, 1/16, 1/32 respectively.
To get the frequencies, we just multiply each probability with 32.
1
∴ a: 1
b: 01
c: 001
d: 0001
e: 0000
3. Suppose the letters a, b, c, d, e have probabilities 1/2, 1/4, 1/8, 1/16, 1/32 respectively. What is
the average number of bits required per message if Huffman code is used for encoding a, b, c, d
& e?
Explanation:
Huffman codes for a, b, c, d, e are 1, 01, 001, 0001, 0000 respectively.
2
Avg. No. of bits required per message = Σ Pi ∙ No. of bits used by letter i,
where Pi is the probability of occurring of each letter
1 1 1 1 1
∴ Average number of bits = (2 ⨯ 1) + (4 ⨯ 2) + (8 ⨯ 3) + (16 ⨯ 4) + (32 ⨯ 4) = 1.75
4. A file contains the following characters and their corresponding frequencies as shown below:
a: 45, b: 13, c: 12, d: 16, e: 9, f: 5
If we use Huffman coding for data comparisons, the average length will be:
Explanation:
a: 45, b: 13, c: 12, d: 16, e: 9, f: 5
Iteration(iv) , a: 45
3
Iteration (vi)
(45⨯1)+(13⨯3)+(12⨯3)+(9⨯4)+(5⨯4) 224
∴ Average length = = 100 = 2.24
100
Explanation:
To analyze the running time of Huffman‘s algorithm,
Huffman (c) Algorithm
2. Q = c // Build a Min-heap
3. For i =1 to n – 1
8. Insert (Q, z)
4
In the above algorithm, line 2 takes O(n) time to build Minheap. The for loop execute exactly n –
1 times during which Extract – Min(Q) takes O(log n) time. Thus the for loop contributes O(n
log n) to the running time.
6. The tree shown below is a Huffman code tree for the letters a, b, c, d, e & f. What would be
the encoding of the string edaa?
Explanation:
a: 00
b: 0100
c: 0101
d: 0110
e: 0111
f: 1
7. Given a Huffman code tree for the letters A, B, C, D & E. What is the last character in the
string encoded by
01110101110110
5
(a) A (b) B
(c) C (d) D
Explanation:
(i) Huffman code is a data compression, encoding technique that follows greedy approach
(ii) An optimal code is always represented by a full binary tree
9. What would be the optimal Huffman code for the string “abbbce” for the given set of
frequencies, based on the first 8 Fibonacci numbers?
(a) 111110000011111011111111110
(b) 100000111100001110000011111
(c) 111111100011111000111111110
(d) 100000111000011110010011111
Explanation:
6
Iteration 1:
Iteration 2:
Iteration 3:
Iteration 4:
7
Iteration 5:
Iteration 6:
Iteration 7:
a: 1111100
b: 1111101
c: 111111
d: 11110
e: 1110
f: 110
g: 10
h: 0
8
ahhhbce
11111000001111011111111110
10. Consider a set of 4 messages (M1 – M4) whose frequency of occurrences in the text is as
given:
(0.37, 0.51, 0.05, 0.07)
Using frequency dependent Huffman Coding the codes of the messages M2 and M3 respectively.
Explanation:
Iteration 1:
Iteration 2:
Iteration 3:
Iteration 4:
M1 – 01
M2 – 1
M3 – 000
M4 – 001