111/06/13 CS4335 Design and Analysis of Algorithms/WANG Page 1 Greedy Algorithms: A greedy algorithm always makes the choice that looks best at the moment. It makes a local optimal choice in the hope that this choice will lead to a globally optimal solution. Greedy algorithms yield optimal solutions for many (but not all) problems.
Greedy Algorithms:. A greedy algorithm always makes the choice that looks best at the moment. It makes a local optimal choice in the hope that this choice will lead to a globally optimal solution. Greedy algorithms yield optimal solutions for many (but not all) problems. . - PowerPoint PPT Presentation
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
112/04/22 CS4335 Design and Analysis of Algorithms/WANG Lusheng
Page 1
Greedy Algorithms: A greedy algorithm always makes the
choice that looks best at the moment. It makes a local optimal choice in the
hope that this choice will lead to a globally optimal solution.
Greedy algorithms yield optimal solutions for many (but not all) problems.
112/04/22 CS4335 Design and Analysis of Algorithms/WANG Lusheng
Page 2
The 0-1 Knapsack problem:
The 0-1 knapsack problem: N items, where the i-th item is worth vi dollars and
weight wi pounds. 11 p 3 p 4p 58 p 8p 88p vi and wi are integers.
3$ 6 $ 35$ 8$ 28$ 66$ We can carry at most W (integer) pounds. How to take as valuable a load as possible.
An item cannot be divided into pieces. The fractional knapsack problem: The same setting, but the thief can take fractions of items. W may not be integer.
W
112/04/22 CS4335 Design and Analysis of Algorithms/WANG Lusheng
Page 3
Solve the fractional Knapsack problem:
Greedy on the value per pound vi/wi. Each time, take the item with maximum vi/wi . If exceeds W, take fractions of the item.
Example: (1, 5$), (2, 9$), (3, 12$), (3, 11$) and w=4. vi/wi : 5 4.5 4.0 3.667 First: (1, 5$), Second: (2, 9$), Third: 1/3 (3, 12$) Total W: 1, 3, 4. Can only take part of
item
112/04/22 CS4335 Design and Analysis of Algorithms/WANG Lusheng
Page 4
Proof of correctness: (The hard part)
Let X = i1, i2, …ik be the optimal items taken. Consider the item j : (vj, wj) with the highest v /w. if j is not used in X (the optimal solution), get rid of some
items with total weight wj (possibly fractional items) and add item j.
(since fractional items are allowed, we can do it.) Total value is increased. Why?
One more item selected by greedy is added to X Repeat the process, X is changed to contain all items selected
by greedy WITHOUT decreasing the total value taken by the thief.
X
i1 w1
i2 w2 wj
.
.
.
ip wp
.
.
.
ikwk
jj
jj
j
jpp
j
jp
j
jp
j
j
j
j
p
pp
p
pp
pp
vwv
wwv
wqwww
wv
wqwv
wwv
wwv
w
wv
wqwv
wwvw
wvw
vqvvv
)()()%...(
)(%)(...)()(
)(%)(...)()(
%...
121
121
1
11
2
22
1
11
121
112/04/22 CS4335 Design and Analysis of Algorithms/WANG Lusheng
Page 5
The 0-1 knapsack problem cannot be solved optimally by