The GATE Grind

GATE 2018 CS – Question 58

Algorithms · Greedy Techniques · 2 marks · Numerical answer

Consider the weights and values of items listed below. Note that there is only one unit of each item.

Item numberWeight (in Kgs)Value (in Rupees)
11060
2728
3420
4224

The task is to pick a subset of these items such that their total weight is no more than 11 Kgs and their total value is maximized. Moreover, no item may be split. The total value of items picked by an optimal algorithm is denoted by $V_{opt}$. A greedy algorithm sorts the items by their value-to-weight ratios in descending order and packs them greedily, starting from the first item in the ordered list. The total value of items picked by the greedy algorithm is denoted by $V_{greedy}$.

The value of $V_{opt}-V_{greedy}$ is ________.

Practise this question in The GATE Grind →

Show answer and explanation

Correct answer: 16

Explanation

The ratios are $6,4,5,12$, so greedy takes item 4 (weight 2, value 24) and item 3 (weight 4, value 20) and then cannot fit item 1 or 2: $V_{greedy}=44$. The best choice is item 1 alone (value 60), so $V_{opt}=60$ and the difference is 16.