View
218
Download
0
Category
Tags:
Preview:
Citation preview
Fundamental TechniquesCS 5050 Chapter 5
Goal: review complexity analysisTalk of categories used to describe
algorithms.
1
Algorithmic Frameworks
• Greedy• Divide-and-conquer• Discard-and-conquer• Top-down Refinement• Dynamic Programming• Backtracking/Brute Force• Branch-and-bound• Local Search (Heuristic)
2
Greedy Algorithms• Short-view, tactical approach to minimize the value of an
objective function.• Eating whatever you want – no regard for health
consequences or leaving some for others.• Example: Cheapest meal – beverage, main course, dessert,
vegetable. Can we minimize cost by satisfying each category separately?
3
Greedy Algorithms• Solutions are often not global, but can be when problems
satisfy the “greedy-choice” property• Example – Making change with US money to minimize
number of coins; not true with other money systems. Example – Knapsack problem. Given items weight and
value. Want best value in knapsack within total weight limit. Cannot be done using greedy methods. Doesn’t have greedy choice property
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.
4
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
• Example: best path to take given I can only use {x1, … xn} locations? Does this have the greedy choice property?
• Example: BST best binary search tree (best expected cost to find each entry). Does this have the greedy choice property? What if I always selected the root with the highest weight?
(a:6, b:5, c:5 – should a be root?)5
Making Change
• Problem: A dollar amount to reach and a collection of coin amounts to use to get there.
• Objective function: Minimize number of coins returned.• Greedy solution: Always return the largest value coin you can• Example 0: Coins are valued $1.00, $.50, $.25, $.10, $.05, $.01• 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).
– There would never be a reason NOT to use the highest value coin.• 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?)
6
The 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.
• Items are indivisible.– In this case, we let xi denote {1 if we take item, 0 otherwise}
– Objective: maximize
– Constraint:
7
Si
ii xb )(
Si
i Ww
The Knapsack Problem
• Greedy will not work• Example: Knapsack of weight 10Tent: weight 6, benefit 7Food: weight 5, benefit 5Clothing: weight 5, benefit 5
Tent has the (1) best total benefit and the (2) best benefit per pound but is not best to include.
8
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:9
Si
iii wxb )/(
Si
i Wx
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.
10
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 5010 ml
Solution:• 1 ml of 5• 2 ml of 3• 6 ml of 4• 1 ml of 2
“knapsack”
The Fractional Knapsack Algorithm• Greedy choice: Keep taking
item with highest value (benefit to weight ratio)– Since – Run time: O(n log n). Why?
Correctness: Suppose some choice k was wrong. Then we should have selected some amount of resource t. Let a be the smaller of the possible amounts of k and t. Since k has the higher value, a units of k must be worth more than a units of t. Hence, there was no mistake.
11
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 {current weight}while w < W
remove item i with highest vi
xi min{wi , W w}
w w + min{wi , W w}
Si
iiiSi
iii xwbwxb )/()/(
Task Scheduling• Given: a set T of n tasks, each having:
– A start time, si
– A finish time, fi (where si < fi)• Notice – the start and finish times are fixed!!• Goal: Perform all the tasks using a minimum number of
“machines.”• How would you place task s so you didn’t have to rethink?
12
1 98765432
Machine 1
Machine 3Machine 2
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?– Must Sort
• Correctness: Suppose there is a better schedule.– We can use k-1 machines– The algorithm uses k– Let i be first task scheduled on
machine k– Machine i must conflict with k-1
other tasks– But that means there is no non-
conflicting schedule using k-1 machines
13
Algorithm taskSchedule(T)
Input: set T of tasks w/ start time si and finish time fi
Output: non-conflicting schedule with minimum number of machinesm 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 1schedule i on machine m
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
14
1 98765432
Machine 1
Machine 3
Machine 2
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
15
Divide and Conquer Algorithms
• Divide the problem into smaller subproblems - these are often equal sized
• Eventually get to a base case• Examples – mergesort or quicksort• Generally recursive• Usually efficient, but sometimes not• For example, Fibonacci numbers – much duplicate
work• Desirability depends on work in splitting/combining
16
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 of about n2 elements each
– Recur: recursively sort S1 and S2
– Conquer: merge S1 and S2
into a unique sorted sequence
17
Algorithm mergeSort(S, C)Input sequence S with n
elements, comparator C Output sequence S sorted
according to Cif S.size() > 1
(S1, S2) partition(S, n/2)
mergeSort(S1, C)
mergeSort(S2, C)
S merge(S1, S2)
Recurrence Equation Analysis
• The conquer step of merge-sort consists of merging two sorted sequences, each with n2 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.
18
2if)2/(2
2if )(
nbnnT
nbnT
Iterative Substitution(telescoping)
• 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).19
ibnnT
bnnT
bnnT
bnnT
bnnbnT
bnnTnT
ii
)2/(2
...
4)2/(2
3)2/(2
2)2/(2
))2/())2/(2(2
)2/(2)(
44
33
22
2
nbnbnnT log)(
• Recursion tree – bn effort at each of log n levels. Show pictures of work (like we did in class)
• Guess and test – eyeball, then attempt an inductive proof. If can’t prove, try something bigger/smaller. This is not a good way for beginning students.
20
The Recursion Tree• Draw the recursion tree for the recurrence relation and look for a
pattern:
21
depth T’s size
0 1 n
1 2 n2
i 2i n2i
… … …
2if)2/(2
2if )(
nbnnT
nbnT
time
bn
bn
bn
…
Total time = bn + bn log n(last level plus all previous levels)
Master method (different from text)method of text can give tighter bounds
• Of the form T(n) = c if n < d
aT(n/b) +O(nk) if n >= d– a > bk then T(n) is O(n log
b a )
– a = bk then T(n) is O(nk log n)– a < bk then T(n) is O(nk)
• Can work this out on your own using telescoping and math skills.
22
Master Method, Example 1
• Example:
• a=4• b=2• k=1Case 1 says
23
Solution: logba=2, so case 1 says T(n) is O(n2).
nnTnT )2/(4)(
Master Method Example 2
• T(n) = 8T(n/2) +n2
• A=8• B=2• K = 2• Case 3 8<4 so O(n2)
24
Master Method Example 3
• T(n) = T(n/2) +1
• A=1• B=2• K = 0• Case 2 1=1 so O(log n)
25
Divide and ConquerWhen does it help?
• Dividing the problem in half doesn’t always help. Example – canning peaches. There is no improvement in dividing the problem in half (and may actually be more work because of the overhead of setup and cleanup).
• There are two ways dividing in half may help– As Jennie said – you may be able to throw part away. Like in a binary
search, when the bad half is just discarded.– If the original complexity is greater than n and the “dividing the
problem” or “putting back together” phase is linear, you can win. This is what happens in Quick sort. The division is linear, yet the work in a small subproblem is greater than linear. Say it is n2. Then the work becomes: (n/2)2 + (n/2)2 + O(n) = 2(n2/4) +n = n2/2 + n
Thus in a single time of dividing in half, the coefficient on n2 is reduced If you do this recursively, the savings will be even greater.
26
Divide and ConquerInteger 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 O(n2).– But that is no better than the algorithm we learned in grade
school. We have four multiplications each ¼ as big.
27
ln
h
ln
h
JJJ
III
2/
2/
2
2
lln
hln
lhn
hh
ln
hln
h
JIJIJIJI
JJIIJI
2/2/
2/2/
222
)2(*)2(*
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.– Suppose we played around with other choices and just happened to
get the same answer but could reuse the same multiplications?– Suppose we tried to just subtract Ih and Il and subtract J the OPPOSITE
way
28
ln
h
ln
h
JJJ
III
2/
2/
2
2
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 O(nlog2
3), by the Master Theorem.
– Thus, T(n) is O(n1.585).
29
ln
h
ln
h
JJJ
III
2/
2/
2
2
lln
hllhn
hh
lln
llhhhlhhlllhn
hh
lln
llhhhllhn
hh
JIJIJIJI
JIJIJIJIJIJIJIJI
JIJIJIJJIIJIJI
2/
2/
2/
2)(2
2])[(2
2]))([(2*
Large Integer multiplication• Idea – How think of? You want a way of
reducing the total number of pieces you need to compute.
• Observe (Ih-Il)(Jl-Jh) = IhJl-IlJl – IhJh +IlJh
• Key is for one multiply, get two terms we need if add in two terms we already have
• So, instead of computing the four pieces shown earlier, we do this one multiplication to get two of the pieces we need!
30
Large Integer multiplication• IJ = IhJh2n +[(Ih-Il)(Jl-Jh)+IhJh+IlJl] 2n/2 + IlJl
• Tada – three multiplications instead of four• by master formula O(n1.585)– a = 3– b = 2– k = 1
31
Try at seats!For Example Mult 75*53
I: 7 5 2 J: 5 3 -235 15 -4 Product35*100 + 10(-4 + 35 + 15) + 1575*53=3975
32
Matrix multiplication
• Same idea Breaking up into four parts doesn’t help• Strassen’s algorithm: complicated patterns of
sum/difference multiply reduce the number of subproblems to 7 (from 8)
• Won’t go through details, as not much is learned from the struggle.
• T(n) = 7T(n/2) + bn2
– Matrix multiplication is O(n2.808)
33
Homework• In using recursion to find best expected cost:Let expectedCost(a,b) return the integer representing the bestExpected cost
in placing nodes a through b (where a,b are subscripts into order array of values to be placed)
We need an auxiliary array to store the weights between subscriptsInt expectedCost(a,b){ if (a >b) return 0; if (a==b) return weight[a]; r = subscript of best root (trial and error to determine) cost1 = expectedCost(a,r-1) cost2 = expectedCost(r+1,b) theCost = weight[r] + cost1 + weight[a,r-1] + cost2 + weight[r+1,b] weight[a,b] = weight[r]+weight[a,r-1] + weight[r+1,b] return theCost;}
34
Homework
• Notice how you repeatedly compute the same problems over and over again.
• In memoizing, you record your results so you don’t have to repeat the problems. Store answers in bestCost[][]. Count the number of times you can just look up the bestCost rather than having to compute it.
35
Discard and Conquer
• similar to divide and conquer• Discard and conquer requires only that we solve one
of several subproblems– Corresponds to proof by cases– Binary search is an example– Finding kth smallest is an example (Quickselect)
36
Dynamic Programming
37
Outline and Reading
• Matrix Chain-Product (§5.3.1)• The General Technique (§5.3.2)• 0-1 Knapsack Problem (§5.3.3)
38
Dynamic Programming Algorithms
• Reverse of divide and conquer, we build solutions from the base cases up
• Avoids possible duplicate calls in the recursive solution
• Implementation is usually iterative• Examples – Fibonacci series, Pascal triangle,
making change with coins of different relatively prime denominations
39
Homeworkx x
x x
x x
x x
x
40
bestCost weight
Notice we fill in main diagonal first.To compute bestCost[1,3] we look at [2,3] [1,1][3,3] [1,2]
Good Dynamic Programming Algorithm Attributes
• Simple Subproblems• Subproblem Optimality: optimal solution
consists of optimal subproblems.• Can you think of a real world example without
subproblem optimality? Round trip discounts.• Subproblem Overlap (sharing)
41
The 0-1 Knapsack Problem• 0-1, means take item or leave it (no fractions)• Now given units which we can take or leave• Obvious solution of enumerating all subsets is Θ(2n)• Difficulty is in characterizing subproblems– Find best solution for first k units – no good, as optimal
solution doesn’t build on earlier solutions– Find best solution, first k units within quantity limit – Either use previous best at this limit, or new item plus
previous best at reduced limit – O(nW)• Pseudo-polynomial – as it depends on a parameter
W, which is not part of other solutions.
42
Solve using recursion:
int value[MAX]; // value of each item int weight[MAX]: // weight of each item
//You can use item "item" or items with lower number //The maximum weight you can have is maxWeight // return the best value possible in the knapsackint bestValue(int item, int maxWeight) { if (item < 0) return 0; if (maxWeight < weight[item]) // current item can't be used, skip it return bestValue(item-1, maxWeight); useIt = bestValue(item-1, maxWeight - weight[item]) + value[item] dontUseIt = bestValue(item-1, maxWeight); return max (useIt, dontUseIt); }
43
Price per Pound• The constant `price-per-pound' knapsack problem is
often called the subset sum problem, because given a set of numbers, we seek a subset that adds up to a specific target number, i.e. the capacity of our knapsack.
• If we have a capacity of 10, consider a tree in which each level corresponds to considering each item (in order). Notice, in this simple example, about a third of the calls are duplicates.
44
45
• We would need to store whether a specific weight could be achieved using only items 1-k.
• possible[item][max] = given the current item (or earlier in the list) and max value, can you achieve max?
46
• Consider the weights: 2, 2, 6,5,4 with limit of 10• We could compute such a table in an iterative
fashion:
1 2 3 4 5 6 7 8 9 10
2 no yes no no no no no no no no
2
6
5
4
47
• Consider the weights: 2, 2, 6,5,4 with limit of 10• We could compute such a table in an iterative
fashion:
1 2 3 4 5 6 7 8 9 10
2 no yes no no no no no no no no
2 no yes no yes no no no no no no
6 no yes no yes no yes no yes no yes
5 no yes no yes yes yes yes yes yes yes
4 no yes no yes yes yes yes yes yes yes
48
From the table
• Can you tell HOW to fill the knapsack?
49
• Let's compare the two strategies: • Normal/forgetful: wait until you are asked for a
value before computing it. You may have to do some things twice, but you will never do anything you don't need.
• Compulsive/elephant: you do everything before you are asked to do it. You may compute things you never need, but you will never compute anything twice (as you never forget).
• Which is better? At first it seems better to wait until you need something, but in large recursions, almost everything is needed somewhere and many things are computed LOTS of times.
50
Consider the complexity for a max capacity of M and N different items.
• Normal: For each item, try it two ways (useIt or dontUseIt). O(N2)
• Compulsive: Fill in array O(M*N) • Which is better depends on values of M and
N. Notice, the two complexities depend on different variables.
51
Clever Observation
• Since only the previous row is ever accessed, don’t really need to store all rows.
• However, couldn’t easily read back optimal choices
52
Matrix Multiplication
53
1 2
3
4 5
6
10 1220 2230 32
1*10+2*20+3*30 1*12+2*22+3*32
4*10+5*20+6*30 4*12+4*22+6*32
# of Columns of A must = # of Rows of B
Matrix Multiplication
54
Matrices A and B can be multiplied if:
[r x c] and [s x d]
c = s
The work required is O(rcd)
Adjacency matrix of a directed graph
55
1
32
4
6
5
At seats, try computing A*A. What do you have?How many ways you can get from one point to another in
exactly two steps.
Exercise 0: If A is the adjacency matrix of a graph, then (Ak)ij=>1 iff there is a
path of length k from i to j.
56
57
• Given a sequence < A1, A2, ..., An> of n matrices, we wish to compute the
product A1 A2 ... An.
• Matrix multiplication is associative, so the product does not depend on how we
parenthesize the matrices
58
At Seats
59
A1*(A2*A3)
(A1*A2) *A3
Which is better?
A1 10 100
A2 100 5
A3 5 50
60
We compute all possibilities and remember the best.
n=5, compute in terms of smaller problems: P1*P4+P2*P3 + P3*P2 + P4*P1 = 14 Looks bad, doubles when problem size increases by 1!!! 61
• Running time:– The number of parenthesizations is equal to
the number of binary search trees with n external nodes (n-1 internal nodes). (The matrices are the external nodes. The internal nodes represent matrix multiplication of the children.)
– This is exponential!– It is called the Catalan number, and it is almost
4n.– This is a terrible algorithm!
62
63
Matrix MultiplicationToo Many Overlapping Subproblems
At the top level decide which two pieces to multiply together
64
At seats -What is Algorithm?
• For each cell N(i,j) represents the best cost of computing the multiplication of matrices i thru j.– Look at each possible division– Pick the best of the possibilities– k is division point • N(i,k) + N(k+1,j) gives each piece• multiply two pieces is di xdk+1 and dk+1 x dj+1 • subscripting – remember di is rowsize of ith matrix
65
Solution?
• How would you fix the problem of re-computing the same problems over and over?
66
Why
• Can you explain• (1) What goes in each cell?• (2) Why we are most interested in the
expected cost of subtrees independent of where they are in the final tree?
Matrix Multiplication Parenthesis
Matrix Rows Cols
1 5 10
2 10 100
3 100 2
4 2 15
5 15 3
6 3 40
Tell me (1) the meaning of the value in each cell (2) how it is computed (3) where the answer is
Operation Count AND division point
1
2
3
4
5
6
“Recursive” Approach• Define subproblems:
– Find the best parenthesization of Ai*Ai+1*…*Aj.– Let Ni,j denote the 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.
69
Characterizing Equation• The global optimal has to be defined in terms of optimal
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 subproblems are not independent–the subproblems overlap.
70
}{min 11,1,, jkijkki
jkiji dddNNN
• The bottom-up construction fills in the N array by diagonals
• Cells in lower diagonal are not used.
• 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
71
answer
N 0 1
0
1
2 …
n-1
…
n-1j
i
Dynamic Programming Algorithm Visualization
}{min 11,1,, jkijkki
jkiji dddNNN
i
j
Dynamic Programming Algorithm
• Since subproblems overlap, 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)
72
Algorithm matrixChain(S):Input: sequence S of n matrices to be multipliedOutput: number of operations in an optimal
parenthesization of Sfor i 1 to n 1 do
Ni,i 0 for subLen 1 to n 1 do
{subLen j i is the length of the problem }
for i 0 to n subLen 1 doj i subLen
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,n1
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 repetition: the subproblems are not
independent, but instead they repeat(hence, should be constructed bottom-up).
73
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:
74
Ti
ib
Ti
i Ww
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.
75
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”
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
76
Best for S4:
Best for S5:
Best from 4 is of
no help in selecting
Best from 5
A 0/1 Knapsack Algorithm, Second Attempt
• Sk: Set of items numbered 1 to k (first k items from set)• Define B[k,w] to be the best selection from Sk (first k items
from set) 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 wwk plus item k
77
else}],1[],,1[max{
if],1[],[
kk
k
bwwkBwkB
wwwkBwkB
0/1 Knapsack Algorithm
• Recall the definition of B[k,w]• Since B[k,w] is defined in
terms of B[k1,*], 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
78
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 + 1for w 0 to W do
B[w] 0for k 1 to n do
copy array B into array A
for w wk to W do
if A[wwk] bk > A[w] then
B[w] A[wwk] bk return B[W]
else}],1[],,1[max{
if],1[],[
kk
k
bwwkBwkB
wwwkBwkB
Example
79
Weight:Benefit:
1 2 3 4 5
1 in 3 in 2 in 6 in 2 in
$10 $30 $6 $75 $60
Items:
box of width 9 in
Solution:• item 5 ($60, 2 in)• item 4 ($75, 6in)• item 1 ($10, 1in)
“knapsack”
At seats, knapsack problem
1 2 3 4 5 6 7 8 9
(1 in/ $10) 1 10 10 10 10 10 10 10 10 10
(3 in/$30) 2
(2in/$6) 3
(6 in/$75) 4
(2in/ $60) 5
80
1 2 3 4 5 6 7 8 9
(1 in/ $10) 1 10 10 10 10 10 10 10 10 10
(3 in/$30) 2 10 10 30 40 40 40 40 40 40
(2in/$6) 3 10 10 30 40 40 46 46 46 46
(6 in/$75) 4 10 10 30 40 40 75 85 85 105
(2in/ $60) 5 10 60 70 70 90 100 100 135 145
81
Recommended