GREEDY ALGORITHMS

undefined
GREEDY ALGORITHMS
David Kauchak
CS 140 – Spring 2024
Admin
Assignment 6
ChatGPT
Collaboration
A problem
Input:  a number k
Output: {n
p
, n
n
, n
d
, n
q
}, where n
p
+5n
n
+10n
d
+25n
q
=k
and n
p
+n
n
+n
d
+n
q
 is minimized
What is this problem?
How would you state it in English?
Making change!
Input:  a number k
Output: {n
p
, n
n
, n
d
, n
q
}, where n
p
+5n
n
+10n
d
+25n
q
=k
and n
p
+n
n
+n
d
+n
q
 is minimized
Provide (U.S.) coins that sum up to 
k
 such
that we minimize the number of coins
Making change!
Input:  a number k
Output: {n
p
, n
n
, n
d
, n
q
}, where n
p
+5n
n
+10n
d
+25n
q
=k
and n
p
+n
n
+n
d
+n
q
 is minimized
Algorithm to solve it?
Making change!
Input:  a number k
Output: {n
p
, n
n
, n
d
, n
q
}, where n
p
+5n
n
+10n
d
+25n
q
=k
and n
p
+n
n
+n
d
+n
q
 is minimized
pick as many quarters as we can 
recurse
Algorithms vs heuristics
What is the difference between an algorithm and a
heuristic?
 
Algorithm: a set of steps for arriving at the 
correct
solution
Heuristic: a set of steps that will arrive at some
solution
Making change!
Algorithm or heuristic?
 
Need to prove its correct!
pick as many quarters as we can 
recurse
Greedy algorithms
 
What is a greedy algorithm?
 
Algorithm that makes a local decision with the goal of creating a
globally optimal solution
 
Method for solving problems where optimal solutions can be
defined in terms of optimal solutions to sub-problems
 
What does this mean?  Where have we seen this before?
Divide and conquer
Divide and conquer
To solve the general problem:
Break into sum number of sub problems, solve:
then possibly do a little work
Divide and conquer
Divide and conquer
To solve the general problem:
The solution to the general problem is solved with
respect to solutions to sub-problems!
Greedy vs. divide and conquer
Greedy
To solve the general problem:
Pick a locally optimal solution and repeat
Greedy vs. divide and conquer
Greedy
To solve the general problem:
The solution to the general problem is solved with respect to
solutions to sub-problems!
Slightly different than divide and conquer
undefined
Greedy vs. DP
greedy
dynamic 
programming
Need to solve (recurse on) subproblems to figure out optimal answer
Only recurse
on one
subproblem
Proving correctness:
greedy choice property
The greedy choice results in an optimal solution
Greedy choice property: The greedy choice
is contained within some optimal solution
Making change!
solution: individual coins selected
pick as many quarters as we can 
recurse
Optimal substructure
 
We can combine a greedy choice with the
optimal solution for the remaining problem
and get a solution to the general problem
Optimal substructure
Proof by contradiction:
 
What does that imply?
Optimal substructure
Proof by contradiction:
 
Any problem contradiction?
Proof by contradiction:
Optimal substructure
Optimal substructure
We can make greedy decisions
Proving greedy choice property
Option 1: proof by contradiction
Assume you have an optimal solution to the problem
Sometimes you have to think about it ordered/arranged a
particular way
Assume that somewhere along the way the solution contains
a decision that is 
different
 than your greedy algorithm
Argue this results in a contradiction, i.e., that the solution
you’re considering is not optimal
Greedy choice property
Proof by contradiction:
 
Any problem contradiction?
Greedy choice property
Proof by contradiction:
g
i
 > c
i
.  We need at least one more lower
denomination coin because g
i
 can be made up of c
i
and one or more of the other denominations
but that would mean that the solution is longer than
the greedy!
Greedy choice property
g
i
 > c
i
g
i
 = 5
c
i
 = 1
there are at least 4 other pennies
could always replace 5 pennies with a nickel to
create shorter solution!
Greedy choice property
g
i
 > c
i
g
i
 = 10
c
i
 = 5
there are at least 2 nickels (assuming we’ve dealt
with pennies first)
could always replace those coins with a dime to
create a shorter solution
Greedy choice property
g
i
 > c
