knapsack problem

42
Knapsack Problem

Upload: jenny-galino

Post on 25-May-2015

23.824 views

Category:

Education


2 download

TRANSCRIPT

Page 1: Knapsack Problem

Knapsack Problem

Page 2: Knapsack Problem

Knapsack Problem• In a knapsack problem or rucksack

problem, we are given a set of items, where each item is specified by a size and a value . We are also given a size bound , the size of our knapsack.

Item # Size Value1 1 82 3 63 5 5

Page 3: Knapsack Problem

Knapsack ProblemThere are two versions of the problem:

1. 0-1 Knapsack Problem2. Fractional Knapsack Problem

i. Bounded Knapsack Problemii. Unbounded Knapsack Problem

Page 4: Knapsack Problem

Knapsack ProblemSample Problem

A B C D E F Gvalue 7 9 5 12 14 6 12time 3 4 2 6 7 3 5

Page 5: Knapsack Problem

Solutions to Knapsack Problems

Brute-Force Approach – solve the problem with a straightforward algorithm

Page 6: Knapsack Problem

Solutions to Knapsack Problems

{}{1, 2, 3, 4,

5}

{}{2, 3, 4, 5}

{}{3, 4, 5}

{2}{3, 4, 5}

{1}{2, 3, 4, 5}

{1}{3, 4, 5}

{1, 2}{3, 4, 5}

Page 7: Knapsack Problem

Solutions to Knapsack Problems

Greedy Algorithm – keep taking most valuable items until maximum weight is reached or taking the largest value of each item by calculating

Dynamic Programming – solve each sub problem once and store their solutions in an array

Page 8: Knapsack Problem

