Knapsack Problem

Post on 25-May-2015

23.799 views 2 download

Tags:

Transcript of Knapsack Problem

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

Knapsack ProblemThere are two versions of the problem:

1. 0-1 Knapsack Problem2. Fractional Knapsack Problem

i. Bounded Knapsack Problemii. Unbounded 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

Solutions to Knapsack Problems

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

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}

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

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

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

Greedy AlgorithmSample Problem

A B C D

cost200

240

140

150

weight 1 3 2 5

value200

80 70 30

Greedy Algorithm The optimal solution to the

fractional knapsack Not an optimal solution to the 0-1

knapsack

Dynamic ProgrammingRecursive formula for sub problems:

Dynamic Programmingfor = 0 to

V[ 0, ] = 0for = 1 to

V[, 0 ] = 0

Dynamic Programmingfor = 1 to if

if

else

else

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

Examplei\s 0 1 2 3 4 5

0

1

2

3

4

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

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

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

Dynamic ProgrammingLet and

if

else

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)

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)

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)

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)

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)

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)

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)

Knapsack ProblemCommon Applications• Resource allocation with financial

constraints• Construction and scoring of

heterogeneous test• Selection of capital investments