Fundamental Techniques Chapter 5: Techniques 1 Outline and Reading - - PowerPoint PPT Presentation

fundamental techniques
SMART_READER_LITE
LIVE PREVIEW

Fundamental Techniques Chapter 5: Techniques 1 Outline and Reading - - PowerPoint PPT Presentation

Fundamental Techniques Chapter 5: Techniques 1 Outline and Reading The Greedy Method Technique (5.1) Fractional Knapsack Problem (5.1.1) Task Scheduling (5.1.2) Divide-and-conquer paradigm (5.2) Recurrence Equations


slide-1
SLIDE 1

Chapter 5: Techniques 1

Fundamental Techniques

slide-2
SLIDE 2

Chapter 5: Techniques 2

Outline and Reading

The Greedy Method Technique (§5.1)

Fractional Knapsack Problem (§5.1.1)

Task Scheduling (§5.1.2)

Divide-and-conquer paradigm (§5.2)

Recurrence Equations (§5.2.1)

Integer Multiplication (§5.2.2)

Optional: Matrix Multiplication (§5.2.3)

Dynamic Programming (§5.3)

Matrix Chain-Product (§5.3.1)

The General Technique (§5.3.2)

0-1 Knapsack Problem (§5.3.3)

slide-3
SLIDE 3

Chapter 5: Techniques 3

The Greedy Method Technique

The greedy method is a general algorithm

design paradigm, built on the following elements:

 configurations: different choices, collections, or

values to find

 objective function: a score assigned to

configurations, which we want to either maximize or minimize

It works best when applied to problems with the

greedy-choice property:

 a globally-optimal solution can always be found by a

series of local improvements from a starting configuration.

slide-4
SLIDE 4

Chapter 5: Techniques 4

Making Change

Problem: A dollar amount to reach and a collection of coin amounts to use to get there. Configuration: A dollar amount yet to return to a customer plus the coins already returned Objective function: Minimize number of coins returned. Greedy solution: Always return the largest coin you can Example 1: Coins are valued $.32, $.08, $.01

 Has the greedy-choice property, since no amount over $.32 can

be made with a minimum number of coins by omitting a $.32 coin (similarly for amounts over $.08, but under $.32).

Example 2: Coins are valued $.30, $.20, $.05, $.01

 Does not have greedy-choice property, since $.40 is best made

with two $.20’s, but the greedy solution will pick three coins (which ones?)

slide-5
SLIDE 5

Chapter 5: Techniques 5

The Fractional Knapsack Problem

Given: A set S of n items, with each item i having

 bi - a positive benefit  wi - a positive weight

Goal: Choose items with maximum total benefit but with weight at most W. If we are allowed to take fractional amounts, then this is the fractional knapsack problem.

 In this case, we let xi denote the amount we take of item i  Objective: maximize  Constraint:

∈S i i i i