ExampleGiven: = 4 (# of elements) = 5 pounds (maximum size)Elements (size, value) = { (1, 200), (3, 240), (2, 140), (5, 150) }

Page 9: Knapsack Problem

Greedy Algorithm1. Calculate for 2. Sort the items by decreasing 3. Find j, such that

Page 10: Knapsack Problem

Greedy AlgorithmSample Problem

A B C D

cost200

240

140

150

weight 1 3 2 5

value200

80 70 30

Page 11: Knapsack Problem

Greedy Algorithm The optimal solution to the

fractional knapsack Not an optimal solution to the 0-1

knapsack

Page 12: Knapsack Problem

Dynamic ProgrammingRecursive formula for sub problems:

Page 13: Knapsack Problem

Dynamic Programmingfor = 0 to

V[ 0, ] = 0for = 1 to

V[, 0 ] = 0

Page 14: Knapsack Problem

Dynamic Programmingfor = 1 to if

if

else

else

Page 15: Knapsack Problem

ExampleGiven: = 4 (# of elements) = 5 (maximum size)Elements (size, value) = { (2, 3), (3, 4), (4, 5), (5, 6) }

Page 16: Knapsack Problem

Examplei\s 0 1 2 3 4 5

0

1

2

3

4

Page 17: Knapsack Problem

Examplefor = 0 to

V[ 0, ] = 0

i\s 0 1 2 3 4 5

0 0 0 0 0 0 0

1

2

3

4

Page 18: Knapsack Problem

Examplefor = 0 to

V[, 0 ] = 0

i\s 0 1 2 3 4 5

0 0 0 0 0 0 0

1 0

2 0

3 0

4 0

Page 19: Knapsack Problem

Exampleif if

else

else i\s 0 1 2 3 4 5

0 0 0 0 0 0 0

1 0 0

2 0

3 0

4 0

Items (, )1: (2, 3)2: (3, 4)3: (4, 5)4: (5, 6)

Page 20: Knapsack Problem

Exampleif

if

else

else

i\s 0 1 2 3 4 5

0 0 0 0 0 0 0

1 0 0 3

2 0

3 0

4 0

Items (, )1: (2, 3)2: (3, 4)3: (4, 5)4: (5, 6)

Page 21: Knapsack Problem

Exampleif

if

else

else

i\s 0 1 2 3 4 5

0 0 0 0 0 0 0

1 0 0 3 3

2 0

3 0

4 0

Items (, )1: (2, 3)2: (3, 4)3: (4, 5)4: (5, 6)

Page 22: Knapsack Problem

Exampleif

if

else

else

i\s 0 1 2 3 4 5

0 0 0 0 0 0 0

1 0 0 3 3 3

2 0

3 0

4 0

Items (, )1: (2, 3)2: (3, 4)3: (4, 5)4: (5, 6)

Page 23: Knapsack Problem

Exampleif if

else

else

i\s 0 1 2 3 4 5

0 0 0 0 0 0 0

1 0 0 3 3 3 3

2 0

3 0

4 0

Items (, )1: (2, 3)2: (3, 4)3: (4, 5)4: (5, 6)

Page 24: Knapsack Problem

Exampleif if

else

else i\s 0 1 2 3 4 5

0 0 0 0 0 0 0

1 0 0 3 3 3 3

2 0 0

3 0

4 0

Items (, )1: (2, 3)2: (3, 4)3: (4, 5)4: (5, 6)

Page 25: Knapsack Problem

Exampleif if

else

else i\s 0 1 2 3 4 5

0 0 0 0 0 0 0

1 0 0 3 3 3 3

2 0 0 3

3 0

4 0

Items (, )1: (2, 3)2: (3, 4)3: (4, 5)4: (5, 6)

Page 26: Knapsack Problem

Exampleif if

else

else

i\s 0 1 2 3 4 5

0 0 0 0 0 0 0

1 0 0 3 3 3 3

2 0 0 3 4

3 0

4 0

Items (, )1: (2, 3)2: (3, 4)3: (4, 5)4: (5, 6)

Page 27: Knapsack Problem

Example

i\s 0 1 2 3 4 5

0 0 0 0 0 0 0

1 0 0 3 3 3 3

2 0 0 3 4 4

3 0

4 0

if if

else

else

Items (, )1: (2, 3)2: (3, 4)3: (4, 5)4: (5, 6)

Page 28: Knapsack Problem

Exampleif if

else

else

i\s 0 1 2 3 4 5

0 0 0 0 0 0 0

1 0 0 3 3 3 3

2 0 0 3 4 4 7

3 0

4 0

Items (, )1: (2, 3)2: (3, 4)3: (4, 5)4: (5, 6)

Page 29: Knapsack Problem

Exampleif if

else

else i\s 0 1 2 3 4 5

0 0 0 0 0 0 0

1 0 0 3 3 3 3

2 0 0 3 4 4 7

3 0 0 3 4

4 0

Items (, )1: (2, 3)2: (3, 4)3: (4, 5)4: (5, 6)

Page 30: Knapsack Problem

Exampleif if

else

else

i\s 0 1 2 3 4 5

0 0 0 0 0 0 0

1 0 0 3 3 3 3

2 0 0 3 4 4 7

3 0 0 3 4 5

4 0

Items (, )1: (2, 3)2: (3, 4)3: (4, 5)4: (5, 6)

Page 31: Knapsack Problem

Exampleif if

else

else

i\s 0 1 2 3 4 5

0 0 0 0 0 0 0

1 0 0 3 3 3 3

2 0 0 3 4 4 7

3 0 0 3 4 5 7

4 0

Items (, )1: (2, 3)2: (3, 4)3: (4, 5)4: (5, 6)

Page 32: Knapsack Problem

Exampleif if

else

else i\s 0 1 2 3 4 5

0 0 0 0 0 0 0

1 0 0 3 3 3 3

2 0 0 3 4 4 7

3 0 0 3 4 5 7

4 0 0 3 4 5

Items (, )1: (2, 3)2: (3, 4)3: (4, 5)4: (5, 6)

Page 33: Knapsack Problem

Example

i\s 0 1 2 3 4 5

0 0 0 0 0 0 0

1 0 0 3 3 3 3

2 0 0 3 4 4 7

3 0 0 3 4 5 7

4 0 0 3 4 5 7

if if

else

else

Items (, )1: (2, 3)2: (3, 4)3: (4, 5)4: (5, 6)

Page 34: Knapsack Problem

Dynamic ProgrammingLet and

if

else

Page 35: Knapsack Problem

Example

i\s 0 1 2 3 4 5

0 0 0 0 0 0 0

1 0 0 3 3 3 3

2 0 0 3 4 4 7

3 0 0 3 4 5 7

4 0 0 3 4 5 7

while if

else

Items (, )1: (2, 3)2: (3, 4)3: (4, 5)4: (5, 6)

Page 36: Knapsack Problem

Example

i\s 0 1 2 3 4 5

0 0 0 0 0 0 0

1 0 0 3 3 3 3

2 0 0 3 4 4 7

3 0 0 3 4 5 7

4 0 0 3 4 5 7

while if

else

Items (, )1: (2, 3)2: (3, 4)3: (4, 5)4: (5, 6)

Page 37: Knapsack Problem

Example

i\s 0 1 2 3 4 5

0 0 0 0 0 0 0

1 0 0 3 3 3 3

2 0 0 3 4 4 7

3 0 0 3 4 5 7

4 0 0 3 4 5 7

while if

else

Items (, )1: (2, 3)2: (3, 4)3: (4, 5)4: (5, 6)

Page 38: Knapsack Problem

Example

i\s 0 1 2 3 4 5

0 0 0 0 0 0 0

1 0 0 3 3 3 3

2 0 0 3 4 4 7

3 0 0 3 4 5 7

4 0 0 3 4 5 7

while if

else

Items (, )1: (2, 3)2: (3, 4)3: (4, 5)4: (5, 6)

Page 39: Knapsack Problem

Example

i\s 0 1 2 3 4 5

0 0 0 0 0 0 0

1 0 0 3 3 3 3

2 0 0 3 4 4 7

3 0 0 3 4 5 7

4 0 0 3 4 5 7

while if

else

Items (, )1: (2, 3)2: (3, 4)3: (4, 5)4: (5, 6)

Page 40: Knapsack Problem

Example

i\s 0 1 2 3 4 5

0 0 0 0 0 0 0

1 0 0 3 3 3 3

2 0 0 3 4 4 7

3 0 0 3 4 5 7

4 0 0 3 4 5 7

while if

else

Items (, )1: (2, 3)2: (3, 4)3: (4, 5)4: (5, 6)

Page 41: Knapsack Problem

Example

i\s 0 1 2 3 4 5

0 0 0 0 0 0 0

1 0 0 3 3 3 3

2 0 0 3 4 4 7

3 0 0 3 4 5 7

4 0 0 3 4 5 7

The optimal knapsack should contain {1,2} = 7

Items (, )1: (2, 3)2: (3, 4)3: (4, 5)4: (5, 6)

Page 42: Knapsack Problem

Knapsack ProblemCommon Applications• Resource allocation with financial

constraints• Construction and scoring of

heterogeneous test• Selection of capital investments