22
CMSC 451: Subset Sum & Knapsack Slides By: Carl Kingsford Department of Computer Science University of Maryland, College Park Based on Section 6.4 of Algorithm Design by Kleinberg & Tardos.

CMSC 451: Subset Sum & Knapsack - Carnegie Mellon …ckingsf/bioinfo-lectures/subsetsum.pdf · Subset Sum Subset Sum Given: an integer bound W, and a collection of n items, each with

  • Upload
    lamlien

  • View
    219

  • Download
    0

Embed Size (px)

Citation preview

Page 1: CMSC 451: Subset Sum & Knapsack - Carnegie Mellon …ckingsf/bioinfo-lectures/subsetsum.pdf · Subset Sum Subset Sum Given: an integer bound W, and a collection of n items, each with

CMSC 451: Subset Sum & Knapsack

Slides By: Carl Kingsford

Department of Computer Science

University of Maryland, College Park

Based on Section 6.4 of Algorithm Design by Kleinberg & Tardos.

Page 2: CMSC 451: Subset Sum & Knapsack - Carnegie Mellon …ckingsf/bioinfo-lectures/subsetsum.pdf · Subset Sum Subset Sum Given: an integer bound W, and a collection of n items, each with

Subset Sum

Subset Sum

Given:

• an integer bound W , and

• a collection of n items, each with a positive, integer weight wi ,

find a subset S of items that:

maximizes∑

i∈S wi while keeping∑

i∈S wi ≤W .

Motivation: you have a CPU with W free cycles, and want tochoose the set of jobs (each taking wi time) that minimizes thenumber of idle cycles.

Page 3: CMSC 451: Subset Sum & Knapsack - Carnegie Mellon …ckingsf/bioinfo-lectures/subsetsum.pdf · Subset Sum Subset Sum Given: an integer bound W, and a collection of n items, each with

Assumption

We assume W and each wi is an integer.

Page 4: CMSC 451: Subset Sum & Knapsack - Carnegie Mellon …ckingsf/bioinfo-lectures/subsetsum.pdf · Subset Sum Subset Sum Given: an integer bound W, and a collection of n items, each with

Optimal Notation

Notation:

• Let S∗ be an optimal choice of items (e.g. a set {1,4,8}).

• Let OPT (n, W ) be the value of the optimal solution.

• We design an dynamic programming algorithm to computeOPT (n, W ).

Subproblems:

• To compute OPT (n, W ): We need the optimal value forsubproblems consisting of the first j items for every knapsacksize 0 ≤ w ≤W .

• Denote the optimal value of these subproblems by OPT (j , w).

Page 5: CMSC 451: Subset Sum & Knapsack - Carnegie Mellon …ckingsf/bioinfo-lectures/subsetsum.pdf · Subset Sum Subset Sum Given: an integer bound W, and a collection of n items, each with

Recurrence

Recurrence: How do we compute OPT (j , w) given solutions tosmaller subproblems?

OPT (j , W ) = max

