lecture09-knapsack
-
Upload
rashedul-islam -
Category
Documents
-
view
219 -
download
0
Transcript of lecture09-knapsack
-
8/7/2019 lecture09-knapsack
1/8
1
CS 312: Algorithm Analysis
Lecture #9: Knapsack
Objectives
Remember the form of a generic greedyalgorithm
Define the Knapsack Problem
Learn how to formulate the problem inalgebraic terms
Get exposed to the formulation as a linearprogram (for future reference)
Work through an example
Generic Greedy Alg.
Function greedy (C:set): set{C is the set of candidates}
S {S will be the solution}
while (C != & !solution(S))
x select(C)
C C \ {x}
if feasible(S{x}) then
S S {x}
if solution(S) then return S
else there are no solutions
The Knapsack Problem
Given n objects withweight wi and value vi
Knapsack can only hold atotal weight of W
Fill Knapsack
Maximize total value
Dont exceed W
The Knapsack ProblemMathematically, represent a selectionfrom n objects by a vector x
Fix Order
of Objects
0
0
0
0
0
1
means
and
0
1
0
0
0
1
means{ , }
and
0
0
0
0
0
75.
means
The Knapsack ProblemMathematically, represent a selectionfrom n objects by a vector x
Fix Order
of Objects
=
6
5
4
3
2
1
x
x
x
x
x
x
x
10 ix
=
6
5
4
3
2
1
w
w
w
w
w
w
w
Corresponding
Object Weights
=
6
5
4
3
2
1
v
v
v
v
v
v
v
Corresponding
Object Values
-
8/7/2019 lecture09-knapsack
2/8
2
The Knapsack Problem
The problem is written mathematically as
Wxw
xvx
T
T
max
subject to
where vi>0, wi>0, and 0
-
8/7/2019 lecture09-knapsack
3/8
3
Knapsack
function knapsack (w[1n], v[1n], W) : array [1n]for i = 1 to n do x[i] 0weight 0while weight < W do
i the best remaining objectif weight + w[i]
-
8/7/2019 lecture09-knapsack
4/8
4
The Knapsack Problem
Example: Let W=100 and consider
Selection Function Ideas Most valuable Lightest weight Highest value/unit weight
=
50
40
30
20
10
w
=
60
40
66
30
20
v
=
2.1
1
2.2
5.1
2
/wv
=
0
0
0
0
0
x weight = 0
The Knapsack Problem
Example: Let W=100 and consider
Selection Function Ideas Most valuable Lightest weight Highest value/unit weight
=
50
40
30
20
10
w
=
60
40
66
30
20
v
=
2.1
1
2.2
5.1
2
/wv
=
0
0
0
0
0
x weight = 0
The Knapsack Problem
Example: Let W=100 and consider
Selection Function Ideas Most valuable Lightest weight Highest value/unit weight
=
50
40
30
20
10
w
=
60
40
66
30
20
v
=
2.1
1
2.2
5.1
2
/wv
=
00
1
0
0
x weight = 30
=
50
40
20
10
w
=
60
40
30
20
v
The Knapsack Problem
Example: Let W=100 and consider
Selection Function Ideas Most valuable Lightest weight Highest value/unit weight
=
2.1
1
2.2
5.1
2
/wv
=
00
1
0
0
x weight = 30
=
50
40
20
10
w
=
60
40
30
20
v
The Knapsack Problem
Example: Let W=100 and consider
Selection Function Ideas Most valuable Lightest weight Highest value/unit weight
=
2.1
1
2.2
5.1
2
/wv
=
0
0
1
0
0
x weight = 30
=
50
40
20
10
w
=
60
40
30
20
v
The Knapsack Problem
Example: Let W=100 and consider
Selection Function Ideas Most valuable Lightest weight Highest value/unit weight
=
2.1
1
2.2
5.1
2
/wv
=
1
0
1
0
0
x weight = 80
-
8/7/2019 lecture09-knapsack
5/8
5
=
40
20
10
w
=
1
0
1
0
0
x
=
40
30
20
v
The Knapsack Problem
Example: Let W=100 and consider
Selection Function Ideas Most valuable Lightest weight Highest value/unit weight
=
2.1
1
2.2
5.1
2
/wv
weight = 80
=
1
0
1
0
0
x
=
40
30
20
v
=
40
20
10
w
The Knapsack Problem
Example: Let W=100 and consider
Selection Function Ideas Most valuable Lightest weight Highest value/unit weight
=
2.1
1
2.2
5.1
2
/wv
weight = 80
=
15.0
1
0
0
x
=
40
30
20
v
=
40
20
10
w
The Knapsack Problem
Example: Let W=100 and consider
Selection Function Ideas Most valuable Lightest weight Highest value/unit weight
=
2.1
1
2.2
5.1
2
/wv
weight = 100
=
15.0
1
0
0
x
=
30
20
v
=
20
10
w
The Knapsack Problem
Example: Let W=100 and consider
Selection Function Ideas Most valuable Lightest weight Highest value/unit weight
=
2.1
1
2.2
5.1
2
/wv
weight = 100Score 1 = 146
Selection Function Ideas Most valuable Lightest weight Highest value/unit weight
=
0
0
0
0
0
x
=
60
40
66
30
20
v
=
50
40
30
20
10
w
The Knapsack Problem
Example: Let W=100 and consider
=
2.1
1
2.2
5.1
2
/wv
weight = 0Score 1 = 146
Selection Function Ideas Most valuable Lightest weight Highest value/unit weight
=
0
1
1
1
1
x
=
60
40
66
30
20
v
=
50
40
30
20
10
w
The Knapsack Problem
Example: Let W=100 and consider
=
2.1
1
2.2
5.1
2
/wv
weight = 100Score 1 = 146
-
8/7/2019 lecture09-knapsack
6/8
-
8/7/2019 lecture09-knapsack
7/8
7
=
50
40
20
w
=
2.1
1
2.2
5.1
2
/wv
Selection Function Ideas Most valuable Lightest weight Highest value/unit weight
Score 1 = 146Score 2 = 156
=
0
0
1
0
1
x
=
60
40
66
30
20
v
The Knapsack Problem
Example: Let W=100 and consider
weight = 40
=
2.1
1
2.2
5.1
2
/wv
=
50
40
20
w
Selection Function Ideas Most valuable Lightest weight Highest value/unit weight
Score 1 = 146Score 2 = 156
=
0
0
1
0
1
x
=
60
40
66
30
20
v
The Knapsack Problem
Example: Let W=100 and consider
weight = 40
=
2.1
1
2.2
5.1
2
/wv
=
50
40
w
Selection Function Ideas Most valuable Lightest weight Highest value/unit weight
Score 1 = 146Score 2 = 156
=
00
1
1
1
x
=
60
40
66
30
20
v
The Knapsack Problem
Example: Let W=100 and consider
weight = 60
=
2.1
1
2.2
5.1
2
/wv
=
50
40
w
Selection Function Ideas Most valuable Lightest weight Highest value/unit weight
Score 1 = 146Score 2 = 156
=
00
1
1
1
x
=
60
40
66
30
20
v
The Knapsack Problem
Example: Let W=100 and consider
weight = 60
=
2.1
1
2.2
5.1
2
/wv
=
8.0
0
1
1
1
x
=
40
w
Selection Function Ideas Most valuable Lightest weight Highest value/unit weight
Score 1 = 146Score 2 = 156
=
60
40
66
30
20
v
The Knapsack Problem
Example: Let W=100 and consider
weight = 100
=
8.0
0
1
1
1
x
=
2.1
1
2.2
5.1
2
/wv
=
40
w
Selection Function Ideas Most valuable Lightest weight Highest value/unit weight
Score 1 = 146Score 2 = 156Score 3 = 164
=
60
40
66
30
20
v
The Knapsack Problem
Example: Let W=100 and consider
weight = 100
-
8/7/2019 lecture09-knapsack
8/8
8
Selection Function Ideas Most valuable Lightest weight Highest value/unit weight
Score 1 = 146Score 2 = 156Score 3 = 164
The Knapsack Problem
Theorem: If objects are selected in order of decreasing
vi/wi, then algorithmknapsack finds an optimal solution.
Proof: Work through proofIn book on page 204.
Efficiency: Show that theAlgorithm exhibits timecomplexity that grows inO(nlogn).
Assignment
HW #6.5 Question 6.18
Read 7.1, 7.2