演算法›Ch3 動態規劃第 19 題/共 40 題
19. Knapsack、Dynamic Programming、Greedy Algorithm
#AL-03-019中KnapsackDynamic ProgrammingGreedy Algorithm
- Consider the Knapsack problem with n items and a knapsack size W. Let be the size and profit of the items in order, i.e., is the size of the item and is its profit.
(a) (2%). Consider the following simple greedy algorithm.
- , .
- Repeat until , do
- Find with the maximum profit-cost ratio. That is, .
- Add i to C if .
- Remove i from S.
Does this algorithm correctly compute an optimal solution for the Knapsack problem? If so, briefly justify its correctness. If not, provide a counter-example with the smallest number of items.
(b) (2%). For any i and w with and , define to be the maximum profit we can obtain, if we are using only the first i items and the knapsack size is w. Consider the following recurrence formula for .
Suppose that W = 5 and we have 3 items with size and profit (4,20), (2,8), (3,14). Use the recurrence to calculate the value for all and in a table, as shown below. What is the optimal profit?
| i\w | 0 | 1 | 2 | 3 | 4 | 5 |
|---|---|---|---|---|---|---|
| i=0 | ||||||
| i=1 | ||||||
| i=2 | ||||||
| i=3 |
📄 交大112
▤完整推導請見《WH 資工筆記 · 演算法》Ch3 動態規劃