i
g
i
 = 25
r = 
remaining sum
coins(r – 25)
: number of coins to get remaining sum - 25
c
i
 = 10: 10 + 10 + 5 + coins(r-25)
c
i
 = 5: 5 + 5 + 5 + 5 +5 + coins(r-25)
The greedy solution will always be better
Greedy choice property fails
Coins: 9, 4, 1
What’s the best way to make 12?
Greedy choice property fails
Coins: 9, 4, 1
g
i
 > c
i
g
i
 = 9
r = 
remaining sum
coins(r – 9)
: number of coins to get remaining sum - 9
c
i
 = 4: 4 + coins(r-4)
There is no way to guarantee that we would have to use the same
set of coins are coins(r-9)
Interval scheduling
Given 
n
 activities A = [a
1
,a
2
, .., a
n
] where each activity
has start time s
i
 and a finish time f
i
.  Schedule as many
as possible of these activities such that they don’
t conflict.
Interval scheduling
Given 
n
 activities A = [a
1
,a
2
, .., a
n
] where each activity
has start time s
i
 and a finish time f
i
.  Schedule as many
as possible of these activities such that they don’
t conflict.
Which activities conflict?
Interval scheduling
Which activities conflict?
Given 
n
 activities A = [a
1
,a
2
, .., a
n
] where each activity
has start time s
i
 and a finish time f
i
.  Schedule 
as many
as possible 
of these activities such that they 
don’
t conflict
.
Simple recursive solution
Enumerate all possible solutions and find which
schedules the most activities
Simple recursive solution
 
Is it correct?
max{all possible solutions}
Running time?
O(n!)
Can we do better?
 
Dynamic programming
O(n
2
)
Greedy solution – Is there a way to repeatedly make local
decisions?
Key: we’d still like to end up with the 
optimal
 solution
Overview of a greedy approach
Greedily pick an activity to schedule
Add that activity to the answer
Remove that activity and all conflicting activities.  Call this A
.
Repeat on A
 until A
 is empty
Greedy options
Greedy options
Select the activity that starts the earliest, i.e.
argmin{s
1
, s
2
, s
3
, …, s
n
}?
Greedy options
non-optimal
Select the activity that starts the earliest, i.e.
argmin{s
1
, s
2
, s
3
, …, s
n
}?
Greedy options
Select the shortest activity, i.e.
argmin{f
1
-s
1
, f
2
-s
2
, f
3
-s
3
, …, f
n
-s
n
}
Greedy options
Select the shortest activity, i.e.
argmin{f
1
-s
1
, f
2
-s
2
, f
3
-s
3
, …, f
n
-s
n
}
non-optimal
Greedy options
Select the activity with the smallest number of conflicts
Greedy options
Select the activity with the smallest number of conflicts
Greedy options
Select the activity with the smallest number of conflicts
Greedy options
Select the activity that ends the earliest, i.e.
argmin{f
1
, f
2
, f
3
, …, f
n
}?
Greedy options
Select the activity that ends the earliest, i.e.
argmin{f
1
, f
2
, f
3
, …, f
n
}?
remove the conflicts
Greedy options
Select the activity that ends the earliest, i.e.
argmin{f
1
, f
2
, f
3
, …, f
n
}?
Greedy options
Select the activity that ends the earliest, i.e.
argmin{f
1
, f
2
, f
3
, …, f
n
}?
Greedy options
Select the activity that ends the earliest, i.e.
argmin{f
1
, f
2
, f
3
, …, f
n
}?
remove the conflicts
Greedy options
Select the activity that ends the earliest, i.e.
argmin{f
1
, f
2
, f
3
, …, f
n
}?
Greedy options
Select the activity that ends the earliest, i.e.
argmin{f
1
, f
2
, f
3
, …, f
n
}?
Greedy options
Select the activity that ends the earliest, i.e.
argmin{f
1
, f
2
, f
3
, …, f
n
}?
Greedy options
Select the activity that ends the earliest, i.e.
argmin{f
1
, f
2
, f
3
, …, f
n
}?
Greedy options
Select the activity that ends the earliest, i.e.
argmin{f
1
, f
2
, f
3
, …, f
n
}?
Multiple optimal
solutions
Greedy options
Select the activity that ends the earliest, i.e.
argmin{f
1
, f
2
, f
3
, …, f
n
}?
Greedy options
Select the activity that ends the earliest, i.e.
argmin{f
1
, f
2
, f
3
, …, f
n
}?
Efficient greedy algorithm
 
