0% found this document useful (0 votes)
32 views

CS 312: Algorithm Analysis: Objectives

The document discusses the knapsack problem and how to formulate it as a linear program. The knapsack problem involves selecting a subset of objects with weights and values to maximize total value without exceeding a weight limit. It can be represented mathematically with vectors and variables. A greedy algorithm selects the most valuable remaining object at each step until the knapsack is full. Linear programs like the knapsack problem can be solved with greedy approaches using an appropriate selection function.

Uploaded by

Rashedul Islam
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

CS 312: Algorithm Analysis: Objectives

The document discusses the knapsack problem and how to formulate it as a linear program. The knapsack problem involves selecting a subset of objects with weights and values to maximize total value without exceeding a weight limit. It can be represented mathematically with vectors and variables. A greedy algorithm selects the most valuable remaining object at each step until the knapsack is full. Linear programs like the knapsack problem can be solved with greedy approaches using an appropriate selection function.

Uploaded by

Rashedul Islam
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Objectives

CS 312: Algorithm Analysis


ƒ Remember the form of a generic greedy
algorithm
ƒ Define the Knapsack Problem
ƒ Learn how to formulate the problem in
algebraic terms
Lecture #9: Knapsack ƒ Get exposed to the formulation as a linear
program (for future reference)
ƒ Work through an example

Generic Greedy Alg. The Knapsack Problem


Function greedy (C:set): set
{C is the set of candidates} ƒ Given n objects with
S ← ∅ {S will be the solution} weight wi and value vi
while (C != ∅ & !solution(S))
ƒ Knapsack can only hold a
x ← select(C)
total weight of W
C ← C \ {x}
if feasible(S∪{x}) then
ƒ Fill Knapsack
ƒ Maximize total value
S ← S ∪ {x}
if solution(S) then return S ƒ Don’t exceed W
else “there are no solutions”

The Knapsack Problem The Knapsack Problem


Mathematically, represent a selection Mathematically, represent a selection
from n objects by a vector x from n objects by a vector x

⎡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⎦

The Knapsack Problem The Knapsack Problem


The problem is written mathematically as Problems of this form are called Linear Programs.
n

∑ 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.

The Knapsack Problem The Knapsack Problem


Problems of this form are called Linear Programs. Problems of this form are called Linear Programs.
Objective Function Objective Function
T
max v x max f ( x ) max vT x max f ( x )
x Candidates x x Candidates x
subject to subject to subject to subject to

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

The Knapsack Problem The Knapsack Problem


