You are on page 1of 6

Sheet 3

Q1) Consider the following instance of knapsack problem:

The maximum weight of 12 is allowed in the knapsack.

Find the value of maximum profit with the optimal solution of the fractional
knapsack problem.
(A) 31
(B) 40.2
(C) 48.5
(D) None of these

Answer: (C)

Explanation:

Decreasing order of P i /W i is
X1, X4, X3, X5, X2
X1 –> profit = 15 and weight = 2
Including X4 –> profit = 15 + 16 and weight = 2 + 4 = 6
Including X3 –> profit = 40 and weight = 9
Now weight left = 3
Weight of X5 = 6 –> half of X5 can be included.
Profit = 40 + 17/2 = 48.5.

Q2)
Let us consider that the capacity of the knapsack W = 60 and the list of provided
items are shown in the following table −

use greedy algorithm to find the optimal solution.

Answer:

After sorting all the items according to $\frac{p_{i}}{w_{i}}$. First all of B is


chosen as weight of B is less than the capacity of the knapsack. Next, item A is
chosen, as the available capacity of the knapsack is greater than the weight of A.
Now, C is chosen as the next item. However, the whole item cannot be chosen as
the remaining capacity of the knapsack is less than the weight of C.
Hence, fraction of C (i.e. (60 − 50)/20) is chosen.
Now, the capacity of the Knapsack is equal to the selected items. Hence, no
more item can be selected.
The total weight of the selected items is 10 + 40 + 20 * (10/20) = 60
And the total profit is 100 + 280 + 120 * (10/20) = 380 + 60 = 440
This is the optimal solution. We cannot gain more profit selecting any different
combination of items.

Q3)use Huffman code to find how many bits may be required for encoding
the message ‘mississippi’?

Answer :

alphabet i s p m
frequency 4 4 2 1
Huffman code 1 00 010 011

Smpi (11)

0 1

Spm (7)
I (4)

0 1

S (4) pm(3)

0 1

p (2) m(1)

total number of bits needed = 4*1 + 4*2 + 2*3 +1*3 = 21 bits


Q 4)A networking company uses a compression technique to encode the
message before transmitting over the network. Suppose the message contains the
following characters with their frequency:

Note that each character in input message takes 1 byte.


If the compression technique used is Huffman Coding, how many bits will be
saved in the message?
(A) 224
(B) 800
(C) 576
(D) 324
Solutions:

100

0 1

55 F (45)

0 1

30 25

0 1 0 1

E (16) 14 D (13) c(12)

0 1

B (9) A (5)
alphabet f e d c b a
frequency 45 16 13 12 9 5
Huffman 1 000 010 011 0010 0011
code

Finding number of bits without using Huffman,


Total number of characters = sum of frequencies = 100
size of 1 character = 1byte = 8 bits
Total number of bits = 8*100 = 800
Using Huffman Encoding, Total number of bits needed can be calculated as:
5*4 + 9*4 + 12*3 + 13*3 + 16*3 + 45* 1 = 224
Bits saved = 800-224 = 576.

Q 5) Choose the correct answer

1. Fractional knapsack problem is solved most efficiently by which of the


following algorithm?
a) Divide and conquer
b) Dynamic programming
c) Greedy algorithm
d) Backtracking

Answer: C

2. What is the objective of the knapsack problem?


a) To get maximum total value in the knapsack
b) To get minimum total value in the knapsack
c) To get maximum weight in the knapsack
d) To get minimum weight in the knapsack

Answer: a

3. Given items as {value,weight} pairs {{40,20},{30,10},{20,5}}. The capacity


of knapsack=20. Find the maximum value output assuming items to be divisible.
a) 60
b) 80
c) 100
d) 40

Answer: a

4.Given items as {value,weight} pairs {{60,20},{50,25},{20,5}}. The capacity


of knapsack=40. Find the maximum value output assuming items to be divisible
and nondivisible respectively.
a) 100, 80
b) 110, 70
c) 130, 110
d) 110, 80

Answer: d

You might also like