Once you’
ve identified a reasonable greedy heuristic:
Prove that it always gives the correct answer
Develop an efficient solution
Is our greedy approach correct?
Option 1: proof by contradiction
Option 2:
Stays ahead
 argument
:
show that no matter what other solution someone provides
you, the solution provided by your algorithm always 
stays
ahead
, in that no other choice could do better
Is our greedy approach correct?
Stays ahead
 argument
Let r
1
, r
2
, r
3
, …, r
k
 be the solution found by our approach
Let o
1
, o
2
, o
3
, …, o
k
 be another optimal solution
Show our approach 
stays ahead
 of any other solution
Stays ahead
Compare first activities of each solution
what do we know?
Stays ahead
finish(r
1
) 
≤ finish(o
1
)
what does this imply?
Stays ahead
r
2
r
3
r
k
o
2
o
3
o
k
W
e
 
h
a
v
e
 
a
t
 
l
e
a
s
t
 
a
s
 
m
u
c
h
 
t
i
m
e
a
s
 
a
n
y
 
o
t
h
e
r
 
s
o
l
u
t
i
o
n
 
t
o
 
s
c
h
e
d
u
l
e
t
h
e
 
r
e
m
a
i
n
i
n
g
 
2
k
 
t
a
s
k
s
Stays ahead
r
2
r
3
r
k
o
2
o
3
o
k
W
e
 
h
a
v
e
 
a
t
 
l
e
a
s
t
 
a
s
 
m
u
c
h
 
t
i
m
e
a
s
 
a
n
y
 
o
t
h
e
r
 
s
o
l
u
t
i
o
n
 
t
o
 
s
c
h
e
d
u
l
e
t
h
e
 
r
e
m
a
i
n
i
n
g
 
2
k
 
t
a
s
k
s
What kind of proof is this?
An efficient solution
Running time?
 
Θ
(n log n)
 
Θ
(n)
 
Overall: 
Θ
(n log n)
 
Better than:
O(n!)
O(n
2
)
Scheduling 
all
 intervals
Given 
n
 activities, we need to schedule 
all
 activities.
Goal:
 minimize the number of resources required.
Greedy approach?
 
The best we could ever do is the maximum
number of conflicts for any time period
Calculating max conflicts efficiently
Calculating max conflicts efficiently
3
Calculating max conflicts efficiently
1
Calculating max conflicts efficiently
3
Calculating max conflicts efficiently
1
Calculating max conflicts efficiently
Calculating max conflicts
Correctness?
 
We can do no better then the max number of conflicts.
This exactly counts the max number of conflicts.
Runtime?
 
O(2n log 2n + n) = 
O(n log n)
Knapsack problems:
Greedy or not?
0-1 Knapsack
 – A thief robbing a store finds n items worth v
1
,
v
2
, .., v
n
 dollars and weight w
1
, w
2
, …, w
n
 pounds, where v
i
 and
w
i
 are integers.  The thief can carry at most W pounds in the
knapsack.  Which items should the thief take if he wants to
maximize value.
Fractional knapsack problem
 – Same as above, but the thief
happens to be at the bulk section of the store and can carry
fractional portions of the items.  For example, the thief could
take 20% of item i for a weight of 0.2w
i
 and a value of 0.2v
i
.
undefined
Handout
undefined
Here are some options for greedy algorithms. Do they work?
Can you come up with counterexamples?
Starts earliest
Least number of conflicts
Shortest
undefined
Knapsack problems:  Greedy or not?
0-1 Knapsack
 – A thief robbing a store finds n items worth v
1
, v
2
, .., v
n
 dollars
and weight w
1
, w
2
, …, w
n
 pounds, where v
i
 and w
i
 are integers.  The thief can
