Lec 5 Inclass
Lec 5 Inclass
Course Logistics
1 Making Change
Say a cashier has to give someone 68 cents in change to a customer for a purchase.
What is the smallest number of coins the cashier can use to give this change, assuming
they can give quarters (25 cents), dimes (10 cents), nickels (5 cents) and pennies (1 cent)?
68 25 25 10 S I
left 43 18 8 3
7 coinstotal
1
1.1 The Change-Making Problem
Assume we have n coins, and let
vi = value
ofthetype i coin
mi =
number of coinsoftype i that you give
The Change-Making Problem Given an integer value C > 0 and coin values
a
h
Emiri
i iitiiiiiiiii
m is minimized
i
1.2 The greedy algorithm for change-making
Tr
while C vi do
mok.ie
m[i] =
C=
end while
C Vi
end for
Return m
2
Correctness: The greedy algorithm correctly gives change
Proof. At each step, we keep updated the amount left that we have to pay. We only
add another coin of type i if vi is less than this amount, so we never go over. We will
always reach the exact amount, because C is an integer value and we can always add
coins of values vn = 1.
18 The greedy algorithm works for some values of C when we use coin values (1,5,10,25)
(like C = 68), but is sometimes not optimal when we use these coin values
(1,5,10,25)
66 0 The greedy algorithm is always optimal for giving change for coin values (1,5,10,25),
but for other coin types v = (v1 , v2 , . . . , vn ) it may not be optimal
14 4 The greedy algorithm is always optimal for the coin change problem, regardless
of coin values, as long as vn = 1.
2 4
Any computational problem can be solved optimally by a greedy algorithm
Vine.us 8,7,1 C
1 Greedy
1tItltltlt
7 7 Opt
8
3
2 Greedy Algorithm Basics
A greedy algorithm is an algorithm that
througha sequence
goes of steps
Elements of greedy algorithms. There are two key elements we look for when
applying a greedy algorithm:
4
3 The Activity-Selection Problem
The activity-selection problem Let (a1 , a2 , a3 , . . . an ) represent a set of activities,
where:
si =
start time of activity
fi =
finish time of activity
where si < fi for i = 1, 2, · · · n. We assume a half-open interval [si , fi ) for the activity
ai . Assume these activities are ordered by finish times to that so that
f cf2cfzc fn
The activity-selection problem
to find list of activities
a
i 1 2 3 4 5 that maximizes the
Example: si 1 3 2 4 7 number of non overlapping
fi 4 5 6 8 9 activities
s m end
End end
we
5
3.1 Optimal Substructure
Consider an activity-selection problem defined by (a1 , a2 , . . . , an ) and the time interval
[s1 , fn ). If we know that an optimal solution contains activity ak . Then:
anoptimal solutioncanbefoundbycombining ak
with optimalsolutionsthatfinish before 5k
andoptimalsolutions that start after fk
everythingthatffhes
ic.hr mi
A dynamic programming approach: Try each ak , solving two smaller subproblems
in each case, and taking the best answer.
6
3.2 A greedy choice lemma
Lemma 3.1. There exists an optimal solution to the activity-selection problem defined
by (a1 , a2 , . . . , an ) that contains a1 (the activity with the earliest finish time).
Proof.
contradiction Aita or else already done
by Let S be an optimal solution, and let ai be its activity with the earliest start and
end time.
setII
with and
I a
4 replace
can ai
ineraldInterests Sactivities
4
S
But S
MI
includes a which
contradicts our assumption
so there must be
an optimal strategy
end if
end for
Return S
iteratingbasedonendtimes
r tittatgta.to
1 2 4
3
a as 94 96