0% found this document useful (0 votes)
2 views18 pages

Lesson25 Greedy

The document provides an introduction to greedy algorithms, illustrating their core principle of making locally optimal choices with the hope of achieving a global optimum. It includes examples such as Tony's ice cream adventure, Amy's weekend activities, and various problem scenarios like the Pirate's Gold Dilemma and cutting a wooden board. Each example demonstrates the strengths and limitations of greedy approaches in problem-solving.

Uploaded by

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

Lesson25 Greedy

The document provides an introduction to greedy algorithms, illustrating their core principle of making locally optimal choices with the hope of achieving a global optimum. It includes examples such as Tony's ice cream adventure, Amy's weekend activities, and various problem scenarios like the Pirate's Gold Dilemma and cutting a wooden board. Each example demonstrates the strengths and limitations of greedy approaches in problem-solving.

Uploaded by

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

Greedy Algorithms :

Eat Cake First, Ask Questions


Later!
Introduction to
Greedy Algorithms
Definition and Analogy

Imagine you’re at an all-you-can-eat buffet. Do


you…

A) at the best food first (greedy!)


B) Save room for dessert (patient, but not
Choosing the best food first is like a
greedy).
greedy algorithm, focusing on
immediate satisfaction.
This approach mirrors how greedy algorithms
make locally optimal choices without considering
the bigger picture.
Core Principle

Greedy Choice Property


The greedy choice property means making the best
choice at each step, hoping it leads to a global
optimum.
However, this doesn't always guarantee the best
overall solution, as future consequences aren't
considered.
Local vs Global Optimum
In greedy algorithms, local optimum is pursued with
the hope that it will result in the global optimum.
But sometimes, short- term gains can lead to
suboptimal long- term outcomes.
Examples of
Greedy Algorithms
The Ice Cream Adventure

Tony buys 1 ice-cream with


$1. The cashier says,
"Sorry, kiddo! No change.
But here’s a deal: Find the
fewest coins to make $0.75,
and
0.5 I’ll
0.25give0.1
you 0.05
a free 0.01
sprinkles upgrade!"
The Ice Cream Adventure

Greedy Mission:

First Choice: Tony grabs the biggest coin—


$0.50. Why? "Biggest = fewest coins!“

Remaining: $0.75− $0.50 = $0.25 left.

Second Choice: He spots a $0.25 coin.


“Jackpot!”

Remaining: $0.25− $0.25 = $0.00!


The Ice Cream Adventure

But
The cashier laughs, "What if the coins were
$0.50, $0.30, $0.05? Would your greedy
strategy still works?"
Greedy works when the
biggest/best choice doesn’t block
better future choices!
Amy's Weekend Adventure

It’s Saturday!
Amy has 4 hours
Activity List (with start/end times and "fun/skill score"):
to explore 9:00-10:30 AM: Pottery (7/10) – sculpting cute mugs.
10:00-11:30 AM: Coding (9/10) – build a robot!
activities. She 11:00 AM-12:30 PM: Dance (8/10) – learn K-pop moves.
1:00-2:30 PM: Painting (6/10) – watercolor flowers.
wants to 2:00-3:30 PM: Cooking (8/10) – bake cookies!

maximize
fun/skills learned.
Amy's Weekend Adventure

Greedy Goal: Pick activities to maximize total


fun/skill score without overlapping.
Amy’s Choices:
First Pick: Coding (9/10) at 10 AM. "Highest
score!"
Next Pick: Dance (8/10) at 11 AM. "But
wait—ends at 12:30 PM. Still time after?"

Greedy Adjustment: Skip Dance. Choose Cooking (8/10) instead.

Final Schedule:
10-11:30 AM: Coding (9)
2-3:30 PM: Cooking (8)
The Pirate's Gold Dilemma

Characters: Five pirates—Blackbeard, Red Ruth, Silver


Sam, Blue Belle, and Green George—each with a
unique greed level.

Scenario: They stole 100 gold coins! Now, they must


