14 greedy-II
14 greedy-II
14 greedy-II
2
When can we use Greedy algorithms?
3
Designing Greedy Algorithms
1. Cast the optimization problem as one for which:
• we make a choice and are left with only one subproblem
to solve
2. Prove the GREEDY CHOICE
• that there is always an optimal solution to the original
problem that makes the greedy choice
3. Prove the OPTIMAL SUBSTRUCTURE:
• the greedy choice + an optimal solution to the resulting
subproblem leads to an optimal solution
4
Example: Making Change
• Instance: amount (in cents) to return to customer
• Problem: do this using fewest number of coins
• Example:
– Assume that we have an unlimited number of coins of
various denominations:
– 1c (pennies), 5c (nickels), 10c (dimes), 25c (quarters), 1$
(loonies)
– Objective: Pay out a given sum $5.64 with the
smallest number of coins possible.
5
The Coin Changing Problem
• Assume that we have an unlimited number of coins of various
denominations:
• 1c (pennies), 5c (nickels), 10c (dimes), 25c (quarters), 1$ (loonies)
• Objective: Pay out a given sum S with the smallest number of
coins possible.
while S > 0 do
c := value of the largest coin no larger than S;
num := S / c;
pay out num coins of value c;
S := S - num*c;
6
Example: Making Change
• E.g.:
$5.64 = $2 +$2 + $1 +
.25 + .25 + .10 +
.01 + .01 + .01 +.01
7
Making Change – A big problem
• Example 2: Coins are valued $.30, $.20, $.05,
$.01
– Does not have greedy-choice property, since $.40 is
best made with two $.20’s, but the greedy solution will
pick three coins (which ones?)
8
The Fractional Knapsack Problem
• Given: A set S of n items, with each item i having
– bi - a positive benefit
– wi - a positive weight
• Goal: Choose items with maximum total benefit but with weight at
most W.
• If we are allowed to take fractional amounts, then this is the fractional
knapsack problem.
– In this case, we let xi denote the amount we take of item i
– Objective: maximize
– Constraint:
9
Example
• Given: A set S of n items, with each item i having
– bi - a positive benefit
– wi - a positive weight
• Goal: Choose items with maximum total benefit but with total weight at
most W.
“knapsack”
Solution: P
• 1 ml of 5 50$
Items:
1 2 3 4 5 • 2 ml of 3 40$
• 6 ml of 4 30$
Weight: 4 ml 8 ml 2 ml 6 ml 1 ml • 1 ml of 2 4$
Benefit: $12 $32 $40 $30 $50 10 ml
•Total Profit:124$
Value: 3 4 20 5 50
($ per ml)
10
The Fractional Knapsack Algorithm
• Greedy choice: Keep taking item with highest value (benefit to
weight ratio)
– Since
Algorithm fractionalKnapsack(S, W)
Input: set S of items w/ benefit bi and weight wi; max. weight W
Output: amount xi of each item i to maximize benefit w/ weight at most W
12
An Activity Selection Problem
(Conference Scheduling Problem)
13
The Activity Selection Problem
• Here are a set of start and finish times
14
The Activity Selection Problem
Input: list of time-intervals L
17
The Activity Selection Problem
Algorithm 1:
18
The Activity Selection Problem
Algorithm 1:
19
The Activity Selection Problem
Algorithm 2:
20
The Activity Selection Problem
Algorithm 2:
21
The Activity Selection Problem
Algorithm 2:
22
The Activity Selection Problem
Algorithm 3:
23
The Activity Selection Problem
Algorithm 3:
24
The Activity Selection Problem
Algorithm 3:
25
The Activity Selection Problem
Algorithm 3:
26
The Activity Selection Problem
Algorithm 3:
Theorem:
Idea: At each step, select the activity with the smallest finish time
that is compatible with the activities already chosen.
Greedy-Activity-Selector(s, f)
n <− length[s]
A <− {1} {Automatically select first activity}
j <− 1 {Last activity selected so far}
for i <− 2 to n do
if si >= fj then
A <− A U {i} {Add activity i to the set}
j <− i {record last activity added}
return A
28
The Activity Selection Problem
• Here are a set of start and finish times
29
Interval Representation
30
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 3115
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 3215
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 3315
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 3415
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 3515
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 3615
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 3715
Why this Algorithm is Optimal?
• We will show that this algorithm uses the
following properties
• The problem has the optimal substructure
property
• The algorithm satisfies the greedy-choice
property
• Thus, it is Optimal
38
Greedy-Choice Property
39
Optimal Substructures
– Once the greedy choice of activity 1 is made, the problem reduces
to finding an optimal solution for the activity-selection problem over
those activities in S that are compatible with activity 1
• Optimal Substructure
• If A is optimal to S, then A’ = A – {1} is optimal to S’={i ∈S: si ≥ f1}
• Why?
– If we could find a solution B’ to S’ with more activities than A’, adding
activity 1 to B’ would yield a solution B to S with more activities than A
contradicting the optimality of A
– After each greedy choice is made, we are left with an optimization
problem of the same form as the original problem
• By induction on the number of choices made, making the greedy choice
at every step produces an optimal solution
40