carry at most W pounds in the knapsack.  Which items should the thief take if
he wants to maximize value.
Fractional knapsack problem
 – Same as above, but the thief happens to be
at the bulk section of the store and can carry fractional portions of the items.
For example, the thief could take 20% of item i for a weight of 0.2w
i
 and a
value of 0.2v
i
.
Slide Note
Embed
Share

Exploring the concept of greedy algorithms through examples like making change and problem-solving. Understand how local decisions lead to globally optimal solutions. Dive into the principles of algorithms versus heuristics and the divide-and-conquer approach. Discover practical applications in problem-solving and optimization.

  • Greedy Algorithms
  • Problem-Solving
  • Optimization
  • Divide and Conquer
  • Heuristics

Uploaded on Feb 24, 2025 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. GREEDY ALGORITHMS David Kauchak CS 140 Spring 2024

  2. Admin Assignment 6 ChatGPT Collaboration

  3. A problem Input: a number k Output: {np, nn, nd, nq}, where np+5nn+10nd+25nq=k and np+nn+nd+nqis minimized What is this problem? How would you state it in English?

  4. Making change! Input: a number k Output: {np, nn, nd, nq}, where np+5nn+10nd+25nq=k and np+nn+nd+nq is minimized Provide (U.S.) coins that sum up to k such that we minimize the number of coins

  5. Making change! Input: a number k Output: {np, nn, nd, nq}, where np+5nn+10nd+25nq=k and np+nn+nd+nq is minimized Algorithm to solve it?

  6. Making change! Input: a number k Output: {np, nn, nd, nq}, where np+5nn+10nd+25nq=k and np+nn+nd+nq is minimized pick as many quarters as we can ??= ? / 25 Solve: ??+ 5??+ 10??=k 25 ? / 25 recurse

  7. Algorithms vs heuristics What is the difference between an algorithm and a heuristic? Algorithm: a set of steps for arriving at the correct solution Heuristic: a set of steps that will arrive at some solution

  8. Making change! pick as many quarters as we can ??= ? / 25 Solve: ??+ 5?? + 10?? =k 25 ? / 25 recurse Algorithm or heuristic? Need to prove its correct!

  9. Greedy algorithms What is a greedy algorithm? Algorithm that makes a local decision with the goal of creating a globally optimal solution Method for solving problems where optimal solutions can be defined in terms of optimal solutions to sub-problems What does this mean? Where have we seen this before?

  10. Divide and conquer Divide and conquer To solve the general problem: Break into sum number of sub problems, solve: then possibly do a little work

  11. Divide and conquer Divide and conquer To solve the general problem: The solution to the general problem is solved with respect to solutions to sub-problems!

  12. Greedy vs. divide and conquer Greedy To solve the general problem: Pick a locally optimal solution and repeat

  13. Greedy vs. divide and conquer Greedy To solve the general problem: The solution to the general problem is solved with respect to solutions to sub-problems! Slightly different than divide and conquer

  14. Greedy vs. DP greedy Only recurse on one subproblem dynamic programming Need to solve (recurse on) subproblems to figure out optimal answer

  15. Proving correctness: greedy choice property Greedy choice property: The greedy choice is contained within some optimal solution The greedy choice results in an optimal solution

  16. Making change! pick as many quarters as we can ??= ? / 25 Solve: ??+ 5?? + 10?? =k 25 ? / 25 recurse {?1,?2,?3, ,??} solution: individual coins selected

  17. Proving greedy choice property Option 1: proof by contradiction Assume you have an optimal solution to the problem Sometimes you have to think about it ordered/arranged a particular way Assume that somewhere along the way the solution contains a decision that is different than your greedy algorithm Argue this results in a contradiction, i.e., that the solution you re considering is not optimal

  18. Greedy choice property Proof by contradiction: Let {?1,?2,?3, ,??} be an optimal solution Assume it is ordered from largest to smallest Assume that it does not make the greedy choice at some coin ?? ?1,?2,?3, ,??, ,?? ?1 ?2,?3, ,??, ,?? Any problem contradiction?

  19. Greedy choice property Proof by contradiction: ?1,?2,?3, ,??, ,?? ?1 ?2,?3, ,??, ,?? gi > ci. We need at least one more lower denomination coin because gi can be made up of ci and one or more of the other denominations but that would mean that the solution is longer than the greedy!

  20. Greedy choice property gi > ci gi = 5 ci = 1 there are at least 4 other pennies could always replace 5 pennies with a nickel to create shorter solution!

  21. Greedy choice property gi > ci gi = 10 ci = 5 there are at least 2 nickels (assuming we ve dealt with pennies first) could always replace those coins with a dime to create a shorter solution

  22. Greedy choice property gi > ci gi = 25 r = remaining sum coins(r 25): number of coins to get remaining sum - 25 ci = 10: 10 + 10 + 5 + coins(r-25) ci = 5: 5 + 5 + 5 + 5 +5 + coins(r-25) The greedy solution will always be better

  23. Greedy choice property fails Coins: 9, 4, 1 What s the best way to make 12?

  24. Greedy choice property fails Coins: 9, 4, 1 gi > ci gi = 9 r = remaining sum coins(r 9): number of coins to get remaining sum - 9 ci = 4: 4 + coins(r-4) There is no way to guarantee that we would have to use the same set of coins are coins(r-9)

  25. Interval scheduling Given n activities A = [a1,a2, .., an] where each activity has start time si and a finish time fi. Schedule as many as possible of these activities such that they don t conflict.

  26. Interval scheduling Given n activities A = [a1,a2, .., an] where each activity has start time si and a finish time fi. Schedule as many as possible of these activities such that they don t conflict. Which activities conflict?

  27. Interval scheduling Given n activities A = [a1,a2, .., an] where each activity has start time si and a finish time fi. Schedule as many as possible of these activities such that they don t conflict. Which activities conflict?

  28. Simple recursive solution Enumerate all possible solutions and find which schedules the most activities

  29. Simple recursive solution Is it correct? max{all possible solutions} Running time? O(n!)

  30. Can we do better? Dynamic programming O(n2) Greedy solution Is there a way to repeatedly make local decisions? Key: we d still like to end up with the optimal solution

  31. Overview of a greedy approach Greedily pick an activity to schedule Add that activity to the answer Remove that activity and all conflicting activities. Call this A . Repeat on A until A is empty

  32. Greedy options

  33. Greedy options Select the activity that starts the earliest, i.e. argmin{s1, s2, s3, , sn}?

  34. Greedy options Select the activity that starts the earliest, i.e. argmin{s1, s2, s3, , sn}? non-optimal

  35. Greedy options Select the shortest activity, i.e. argmin{f1-s1, f2-s2, f3-s3, , fn-sn}

  36. Greedy options Select the shortest activity, i.e. argmin{f1-s1, f2-s2, f3-s3, , fn-sn} non-optimal

  37. Greedy options Select the activity with the smallest number of conflicts

  38. Greedy options Select the activity with the smallest number of conflicts

  39. Greedy options Select the activity with the smallest number of conflicts

  40. Greedy options Select the activity that ends the earliest, i.e. argmin{f1, f2, f3, , fn}?

  41. Greedy options Select the activity that ends the earliest, i.e. argmin{f1, f2, f3, , fn}? remove the conflicts

  42. Greedy options Select the activity that ends the earliest, i.e. argmin{f1, f2, f3, , fn}?

  43. Greedy options Select the activity that ends the earliest, i.e. argmin{f1, f2, f3, , fn}?

  44. Greedy options Select the activity that ends the earliest, i.e. argmin{f1, f2, f3, , fn}? remove the conflicts

  45. Greedy options Select the activity that ends the earliest, i.e. argmin{f1, f2, f3, , fn}?

  46. Greedy options Select the activity that ends the earliest, i.e. argmin{f1, f2, f3, , fn}?

  47. Greedy options Select the activity that ends the earliest, i.e. argmin{f1, f2, f3, , fn}?

  48. Greedy options Select the activity that ends the earliest, i.e. argmin{f1, f2, f3, , fn}?

  49. Greedy options Select the activity that ends the earliest, i.e. argmin{f1, f2, f3, , fn}? Multiple optimal solutions

  50. Greedy options Select the activity that ends the earliest, i.e. argmin{f1, f2, f3, , fn}?

More Related Content

giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#