Greedy Algorithm: function knapsack(w[1..n],v[1..n],W):array [1..n] Greedy Algorithm: function knapsack(w[1..n],v[1..n],W):array [1..n]
{initialization} {initialization}
T
max v x for i = 1 to n do x[i] ← 0
weight ← 0
for i = 1 to n do x[i] ← 0
weight ← 0
x {Greedy Loop} {Greedy Loop}
Selection Function Ideas while weight < W do
while weight < W do
• Most valuable
subject to i ← the best remaining object • Lightest weight i ← the best remaining object
if weight+w[i]<=W then • Highest value/unit weight if weight+w[i]<=W then

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

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⎥
⎢ ⎥
x = ⎢0⎥ weight = 0
⎢ ⎥
⎢0⎥
⎢⎣0⎥⎦

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⎥⎦

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=⎢−⎥ v=⎢−⎥ 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 = ⎢1⎥ weight = 30 x = ⎢1⎥ weight = 30
⎢ ⎥ ⎢ ⎥
⎢0⎥ ⎢0⎥
⎢⎣0⎥⎦ ⎢⎣0⎥⎦

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 ⎣⎢50⎦⎥ ⎣⎢60⎦⎥ ⎣⎢1.2 ⎦⎥ • Lightest weight ⎣⎢50⎦⎥ ⎣⎢60⎦⎥ ⎣⎢1.2 ⎦⎥
• Highest value/unit weight • Highest value/unit weight
⎡0⎤ ⎡0⎤
⎢0⎥ ⎢0⎥
⎢ ⎥ ⎢ ⎥
x = ⎢1⎥ weight = 30 x = ⎢1⎥ weight = 80
⎢ ⎥ ⎢ ⎥
⎢0⎥ ⎢0⎥
⎢⎣0⎥⎦ ⎢⎣1⎥⎦

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⎥⎦

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 ⎢−⎥ ⎢−⎥ ⎢1⎥
• Lightest weight ⎣⎢ − ⎦⎥ ⎣⎢ − ⎦⎥ ⎣⎢1.2 ⎦⎥ • Lightest weight ⎣⎢ − ⎦⎥ ⎣⎢ − ⎦⎥ ⎣⎢1.2 ⎦⎥
• Highest value/unit weight • Highest value/unit weight
⎡0⎤ ⎡0⎤
⎢0⎥ ⎢0⎥
⎢ ⎥ Score 1 = 146 ⎢ ⎥
x=⎢ 1 ⎥ weight = 100 x=⎢ 1 ⎥ weight = 100
⎢ ⎥ ⎢ ⎥
⎢0.5⎥ ⎢0.5⎥
⎢⎣ 1 ⎥⎦ ⎢⎣ 1 ⎥⎦

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⎤ ⎡1⎤
⎢0⎥ ⎢1⎥
Score 1 = 146 ⎢ ⎥ Score 1 = 146 ⎢ ⎥
x = ⎢0⎥ weight = 0 x = ⎢1⎥ weight = 100
⎢ ⎥ ⎢ ⎥
⎢0⎥ ⎢1⎥
⎢⎣0⎥⎦ ⎢⎣0⎥⎦

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⎥⎦

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⎥
Score 1 = 146 ⎢ ⎥ Score 1 = 146 ⎢ ⎥
Score 2 = 156 x = ⎢0⎥ weight = 0 Score 2 = 156 x = ⎢0⎥ weight = 0
⎢ ⎥ ⎢ ⎥
⎢0⎥ ⎢0⎥
⎢⎣0⎥⎦ ⎢⎣0⎥⎦

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 = ⎢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
⎡0⎤ ⎡0⎤
⎢0⎥ ⎢0⎥
Score 1 = 146 ⎢ ⎥ Score 1 = 146 ⎢ ⎥
Score 2 = 156 x = ⎢1⎥ weight = 30 Score 2 = 156 x = ⎢1⎥ weight = 30
⎢ ⎥ ⎢ ⎥
⎢0⎥ ⎢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⎥⎦

The Knapsack Problem The Knapsack Problem


Example: Let W=100 and consider Example: Let W=100 and consider
⎡−⎤ ⎡20⎤ ⎡2⎤ ⎡−⎤ ⎡20⎤ ⎡2⎤
⎢−⎥ ⎢30 ⎥ ⎢1.5 ⎥ ⎢−⎥ ⎢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⎤
⎢1⎥ ⎢1⎥
Score 1 = 146 ⎢ ⎥ Score 1 = 146 ⎢ ⎥
Score 2 = 156 x = ⎢1⎥ weight = 60 Score 2 = 156 x = ⎢1⎥ weight = 60
⎢ ⎥ ⎢ ⎥
⎢0⎥ ⎢0⎥
⎢⎣0⎥⎦ ⎢⎣0⎥⎦

The Knapsack Problem The Knapsack Problem


Example: Let W=100 and consider Example: Let W=100 and consider
⎡−⎤ ⎡20⎤ ⎡2⎤ ⎡−⎤ ⎡20⎤ ⎡2⎤
⎢−⎥ ⎢30 ⎥ ⎢1.5 ⎥ ⎢−⎥ ⎢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 ⎣⎢ − ⎦⎥ ⎣⎢60⎦⎥ ⎣⎢1.2 ⎦⎥ • Lightest weight ⎣⎢ − ⎦⎥ ⎣⎢60⎦⎥ ⎣⎢1.2 ⎦⎥
• Highest value/unit weight • Highest value/unit weight
⎡1⎤ ⎡1⎤
⎢1⎥ ⎢1⎥
Score 1 = 146 ⎢ ⎥ Score 1 = 146 ⎢ ⎥
Score 2 = 156 x=⎢ 1 ⎥ weight = 100 Score 2 = 156 x=⎢ 1 ⎥ weight = 100
⎢ ⎥ Score 3 = 164 ⎢ ⎥
⎢0⎥ ⎢0⎥
⎢⎣0.8⎥⎦ ⎢⎣0.8⎥⎦

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

You might also like