ADA 6.5
猴子中文翻譯
Input 就是他有 n 個 items(有n個東西你可以選擇要不要裝進去),每個 item 都有它的 value 和他的 weighs (可以想像 value 是價值,weighs 是重量)
我們要讓 value 也就是包包裡面裝了越多價值的東西越好,但是我們包包會有它的限制就是
Input: items where -th item has value and weighs
Output: the max value within capacity, where each item is chosen at most once
不拿物品,或是只拿一個的狀況。
這句我反覆咀嚼,好像懂了又好像沒有懂
Andy: 假設你可以背10kg,拿了一個3kg的水,剩下7kg的預算還可以使用。這裡的3kg就是w
這裡的 W 和上面 w 有什麼不一樣?
Andy:承上題 W = (w == 10kg)
= +
=
Recursively define the value
item List
1 | 1 | 4 |
2 | 2 | 9 |
3 | 4 | 20 |
= 5
table:
i\w | 0 | 1 | 2 | 3 | 4 | 5 |
---|---|---|---|---|---|---|
0 | 0 | 0 | 0 | 0 | 0 | 0 |
1 | 0 | 4 | 4 | 4 | 4 | 4 |
2 | 0 | 4 | 9 | 13 | 13 | 13 |
3 | 0 | 4 | 9 | 13 | 20 | 24 |
Input: items where -th item has value and weighs has unlimted supplies
Output: the max value within capacity
沒有限制可以拿幾個物品的狀況。
Subproblems
Time complexity = , not a good solution.
因為不用再考慮拿過不能再拿的狀況
所以就不用再考慮
Optimal substructure: suppose OPT is an optimal solution to U-KP(), there are n cases:
Recursively define the value
item List
1 | 1 | 4 |
2 | 2 | 9 |
3 | 4 | 17 |
= 5
table:
0 | 1 | 2 | 3 | 4 | 5 | |
---|---|---|---|---|---|---|
0 | 4 | 9 | 13 | 18 | 22 |
Input: items where -th item has value and weighs and size
Output: the max value within capacity and with the size of , where each item is chosen at most once
Subproblems
Optimal substructure: suppose OPT is an optimal solution to M-KP(), there are 2 cases:
Recursively define the value
item List
1 | 1 | 4 | 2 |
2 | 2 | 9 | 4 |
3 | 4 | 20 | 1 |
= 4
= 6
table:
= 0
i\w | 0 | 1 | 2 | 3 | 4 |
---|---|---|---|---|---|
0 | 0 | 0 | 0 | 0 | 0 |
1 | 0 | 0 | 0 | 0 | 0 |
2 | 0 | 0 | 0 | 0 | 0 |
3 | 0 | 0 | 0 | 0 | 0 |
= 1
i\w | 0 | 1 | 2 | 3 | 4 |
---|---|---|---|---|---|
0 | 0 | 0 | 0 | 0 | 0 |
1 | 0 | 0 | 0 | 0 | 0 |
2 | 0 | 0 | 0 | 0 | 0 |
3 | 0 | 0 | 0 | 0 | 20 |
= 2
i\w | 0 | 1 | 2 | 3 | 4 |
---|---|---|---|---|---|
0 | 0 | 0 | 0 | 0 | 0 |
1 | 0 | 4 | 4 | 4 | 4 |
2 | 0 | 4 | 4 | 4 | 4 |
3 | 0 | 4 | 4 | 4 | 20 |
= 3
i\w | 0 | 1 | 2 | 3 | 4 |
---|---|---|---|---|---|
0 | 0 | 0 | 0 | 0 | 0 |
1 | 0 | 4 | 4 | 4 | 4 |
2 | 0 | 4 | 4 | 4 | 4 |
3 | 0 | 4 | 4 | 4 | 20 |
= 4
i\w | 0 | 1 | 2 | 3 | 4 |
---|---|---|---|---|---|
0 | 0 | 0 | 0 | 0 | 0 |
1 | 0 | 4 | 4 | 4 | 4 |
2 | 0 | 4 | 9 | 9 | 9 |
3 | 0 | 4 | 9 | 9 | 20 |
= 5
i\w | 0 | 1 | 2 | 3 | 4 |
---|---|---|---|---|---|
0 | 0 | 0 | 0 | 0 | 0 |
1 | 0 | 4 | 4 | 4 | 4 |
2 | 0 | 4 | 9 | 9 | 9 |
3 | 0 | 4 | 9 | 9 | 20 |
= 6
i\w | 0 | 1 | 2 | 3 | 4 |
---|---|---|---|---|---|
0 | 0 | 0 | 0 | 0 | 0 |
1 | 0 | 4 | 4 | 4 | 4 |
2 | 0 | 4 | 9 | 13 | 13 |
3 | 0 | 4 | 9 | 13 | 20 |
Input: items
* : value of -th item in the group
* : weight of -th item in the group
* : number of items in group
* : total number of items
* : total number of groups
Output: the maximum value for the knapsack with capacity of , where the item from each group can be selected at most once
一個群組只能拿一個的狀況
Subproblems
Optimal substructure: suppose OPT is an optimal solution to MC-KP(), for the group , there are cases:
Recursively define the value
這題老師就沒有舉例了
複雜度計算蠻有趣的,所以就記下來了。
可以隨便分割物品的狀況
簡單到我就不做筆記了
找到CP值最高的那個物品,就拿到滿就好了