{OPT (j − 1, W ) if j 6∈ S∗

wj + OPT (j − 1, W − wj) if j ∈ S∗

Special case: if wj > W then OPT (j , W ) = OPT (j − 1, W ).

Page 6: CMSC 451: Subset Sum & Knapsack - Carnegie Mellon …ckingsf/bioinfo-lectures/subsetsum.pdf · Subset Sum Subset Sum Given: an integer bound W, and a collection of n items, each with

Another way to write it. . .

OPT (j , W ) =

OPT (j − 1, W ) if wj > W

max

{OPT (j − 1, W ) if j 6∈ S∗

wj + OPT (j − 1, W − wj) if j ∈ S∗

Note: Because we don’t know the answer to the blue questions, wehave to try both.

Page 7: CMSC 451: Subset Sum & Knapsack - Carnegie Mellon …ckingsf/bioinfo-lectures/subsetsum.pdf · Subset Sum Subset Sum Given: an integer bound W, and a collection of n items, each with

The table of solutions

0 1 2 3 4 5 6 7 8 9 10 11 12

9

8

7

6

5

4

3

2

1

0

0000000000 0 0 0 0 0 0 0 0 0 0 0 0

OPT(4, 11)

OPT(n, W)

Page 8: CMSC 451: Subset Sum & Knapsack - Carnegie Mellon …ckingsf/bioinfo-lectures/subsetsum.pdf · Subset Sum Subset Sum Given: an integer bound W, and a collection of n items, each with

Filling in a box using smaller problems

0 1 2 3 4 5 6 7 8 9 10 11 12

9

8

7

6

5

4

3

2

1

0

0000000000 0 0 0 0 0 0 0 0 0 0 0 0

OPT(j, w)

OPT(j-1, W)

OPT(j-1, W-wj)

Page 9: CMSC 451: Subset Sum & Knapsack - Carnegie Mellon …ckingsf/bioinfo-lectures/subsetsum.pdf · Subset Sum Subset Sum Given: an integer bound W, and a collection of n items, each with

Filling in a box using smaller problems

0 1 2 3 4 5 6 7 8 9 10 11 12

9

8

7

6

5

4

3

2

1

0

0000000000 0 0 0 0 0 0 0 0 0 0 0 0

OPT(j, w)

OPT(j-1, W)

OPT(j-1, W-wj)

Page 10: CMSC 451: Subset Sum & Knapsack - Carnegie Mellon …ckingsf/bioinfo-lectures/subsetsum.pdf · Subset Sum Subset Sum Given: an integer bound W, and a collection of n items, each with

Remembering Which Subproblem Was Used

When we fill in the gray box, we also record which subproblem waschosen in the maximum:

0 1 2 3 4 5 6 7 8 9 10 11 12

9

8

7

6

5

4

3

2

1

0

0000000000 0 0 0 0 0 0 0 0 0 0 0 0

OPT(j, w)

OPT(j-1, W)

OPT(j-1, W-wj)

Page 11: CMSC 451: Subset Sum & Knapsack - Carnegie Mellon …ckingsf/bioinfo-lectures/subsetsum.pdf · Subset Sum Subset Sum Given: an integer bound W, and a collection of n items, each with

Filling in the Matrix

Fill matrix from bottom to top, left to right.

0 1 2 3 4 5 6 7 8 9 10 11 12

9

8

7

6

5

4

3

2

1

0

0000000000 0 0 0 0 0 0 0 0 0 0 0 0

When you are filling in box, you only need to look at boxes you’vealready filled in.

Page 12: CMSC 451: Subset Sum & Knapsack - Carnegie Mellon …ckingsf/bioinfo-lectures/subsetsum.pdf · Subset Sum Subset Sum Given: an integer bound W, and a collection of n items, each with

Pseudocode

SubsetSum(n, W):Initialize M[0,w] = 0 for each w = 0,...,WInitialize M[i,0] = 0 for each i = 1,...,n

For i = 1,...,n: for every rowFor w = 0,...,W: for every column

If w[i] > w: case where item can’t fitM[i,w] = M[i-1,w]

M[i,w] = max( which is best?M[i-1,w],w[j] + M[i-1, W-w[j]]

)Return M[n,W]

Page 13: CMSC 451: Subset Sum & Knapsack - Carnegie Mellon …ckingsf/bioinfo-lectures/subsetsum.pdf · Subset Sum Subset Sum Given: an integer bound W, and a collection of n items, each with

Finding The Choice of Items

Follow the arrows backward starting at the top right:

0 1 2 3 4 5 6 7 8 9 10 11 12

9

8

7

6

5

4

3

2

1

0

0000000000 0 0 0 0 0 0 0 0 0 0 0 0

Which items does this path imply?

8, 5, 4, 2

Page 14: CMSC 451: Subset Sum & Knapsack - Carnegie Mellon …ckingsf/bioinfo-lectures/subsetsum.pdf · Subset Sum Subset Sum Given: an integer bound W, and a collection of n items, each with

Finding The Choice of Items

Follow the arrows backward starting at the top right:

0 1 2 3 4 5 6 7 8 9 10 11 12

9

8

7

6

5

4

3

2

1

0

0000000000 0 0 0 0 0 0 0 0 0 0 0 0

Which items does this path imply? 8, 5, 4, 2

Page 15: CMSC 451: Subset Sum & Knapsack - Carnegie Mellon …ckingsf/bioinfo-lectures/subsetsum.pdf · Subset Sum Subset Sum Given: an integer bound W, and a collection of n items, each with

Runtime

Runtime:

• O(nW ) to fill in the matrix.

• O(n) time to follow the path backwards.

• Total running time is O(nW ).

This is pseudo-polynomial because it depends on the size of theinput numbers.

Page 16: CMSC 451: Subset Sum & Knapsack - Carnegie Mellon …ckingsf/bioinfo-lectures/subsetsum.pdf · Subset Sum Subset Sum Given: an integer bound W, and a collection of n items, each with

Knapsack

Knapsack

Given:

• a bound W , and

• a collection of n items, each with a weight wi ,

• a value vi for each weight

Find a subset S of items that:

maximizes∑

i∈S vi while keeping∑

i∈S wi ≤W .

Difference from Subset Sum: want to maximize value instead ofweight.

Page 17: CMSC 451: Subset Sum & Knapsack - Carnegie Mellon …ckingsf/bioinfo-lectures/subsetsum.pdf · Subset Sum Subset Sum Given: an integer bound W, and a collection of n items, each with

How can we solve Knapsack?

How can we solve Knapsack?

Page 18: CMSC 451: Subset Sum & Knapsack - Carnegie Mellon …ckingsf/bioinfo-lectures/subsetsum.pdf · Subset Sum Subset Sum Given: an integer bound W, and a collection of n items, each with

Knapsack

Subset Sum:

OPT (j , W ) = max

{OPT (j − 1, W ) if j 6∈ S∗

wj + OPT (j − 1, W − wj) if j ∈ S∗

Knapsack:

OPT (j , W ) = max

{OPT (j − 1, W ) if j 6∈ S∗

vj + OPT (j − 1, W − wj) if j ∈ S∗

Page 19: CMSC 451: Subset Sum & Knapsack - Carnegie Mellon …ckingsf/bioinfo-lectures/subsetsum.pdf · Subset Sum Subset Sum Given: an integer bound W, and a collection of n items, each with

Knapsack

Subset Sum:

OPT (j , W ) = max

{OPT (j − 1, W ) if j 6∈ S∗

wj + OPT (j − 1, W − wj) if j ∈ S∗

Knapsack:

OPT (j , W ) = max

{OPT (j − 1, W ) if j 6∈ S∗

vj + OPT (j − 1, W − wj) if j ∈ S∗

Page 20: CMSC 451: Subset Sum & Knapsack - Carnegie Mellon …ckingsf/bioinfo-lectures/subsetsum.pdf · Subset Sum Subset Sum Given: an integer bound W, and a collection of n items, each with

Fractional Knapsack

0-1 Knapsack

You’re presented with n, where item i has value vi and size wi .You have a knapsack of size W , and you want to take the items Sso that

•∑

i∈S vi is maximized, and

•∑

i∈S wi ≤W .

This is a hard problem. However, if we are allowed to takefractions of items we can do it with a simple greedy algorithm:

• Value of a fraction f of item i is f · vi

• Weight of a fraction f is f · wi .

Page 21: CMSC 451: Subset Sum & Knapsack - Carnegie Mellon …ckingsf/bioinfo-lectures/subsetsum.pdf · Subset Sum Subset Sum Given: an integer bound W, and a collection of n items, each with

Knapsack Example

Idea: Sort the items by pi = vi/wi

Larger vi is better, smaller wi is better.

1

2

3

4

knapsack size = 6

$30

$40

$45

$100

p1 = 30

p2 = 20

p3 = 15

p4 = 25

1 4 1/2

$30 + $100 + (1/2)*$40 = $150

Page 22: CMSC 451: Subset Sum & Knapsack - Carnegie Mellon …ckingsf/bioinfo-lectures/subsetsum.pdf · Subset Sum Subset Sum Given: an integer bound W, and a collection of n items, each with

0-1 Knapsack

This greedy algorithm doesn’t work for 0-1 knapsack, where wecan’t take part of an item:

1

2

3

4

knapsack size = 6Greedy Choice:

$30

$40

$45

$100

p1 = 30

p2 = 20

p3 = 15

p4 = 25

1 4

$30 + $100 = $130

4 2A better choice: