CS 312: Algorithm Analysis: Objectives
CS 312: Algorithm Analysis: Objectives
⎡1 ⎤ ⎡1 ⎤ ⎡.75⎤ ⎡ x1 ⎤ ⎡ w1 ⎤ ⎡ v1 ⎤
⎢0 ⎥ ⎢0 ⎥ ⎢0⎥ ⎢x ⎥ ⎢w ⎥ ⎢v ⎥
⎢ ⎥ ⎢ ⎥ ⎢ ⎥ ⎢ 2⎥ ⎢ 2⎥ ⎢ 2⎥
⎢0 ⎥ and ⎢0 ⎥ and ⎢0⎥ ⎢x ⎥ ⎢w ⎥ ⎢v ⎥
⎢ ⎥ ⎢ ⎥ ⎢ ⎥ x = ⎢ 3⎥ w = ⎢ 3⎥ v = ⎢ 3⎥
⎢0 ⎥ ⎢0 ⎥ ⎢0⎥ ⎢ x4 ⎥ ⎢ w4 ⎥ ⎢v4 ⎥
⎢0 ⎥ ⎢1 ⎥ ⎢0⎥ ⎢ x5 ⎥ ⎢ w5 ⎥ ⎢v5 ⎥
⎢ ⎥ ⎢ ⎥ ⎢ ⎥ ⎢ ⎥ ⎢ ⎥ ⎢ ⎥
⎣⎢0⎦⎥ ⎣⎢0⎦⎥ ⎣⎢ 0 ⎦⎥ ⎣⎢ x6 ⎦⎥ ⎣⎢ w6 ⎦⎥ ⎣⎢v6 ⎦⎥
Fix Order
of Objects means means
{ , } means
Fix Order
of Objects 0 ≤ xi ≤ 1
Corresponding
Object Weights
Corresponding
Object Values
1
The Knapsack Problem The Knapsack Problem
⎡ x1 ⎤
The problem is written mathematically as ⎢x ⎥ The problem is written mathematically as
⎢ 2⎥
T ⎢x ⎥ n
max v x [v1 v2 v3 v4 v5 v6 ]⎢ 3 ⎥ = ∑ vi xi max vT x
⎢ x4 ⎥ i =1
x ⎢ x5 ⎥ x
⎢ ⎥ ⎡ x1 ⎤
⎣⎢ x6 ⎦⎥ ⎢x ⎥
subject to subject to
⎢ 2⎥
⎢x ⎥ n
wT x ≤ W w x ≤W
T [w1 w2 w3 w4 w5 w6 ]⎢ 3 ⎥ = ∑ wi xi
⎢ x4 ⎥ i =1
⎢ x5 ⎥
where vi>0, wi>0, and 0<=xi<=1 for all 1<= i<=n. where vi>0, wi>0, and 0<=xi<=1 for all 1<= i<=n. ⎢⎢ x ⎥⎥
⎣ 6⎦
∑ vi xi
T
max v x max max vT x
x x i =1 x
subject to
= n subject to
subject to ∑ wi xi ≤ W
wT x ≤ W i =1 wT x ≤ W
where vi>0, wi>0, and 0<=xi<=1 for all 1<= i<=n.
wT x ≤ W Feasibility
Check
g ( x )≤ W wT x ≤ W Feasibility
Check
g ( x )≤ W
They are a special case of general mathematical programs, If we can introduce a meaningful Selection Function, we
or optimization problems. can design Greedy Algorithms to try to solve the
optimization problem!
2
Knapsack Knapsack Selection Fn.
What is the selection function if…
function knapsack (w[1…n], v[1…n], W) : array [1…n]
for i = 1 to n do x[i] ← 0 want to maximize profit?
weight ← 0
while weight < W do
i ← the best remaining object want to minimize profit (cost)?
if weight + w[i] <= W then x[i] ← 1
weight ← weight + w[i]
else x[i] ← (W - weight)/w[i]
weight ← W What is the complexity of the algorithm?
return x
wT x ≤ W x[i] ← 1
weight ← weight + w[i]
x[i] ← 1
weight ← weight + w[i]
else x[i] ← (W – weight)/w[i] else x[i] ← (W – weight)/w[i]
weight ← W weight ← W
return x return x
3
The Knapsack Problem The Knapsack Problem
Example: Let W=100 and consider Example: Let W=100 and consider
⎡10 ⎤ ⎡20⎤ ⎡2⎤ ⎡10 ⎤ ⎡20⎤ ⎡2⎤
⎢20⎥ ⎢30 ⎥ ⎢1.5 ⎥ ⎢20⎥ ⎢30 ⎥ ⎢1.5 ⎥
⎢ ⎥ ⎢ ⎥ ⎢ ⎥ ⎢ ⎥ ⎢ ⎥ ⎢ ⎥
w = ⎢30⎥ v = ⎢66⎥ v / w = ⎢2.2⎥ w = ⎢30⎥ v = ⎢66⎥ v / w = ⎢2.2⎥
Selection Function Ideas ⎢ ⎥ ⎢ ⎥ ⎢ ⎥ Selection Function Ideas ⎢ ⎥ ⎢ ⎥ ⎢ ⎥
• Most valuable ⎢40⎥ ⎢40⎥ ⎢1⎥ • Most valuable ⎢40⎥ ⎢40⎥ ⎢1⎥
• Lightest weight ⎣⎢50⎦⎥ ⎣⎢60⎦⎥ ⎣⎢1.2 ⎦⎥ • Lightest weight ⎣⎢50⎦⎥ ⎣⎢60⎦⎥ ⎣⎢1.2 ⎦⎥
• Highest value/unit weight • Highest value/unit weight
⎡0⎤ ⎡0⎤
⎢0⎥ ⎢0⎥
⎢ ⎥ ⎢ ⎥
x = ⎢0⎥ weight = 0 x = ⎢0⎥ weight = 0
⎢ ⎥ ⎢ ⎥
⎢0⎥ ⎢0⎥
⎢⎣0⎥⎦ ⎢⎣0⎥⎦
4
The Knapsack Problem The Knapsack Problem
Example: Let W=100 and consider Example: Let W=100 and consider
⎡10 ⎤ ⎡20⎤ ⎡2⎤ ⎡10 ⎤ ⎡20⎤ ⎡2⎤
⎢20⎥ ⎢30 ⎥ ⎢1.5 ⎥ ⎢20⎥ ⎢30 ⎥ ⎢1.5 ⎥
⎢ ⎥ ⎢ ⎥ ⎢ ⎥ ⎢ ⎥ ⎢ ⎥ ⎢ ⎥
w=⎢−⎥ v=⎢−⎥ v / w = ⎢2.2⎥ w=⎢−⎥ v=⎢−⎥ v / w = ⎢2.2⎥
Selection Function Ideas ⎢ ⎥ ⎢ ⎥ ⎢ ⎥ Selection Function Ideas ⎢ ⎥ ⎢ ⎥ ⎢ ⎥
• Most valuable ⎢40⎥ ⎢40⎥ ⎢1⎥ • Most valuable ⎢40⎥ ⎢40⎥ ⎢1⎥
• Lightest weight ⎣⎢ − ⎦⎥ ⎣⎢ − ⎦⎥ ⎣⎢1.2 ⎦⎥ • Lightest weight ⎣⎢ − ⎦⎥ ⎣⎢ − ⎦⎥ ⎣⎢1.2 ⎦⎥
• Highest value/unit weight • Highest value/unit weight
⎡0⎤ ⎡0⎤
⎢0⎥ ⎢0⎥
⎢ ⎥ ⎢ ⎥
x = ⎢1⎥ weight = 80 x = ⎢1⎥ weight = 80
⎢ ⎥ ⎢ ⎥
⎢0⎥ ⎢0⎥
⎢⎣1⎥⎦ ⎢⎣1⎥⎦
5
The Knapsack Problem The Knapsack Problem
Example: Let W=100 and consider Example: Let W=100 and consider
⎡10 ⎤ ⎡20⎤ ⎡2⎤ ⎡10 ⎤ ⎡20⎤ ⎡2⎤
⎢20⎥ ⎢30 ⎥ ⎢1.5 ⎥ ⎢20⎥ ⎢30 ⎥ ⎢1.5 ⎥
⎢ ⎥ ⎢ ⎥ ⎢ ⎥ ⎢ ⎥ ⎢ ⎥ ⎢ ⎥
w = ⎢30⎥ v = ⎢66⎥ v / w = ⎢2.2⎥ w = ⎢30⎥ v = ⎢66⎥ v / w = ⎢2.2⎥
Selection Function Ideas ⎢ ⎥ ⎢ ⎥ ⎢ ⎥ Selection Function Ideas ⎢ ⎥ ⎢ ⎥ ⎢ ⎥
• Most valuable ⎢40⎥ ⎢40⎥ ⎢1⎥ • Most valuable ⎢40⎥ ⎢40⎥ ⎢1⎥
• Lightest weight ⎣⎢50⎦⎥ ⎣⎢60⎦⎥ ⎣⎢1.2 ⎦⎥ • Lightest weight ⎣⎢50⎦⎥ ⎣⎢60⎦⎥ ⎣⎢1.2 ⎦⎥
• Highest value/unit weight • Highest value/unit weight
⎡1⎤ ⎡0⎤
⎢1⎥ ⎢0⎥
Score 1 = 146 ⎢ ⎥ Score 1 = 146 ⎢ ⎥
Score 2 = 156 x = ⎢1⎥ weight = 100 Score 2 = 156 x = ⎢0⎥ weight = 0
⎢ ⎥ ⎢ ⎥
⎢1⎥ ⎢0⎥
⎢⎣0⎥⎦ ⎢⎣0⎥⎦
6
The Knapsack Problem The Knapsack Problem
Example: Let W=100 and consider Example: Let W=100 and consider
⎡−⎤ ⎡20⎤ ⎡2⎤ ⎡−⎤ ⎡20⎤ ⎡2⎤
⎢20⎥ ⎢30 ⎥ ⎢1.5 ⎥ ⎢20⎥ ⎢30 ⎥ ⎢1.5 ⎥
⎢ ⎥ ⎢ ⎥ ⎢ ⎥ ⎢ ⎥ ⎢ ⎥ ⎢ ⎥
w=⎢−⎥ v = ⎢66⎥ v / w = ⎢2.2⎥ w=⎢−⎥ v = ⎢66⎥ v / w = ⎢2.2⎥
Selection Function Ideas ⎢ ⎥ ⎢ ⎥ ⎢ ⎥ Selection Function Ideas ⎢ ⎥ ⎢ ⎥ ⎢ ⎥
• Most valuable ⎢40⎥ ⎢40⎥ ⎢1⎥ • Most valuable ⎢40⎥ ⎢40⎥ ⎢1⎥
• Lightest weight ⎣⎢50⎦⎥ ⎣⎢60⎦⎥ ⎣⎢1.2 ⎦⎥ • Lightest weight ⎣⎢50⎦⎥ ⎣⎢60⎦⎥ ⎣⎢1.2 ⎦⎥
• Highest value/unit weight • Highest value/unit weight
⎡1⎤ ⎡1⎤
⎢0⎥ ⎢0⎥
Score 1 = 146 ⎢ ⎥ Score 1 = 146 ⎢ ⎥
Score 2 = 156 x = ⎢1⎥ weight = 40 Score 2 = 156 x = ⎢1⎥ weight = 40
⎢ ⎥ ⎢ ⎥
⎢0⎥ ⎢0⎥
⎢⎣0⎥⎦ ⎢⎣0⎥⎦
7
The Knapsack Problem Assignment
Theorem: If objects are selected in order of decreasing HW #6.5
vi/wi, then algorithm knapsack finds an optimal solution.
Question 6.18
Proof: Work through proof
Selection Function Ideas In book on page 204. Read 7.1, 7.2
• Most valuable
• Lightest weight
• Highest value/unit weight
Efficiency: Show that the
Algorithm exhibits time
Score 1 = 146
complexity that grows in
Score 2 = 156
O(nlogn).
Score 3 = 164