w x b ) / (

S i i

W x

slide-6
SLIDE 6

Chapter 5: Techniques 6

Example

Given: A set S of n items, with each item i having

 bi - a positive benefit  wi - a positive weight

Goal: Choose items with maximum total benefit but with weight at most W. Weight: Benefit:

1 2 3 4 5

4 ml 8 ml 2 ml 6 ml 1 ml $12 $32 $40 $30 $50

Items: Value:

3 ($ per ml) 4 20 5 50 10 ml

Solution:

  • 1 ml of 5
  • 2 ml of 3
  • 6 ml of 4
  • 1 ml of 2

“knapsack”

slide-7
SLIDE 7

Chapter 5: Techniques 7

The Fractional Knapsack Algorithm

Greedy choice: Keep taking item with highest value (benefit to weight ratio)

Since

Run time: O(n log n). See P . 260

Knapsack satisfies Greedy-Choice Property:

there is an item i with higher value than a chosen item j (i.e., vi> vj) but xi< wi and xj> 0 If we substitute some i with j, we get a better solution

How much of i: y= min{ wi-xi, xj} . Thus we can replace y of item j with an equal amount of item I, which is the greedy choice property.

Algorithm fractionalKnapsack(S, W) Input: set S of items w/ benefit bi and weight wi; max. weight W Output: amount xi of each item i to maximize benefit with weight at most W for each item i in S xi ← 0 vi ← bi / wi {value} w ← 0 {total weight} while w < W remove item i with highest vi xi ← min{wi , W − w} w ← w + min{wi , W − w}

∑ ∑

∈ ∈

=

S i i i i S i i i i

x w b w x b ) / ( ) / (

slide-8
SLIDE 8

Chapter 5: Techniques 8

Task Scheduling

Given: a set T of n tasks, each having:

 A start time, si  A finish time, fi (where si < fi)

Goal: Perform all the tasks using a minimum number of “machines.” Note only one task per machine at atime.

1 9 8 7 6 5 4 3 2

Machine 1 Machine 3 Machine 2

slide-9
SLIDE 9

Chapter 5: Techniques 9

Task Scheduling Algorithm

Greedy choice: consider tasks by their start time and use as few machines as possible with this order.

 Run time: O(n log n). Why?

Correctness: Suppose there is a better schedule.

 We can use k-1 machines  The algorithm uses k  Let i be first task scheduled

  • n machine k

 Machine i must conflict with

k-1 other tasks

 But that means there is no

non-conflicting schedule using k-1 machines

Algorithm taskSchedule(T) Input: set T of tasks w/ start time si and finish time fi Output: non-conflicting schedule with minimum number of machines m ← 0

{no. of machines}

while T is not empty remove task i w/ smallest si if there’s a machine j for i then schedule i on machine j else m ← m + 1 schedule i on machine m

slide-10
SLIDE 10

Chapter 5: Techniques 10

Example

Given: a set T of n tasks, each having:

 A start time, si  A finish time, fi (where si < fi)  [1,4], [1,3], [2,5], [3,7], [4,7], [6,9], [7,8] (ordered by start)

Goal: Perform all tasks on min. number of machines

1 9 8 7 6 5 4 3 2 Machine 1 Machine 3 Machine 2

slide-11
SLIDE 11

Chapter 5: Techniques 11

Divide-and-Conquer

7 2  9 4 → 2 4 7 9 7  2 → 2 7 9  4 → 4 9 7 → 7 2 → 2 9 → 9 4 → 4

slide-12
SLIDE 12

Chapter 5: Techniques 12

Divide-and-Conquer

Divide-and conquer is a general algorithm design paradigm:

 Divide: divide the input data S in

two or more disjoint subsets S1,

S2, …

 Recur: solve the subproblems

recursively

 Conquer: combine the solutions

for S1, S2, …, into a solution for S

The base case for the recursion are subproblems of constant size Analysis can be done using

recurrence equations

slide-13
SLIDE 13

Chapter 5: Techniques 13

Merge-Sort Review

Merge-sort on an input sequence S with n elements consists of three steps:

 Divide: partition S into

two sequences S1 and S2

  • f about n/2 elements

each

 Recur: recursively sort S1

and S2

 Conquer: merge S1 and

S2 into a unique sorted

sequence

Algorithm mergeSort(S, C) Input sequence S with n elements, comparator C Output sequence S sorted according to C if S.size() > 1 (S1, S2) ← partition(S, n/2) mergeSort(S1, C) mergeSort(S2, C) S ← merge(S1, S2)

slide-14
SLIDE 14

Chapter 5: Techniques 14

Recurrence Equation Analysis

The conquer step of merge-sort consists of merging two sorted sequences, each with n/2 elements and implemented by means of a doubly linked list, takes at most bn steps, for some constant b. Likewise, the basis case (n < 2) will take at b most steps. Therefore, if we let T(n) denote the running time of merge-sort: We can therefore analyze the running time of merge-sort by finding a closed form solution to the above equation.

That is, a solution that has T(n) only on the left-hand side.

   ≥ + < = 2 if ) 2 / ( 2 2 if ) ( n bn n T n b n T

slide-15
SLIDE 15

Chapter 5: Techniques 15

Iterative Substitution

In the iterative substitution, or “plug-and-chug,” technique, we iteratively apply the recurrence equation to itself and see if we can find a pattern: Note that base, T(n)= b, case occurs when 2i= n. That is, i = log n. So, Thus, T(n) is O(n log n).

ibn n T bn n T bn n T bn n T bn n b n T bn n T n T

i i

+ = = + = + = + = + + = + = ) 2 / ( 2 ... 4 ) 2 / ( 2 3 ) 2 / ( 2 2 ) 2 / ( 2 )) 2 / ( )) 2 / ( 2 ( 2 ) 2 / ( 2 ) (

4 4 3 3 2 2 2

n bn bn n T log ) ( + =

slide-16
SLIDE 16

Chapter 5: Techniques 16

The Recursion Tree

Draw the recursion tree for the recurrence relation and look for a pattern:

depth T’s size

1 n 1 2 n/2 i 2i n/2i … … …

   ≥ + < = 2 if ) 2 / ( 2 2 if ) ( n bn n T n b n T

time

bn bn bn …

Total time = bn + bn log n

(last level plus all previous levels)

slide-17
SLIDE 17

Chapter 5: Techniques 17

Guess-and-Test Method

In the guess-and-test method, we guess a closed form solution and then try to prove it is true by induction: Guess: T(n) < cn log n. Wrong: we cannot make this last line be less than cn log n

n bn cn n cn n bn n cn n bn n n c n bn n T n T log log log ) 2 log (log log )) 2 / log( ) 2 / ( ( 2 log ) 2 / ( 2 ) ( + − = + − = + = + =

   ≥ + < = 2 if log ) 2 / ( 2 2 if ) ( n n bn n T n b n T

slide-18
SLIDE 18

Chapter 5: Techniques 18

Guess-and-Test Method, Part 2

Recall the recurrence equation: Guess # 2: T(n) < cn log2 n.

if c > b.

So, T(n) is O(n log2 n). In general, to use this method, you need to have a good guess and you need to be good at induction proofs.

n cn n bn cn n cn n cn n bn n cn n bn n n c n bn n T n T

2 2 2 2

log log log 2 log log ) 2 log (log log )) 2 / ( log ) 2 / ( ( 2 log ) 2 / ( 2 ) ( ≤ + + − = + − = + = + =

   ≥ + < = 2 if log ) 2 / ( 2 2 if ) ( n n bn n T n b n T

slide-19
SLIDE 19

Chapter 5: Techniques 19

Master Method

Many divide-and-conquer recurrence equations have the form: The Master Theorem:

   ≥ + < = d n n f b n aT d n c n T if ) ( ) / ( if ) (

. 1 some for ) ( ) / ( provided )), ( ( is ) ( then ), ( is ) ( if 3. ) log ( is ) ( then ), log ( is ) ( if 2. ) ( is ) ( then ), ( is ) ( if 1.

log 1 log log log log

< ≤ Θ Ω Θ Θ Θ

+ + −

δ δ

ε ε

n f b n af n f n T n n f n n n T n n n f n n T n O n f

a k a k a a a

b b b b b

slide-20
SLIDE 20

Chapter 5: Techniques 20

Master Method, Example 1

The form: The Master Theorem: Example:

   ≥ + < = d n n f b n aT d n c n T if ) ( ) / ( if ) (

. 1 some for ) ( ) / ( provided )), ( ( is ) ( then ), ( is ) ( if 3. ) log ( is ) ( then ), log ( is ) ( if 2. ) ( is ) ( then ), ( is ) ( if 1.

log 1 log log log log

< ≤ Θ Ω Θ Θ Θ

+ + −

δ δ

ε ε

n f b n af n f n T n n f n n n T n n n f n n T n O n f

a k a k a a a

b b b b b

n n T n T + = ) 2 / ( 4 ) (

Solution: logba= 2, so case 1 says T(n) is Θ(n2).

slide-21
SLIDE 21

Chapter 5: Techniques 21

Master Method, Example 2

The form: The Master Theorem: Example:

   ≥ + < = d n n f b n aT d n c n T if ) ( ) / ( if ) (

. 1 some for ) ( ) / ( provided )), ( ( is ) ( then ), ( is ) ( if 3. ) log ( is ) ( then ), log ( is ) ( if 2. ) ( is ) ( then ), ( is ) ( if 1.

log 1 log log log log

< ≤ Θ Ω Θ Θ Θ

+ + −

δ δ

ε ε

n f b n af n f n T n n f n n n T n n n f n n T n O n f

a k a k a a a

b b b b b

n n n T n T log ) 2 / ( 2 ) ( + =

Solution: logba= 1, so case 2 says T(n) is (n log2 n).

Θ

slide-22
SLIDE 22

Chapter 5: Techniques 22

Master Method, Example 3

The form: The Master Theorem: Example:

   ≥ + < = d n n f b n aT d n c n T if ) ( ) / ( if ) (

. 1 some for ) ( ) / ( provided )), ( ( is ) ( then ), ( is ) ( if 3. ) log ( is ) ( then ), log ( is ) ( if 2. ) ( is ) ( then ), ( is ) ( if 1.

log 1 log log log log

< ≤ Θ Ω Θ Θ Θ

+ + −

δ δ

ε ε

n f b n af n f n T n n f n n n T n n n f n n T n O n f

a k a k a a a

b b b b b

n n n T n T log ) 3 / ( ) ( + =

Solution: logba= 0, so case 3 says T(n) is Θ(n log n).

slide-23
SLIDE 23

Chapter 5: Techniques 23

Master Method, Example 4

The form: The Master Theorem: Example:

   ≥ + < = d n n f b n aT d n c n T if ) ( ) / ( if ) (

. 1 some for ) ( ) / ( provided )), ( ( is ) ( then ), ( is ) ( if 3. ) log ( is ) ( then ), log ( is ) ( if 2. ) ( is ) ( then ), ( is ) ( if 1.

log 1 log log log log

< ≤ Θ Ω Θ Θ Θ

+ + −

δ δ

ε ε

n f b n af n f n T n n f n n n T n n n f n n T n O n f

a k a k a a a

b b b b b

2

) 2 / ( 8 ) ( n n T n T + =

Solution: logba= 3, so case 1 says T(n) is Θ(n3).

slide-24
SLIDE 24

Chapter 5: Techniques 24

Master Method, Example 5

The form: The Master Theorem: Example:

   ≥ + < = d n n f b n aT d n c n T if ) ( ) / ( if ) (

. 1 some for ) ( ) / ( provided )), ( ( is ) ( then ), ( is ) ( if 3. ) log ( is ) ( then ), log ( is ) ( if 2. ) ( is ) ( then ), ( is ) ( if 1.

log 1 log log log log

< ≤ Θ Ω Θ Θ Θ

+ + −

δ δ

ε ε

n f b n af n f n T n n f n n n T n n n f n n T n O n f

a k a k a a a

b b b b b

3

) 3 / ( 9 ) ( n n T n T + =

Solution: logba= 2, so case 3 says T(n) is Θ(n3).

slide-25
SLIDE 25

Chapter 5: Techniques 25

Master Method, Example 6

The form: The Master Theorem: Example:

   ≥ + < = d n n f b n aT d n c n T if ) ( ) / ( if ) (

. 1 some for ) ( ) / ( provided )), ( ( is ) ( then ), ( is ) ( if 3. ) log ( is ) ( then ), log ( is ) ( if 2. ) ( is ) ( then ), ( is ) ( if 1.

log 1 log log log log

< ≤ Θ Ω Θ Θ Θ

+ + −

δ δ

ε ε

n f b n af n f n T n n f n n n T n n n f n n T n O n f

a k a k a a a

b b b b b

1 ) 2 / ( ) ( + = n T n T

Solution: logba= 0, so case 2 says T(n) is Θ(log n). (binary search)

slide-26
SLIDE 26

Chapter 5: Techniques 26

Master Method, Example 7

The form: The Master Theorem: Example:

   ≥ + < = d n n f b n aT d n c n T if ) ( ) / ( if ) (

. 1 some for ) ( ) / ( provided )), ( ( is ) ( then ), ( is ) ( if 3. ) log ( is ) ( then ), log ( is ) ( if 2. ) ( is ) ( then ), ( is ) ( if 1.

log 1 log log log log

< ≤ Θ Ω Θ Θ Θ

+ + −

δ δ

ε ε

n f b n af n f n T n n f n n n T n n n f n n T n O n f

a k a k a a a

b b b b b

n n T n T log ) 2 / ( 2 ) ( + =

Solution: logba= 1, so case 1 says T(n) is Θ(n). (heap construction)

slide-27
SLIDE 27

Chapter 5: Techniques 27

Iterative “Proof” of the Master Theorem

Using iterative substitution, let us see if we can find a pattern: We then distinguish the three cases as

The first term is dominant

Each part of the summation is equally dominant

The summation is a geometric series (See Page 270)

∑ ∑

− = − =

+ = + = = + + + = + + = + + = + =

1 ) (log log 1 ) (log log 2 2 3 3 2 2 2

) / ( ) 1 ( ) / ( ) 1 ( . . . ) ( ) / ( ) / ( ) / ( ) ( ) / ( ) / ( )) / ( )) / ( ( ) ( ) / ( ) (

n i i i a n i i i n

b b b b

b n f a T n b n f a T a n f b n af b n f a b n T a n f b n af b n T a bn b n f b n aT a n f b n aT n T

slide-28
SLIDE 28

Chapter 5: Techniques 28

Integer Multiplication

Algorithm: Multiply two n-bit integers I and J.

 Divide step: Split I and J into high-order and low-order bits  We can then define I* J by multiplying the parts and adding:  So, T(n) = 4T(n/2) + n, which implies T(n) is θ(n2).  But that is no better than the algorithm we learned in grade

school.

l n h l n h

J J J I I I + = + =

2 / 2 /

2 2

l l n h l n l h n h h l n h l n h

J I J I J I J I J J I I J I + + + = + + =

2 / 2 / 2 / 2 /

2 2 2 ) 2 ( * ) 2 ( *

slide-29
SLIDE 29

Chapter 5: Techniques 29

An Improved Integer Multiplication Algorithm

Algorithm: Multiply two n-bit integers I and J.

 Divide step: Split I and J into high-order and low-order bits  Observe that there is a different way to multiply parts:  So, T(n) = 3T(n/2) + n, which implies T(n) is Θ(nlog

2 3), by

the Master Theorem.

 Thus, T(n) is Θ(n1.585).

l n h l n h

J J J I I I + = + =

2 / 2 /

2 2

l l n h l l h n h h l l n l l h h h l h h l l l h n h h l l n l l h h h l l h n h h

J I J I J I J I J I J I J I J I J I J I J I J I J I J I J I J J I I J I J I + + + = + + + + − − + = + + + − − + =

2 / 2 / 2 /

2 ) ( 2 2 ] ) [( 2 2 ] ) )( [( 2 *

slide-30
SLIDE 30

Chapter 5: Techniques 30

Dynamic Programming

slide-31
SLIDE 31

Chapter 5: Techniques 31

Matrix Chain-Products

Dynamic Programming is a general algorithm design paradigm.

 Rather than give the general structure, let

us first give a motivating example:

 Matrix Chain-Products

Review: Matrix Multiplication.

 C = A*B  A is d × e and B is e × f

 O(d⋅e⋅f ) time

A C B d d f e f e i j i,j

− =

=

1

] , [ * ] , [ ] , [

e k

j k B k i A j i C

slide-32
SLIDE 32

Chapter 5: Techniques 32

Matrix Chain-Products

Matrix Chain-Product:

 Compute A= A0* A1* …* An-1  Ai is di × di+ 1  Problem: How to parenthesize?

Example

 B is 3 × 100  C is 100 × 5  D is 5 × 5  (B* C)* D takes 1500 + 75 = 1575 ops  B* (C* D) takes 1500 + 2500 = 4000 ops

slide-33
SLIDE 33

Chapter 5: Techniques 33

Enumeration Approach

Matrix Chain-Product Alg.:

 Try all possible ways to parenthesize

A= A0* A1* …* An-1

 Calculate number of ops for each one  Pick the one that is best

Running time:

 The number of parenthesizations is equal

to the number of binary trees with n nodes

 This is exponential!  It is called the Catalan number, and it is

almost 4n.

 This is a terrible algorithm!

slide-34
SLIDE 34

Chapter 5: Techniques 34

Greedy Approach

Idea # 1: repeatedly select the product that uses (up) the most operations. Counter-example:

 A is 10 × 5  B is 5 × 10  C is 10 × 5  D is 5 × 10  Greedy idea # 1 gives (A* B)* (C* D), which takes

500+ 1000+ 500 = 2000 ops

 A* ((B* C)* D) takes 500+ 250+ 250 = 1000 ops

slide-35
SLIDE 35

Chapter 5: Techniques 35

Another Greedy Approach

Idea # 2: repeatedly select the product that uses the fewest operations. Counter-example:

 A is 101 × 11  B is 11 × 9  C is 9 × 100  D is 100 × 99  Greedy idea # 2 gives A* ((B* C)* D)), which takes

109989+ 9900+ 108900= 228789 ops

 (A* B)* (C* D) takes 9999+ 89991+ 89100= 189090 ops

The greedy approach is not giving us the

  • ptimal value.
slide-36
SLIDE 36

Chapter 5: Techniques 36

“Recursive” Approach

Define subproblems:

 Find the best parenthesization of Ai* Ai+ 1* …* Aj.  Let Ni,j denote the minimum number of operations done by this

subproblem.

 The optimal solution for the whole problem is N0,n-1.

Subproblem optimality: The optimal solution can be

defined in terms of optimal subproblems

 There has to be a final multiplication (root of the expression

tree) for the optimal solution.

 Say, the final multiply is at index i: (A0* …* Ai)* (Ai+ 1* …* An-1).  Then the optimal solution N0,n-1 is the sum of two optimal

subproblems, N0,i and Ni+ 1,n-1 plus the time for the last multiply.

If the global optimum did not have these optimal subproblems, we could define an even better “optimal” solution.

slide-37
SLIDE 37

Chapter 5: Techniques 37

Characterizing Equation

The global optimal has to be defined in terms of

  • ptimal subproblems, depending on where the final

multiply is at. Let us consider all possible places for that final multiply:

 Recall that Ai is a di × di+ 1 dimensional matrix.  So, a characterizing equation for Ni,j is the following:

Note that Ni,i= 0. Note that subproblems are not independent–the

subproblems overlap.

This image cannot currently be displayed. This image cannot currently be displayed.

} { min

1 1 , 1 , , + + + < ≤

+ + =

j k i j k k i j k i j i

d d d N N N

slide-38
SLIDE 38

Chapter 5: Techniques 38

answer

N

1 1 2 … n-1 … n-1 j i

Dynamic Programming Algorithm Visualization

The bottom-up construction fills in the N array by diagonals Ni,j gets values from previous entries in i-th row and j-th column Filling in each entry in the N table takes O(n) time. Total run time: O(n3) Getting actual parenthesization can be done by remembering “k” for each N entry

} { min

1 1 , 1 , , + + + < ≤

+ + =

j k i j k k i j k i j i

d d d N N N

i j

slide-39
SLIDE 39

Chapter 5: Techniques 39

Dynamic Programming Algorithm

Since subproblems

  • verlap, we don’t

use recursion. Instead, we construct optimal subproblems “bottom-up.” Ni,i’s are easy, so start with them Then do problems of “length” 2,3,… subproblems, and so on. Running time: O(n3)

Algorithm matrixChain(S): Input: sequence S of n matrices to be multiplied Output: number of operations in an optimal parenthesization of S for i ← 1 to n − 1 do Ni,i ← 0 for b ← 1 to n − 1 do { b = j − i is the length of the problem } for i ← 0 to n − b − 1 do j ← i + b Ni,j ← +∞ for k ← i to j − 1 do Ni,j ← min{Ni,j, Ni,k + Nk+1,j + di dk+1 dj+1} return N0,n−1

slide-40
SLIDE 40

Chapter 5: Techniques 40

The General Dynamic Programming Technique

Applies to a problem that at first seems to require a lot of time (possibly exponential), provided we have:

 Simple subproblems: the subproblems can be

defined in terms of a few variables, such as j, k, l, m, and so on.

 Subproblem optimality: the global optimum value

can be defined in terms of optimal subproblems

 Subproblem overlap: the subproblems are not

independent, but instead they overlap (hence, should be constructed bottom-up).

slide-41
SLIDE 41

Chapter 5: Techniques 41

The 0/1 Knapsack Problem

Given: A set S of n items, with each item i having

 wi - a positive weight  bi - a positive benefit

Goal: Choose items with maximum total benefit but with weight at most W. If we are not allowed to take fractional amounts, then this is the 0/ 1 knapsack problem.

 In this case, we let T denote the set of items we take  Objective: maximize  Constraint:

∈T i i

b

T i i

W w

slide-42
SLIDE 42

Chapter 5: Techniques 42

Given: A set S of n items, with each item i having

 bi - a positive “benefit”  wi - a positive “weight”

Goal: Choose items with maximum total benefit but with weight at most W.

Example

Weight: Benefit:

1 2 3 4 5

4 in 2 in 2 in 6 in 2 in $20 $3 $6 $25 $80

Items:

box of width 9 in

Solution:

  • item 5 ($80, 2 in)
  • item 3 ($6, 2in)
  • item 1 ($20, 4in)

“knapsack”

slide-43
SLIDE 43

Chapter 5: Techniques 43

A 0/1 Knapsack Algorithm, First Attempt

Sk: Set of items numbered 1 to k. Define B[k] = best selection from Sk. Problem: does not have subproblem optimality:

 Consider set S= { (3,2),(5,4),(8,5),(4,3),(10,9)} of

(benefit, weight) pairs and total weight W = 20

Best for S4: Best for S5:

slide-44
SLIDE 44

Chapter 5: Techniques 44

A 0/1 Knapsack Algorithm, Second Attempt

Sk: Set of items numbered 1 to k. Define B[k,w] to be the best selection from Sk with weight at most w Good news: this does have subproblem optimality. I.e., the best subset of Sk with weight at most w is either

 the best subset of Sk-1 with weight at most w or  the best subset of Sk-1 with weight at most w−wk plus item k

   + − − − > − = else } ] , 1 [ ], , 1 [ max{ if ] , 1 [ ] , [

k k k

b w w k B w k B w w w k B w k B

slide-45
SLIDE 45

Chapter 5: Techniques 45

0/1 Knapsack Algorithm

Recall the definition of B[k,w] Since B[k,w] is defined in terms of B[k−1,* ], we can use two arrays of instead of a matrix Running time: O(nW). Not a polynomial-time algorithm since W may be large This is a pseudo-polynomial time algorithm

Algorithm 01Knapsack(S, W): Input: set S of n items with benefit bi and weight wi; maximum weight W Output: benefit of best subset of S with weight at most W let A and B be arrays of length W + 1 for w ← 0 to W do B[w] ← 0 for k ← 1 to n do copy array B into array A for w ← wk to W do if A[w−wk] + bk > A[w] then B[w] ← A[w−wk] + bk return B[W]    + − − − > − = else } ] , 1 [ ], , 1 [ max{ if ] , 1 [ ] , [

k k k

b w w k B w k B w w w k B w k B