The task is to pick a subset of these items such that their total weight is no more than 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 . 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 .
The value of is ____
View worked solution
Answer: 16
To find the difference , we first compute the value-to-weight ratio for each item and trace both the greedy and optimal strategies for a knapsack capacity of Kgs.
Step 1: Calculate Value-to-Weight Ratios
- Item 1: Ratio =
- Item 2: Ratio =
- Item 3: Ratio =
- Item 4: Ratio =
Sorting the items in descending order of their ratios gives: Item 4, Item 1, Item 3, Item 2.
Step 2: Greedy Algorithm Selection ()
- Pick Item 4 (weight 2, value 24). Remaining capacity = .
- Try Item 1 (weight 10). Since , it cannot be picked.
- Pick Item 3 (weight 4, value 20). Remaining capacity = .
- Try Item 2 (weight 7). Since , it cannot be picked.
Thus, the greedy algorithm picks {Item 4, Item 3} with total value:
Step 3: Optimal Selection ()
We find the subset of items whose total weight is and maximizes the total value:
- Subset {Item 1}: Weight = 10, Value = 60
- Subset {Item 2, Item 3}: Weight = 11, Value = 48
- Subset {Item 2, Item 4}: Weight = 9, Value = 52
The maximum value is obtained by picking only Item 1, giving:
Step 4: Difference
Free practice · Your answers on this page are not saved.
