You are on page 1of 5

CS702 – Advanced Algorithms Analysis and Design

Assignment 3

Question: 1 (30 Marks)


Determine the cost and structure of an optimal binary search tree (OBST) for a set of n = 5 keys
with the probabilities given below. You need to calculate the tables w[i, j], e[i, j] and root[i, j].

I 0 1 2 3 4 5
pi Null 0.12 0.06 0.10 0.12 0.16
qi 0.06 0.08 0.06 0.12 0.04 0.08

Solution:

Question: 2 (20 Marks)

What is an optimal Huffman Code for the following set of frequencies?


a:14 b:41 c:17 d:33 e:8 f:7 g:22 h:11

Solution:
First we put the values of all elements in the table
f
a 14
b 41
c 17
d 33
e 8
f 7
g 22
h 11

Now we shall construct Huffman tree.


Step 1: We take minimum values which are 7 and 8. We put these values in the tree nodes as:

15

7 8
Now remove these values from the table and add 15.
Step 2: take the next smallest values and add them in the tree nodes and remove from table:
25

11 14

Step 3: Take the next two smallest values:

47

22 25

11 14

32
Step 4:

15 17

7 8
Step 5:
65

32 33

Step 6: Step 7:
88 153 153

0 1

65
41 65 88
47 88

0 1 0 1

32 d:33 b:41 47

0 1 1
0 we shall make complete
As we have inserted all the elements of the table in the tree nodes, now
Huffman tree.
15 c:17 g:22 25

0
1 0 1

f:7 e:8 h:11 a:14

32
The Huffman codes for the frequencies are:

f Optimal
Huffman Codes
a 14 1111
b 41 10
c 17 001
d 33 01
e 8 0001
f 7 0000
g 22 110
h 11 1110

The computational cost is:


14*4+41*2+17*3+33*2+8*4+7*4+22*3+11*4 = 425

You might also like