Try   HackMD
tags: ADA 6.5

ADA 6.5: Knapsack Problem 背包問題

前言

  • Input:n items where
    i
    -th item has value
    vi
    and weighs
    wi
    (
    vi
    and
    wi
    are positive integers)
  • Output:the maximum value for the knapsack with capacity of
    W

猴子中文翻譯
Input 就是他有 n 個 items(有n個東西你可以選擇要不要裝進去),每個 item 都有它的 value

vi 和他的 weighs
wi
(可以想像 value 是價值,weighs 是重量)
我們要讓 value 也就是包包裡面裝了越多價值的東西越好,但是我們包包會有它的限制就是
W

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →


0-1 Knapsack Problem

Input:

n items where
i
-th item has value
vi
and weighs
wi

Output: the max value within
W
capacity, where each item is chosen at most once

不拿物品,或是只拿一個的狀況。

Step 1: Characterize an OPT Solution

  • Subproblems ZO-KP(
    i,w
    )
    • ZO-KP(
      i,w
      )
      : 0-1 knapsack proble within
      w
      capacity for the first
      i
      items
      考慮
      i
      items 然後 capacity 剩下
      w
      的狀況

      這句我反覆咀嚼,好像懂了又好像沒有懂
      Andy: 假設你可以背10kg,拿了一個3kg的水,剩下7kg的預算還可以使用。這裡的3kg就是w
    • Goal: ZO-KP(
      n,W
      )

      目標就是要找到這個 n 和 W 的值
      這裡的 W 和上面 w 有什麼不一樣?
      Andy:承上題 W = (w == 10kg)
  • Optimal substructure: suppose OPT is an optimal solution to ZO-KP(i, w), there are 2 cases:(拿跟不拿的 case)
    • Case 1: item
      i
      in OPT
      • OPT{
        i
        } is an optimal solution of ZO-KP(i - 1, w - w_i)

    Mi,w =
    vi
    +
    Mi1,wwi

    • Case 2: item
      i
      not in OPT
      • OPT is an optimal solution of ZO-KP(i - 1, w )

    Mi,w =
    Mi1,w

Step 2: Recursively Define the Value of an OPT Solution

  • Recursively define the value

    Mi,w={0,if i=0Mi1,w,if wi > wmax([vi+Mi1,wwi],Mi1,w),otherwise

Step 3: Compute Value of an OPT Solution

item List

i
wi
vi
1 1 4
2 2 9
3 4 20

W = 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

Unbounded Knapsack Problem

Input:

n items where
i
-th item has value
vi
and weighs
wi
has unlimted supplies
Output: the max value within
W
capacity

沒有限制可以拿幾個物品的狀況。

Step 1: Characterize an OPT Solution

  • Subproblems

    • U-KP(
      i
      ,
      w
      )
      : unbounded knapsack proble within
      w
      capacity for the first
      i
      items
    • Goal: U-KP(
      n
      ,
      W
      )

    Time complexity =

    Θ(n2W), not a good solution.
    因為不用再考慮拿過不能再拿的狀況
    所以就不用再考慮
    i

    • U-KP(
      w
      )
      : unbounded knapsack proble within
      w
      capacity for the first
      i
      items
    • Goal:U-KP(
      W
      )
  • Optimal substructure: suppose OPT is an optimal solution to U-KP(

    w), there are n cases:

    • Case 1: item
      1
      in OPT
      • Removing an item 1 from OPT is an optimal solution of U-KP(
        ww1
        )
    • Case 2: item
      2
      in OPT
      • Removing an item 2 fromOPT is an optimal solution of U-KP(
        ww2
        )
    • Case n: item
      n
      in OPT

    Mw=vi+Mwwi

Step 2: Recursively Define the Value of an OPT Solution

  • Recursively define the value

    Mi,w={0,if w=0 or wi>w for all imax(1in,wiw)(vi+Mwwi),otherwise

Step 3: Compute Value of an OPT Solution

item List

i
wi
vi
1 1 4
2 2 9
3 4 17

W = 5

table:

w
0 1 2 3 4 5
M[w]
0 4 9 13 18 22

w1=max(4+0)
w2=max(4+4,9+0)

w3=max(4+9,9+4)

w4=max(4+13,9+9,17+0)

w5=max(4+18,9+13,17+4)

Multidimensional Knapsack Problem

Input:

n items where
i
-th item has value
vi
and weighs
wi
and size
di

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

Step 1: Characterize an OPT Solution

  • Subproblems

    • M-KP(
      i,w,d
      )
      : multidimensional knapsack proble within
      w
      capacity and
      d
      size for the first
      i
      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
      i
      in OPT
      • OPT{
        i
        } is an optimal solution of M-KP(
        i1,wwi,ddi
        )
    • Case 2: item
      i
      not in OPT
      • OPT is an optimal solution of M-KP(
        i1,w,d
        )

Step 2: Recursively Define the Value of an OPT Solution

  • Recursively define the value

    Mi,w={0,if i=0Mi1,w,d,if wi>w or di>dmax([vi+Mi1,wwi,ddi],Mi1,w,d),otherwise

Step 3: Compute Value of an OPT Solution

item List

i
wi
vi
di
1 1 4 2
2 2 9 4
3 4 20 1

W = 4
D
= 6

table:

d = 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

d = 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

d = 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

d = 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

d = 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

d = 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

d = 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

Multiple-Choice Knapsack Problem

Input:

n items
*
vi,j
: value of
j
-th item in the group
i

*
wi,j
: weight of
j
-th item in the group
i

*
ni
: number of items in group
i

*
n
: total number of items
(Σni)

*
G
: total number of groups

Output: the maximum value for the knapsack with capacity of

W, where the item from each group can be selected at most once

一個群組只能拿一個的狀況

Step 1: Characterize an OPT Solution

  • Subproblems

    • MC-KP(
      w
      )
      :
      w
      capacity
    • MC-KP(
      i,w
      )
      :
      w
      capacity for the first
      i
      groups
    • MC-KP(
      i,j,w
      )
      :
      w
      capacity for the first j items from first
      i
      groups
    • Goal: MC-KP(
      G,W
      )
  • Optimal substructure: suppose OPT is an optimal solution to MC-KP(

    i,w), for the group
    i
    , there are
    ni+1
    cases:

    • Case 1: no item from
      i
      -th group in OPT
      • OPT{
        i
        } is an optimal solution of MC-KP(
        i1,w
        )
    • Case j+2:
      j
      -th item from
      i
      -th group (
      itemi,j
      ) in OPT
      • OPT (
        itemi,j
        ) is an optimal solution of MC-KP(
        i1,wwi,j
        )

Step 2: Recursively Define the Value of an OPT Solution

  • Recursively define the value

    Mi,w={0,if i=0Mi1,w,if wi,j>w for all jdmax1jni([vi,j+Mi1,wwi,j],Mi1,w),otherwise

Step 3: Compute Value of an OPT Solution

這題老師就沒有舉例了

複雜度計算蠻有趣的,所以就記下來了。

i=1Gw=0Wj=1nic=cw=0Wi=1Gj=1ni1=cw=0Wn=cnW

T(n)=Θ(nW)

Fractional Knapsack Problem

可以隨便分割物品的狀況

簡單到我就不做筆記了

找到CP值最高的那個物品,就拿到滿就好了