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

Introduction To Greedy Algorithms

Uploaded by

vihaan
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

Introduction To Greedy Algorithms

Uploaded by

vihaan
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Introduction to Greedy

Algorithms
https://usaco.guide/bronze/intro-greedy

CP
Initiative
joincpi.org
Greedy Algorithms
A greedy algorithm constructs a solution to the problem by always
making a choice that looks the best at the moment.

A greedy algorithm never takes back its choices, but directly constructs
the final solution. For this reason, greedy algorithms are usually very
efficient.

CP
joincpi.or
Initiative g
Greedy Algorithms
Greedy does not refer to a single algorithm, but rather a way of
thinking that is applied to problems; there's no one way to do greedy
algorithms.

Hence, we use a well-known example to help you understand the


greedy paradigm.

CP
joincpi.or
Initiative g
Coin Problem
Consider the following problem:

We have N coins of the denominations {1, 2, 5, 10, 20, 50, 100, 200},
and we need to pick the minimum number of coins that add up to some
sum, S.

CP
joincpi.or
Initiative g
Coin Problem Solution
For this problem, the greedy algorithm of “keep picking the largest coin
as long as it does not exceed S” works.

This is a greedy algorithm because we never take back our “greedy”


decision of always taking the largest coin that doesn’t exceed S.

CP
joincpi.or
Initiative g
Coin Problem Note
However, this greedy algorithm does not work for the general case, or
coins of any denominations.

A counterexample would be with the denominations {1, 3, 4} and S =


6. Taking the largest value, 4, prevents you from getting the target
sum. However, taking 3 twice achieves S = 6.

Try to figure out for yourself when this algorithm works, and why it
does.

CP
joincpi.or
Initiative g
Proof by AC
Note that not all greedy problems necessarily require mathematical
proofs of correctness. It is often sufficient to intuitively convince
yourself your algorithm is correct.

Sometimes, if the algorithm is easy enough to implement, you don't


even need to convince yourself the greedy solution is correct.

Just code it and see if it passes. Competitive programmers refer to this


as "Proof by AC," or "Proof by Accepted."

CP
joincpi.or
Initiative g
Practice
The best way to get better at greedy problems is to see a lot of greedy
ideas.

There is a list of practice problems you can try on the USACO Guide.

CP
joincpi.or
Initiative g
Example Problem
USACO - Mad Scientist

CP
joincpi.or
Initiative g
Solution Code (with Video Editorial)

CP
joincpi.or
Initiative g
Challenge Problem
USACO - Rest Stops

CP
joincpi.or
Initiative g
Challenge Problem Solution

CP
joincpi.or
Initiative g

You might also like