7/30/2019 Dynamic vs Greedy
1/56
7/30/2019 Dynamic vs Greedy
2/56
Dynamic Programming is an algorithm designtechnique for optimization problems: oftenminimizing or maximizing.
Like divide and conquer, DP solves problems bycombining solutions to subproblems.
Unlike divide and conquer, subproblems are notindependent. Subproblems may share subsubproblems, However, solution to one subproblem may not affect the
solutions to other subproblems of the same problem.
DP reduces computation by Solving subproblems in a bottom-up fashion. Storing solution to a subproblem the first time it is solved. Looking up the solution when subproblem is encountered
again.
Key: determine structure of optimal solutions
7/30/2019 Dynamic vs Greedy
3/56
1. Characterize structure of an optimalsolution.
2. Define value of optimal solution
recursively.3. Compute optimal solution values bottom-
up in a table.
4. Construct an optimal solution from
computed values.Well study these with the help of examples.
7/30/2019 Dynamic vs Greedy
4/56
Optimal substructure
Overlapping sub-problems
7/30/2019 Dynamic vs Greedy
5/56
Applies to a problem that at first seems torequire a lot of time (possibly exponential),provided we have:
Simple subproblems: the subproblems can be defined interms of a few variables, such as j, k, l, m, and so on.
Subproblem optimality: the global optimum value canbe defined in terms of optimal subproblems.
Subproblem overlap: the subproblems are notindependent, but instead they overlap (hence, should beconstructed bottom-up).
7/30/2019 Dynamic vs Greedy
6/56
If the subproblems are not independent,i.e. subproblems share subsubproblems,then a divide-and-conquer algorithm
repeatedly solves the commonsubsubproblems.
Thus, it does more work than necessary!
Question: Any better solution?
YesDynamic programming (DP)!
7/30/2019 Dynamic vs Greedy
7/56
There are two versions of the problem:
(1) 0-1 knapsack problem and
(2) Fractional knapsack problem
(1) Items are indivisible; you either take an item ornot. Solved with dynamic programming
(2) Items are divisible: you can take any fraction of
an item. Solved with agreedy algorithm.
7/30/2019 Dynamic vs Greedy
8/56
Given a knapsack with maximum capacity W,
and a set S consisting of n items
Each item i has some weight wi and benefit
value bi (all wi , biand Ware integer values)
7/30/2019 Dynamic vs Greedy
9/56
Ti
i
Ti
iWwb subject tomax
The problem is called a 0-1 problem, because each itemmust be entirely accepted or rejected.
7/30/2019 Dynamic vs Greedy
10/56
Sk: Set of items numbered 1 to k.
Define B[k,w] = best selection from Sk with weightexactly equal to w
Good news: this does have subproblem optimality:
I.e., best subset of Sk with weight exactly w is eitherthe best subset of Sk-1 w/ weight w or the bestsubset of Sk-1 w/ weight w-wk plus item k.
else}],1[],,1[max{
if],1[],[
kk
k
bwwkBwkB
wwwkBwkB
7/30/2019 Dynamic vs Greedy
11/564/22/2013 11
for w = 0 to W
B[0,w] = 0for i = 0 to n
B[i,0] = 0
for w = 0 to Wif wi B[i-1,w]
B[i,w] = bi+ B[i-1,w- w
i]
else
B[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // wi > w
7/30/2019 Dynamic vs Greedy
12/564/22/2013 12
for w = 0 to W
B[0,w] = 0
for i = 0 to n
B[i,0] = 0
for w = 0 to W
< the rest of the code >
What is the running time ofthis algorithm? ---------
O(W)
O(W)
Repeat n times
O(n*W)
Remember that the brute-forcealgorithm takes O(2n)
7/30/2019 Dynamic vs Greedy
13/564/22/2013 13
Lets run our algorithm on thefollowing data:
n = 4 (# of elements)W = 5 (max weight)Elements (weight, benefit):(2,3), (3,4), (4,5), (5,6)
7/30/2019 Dynamic vs Greedy
14/564/22/2013 14
for w = 0 to WB[0,w] = 0
0
0
0
0
0
0
W
0
1
2
3
4
5
i 0 1 2 3
4
7/30/2019 Dynamic vs Greedy
15/564/22/2013 15
for i = 0 to nB[i,0] = 0
0
0
0
0
0
0
W
0
1
2
3
4
5
i 0 1 2 3
0 0 0 0
4
7/30/2019 Dynamic vs Greedy
16/564/22/2013 16
if wi B[i-1,w]B[i,w] = bi + B[i-1,w- wi]
elseB[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // wi > w
0
0
0
0
0
0
W
0
1
2
3
4
5
i 0 1 2 3
0 0 0 0
i=1
bi=3
wi=2
w=1
w-wi =-
1
Items:1: (2,3)2: (3,4)3: (4,5)4: (5,6)
4
0
7/30/2019 Dynamic vs Greedy
17/564/22/2013 17
if wi B[i-1,w]B[i,w] = bi + B[i-1,w- wi]
elseB[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // wi > w
0
0
0
0
0
0
W
0
1
2
3
4
5
i 0 1 2 3
0 0 0 0
i=1
bi=3
wi=2
w=2
w-wi
=0
Items:1: (2,3)2: (3,4)3: (4,5)4: (5,6)
4
0
3
7/30/2019 Dynamic vs Greedy
18/564/22/2013 18
if wi B[i-1,w]B[i,w] = bi + B[i-1,w- wi]
elseB[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // wi > w
0
0
0
0
0
0
W
0
1
2
3
4
5
i 0 1 2 3
0 0 0 0
i=1
bi=3
wi=2
w=3
w-
wi=1
Items:1: (2,3)2: (3,4)3: (4,5)4: (5,6)
4
0
3
3
7/30/2019 Dynamic vs Greedy
19/564/22/2013 19
if wi B[i-1,w]B[i,w] = bi + B[i-1,w- wi]
elseB[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // wi > w
0
0
0
0
0
0
W
0
1
2
3
4
5
i 0 1 2 3
0 0 0 0
i=1
bi=3
wi=2
w=4
w-
wi=2
Items:1: (2,3)2: (3,4)
3: (4,5)4: (5,6)
4
0
3
3
3
7/30/2019 Dynamic vs Greedy
20/56
4/22/2013 20
if wi B[i-1,w]B[i,w] = bi + B[i-1,w- wi]
elseB[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // wi > w
0
0
0
0
0
0
W
0
1
2
3
4
5
i 0 1 2 3
0 0 0 0
i=1
bi=3
wi=2
w=5
w-
wi=2
Items:1: (2,3)2: (3,4)
3: (4,5)4: (5,6)
4
0
3
3
3
3
7/30/2019 Dynamic vs Greedy
21/56
4/22/2013 21
if wi B[i-1,w]B[i,w] = bi + B[i-1,w- wi]
elseB[i,w] = B[i-1,w]
elseB[i,w] = B[i-1,w] // wi > w
0
0
0
0
0
0
W
0
1
2
3
4
5
i 0 1 2 3
0 0 0 0
i=2
bi=4
wi=3
w=1
w-wi=-2
Items:1: (2,3)2: (3,4)
3: (4,5)4: (5,6)
4
0
3
3
3
3
0
7/30/2019 Dynamic vs Greedy
22/56
4/22/2013 22
if wi B[i-1,w]B[i,w] = bi + B[i-1,w- wi]
elseB[i,w] = B[i-1,w]
elseB[i,w] = B[i-1,w] // wi > w
0
0
0
0
0
0
W
0
1
2
3
4
5
i 0 1 2 3
0 0 0 0
i=2
bi=4
wi=3
w=2
w-wi=-1
Items:1: (2,3)2: (3,4)
3: (4,5)4: (5,6)
4
0
3
3
3
3
0
3
7/30/2019 Dynamic vs Greedy
23/56
4/22/2013 23
if wi B[i-1,w]B[i,w] = bi + B[i-1,w- wi]
elseB[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // wi > w
0
0
0
0
0
0
W
0
1
2
3
4
5
i 0 1 2 3
0 0 0 0
i=2
bi=4
wi=3
w=3
w-wi=0
Items:1: (2,3)2: (3,4)
3: (4,5)4: (5,6)
4
0
3
3
3
3
0
3
4
7/30/2019 Dynamic vs Greedy
24/56
4/22/2013 24
if wi B[i-1,w]B[i,w] = bi + B[i-1,w- wi]
elseB[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // wi > w
0
0
0
0
0
0
W
0
1
2
3
4
5
i 0 1 2 3
0 0 0 0
i=2
bi=4
wi=3
w=4
w-wi=1
Items:1: (2,3)2: (3,4)
3: (4,5)4: (5,6)
4
0
3
3
3
3
0
3
4
4
7/30/2019 Dynamic vs Greedy
25/56
4/22/2013 25
if wi B[i-1,w]B[i,w] = bi + B[i-1,w- wi]
elseB[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // wi > w
0
0
0
0
0
0
W
0
1
2
3
4
5
i 0 1 2 3
0 0 0 0
i=2
bi=4
wi=3
w=5
w-wi=2
Items:1: (2,3)2: (3,4)
3: (4,5)4: (5,6)
4
0
3
3
3
3
0
3
4
4
7
7/30/2019 Dynamic vs Greedy
26/56
4/22/2013 26
if wi B[i-1,w]B[i,w] = bi + B[i-1,w- wi]
elseB[i,w] = B[i-1,w]
elseB[i,w] = B[i-1,w] // wi > w
0
0
0
0
0
0
W
0
1
2
3
4
5
i 0 1 2 3
0 0 0 0
i=3
bi=5
wi=4
w=1..3
Items:1: (2,3)2: (3,4)
3: (4,5)4: (5,6)
4
0
3
3
3
3
00
3
4
4
7
0
3
4
7/30/2019 Dynamic vs Greedy
27/56
4/22/2013 27
if wi B[i-1,w]B[i,w] = bi + B[i-1,w- wi]
elseB[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // wi > w
0
0
0
0
0
0
W
0
1
2
3
4
5
i 0 1 2 3
0 0 0 0
i=3
bi=5
wi=4
w=4
w- wi=0
Items:1: (2,3)2: (3,4)
3: (4,5)4: (5,6)
4
0 00
3
4
4
7
0
3
4
5
3
3
3
3
7/30/2019 Dynamic vs Greedy
28/56
4/22/2013 28
if wi B[i-1,w]B[i,w] = bi + B[i-1,w- wi]
elseB[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // wi > w
0
0
0
0
0
0
W
0
1
2
3
4
5
i 0 1 2 3
0 0 0 0
i=3
bi=5
wi=4
w=5
w- wi=1
Items:1: (2,3)2: (3,4)
3: (4,5)4: (5,6)
4
0 00
3
4
4
7
0
3
4
5
7
3
3
3
3
7/30/2019 Dynamic vs Greedy
29/56
4/22/2013 29
if wi B[i-1,w]B[i,w] = bi + B[i-1,w- wi]
elseB[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // wi > w
0
0
0
0
0
0
W
0
1
2
3
4
5
i 0 1 2 3
0 0 0 0
i=3
bi=5
wi=4
w=1..4
Items:1: (2,3)2: (3,4)
3: (4,5)4: (5,6)
4
0 00
3
4
4
7
0
3
4
5
7
0
3
4
5
3
3
3
3
7/30/2019 Dynamic vs Greedy
30/56
4/22/2013 30
if wi B[i-1,w]B[i,w] = bi + B[i-1,w- wi]
elseB[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // wi > w
0
0
0
0
0
0
W
0
1
2
3
4
5
i 0 1 2 3
0 0 0 0
i=3
bi=5
wi=4
w=5
Items:1: (2,3)2: (3,4)
3: (4,5)4: (5,6)
4
0 00
3
4
4
7
0
3
4
5
7
0
3
4
5
7
3
3
3
3
7/30/2019 Dynamic vs Greedy
31/56
4/22/2013 31
for w = 0 to W
B[0,w] = 0for i = 0 to n
B[i,0] = 0
for w = 0 to Wif wi B[i-1,w]
B[i,w] = bi
+ B[i-1,w- wi
]
else
B[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // wi > w
7/30/2019 Dynamic vs Greedy
32/56
4/22/2013 32
for w = 0 to W
B[0,w] = 0
for i = 0 to n
B[i,0] = 0
for w = 0 to W
< the rest of the code >
What is the running time ofthis algorithm? ---------
O(W)
O(W)
Repeat n times
O(n*W)
Remember that the brute-forcealgorithm takes O(2n)
7/30/2019 Dynamic vs Greedy
33/56
All of the information we need is in the table. V[n,W] is the maximal value of items that
can be placed in the Knapsack. Let i=n and k=W
if V[i,k] V[i1,k] then
mark the ith item as in the knapsacki = i1, k = k-wi
else
i = i1 // Assume the ith item is not in the
knapsack
// Could it be in the optimally packedknapsack?
7/30/2019 Dynamic vs Greedy
34/56
7/30/2019 Dynamic vs Greedy
35/56
All of the information we need is in the table.
V[n,W] is the maximal value of items that can beplaced in the Knapsack.
Let i=n and k=W
if V[i,k] V[i1,k] thenmark the ith item as in the knapsacki = i1, k = k-wi
elsei = i1 // Assume the ith item is not in theknapsack
// Could it be in the optimally packed
knapsack?
Items:
7/30/2019 Dynamic vs Greedy
36/56
Items:
1: (2,3)
2: (3,4)
3: (4,5)4: (5,6)
00
0
0
0
0 0 0 0 000
1
2
3
4 50 1 2 3
4
i\Wi=4
k= 5
bi=6
wi=5
V[i,k] = 7
V[i1,k] =7
3 3 3 3
0 3 4 4 7
0 3 4
i=n, k=W
while i,k > 0
ifV[i,k] V[i1,k] then
mark the ith item as in the knapsack
i = i1, k= k-wi
else
i = i1
5 7
0 3 4 5 7
Items:
7/30/2019 Dynamic vs Greedy
37/56
Items:
1: (2,3)
2: (3,4)
3: (4,5)4: (5,6)
00
0
0
0
0 0 0 0 000
1
2
3
4 50 1 2 3
4
i\W
i=4
k= 5
bi=6wi=5
V[i,k] = 7
V[i1,k] =7
3 3 3 3
0 3 4 4 7
0 3 4
i=n, k=W
while i,k > 0
ifV[i,k] V[i1,k] then
mark the ith item as in the knapsack
i = i1, k= k-wi
else
i = i1
5 7
0 3 4 5 7
Items:
7/30/2019 Dynamic vs Greedy
38/56
Items:
1: (2,3)
2: (3,4)
3: (4,5)4: (5,6)
00
0
0
0
0 0 0 0 000
1
2
3
4 50 1 2 3
4
i\Wi=3
k= 5
bi=5
wi=4
V[i,k] = 7
V[i1,k] =7
3 3 3 3
0 3 4 4 7
0 3 4
i=n, k=W
while i,k > 0
ifV[i,k] V[i1,k] then
mark the ith item as in the knapsack
i = i1, k= k-wi
else
i = i1
5 7
0 3 4 5 7
Items:
7/30/2019 Dynamic vs Greedy
39/56
Items:
1: (2,3)
2: (3,4)
3: (4,5)4: (5,6)
00
0
0
0
0 0 0 0 000
1
2
3
4 50 1 2 3
4
i\W
i=2
k= 5
bi=4
wi=3
V[i,k] = 7
V[i1,k] =3
k wi=2
3 3 3 3
0 3 4 4 7
0 3 4
i=n, k=W
while i,k > 0
ifV[i,k] V[i1,k] then
mark the ith item as in the knapsack
i = i1, k= k-wi
else
i = i1
5 7
0 3 4 5 7
7
Items:
7/30/2019 Dynamic vs Greedy
40/56
Items:
1: (2,3)
2: (3,4)
3: (4,5)4: (5,6)
00
00
0
0 0 0 0 000
1
23
4 50 1 2 3
4
i\W
i=1
k= 2
bi=3
wi=2
V[i,k] = 3
V[i1,k] =0
k wi=0
3 3 3 3
0 3 4 4 70 3 4
i=n, k=W
while i,k > 0
ifV[i,k] V[i1,k] then
mark the ith item as in the knapsack
i = i1, k= k-wi
else
i = i1
5 7
0 3 4 5 7
3
It
7/30/2019 Dynamic vs Greedy
41/56
Items:
1: (2,3)
2: (3,4)
3: (4,5)4: (5,6)
00
00
0
0 0 0 0 000
1
23
4 50 1 2 3
4
i\W
3 3 3 3
0 3 4 4 70 3 4
i=n, k=W
while i,k > 0
ifV[i,k] V[i1,k] then
mark the nth item as in the knapsack
i = i1, k= k-wi
else
i = i1
5 7
0 3 4 5 7
i=0
k= 0
The optimal
knapsack
should contain
{1, 2}
7/30/2019 Dynamic vs Greedy
42/56
Items:
1: (2,3)
2: (3,4)
3: (4,5)4: (5,6)
00
00
0
0 0 0 0 000
1
23
4 50 1 2 3
4
i\W
3 3 3 3
0 3 4 4 70 3 4
i=n, k=W
while i,k > 0
ifV[i,k] V[i1,k] then
mark the nth item as in the knapsack
i = i1, k= k-wi
else
i = i1
5 7
0 3 4 5 7
The optimalknapsack
should contain
{1, 2}
7
3
7/30/2019 Dynamic vs Greedy
43/56
7/30/2019 Dynamic vs Greedy
44/56
7/30/2019 Dynamic vs Greedy
45/56
The greedy method is a general algorithmdesign paradigm, built on the followingelements: configurations: different choices, collections, or values
to find
objective function: a score assigned to configurations,which we want to either maximize or minimize
It works best when applied to problems with thegreedy-choice property:
a globally-optimal solution can always be found by aseries of local improvements from a startingconfiguration.
7/30/2019 Dynamic vs Greedy
46/56
Similar to dynamic programming, but simpler
approach
Also used for optimization problems
Idea: When we have a choice to make, make the onethat looks best right now
Make a locally optimal choice in hope of getting a globally
optimal solution
Greedy algorithms dont always yield an optimalsolution
Makes the choice that looks best at the moment in
order to get optimal solution.
7/30/2019 Dynamic vs Greedy
47/56
The function Select selects an input fromand removes it. The selected inputs value isassigned to .
Feasible solution is a Booleanvaluedfunction that determines whether can beincluded into the solution vector. Thefunction Union combines with the solution &updates the objectives function.
7/30/2019 Dynamic vs Greedy
48/56
( , )
/ / [1: ]
{
0 / /
1
{
( )
( , )
( , )}
}
Greedy a n
a n contains the n inputs
solution initialize the solution
for i to n do
x Select a
if Feasible Solution x then
Solution Union Solution x
return Solution
7/30/2019 Dynamic vs Greedy
49/56
Problem: A dollar amount to reach and a collection of coinamounts to use to get there.
Configuration: A dollar amount yet to return to a customerplus the coins already returned
Objective function: Minimize number of coins returned.
Greedy solution: Always return the largest coin you can Example 1: Coins are valued $.32, $.08, $.01
Has the greedy-choice property, since no amount over $.32 canbe made with a minimum number of coins by omitting a $.32coin (similarly for amounts over $.08, but under $.32).
Example 2: Coins are valued $.30, $.20, $.05, $.01 Does not have greedy-choice property, since $.40 is best made
with two $.20s, but the greedy solution will pick three coins(which ones?)
7/30/2019 Dynamic vs Greedy
50/56
7/30/2019 Dynamic vs Greedy
51/56
Given: A set S of n items, with each item ihaving
bi - a positive benefit
wi - a positive weight
Goal: Choose items with maximum totalbenefit but with weight at most W.
If we are allowed to take fractionalamounts, then this is the fractional knapsackproblem.
7/30/2019 Dynamic vs Greedy
52/56
7/30/2019 Dynamic vs Greedy
53/56
7/30/2019 Dynamic vs Greedy
54/56
Dynamic programming
We make a choice at each step
The choice depends on solutions to subproblems
Bottom up solution, from smaller to larger
subproblems
Greedy algorithm
Make the greedy choice and THEN solve the
subproblem arising after the choice is made
The choice we make may depend on previous choices,
but not on solutions to subproblems
Top down solution, problems decrease in size
7/30/2019 Dynamic vs Greedy
55/56
Greedy and Dynamic Programming aremethods for solving optimization problems.
Greedy algorithms are usually more efficient
than DP solutions.However, often you need to use dynamic
programming since the optimal solutioncannot be guaranteed by a greedy algorithm.
DP provides efficient solutions for someproblems for which a brute force approachwould be very slow.
7/30/2019 Dynamic vs Greedy
56/56