• 沒有找到結果。

Knapsack Problem

• Input: 𝑛 items where 𝑖-th item has value 𝑣

𝑖

and weighs 𝑤

𝑖

(𝑣

𝑖

and 𝑤

𝑖

are positive integers)

• Output: the maximum value for the knapsack with capacity of 𝑊

• Variants of knapsack problem

• 0-1 Knapsack Problem: 每項物品只能拿一個

• Unbounded Knapsack Problem: 每項物品可以拿多個

• Multidimensional Knapsack Problem: 背包空間有限

• Multiple-Choice Knapsack Problem: 每一類物品最多拿一個

• Fractional Knapsack Problem: 物品可以只拿部分

Step 1: Characterize an OPT Solution

• Subproblems

• U-KP(i, w): unbounded knapsack problem with 𝑤 capacity for the first 𝑖 items

• Goal: U-KP(n, W)

Unbounded Knapsack Problem

Input: 𝑛 items where 𝑖-th item has value 𝑣𝑖 and weighs 𝑤𝑖 has unlimited supplies Output: the max value within 𝑊 capacity

0-1 Knapsack Problem Unbounded Knapsack Problem

each item can be chosen at most once each item can be chosen multiple times a sequence of binary choices: whether to

choose item 𝑖

a sequence of 𝑖 choices: which one (from 1 to 𝑖) to choose

Time complexity = Θ 𝑛𝑊 Time complexity = Θ 𝑛2𝑊

Can we do better?

Step 1: Characterize an OPT Solution

• Subproblems

• U-KP(w): unbounded knapsack problem with 𝑤 capacity

• Goal: U-KP(W)

• Optimal substructure: suppose OPT is an optimal solution to

U-KP(w)

, there are 𝑛 cases:

• Case 1: item 1 in OPT

• Removing an item 1 from OPT is an optimal solution of U-KP(w – w1)

• Case 2: item 2 in OPT

• Removing an item 2 from OPT is an optimal solution of U-KP(w – w2) :

• Case 𝑛: item 𝑛 in OPT

Unbounded Knapsack Problem

Input: 𝑛 items where 𝑖-th item has value 𝑣𝑖 and weighs 𝑤𝑖 has unlimited supplies Output: the max value within 𝑊 capacity

Step 2: Recursively Define the Value of an OPT Solution

• Optimal substructure: suppose OPT is an optimal solution to U-KP(w) , there are 𝑛 cases:

• Case 𝑖: item 𝑖 in OPT

• Removing an item i from OPT is an optimal solution of U-KP(w – w1)

• Recursively define the value

只考慮背包還裝的下的情形

Unbounded Knapsack Problem

Input: 𝑛 items where 𝑖-th item has value 𝑣𝑖 and weighs 𝑤𝑖 has unlimited supplies Output: the max value within 𝑊 capacity

Step 3: Compute Value of an OPT Solution

• Bottom-up method: solve smaller subproblems first

w 0 1 2 3 4 5 W

M[w]

i wi vi

1 1 4

2 2 9

3 4 20

𝑊 = 5

Unbounded Knapsack Problem

Input: 𝑛 items where 𝑖-th item has value 𝑣𝑖 and weighs 𝑤𝑖 has unlimited supplies Output: the max value within 𝑊 capacity

Step 3: Compute Value of an OPT Solution

• Bottom-up method: solve smaller subproblems first

w 0 1 2 3 4 5

M[w] 0 4 9 13 18 22

Unbounded Knapsack Problem

Input: 𝑛 items where 𝑖-th item has value 𝑣𝑖 and weighs 𝑤𝑖 has unlimited supplies Output: the max value within 𝑊 capacity

i wi vi

1 1 4

2 2 9

3 4 20

𝑊 = 5

Step 3: Compute Value of an OPT Solution

• Bottom-up method: solve smaller subproblems first

U-KP(v, W)

for w = 0 to W M[w] = 0

for w = 0 to W for i = 1 to n

if(wi <= w)

tmp = vi + M[w - wi] M[w] = max(M[w], tmp)

Unbounded Knapsack Problem

