0% found this document useful (0 votes)
4 views

Lec 5 Inclass

The document discusses greedy algorithms, specifically in the context of making change and the activity-selection problem. It outlines the greedy strategy for minimizing the number of coins used to make change and provides an optimal greedy algorithm for selecting non-overlapping activities. Key elements of greedy algorithms, such as optimal substructure and the greedy-choice property, are also highlighted.

Uploaded by

voxod41758
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Lec 5 Inclass

The document discusses greedy algorithms, specifically in the context of making change and the activity-selection problem. It outlines the greedy strategy for minimizing the number of coins used to make change and provides an optimal greedy algorithm for selecting non-overlapping activities. Key elements of greedy algorithms, such as optimal substructure and the greedy-choice property, are also highlighted.

Uploaded by

voxod41758
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

CSCE 411: Design and Analysis of Algorithms

Lecture 5: Greedy Algorithms


Date: January 30, 2025 Nate Veldt, updated by Samson Zhou

Course Logistics

Greedy algorithms: Chapter 16

Homework 2 is due this Friday (tomorrow)

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)?

Strategy for giving change:

Choose coins one at a time

At each step, choose the coin


thatbringsustheclosest
to thetotalwithoutgoing over

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

v1 > v2 > v3 > · · · > vn = 1,

find the number of coins mi for i = 1, 2, . . . n so that

a
h
Emiri
i iitiiiiiiiii
m is minimized
i
1.2 The greedy algorithm for change-making

GreedyCoinChange(C, v = [v1 , v2 , . . . , vn = 1])


n = length(v)
Let m = [0..0] be an empty array of n zeros
for i = 1 to n do iteratethroughcointypes

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.

Is the greedy algorithm optimal?

Question 1. Which of the following do you think is true?

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

make a locallyoptimal choice


at eachstep

Elements of greedy algorithms. There are two key elements we look for when
applying a greedy algorithm:

1. Optimal substructure: same as DPanddivide andconquer


2. Greedy-choice property: a globally optimal solution can be achieved by

making locally optimal choices


Overall strategy for using greedy algorithms

1. Determine a notion of the


best local choice
the global optimization problem.
that improves

2. Show how making one local choice leads to


a smaller subproblem
3. Prove that the greedy choice is
safe : it leaves us with a subproblem
which, when solves optimally and combined with the first greedy choice,

givestheoptimalsolutiontothe original problem

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.

will work to consider all possible


Ak andthen solve 2 subproblems
memorizing

Butthis is not optimal

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.

a1 has a finish time f1  fi .

This ai overlaps with a1 , otherwise we could add a1 to make S better

If we replace ai with a1 to get S 0 , then |S| = |S 0 |, and this is a valid non-overlapping


solution, because ai was the earliest activity in S.

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

that picks the earliest finishing


activityaining
our strategy must be valid
7
3.3 An Optimal Greedy Algorithm for the Activity-Subset Problem

GreedyActivitySelection(s = (s1 , s2 , . . . , sn ), f = (f1 , f2 , . . . , fn ))


n = length(s)
S = {a1 }
alwaysthefinishtimeforlastaddedactivity
k=1
fic is
for i = 2 to n do
consideractivities thathappen
afterlastfinishtime
if si fk then only
S = S [ {ai }
whenpossible addthe next
earliestfinishing activity
update K
k=i

end if
end for
Return S

iteratingbasedonendtimes
r tittatgta.to
1 2 4
3
a as 94 96

You might also like