Professional Documents
Culture Documents
Greedy Vs Dynamic Programming Approach
Greedy Vs Dynamic Programming Approach
max bi where wi W
iA
iA
Greedy vs
Greedy vs
Brute force!
Generate all 2n subsets
Discard all subsets whose sum of the weights exceed
W (not feasible)
Select the maximum total benefit of the remaining
(feasible) subsets
What is the run time?
O(n 2n), Omega(2n)
Lets try the obvious greedy strategy .
Greedy vs
Profit=$70
Profit=$90
Profit=$140
Greedy vs
$140
10 lb
W=
25lb
$90
$140
25 lb
$70
25 lb
10 lb
10 lb
5 lb
item1
5 lb $70
item2
item3
Knapsack
Greedy
=$140
Solution
$90
Optimal
Solution =$160
Greedy vs
5 lb
$140
$60
$150
5 lb
10 lb
item1
item2
W=
30lb
$140
20 lb
10 lb $60
20 lb
5 lb $150
item3
Knapsack
Greedy
=$210
Solution
5 lb
$150
Optimal =$290
Solution
Greedy vs
10 lb $60
$140
$60
$150
5 lb
10 lb
item1
item2
W=
30lb
5 lb
$140
20 lb
20 lb
20 lb $140
item3
Knapsack
Greedy
Solution =$200
5 lb
$150
Optimal
Solution =$290
Greedy vs
5 lb
B/W: $7
$140
B/W 2: $6
B/W 1: $10
20 lb
$50
5 lb
item1
W=
30lb
20 lb $140
$50
10 lb $60
20 lb
$60
10 lb
item2
$140
item3
5 lb
Knapsack
Greedy
=$190
Solution
Optimal =$200
Solution
Greedy vs
Approximation algorithms
Greedy vs
Approximation algorithms
Use greedy 4: select the items with maximum benefit
per unit.
Implement by Sorting S by benefit per unit.
Example where greedy4 provides a very poor solution:
Assume a 0/1 knapsack problem with n=2
very large W.
S={ ( item1, 1, $2 ), ( item 2, W, $1.5W ) }.
The solution to greedy4 has a benefit of $2
An optimal solution has a benefit of $1.5W.
If we want the best investment and we have W=10,000
dollars. We should choose the 2nd one with a profit of
$15,000, and not the first with a profit of $2.
Greedy vs
Approximation Continued
Let BOpt denote the optimal benefit for the 0/1 knapsack
problem
Let BGreedy4 be the benefit calculated by greedy4.
For last example BOpt / BGreedy4 = $1.5W / 2
Note: W can be arbitrarily large
We would like to find a better algorithm Alg such that
BOpt / Alg K where K is a small constant and is
independent of the problem instance.
Greedy vs
Greedy vs
Greedy vs
KWF
Let k be the index of the last item included in the
knapsack. We may be able to include the whole or only
a fraction of item k
k 1
wi
Without item k totweight =
profitKWF =
k 1
p
i 1
i 1
B/W: $7
$140
B/W 2: $6
B/W 1: $10
20 lb
$50
5 lb
item1
30lb
Max
$60
10 lb
item2
item3
Knapsack
Optimal
$140 benefit $220
Solution:
20 lb
items 1and 2
and 1/2 of
item 3
$50
5 lb
Greedy
Solution
Optimal
Solution
Greedy vs
Greedy vs
Greedy vs
Greedy vs
for i 0 or w 0
0
B[i, w] B[i 1, w]
if w i w
max{B[i 1, w], B[i 1, w w ] b } otherwise
i
i
Greedy vs
Pseudo-code:0/1 Knapsack
(n+1)*(W+1) Matrix
Input: {w1 ,w2,
...
wn }, W , {b1 ,b2,
...
bn }
for w 0 to W do // row 0
B[0,w] 0
for k 1 to n do // rows 1 to n
B[k, 0] 0
// element in column 0
for w 1 to W do // elements in columns 1 to W
if (wk w) and (B[k-1, w- wk ] + bk > B[k-1, w])
then B[k, w] B[k-1, w- wk ] + bk
else B[k, w] B[k-1, w]
Greedy vs
Example:
W = 30, S = { ( i1 , 5, $50 ), (i2 ,10, $60 ), ( i3, 20, $140 ) }
Weight:
MaxProfit { }
0
0
1
0
2
0
3
0
Weight:
0
MaxProfit { }
MaxProfit{i1}
1
0
0
2
0
0
3
0
0
4
0
0
5
0
0
30
0
30
0 0
50 50
Greedy vs
Example continued
W = 30, S = { ( i1 , 5, $50 ), (i2 ,10, $60 ), ( i3, 20, $140 ) }
Weight:
MaxProfit { }
MaxProfit{i1}
0
0
0
... 4
... 0
... 0
5 9 10 14 15 ... 30
0 0 0 0 0 0
50 50 50 ... 50 50 50
MaxProfit{i1, i2} 0
50 50 60 60 110 110
Greedy vs
Example continued
W = 30, S = { ( i1 , 5, $50 ), (i2 ,10, $60 ), ( i3, 20, $140 ) }
Wt:
0...4 5 9 1014 15 19
20 24
2529
MaxP{ }0...0 0 0 0 0 0 0 00 0 0
0
MaxP{i1}0...0 5050
5050 50 50 5050
5050
MaxP{i1, i2} 00 5050 6060110...110 110 110 ...
30
50
110
B[3,20]
B[3,25]
B[3,30]
Greedy vs
Analysis
Greedy vs
Pseudo-code
Using (W +1) *2 matrix
Knapsack01(W, w1, ... wn , b1 ...bn )
Note: Calculating B[k,w] depends only on
B[k-1,w] or B[k-1, w- wk ]. B[n,W] = B[0,W] or B[1,
W] depending on whether n is even or odd.
B 0 // Initialize W by 2 matrix
for k 1 to n do
for w 1 to W do // w is not wi
if (wk w) and (B[mod(k,2), w- wk ] + bk
> B[mod(k,2), w ]) then
B[w,mod(k+1, 2) ]
B[mod(k+1,2), w- wk ] + bk
Greedy vs