Input: 𝑛 items where 𝑖-th item has value 𝑣𝑖 and weighs 𝑤𝑖 has unlimited supplies Output: the max value within 𝑊 capacity

Step 4: Construct an OPT Solution by Backtracking

U-KP(v, W)

for w = 0 to W M[w] = 0

for w = 0 to W for i = 1 to n

if(wi <= w)

tmp = vi + M[w - wi] M[w] = max(M[w], tmp) return M[W]

Find-Solution(M, n, W) for i = 1 to n

C[i] = 0 // C[i] = # of item i in solution w = W

for i = i to n while w > 0

if(wi <= w && M[w] == (vi + M[w - wi])) w = w - wi

C[i] += 1 return C

Knapsack Problem

• Input: 𝑛 items where 𝑖-th item has value 𝑣

𝑖

and weighs 𝑤

𝑖

(𝑣

𝑖

and 𝑤

𝑖

are positive integers)

• Output: the maximum value for the knapsack with capacity of 𝑊

• Variants of knapsack problem

• 0-1 Knapsack Problem: 每項物品只能拿一個

• Unbounded Knapsack Problem: 每項物品可以拿多個

• Multidimensional Knapsack Problem: 背包空間有限

• Multiple-Choice Knapsack Problem: 每一類物品最多拿一個

• Fractional Knapsack Problem: 物品可以只拿部分

Step 1: Characterize an OPT Solution

• Subproblems

• M-KP(i, w, d): multidimensional knapsack problem with 𝑤 capacity and 𝑑 size for the first 𝑖 items

• Goal: M-KP(n, W, D)

• Optimal substructure: suppose OPT is an optimal solution to

M-KP(i, w, d)

, there are 2 cases:

• Case 1: item 𝑖 in OPT

• OPT\{𝑖} is an optimal solution of M-KP(i - 1, w - wi, d – di)

• Case 2: item 𝑖 not in OPT

• OPT is an optimal solution of M-KP(i - 1, w, d)

Multidimensional Knapsack Problem

Input: 𝑛 items where 𝑖-th item has value 𝑣𝑖, weighs 𝑤𝑖, and size 𝑑𝑖

Output: the max value within 𝑊 capacity and with the size of 𝑫, where each item is chosen at most once

Step 2: Recursively Define the Value of an OPT Solution

• Optimal substructure: suppose OPT is an optimal solution to M-KP(i, w, d) , there are 2 cases:

• Case 1: item 𝑖 in OPT

• OPT\{𝑖} is an optimal solution of M-KP(i - 1, w - wi, d – di)

• Case 2: item 𝑖 not in OPT

• OPT is an optimal solution of M-KP(i - 1, w, d)

• Recursively define the value

Multidimensional Knapsack Problem

Input: 𝑛 items where 𝑖-th item has value 𝑣𝑖, weighs 𝑤𝑖, and size 𝑑𝑖

Output: the max value within 𝑊 capacity and with the size of 𝑫, where each item is chosen at most once

Exercise

• Step 3: Compute Value of an OPT Solution

• Step 4: Construct an OPT Solution by Backtracking

• What is the time complexity?

Multidimensional Knapsack Problem

Input: 𝑛 items where 𝑖-th item has value 𝑣𝑖, weighs 𝑤𝑖, and size 𝑑𝑖

Output: the max value within 𝑊 capacity and with the size of 𝑫, where each item is chosen at most once

Knapsack Problem

• Input: 𝑛 items where 𝑖-th item has value 𝑣

𝑖

and weighs 𝑤

𝑖

(𝑣

𝑖

and 𝑤

𝑖

are positive integers)

• Output: the maximum value for the knapsack with capacity of 𝑊

• Variants of knapsack problem

• 0-1 Knapsack Problem: 每項物品只能拿一個

• Unbounded Knapsack Problem: 每項物品可以拿多個

• Multidimensional Knapsack Problem: 背包空間有限

• Multiple-Choice Knapsack Problem: 每一類物品最多拿一個

• Fractional Knapsack Problem: 物品可以只拿部分

相關文件