split the loot using pirate rules:
• The most senior pirate (Blackbeard) proposes a
split.
• If >50% agree (including proposer), the coins are
The Pirate's Gold Dilemma

The Pirates' Personalities:


• Blackbeard (Captain): Ruthless, values power over
coins.
• Red Ruth: Greedy, wants maximum coins.
• Silver Sam: Strategic, fears betrayal.
• Blue Belle: Fair, prefers stability.
• Green George: Naive, easily swayed.
The Pirate's Gold Dilemma

What would Blackbeard propose?


A. keeps 2 for himself, and the rest split 98
B. Average score for all, 20 for each person
C. One for the second person, one for the
fourth person, and the rest for yourself
D. One for the third person, one for the fifth
person, and the rest for themselves
E. keeps 80 for himself and splits 20 between
The Pirate's Gold Dilemma

Your solution !
Section

Problem Description:
Given a sequence of N positive integers Ai, we
need to divide it into consecutive segments such
that the sum of each segment does not exceed M
(and can equal M). Determine the minimum Sample Input
number of segments required. 56
42451
Input Format:
The first line contains two integers N (length of the Sample Output
sequence) and M (maximum segment sum). The 3
second line lists N space-separated non-negative
integers Ai .

Output Format:
Print a single integer representing the minimum
number of segments.
Clumsy Cows S
Problem Description:
Bessie the cow is trying to type a balanced string of parentheses into her new laptop,
but she is sufficiently clumsy (due to her large hooves) that she keeps mis-typing
characters. Please help her by computing the minimum number of characters in the
string that one must reverse (e.g., changing a left parenthesis to a right parenthesis,
or vice versa) so that the string would become balanced.
There are several ways to define what it means for a string of parentheses to be
"balanced". Perhaps the simplest definition is that there must be the same total
number of ('s and )'s, and for any prefix of the string, there must be at least as many
('s as )'s. For example, the following strings are all balanced:
() (()) ()(()())
while these are not:
)( ())( ((())))

Sample
Given a sequence of parentheses of even length, ask the minimum Input
number of
())(
parentheses that can be modified to make it balanced.
Sample Output
Input Format: 2
1 line: A string of parentheses of even length at most 100,000 characters.

Output Format:
Poker Hands

Problem Description:
Bessie and her friends are playing a unique version of poker involving a deck
with N (1 <= N <= 100,000) different ranks, conveniently numbered 1..N (a
normal deck has N = 13). In this game, there is only one type of hand the
cows can play: one may choose a card labeled i and a card labeled j and play
one card of every value from i to j. This type of hand is called a "straight".

Bessie's hand currently holds a_i cards of rank i (0 <= a_i <= 100000). Help
her find the minimum number of hands she must play Sample
to getInput
rid of all her
cards. 5
2
4
Input Format:
1
Line 1: The integer N. 2
Lines 2..1+N: Line i+1 contains the value of a_i. 3

Output Format: Sample Output


6
Line 1: The minimum number of straights Bessie
must play to get rid of all her cards.
Rectangle

Problem Description:
We need to cut an N×M wooden board into 1×1 small squares.
For a wooden board, we can only cut along a horizontal or vertical line (along the grid
lines). Moreover, the board is not uniform, and cutting along different lines incurs
different costs. Additionally, after cutting a board once, it is divided into two pieces,
and we cannot reassemble these two pieces and cut them into four with a single cut.
Instead, we must cut each of the two pieces separately again.
Now, given the costs of cutting along different lines, find the minimum cost required
to divide the entire board into 1×1 small squares.

Input Format:
Line 1: contains N and M, representing the dimensions of the matrix (length
N and width M). Sample Input
Line 2: contains N−1 non-negative integers, representing 2the
2
costs of cutting along the N−1 horizontal lines. 3
3
Line 3: contains M−1 non-negative integers, representing the
costs of cutting along the M−1 vertical lines. Sample Output
9
Output Format:

You might also like