Greedy Algorithm 3
Greedy Algorithm 3
Jianxi Gao
Example: 34¢.
Example: $2.89
General problem statement
Given coin denominations in C = {ci} (where 𝑖 ∈ [0, 𝑚 − 1]), make
change for a given amount A with the minimum number of corns.
Input: C = {ci} and A
Output: number of corns.
What is the answer if you buy another product with value $3.33?
Greedy algorithm.
coins selected
S
while (A 0) {
let k be largest integer such that ck A
if (no such k)
return "no solution found"
A A - ck
S S {k}
}
return S
Q1: What is the smallest number of quarters, dimes, nickels and pennies one can carry
while still being able to give perfect change (two decimals)?
99¢ = 3 × 25¢ + 2 × 10¢ + 1 × 5¢ + 4 × 1¢
Q2: What amount of change requires the largest number of coins?
99¢ = 3 × 25¢ + 2 × 10¢ + 1 × 5¢ + 4 × 1¢
Q3: Can you answer the question for a different system of coins? For example, I am
currently spending the summer in Cambridge, England, where coins are worth 1, 2, 5,
10, 20, and 50 pence. What if you also include the 1- and 2-pound (100 and 200
pence) coins, and want to be able to make change for every amount up to 5 pounds
(the smallest note)?
499p = £2 + £2 + 50p + 20p + 20p + 5p + 2p + 2p = 8 coins.
Q4. If you got to design your own system of coins with whatever denominations
you wanted, how would you design it so that the minimum number of coins
needed to make all amounts between 1 and 99 cents is as small as possible?
98 = 27+27+27+9+3+3+1+1 (8 coins total).
10 mins
Cashier’s algorithm for other denominations
A: No.
Example 1: Consider U.S. postage: 1, 10, 21, 34, 70, 100, 350, 1225, 1500
?
Cashier’s algorithm: 140 = 100 +34 +1 +1 +1 +1 +1
The optimal: 140 = 70+70
Example 2: It may not even lead to a feasible solution. For example ck = 7,8,9
Cashier’s algorithm: 15 = 9 + ??? The optimal: 15 = 7+8.
Cashier’s algorithm for other denominations
A: No.
Example 1: Consider U.S. postage: 1, 10, 21, 34, 70, 100, 350, 1225, 1500
?
Cashier’s algorithm: 140 = 100 +34 +1 +1 +1 +1 +1
The optimal: 140 = 70+70
Example 2: It may not even lead to a feasible solution. For example, c k = 7,8,9
Cashier’s algorithm: 15 = 9 + ??? The optimal: 15 = 7+8.
Other algorithms
Example 2: It may not even lead to a feasible solution. For example ck = 7,8,9
Cashier’s algorithm: 15 = 9 + ??? The optimal: 15 = 7+8.
15
Other algorithms
Example 2: It may not even lead to a feasible solution. For example ck = 7,8,9
Cashier’s algorithm: 15 = 9 + ??? The optimal: 15 = 7+8.
15
8
Other algorithms
Example 2: It may not even lead to a feasible solution. For example ck = 7,8,9
Cashier’s algorithm: 15 = 9 + ??? The optimal: 15 = 7+8.
15
8 7
Other algorithms
Example 2: It may not even lead to a feasible solution. For example ck = 7,8,9
Cashier’s algorithm: 15 = 9 + ??? The optimal: 15 = 7+8.
15
8 7 6
1 0 0
Other algorithms
Example 2: It may not even lead to a feasible solution. For example ck = 7,8,9
Cashier’s algorithm: 15 = 9 + ??? The optimal: 15 = 7+8.
15
8 7 6
Other algorithms
Example 2: It may not even lead to a feasible solution. For example ck = 7,8,9
Cashier’s algorithm: 15 = 9 + ??? The optimal: 15 = 7+8.
15
8 7 6
1
Other algorithms
Example 2: It may not even lead to a feasible solution. For example ck = 7,8,9
Cashier’s algorithm: 15 = 9 + ??? The optimal: 15 = 7+8.
15
8 7 6
1 0
Other algorithms
Example 2: It may not even lead to a feasible solution. For example ck = 7,8,9
Cashier’s algorithm: 15 = 9 + ??? The optimal: 15 = 7+8.
15
8 7 6
1 0 0
We will learn all the related algorithms in the dynamic programming chapter.
Another Problem:
A ctivity S c h ed ulin g
Problem statement
Given n activities with their start and finish times. Select the
maximum number of activities that can be performed by a single
person, assuming that a person can only work on a single
activity at a time.
Activity Scheduling
3 4 5 6 7 8 9 10 11 12 1
S k yd ivin g B o n fire
G a rd en in g Fa n cy D in n er
E ven in g H ik e
Activity Scheduling: Not feasible
3 4 5 6 7 8 9 10 11 12 1
S k yd ivin g B o n fire
G a rd en in g Fa n cy D in n er
E ven in g H ik e
Activity Scheduling
3 4 5 6 7 8 9 10 11 12 1
S k yd ivin g B o n fire
G a rd en in g Fa n cy D in n er
E ven in g H ik e
Activity Scheduling
3 4 5 6 7 8 9 10 11 12 1
S k yd ivin g B o n fire
G a rd en in g Fa n cy D in n er
E ven in g H ik e
Greedy algorithms
S k yd ivin g B o n fire
G a rd en in g Fa n cy D in n er
E ven in g H ik e
Earliest start
3 4 5 6 7 8 9 10 11 12 1
S k yd ivin g B o n fire
G a rd en in g Fa n cy D in n er
E ven in g H ik e
Earliest start
3 4 5 6 7 8 9 10 11 12 1
S k yd ivin g B o n fire
G a rd en in g Fa n cy D in n er
E ven in g H ik e
Earliest start
3 4 5 6 7 8 9 10 11 12 1
B o n fire
Fa n cy D in n er
B a r C r a wling
Earliest start
3 4 5 6 7 8 9 10 11 12 1
B o n fire
Fa n cy D in n er
B a r C r a wling
Earliest start
3 4 5 6 7 8 9 10 11 12 1
L la m a H u g g in g N ig h t S n ork elin g
B o n fire
L la m a H u g g in g N ig h t S n ork elin g
B o n fire
L la m a H u g g in g
B o n fire
N a vel G a zin g
Shortest first
3 4 5 6 7 8 9 10 11 12 1
S k yd ivin g B o n fire
G a rd en in g Fa n cy D in n er
E ven in g H ik e
Shortest first
3 4 5 6 7 8 9 10 11 12 1
S k yd ivin g B o n fire
G a rd en in g Fa n cy D in n er
E ven in g H ik e
Shortest first
3 4 5 6 7 8 9 10 11 12 1
B o n fire
G a rd en in g Fa n cy D in n er
B a r C r a wling
Shortest first
3 4 5 6 7 8 9 10 11 12 1
B o n fire
G a rd en in g Fa n cy D in n er
B a r C r a wling
Shortest first
3 4 5 6 7 8 9 10 11 12 1
G a rd en in g Fa n cy D in n er
Greedy algorithms
10 mins
Finish fast
3 4 5 6 7 8 9 10 11 12 1
S k yd ivin g B o n fire
G a rd en in g Fa n cy D in n er
E ven in g H ik e
Fewest conflict
3 4 5 6 7 8 9 10 11 12 1
S k yd ivin g B o n fire
G a rd en in g Fa n cy D in n er
E ven in g H ik e
Greedy algorithms
• Interval partitioning.
– Lecture j starts at sj and finishes at fj.
– Goal: find minimum number of classrooms to schedule all lectures so that no two
occur at the same time in the same room.
e j
c d g
b h
a f i
• Interval partitioning.
– Lecture j starts at sj and finishes at fj.
– Goal: find minimum number of classrooms to schedule all lectures so that no two
occur at the same time in the same room.
c d f j
b g i
a e h
FOR j = 1 TO n
IF Lecture j is compatible with some classroom
Schedule lecture j in any such classroom k.
ELSE
Allocate a new classroom d + 1.
Schedule lecture j in classroom d + 1.
d←d +1
RETURN schedule.
Interval partitioning: earliest start time
e j
c d g
b h
a f i
e j
c d g
b h
a f i
e j
c d g
b h
a f i
e j
c d g
b h
a f i
e j
c d g
b h
a f i
a d
e j
c d g
b h
a f i
c e
a d
e j
c d g
b h
a f i
c e
a d f
e j
c d g
b h
a f i
c e
b g
a d f
e j
c d g
b h
a f i
c e h
b g
a d f
e j
c d g
b h
a f i
c e h
b g
a d f i
e j
c d g
b h
a f i
c e h
b g j
a d f i
Pf. Store classrooms in a priority queue (key = finish time of its last lecture).
・To determine whether lecture j is compatible with some classroom,
compare sj to key of min classroom k in priority queue.
・ To add lecture j to classroom k, increase key of classroom k to fj.
・Total number of priority queue operations is O(n).
・Sorting by start time takes O(n log n) time.
0 1 2 3 4 5 6 7 8 9 10
Frog Jumping
0 1 2 3 4 5 6 7 8 9 10
Frog Jumping
0 1 2 3 4 5 6 7 8 9 10
M a x ju m p s ize: 3
Frog Jumping
0 1 2 3 4 5 6 7 8 9 10
M a x ju m p s ize: 3
Frog Jumping
0 1 2 3 4 5 6 7 8 9 10
M a x ju m p s ize: 3
Frog Jumping
0 1 2 3 4 5 6 7 8 9 10
M a x ju m p s ize: 3
As a graph
0 1 2 3 4 5 6 7 8 9 10
M a x ju m p s ize: 3
A leap of faith
0 1 2 3 4 5 6 7 8 9 10
M a x ju m p s ize: 2
Algorithm: Always
jump as far forward
as possible.
A leap of faith
0 1 2 3 4 5 6 7 8 9 10
M a x ju m p s ize: 4
Algorithm: Always
jump as far forward
as possible.