Title: Ingredients of Greedy Algorithms
1Ingredients of Greedy Algorithms
When is a greedy strategies applicable?
A sequence of locally optimal choices can yield a
globally optimal solution.
Depends on choices made so far but not those
made in the future.
An optimal solution to a problem contains optimal
solutions to subproblems.
20-1 Knapsack Problem
3
1
30
20 lb.
2
10
100 60 120
Carry as valuable a load W lbs as possible!
But no fractional amount of any item can be taken.
3Fractional Knapsack Problem
3
30
1
30
20
2/3 of item 3
20 lb.
2
(80)
10
knapsack
100 60 120
Same description as 0-1 knapsack except fractions
of items can be taken.
4Optimal Substructure I
0-1 knapsack
Most valuable load W lbs
5Optimal Substructure II
Fractional knapsack (FK)
Most valuable load W lbs
6Greedy Algorithm for FK
knapsack (50 lb.)
O(n lg n) including the sorting time.
7No Greedy for 0-1Knapsack
20
optimal solution
greedy solution
Need to use dynamic programming!
8A DP Solution to 0-1
Subproblem
KW, n will be the solution.
9Recursive Definition
0 if w 0
or i 0
Kw, i
.
Running time O(nW)
.