Lecture Notes for CS 512, Algorithm Design
12 April 2000, Greed
- there are n items, with item i weighing w(i) pounds and
costing c(i) dollars
- the fractional knapsack problem involves filling a knapsack with the
n items such that
- the sum of the weights of the items in the knapsack is no larger than
W
- the sum of the costs of the items in the knapsack is as large as
possible
- fractional items may appear in the knapsack
- use brute force - generate all possible combinations of items that weigh
no more than W and take the costliest combination - omega(2n)
- this problem has optimal subproblems - removing item i from the
knapsack must leave a maximal cost, otherwise the new sub-maximum would
lead to a new maximum
- the recursive algorithm has omega(2n) subproblems - dynamic
programming won't help
- work it in the other direction - remove the most valuable item
- after taking as much as the most valuable as possible, fill the rest of
the knapsack
- greed
- optimal subproblems
- locally greedy (optimal) solutions lead to a globally optimal solution
- this has to be proved
- for the fractional knapsack problem, assume the knapsack items are
ordered such that item 1 has the largest cost-weight ratio, item 2
has the next largest cost-weight ratio and so on.
- suppose an optimum solution S takes f1 of item 1 for
the knapsack
- let the greedy choice for item 1 be f'1; then f'1 >=
f1
- form solution S' by removing f'1w(1) of items from
S and replacing it with f'1w(1) of item 1
- the solutions S' and S weigh the same, and, because item
1 has the highest cost-weight ratio among all items, the cost of
item i in solution S' is at least as much as the cost of the
items removed from solution(S). Solution S' is also optimal
- repeat with the next item chosen in the greedy method
- the 0-1 knapsack problem - fractional items are not allowed
- the subproblems are optimal
- now greed is not optimal
- there are repeated subproblems - dynamic programming can solve this
problem
- scheduling
given n items, item i starts at time si and finishes
at time fi, si <= fi
- a consistent schedule is a subset of the n items with the property
that, for any pair of items i != j in the subset, i and
j don't overlap
- find the largest consistent schedule
- the algorithm
- sort the items by increasing finishing time
- pick the item with the earliest finishing time
- keep picking items with the earliest non-conflicting finishing time
- the optimal subproblem property holds
- the greedy-choice property holds too
- let S be an optimal schedule not containing the earliest
fi
- if activity j finishes first in S, form S' by
removing j and adding i; S' and S have the same
number of elements
- because S is a consistent schedule, for any activity k in
S not equal to j, fj <= sk
- because fi <= fj, then S' is consistent too
- remove i from S' and j from S and repeat the
argument with the next greedy choice
- the greedy choices lead to an optimal schedule, so the
greedy-choice property holds.
This page last modified on 17 April 2000.