




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Notes; Professor: Cohoon; Class: Algorithms; Subject: Computer Science; University: University of Virginia; Term: Unknown 1989;
Typology: Study notes
1 / 8
This page cannot be seen from the preview
Don't miss anything!





Input: Vector p of n profits and a vector w of n weights and a total weight M
Output: An optimal 0/1 vector of length x, where xi = 1 indicates object i is in knapsack and xi = 0 indicates object i is not in the knapsack Maximizes the profit subject to the constraint that the total weight of the included objects is at most M
Value of optimal solution with respect to objects i through j using at most capacity y
Knap(1, n, M) Value of the solution to our problem
fi(y) = max {fi-1(y) , pi + fi-1(y - wi)
f 0 (y) = 0 for y >= 0
f 0 (y) = - for y < 0
Can compute f 1 from f 0 , f 2 from f 1 , and so on ...
S[0] = {(0,0)}
for (i = 1; i < n; ++i) { Si1 = {(P, W) | (P - p[i],W - w[i]) | S[i-1] && W M} S[i] = MergePurge(S[i-1], Si1) }
for (i = n; n >= 1; --i) { (P1, W1) = maxp { (P,W) | (P,W) in S[i-1]} (P2, W2) = maxw { (P + p[i], W + w[i]) | (P,W) in S[i-1], W + w[i] M} if (P1 > P2) x[i] = 0 else x[i] = 1 }
|Si1| |S[i-1]| |S[i]| 2|S[i-1]|
linear time
1 1
n n i n
i i