Greedy1.ppt

David Luebke 1 8/29/2022
CS 332: Algorithms
Greedy Algorithms
David Luebke 2 8/29/2022
Review: Dynamic Programming
● Dynamic programming is another strategy for
designing algorithms
● Use when problem breaks down into recurring
small subproblems
David Luebke 3 8/29/2022
Review: Optimal Substructure of
LCS
● Observation 1: Optimal substructure
■ A simple recursive algorithm will suffice
■ Draw sample recursion tree from c[3,4]
■ What will be the depth of the tree?
● Observation 2: Overlapping subproblems
■ Find some places where we solve the same
subproblem more than once










otherwise
])
,
1
[
],
1
,
[
max(
],
[
]
[
if
1
]
1
,
1
[
]
,
[
j
i
c
j
i
c
j
y
i
x
j
i
c
j
i
c
David Luebke 4 8/29/2022
Review: Structure of Subproblems
● For the LCS problem:
■ There are few subproblems in total
■ And many recurring instances of each
(unlike divide & conquer, where subproblems unique)
● How many distinct problems exist for the LCS
of x[1..m] and y[1..n]?
● A: mn
David Luebke 5 8/29/2022
Memoization
● Memoization is another way to deal with
overlapping subproblems
■ After computing the solution to a subproblem,
store in a table
■ Subsequent calls just do a table lookup
● Can modify recursive alg to use memoziation:
■ There are mn subproblems
■ How many times is each subproblem wanted?
■ What will be the running time for this algorithm?
The running space?
David Luebke 6 8/29/2022
Review: Dynamic Programming
● Dynamic programming: build table bottom-up
■ Same table as memoization, but instead of starting
at (m,n) and recursing down, start at (1,1)
● Least Common Subsequence: LCS easy to
calculate from LCS of prefixes
○ As your homework shows, can actually reduce space to
O(min(m,n))
● Knapsack problem: we’ll review this in a bit
David Luebke 7 8/29/2022
Review: Dynamic Programming
● Summary of the basic idea:
■ Optimal substructure: optimal solution to problem
consists of optimal solutions to subproblems
■ Overlapping subproblems: few subproblems in
total, many recurring instances of each
■ Solve bottom-up, building a table of solved
subproblems that are used to solve larger ones
● Variations:
■ “Table” could be 3-dimensional, triangular, a tree,
etc.
David Luebke 8 8/29/2022
Greedy Algorithms
● A greedy algorithm always makes the choice
that looks best at the moment
■ My everyday examples:
○ Walking to the Corner
○ Playing a bridge hand
■ The hope: a locally optimal choice will lead to a
globally optimal solution
■ For some problems, it works
● Dynamic programming can be overkill; greedy
algorithms tend to be easier to code
David Luebke 9 8/29/2022
Activity-Selection Problem
● Problem: get your money’s worth out of a
carnival
■ Buy a wristband that lets you onto any ride
■ Lots of rides, each starting and ending at different
times
■ Your goal: ride as many rides as possible
○ Another, alternative goal that we don’t solve here:
maximize time spent on rides
● Welcome to the activity selection problem
David Luebke 10 8/29/2022
Activity-Selection
● Formally:
■ Given a set S of n activities
si = start time of activity i
fi = finish time of activity i
■ Find max-size subset A of compatible activities
 Assume (wlog) that f1  f2  …  fn
1
2
3
4
5
6
David Luebke 11 8/29/2022
Activity Selection:
Optimal Substructure
● Let k be the minimum activity in A (i.e., the
one with the earliest finish time). Then A - {k}
is an optimal solution to S’ = {i  S: si  fk}
■ In words: once activity #1 is selected, the problem
reduces to finding an optimal solution for activity-
selection over activities in S compatible with #1
■ Proof: if we could find optimal solution B’ to S’
with |B| > |A - {k}|,
○ Then B U {k} is compatible
○ And |B U {k}| > |A|
David Luebke 12 8/29/2022
Activity Selection:
Repeated Subproblems
● Consider a recursive algorithm that tries all
possible compatible subsets to find a maximal
set, and notice repeated subproblems:
S
1A?
S’
2A?
S-{1}
2A?
S-{1,2}
S’’
S’-{2}
S’’
yes no
no
no
yes yes
David Luebke 13 8/29/2022
Greedy Choice Property
● Dynamic programming? Memoize? Yes, but…
● Activity selection problem also exhibits the
greedy choice property:
■ Locally optimal choice  globally optimal sol’n
■ Them 17.1: if S is an activity selection problem
sorted by finish time, then  optimal solution
A  S such that {1}  A
○ Sketch of proof: if  optimal solution B that does not
contain {1}, can always replace first activity in B with
{1} (Why?). Same number of activities, thus optimal.
David Luebke 14 8/29/2022
Activity Selection:
A Greedy Algorithm
● So actual algorithm is simple:
■ Sort the activities by finish time
■ Schedule the first activity
■ Then schedule the next activity in sorted list which
starts after previous activity finishes
■ Repeat until no more activities
● Intuition is even more simple:
■ Always pick the shortest ride available at the time
David Luebke 15 8/29/2022
Minimum Spanning Tree Revisited
● Recall: MST problem has optimal substructure
■ Prove it
● Is Prim’s algorithm greedy? Why?
● Is Kruskal’s algorithm greedy? Why?
David Luebke 16 8/29/2022
Review:
The Knapsack Problem
● The famous knapsack problem:
■ A thief breaks into a museum. Fabulous paintings,
sculptures, and jewels are everywhere. The thief
has a good eye for the value of these objects, and
knows that each will fetch hundreds or thousands
of dollars on the clandestine art collector’s market.
But, the thief has only brought a single knapsack to
the scene of the robbery, and can take away only
what he can carry. What items should the thief
take to maximize the haul?
David Luebke 17 8/29/2022
Review: The Knapsack Problem
● More formally, the 0-1 knapsack problem:
■ The thief must choose among n items, where the
ith item worth vi dollars and weighs wi pounds
■ Carrying at most W pounds, maximize value
○ Note: assume vi, wi, and W are all integers
○ “0-1” b/c each item must be taken or left in entirety
● A variation, the fractional knapsack problem:
■ Thief can take fractions of items
■ Think of items in 0-1 problem as gold ingots, in
fractional problem as buckets of gold dust
David Luebke 18 8/29/2022
Review: The Knapsack Problem
And Optimal Substructure
● Both variations exhibit optimal substructure
● To show this for the 0-1 problem, consider the
most valuable load weighing at most W pounds
■ If we remove item j from the load, what do we
know about the remaining load?
■ A: remainder must be the most valuable load
weighing at most W - wj that thief could take from
museum, excluding item j
David Luebke 19 8/29/2022
Solving The Knapsack Problem
● The optimal solution to the fractional knapsack
problem can be found with a greedy algorithm
■ How?
● The optimal solution to the 0-1 problem cannot
be found with the same greedy strategy
■ Greedy strategy: take in order of dollars/pound
■ Example: 3 items weighing 10, 20, and 30 pounds,
knapsack can hold 50 pounds
○ Suppose item 2 is worth $100. Assign values to the
other items so that the greedy strategy will fail
David Luebke 20 8/29/2022
The Knapsack Problem:
Greedy Vs. Dynamic
● The fractional problem can be solved greedily
● The 0-1 problem cannot be solved with a
greedy approach
■ As you have seen, however, it can be solved with
dynamic programming
1 de 20

Recomendados

Greedy algorithms por
Greedy algorithmsGreedy algorithms
Greedy algorithmsRajendran
2.1K vistas20 diapositivas
lect por
lectlect
lectfarazch
268 vistas20 diapositivas
lec por
leclec
lecfarazch
474 vistas20 diapositivas
lect por
lectlect
lectfarazch
478 vistas20 diapositivas
Lecture34 por
Lecture34Lecture34
Lecture34guestc24b39
211 vistas20 diapositivas
lecture 26 por
lecture 26lecture 26
lecture 26sajinsc
1.7K vistas20 diapositivas

Más contenido relacionado

Similar a Greedy1.ppt

lecture1.ppt por
lecture1.pptlecture1.ppt
lecture1.pptPallaviDhade1
4 vistas18 diapositivas
Greedy method1 por
Greedy method1Greedy method1
Greedy method1Rajendran
1K vistas14 diapositivas
Greedy algorithms -Making change-Knapsack-Prim's-Kruskal's por
Greedy algorithms -Making change-Knapsack-Prim's-Kruskal'sGreedy algorithms -Making change-Knapsack-Prim's-Kruskal's
Greedy algorithms -Making change-Knapsack-Prim's-Kruskal'sJay Patel
3.1K vistas21 diapositivas
Dynamic programming in Algorithm Analysis por
Dynamic programming in Algorithm AnalysisDynamic programming in Algorithm Analysis
Dynamic programming in Algorithm AnalysisRajendran
1.7K vistas11 diapositivas
Dynamic programming por
Dynamic programmingDynamic programming
Dynamic programmingMelaku Bayih Demessie
14.9K vistas22 diapositivas
slides-linear-programming-introduction.pdf por
slides-linear-programming-introduction.pdfslides-linear-programming-introduction.pdf
slides-linear-programming-introduction.pdfSantiagoGarridoBulln
1 vista33 diapositivas

Similar a Greedy1.ppt(20)

Greedy method1 por Rajendran
Greedy method1Greedy method1
Greedy method1
Rajendran 1K vistas
Greedy algorithms -Making change-Knapsack-Prim's-Kruskal's por Jay Patel
Greedy algorithms -Making change-Knapsack-Prim's-Kruskal'sGreedy algorithms -Making change-Knapsack-Prim's-Kruskal's
Greedy algorithms -Making change-Knapsack-Prim's-Kruskal's
Jay Patel3.1K vistas
Dynamic programming in Algorithm Analysis por Rajendran
Dynamic programming in Algorithm AnalysisDynamic programming in Algorithm Analysis
Dynamic programming in Algorithm Analysis
Rajendran 1.7K vistas
Automatic problem generation por Abhishek Dey
Automatic problem generationAutomatic problem generation
Automatic problem generation
Abhishek Dey390 vistas
Online Algorithms - An Introduction por Amrinder Arora
Online Algorithms - An IntroductionOnline Algorithms - An Introduction
Online Algorithms - An Introduction
Amrinder Arora2.6K vistas
daa-unit-3-greedy method por hodcsencet
daa-unit-3-greedy methoddaa-unit-3-greedy method
daa-unit-3-greedy method
hodcsencet2.1K vistas
Lecture1-Architecture por Danny Albay
Lecture1-ArchitectureLecture1-Architecture
Lecture1-Architecture
Danny Albay272 vistas
Hash table por Rajendran
Hash tableHash table
Hash table
Rajendran 3.7K vistas
Knapsack problem using greedy approach por padmeshagrekar
Knapsack problem using greedy approachKnapsack problem using greedy approach
Knapsack problem using greedy approach
padmeshagrekar617 vistas
Aggregation Heuristics for the Open Pit Scheduling Problem por Patricio Reyes
Aggregation Heuristics for the Open Pit Scheduling ProblemAggregation Heuristics for the Open Pit Scheduling Problem
Aggregation Heuristics for the Open Pit Scheduling Problem
Patricio Reyes1.4K vistas

Último

Searching in Data Structure por
Searching in Data StructureSearching in Data Structure
Searching in Data Structureraghavbirla63
14 vistas8 diapositivas
Web Dev Session 1.pptx por
Web Dev Session 1.pptxWeb Dev Session 1.pptx
Web Dev Session 1.pptxVedVekhande
11 vistas22 diapositivas
2023Dec ASU Wang NETR Group Research Focus and Facility Overview.pptx por
2023Dec ASU Wang NETR Group Research Focus and Facility Overview.pptx2023Dec ASU Wang NETR Group Research Focus and Facility Overview.pptx
2023Dec ASU Wang NETR Group Research Focus and Facility Overview.pptxlwang78
109 vistas19 diapositivas
Update 42 models(Diode/General ) in SPICE PARK(DEC2023) por
Update 42 models(Diode/General ) in SPICE PARK(DEC2023)Update 42 models(Diode/General ) in SPICE PARK(DEC2023)
Update 42 models(Diode/General ) in SPICE PARK(DEC2023)Tsuyoshi Horigome
38 vistas16 diapositivas
DESIGN OF SPRINGS-UNIT4.pptx por
DESIGN OF SPRINGS-UNIT4.pptxDESIGN OF SPRINGS-UNIT4.pptx
DESIGN OF SPRINGS-UNIT4.pptxgopinathcreddy
19 vistas47 diapositivas
START Newsletter 3 por
START Newsletter 3START Newsletter 3
START Newsletter 3Start Project
6 vistas25 diapositivas

Último(20)

Searching in Data Structure por raghavbirla63
Searching in Data StructureSearching in Data Structure
Searching in Data Structure
raghavbirla6314 vistas
Web Dev Session 1.pptx por VedVekhande
Web Dev Session 1.pptxWeb Dev Session 1.pptx
Web Dev Session 1.pptx
VedVekhande11 vistas
2023Dec ASU Wang NETR Group Research Focus and Facility Overview.pptx por lwang78
2023Dec ASU Wang NETR Group Research Focus and Facility Overview.pptx2023Dec ASU Wang NETR Group Research Focus and Facility Overview.pptx
2023Dec ASU Wang NETR Group Research Focus and Facility Overview.pptx
lwang78109 vistas
Update 42 models(Diode/General ) in SPICE PARK(DEC2023) por Tsuyoshi Horigome
Update 42 models(Diode/General ) in SPICE PARK(DEC2023)Update 42 models(Diode/General ) in SPICE PARK(DEC2023)
Update 42 models(Diode/General ) in SPICE PARK(DEC2023)
Tsuyoshi Horigome38 vistas
ASSIGNMENTS ON FUZZY LOGIC IN TRAFFIC FLOW.pdf por AlhamduKure
ASSIGNMENTS ON FUZZY LOGIC IN TRAFFIC FLOW.pdfASSIGNMENTS ON FUZZY LOGIC IN TRAFFIC FLOW.pdf
ASSIGNMENTS ON FUZZY LOGIC IN TRAFFIC FLOW.pdf
AlhamduKure6 vistas
BCIC - Manufacturing Conclave - Technology-Driven Manufacturing for Growth por Innomantra
BCIC - Manufacturing Conclave -  Technology-Driven Manufacturing for GrowthBCIC - Manufacturing Conclave -  Technology-Driven Manufacturing for Growth
BCIC - Manufacturing Conclave - Technology-Driven Manufacturing for Growth
Innomantra 6 vistas
MongoDB.pdf por ArthyR3
MongoDB.pdfMongoDB.pdf
MongoDB.pdf
ArthyR345 vistas
Design of Structures and Foundations for Vibrating Machines, Arya-ONeill-Pinc... por csegroupvn
Design of Structures and Foundations for Vibrating Machines, Arya-ONeill-Pinc...Design of Structures and Foundations for Vibrating Machines, Arya-ONeill-Pinc...
Design of Structures and Foundations for Vibrating Machines, Arya-ONeill-Pinc...
csegroupvn5 vistas
GDSC Mikroskil Members Onboarding 2023.pdf por gdscmikroskil
GDSC Mikroskil Members Onboarding 2023.pdfGDSC Mikroskil Members Onboarding 2023.pdf
GDSC Mikroskil Members Onboarding 2023.pdf
gdscmikroskil58 vistas
fakenews_DBDA_Mar23.pptx por deepmitra8
fakenews_DBDA_Mar23.pptxfakenews_DBDA_Mar23.pptx
fakenews_DBDA_Mar23.pptx
deepmitra816 vistas
Design of machine elements-UNIT 3.pptx por gopinathcreddy
Design of machine elements-UNIT 3.pptxDesign of machine elements-UNIT 3.pptx
Design of machine elements-UNIT 3.pptx
gopinathcreddy33 vistas
SUMIT SQL PROJECT SUPERSTORE 1.pptx por Sumit Jadhav
SUMIT SQL PROJECT SUPERSTORE 1.pptxSUMIT SQL PROJECT SUPERSTORE 1.pptx
SUMIT SQL PROJECT SUPERSTORE 1.pptx
Sumit Jadhav 18 vistas
_MAKRIADI-FOTEINI_diploma thesis.pptx por fotinimakriadi
_MAKRIADI-FOTEINI_diploma thesis.pptx_MAKRIADI-FOTEINI_diploma thesis.pptx
_MAKRIADI-FOTEINI_diploma thesis.pptx
fotinimakriadi8 vistas

Greedy1.ppt

  • 1. David Luebke 1 8/29/2022 CS 332: Algorithms Greedy Algorithms
  • 2. David Luebke 2 8/29/2022 Review: Dynamic Programming ● Dynamic programming is another strategy for designing algorithms ● Use when problem breaks down into recurring small subproblems
  • 3. David Luebke 3 8/29/2022 Review: Optimal Substructure of LCS ● Observation 1: Optimal substructure ■ A simple recursive algorithm will suffice ■ Draw sample recursion tree from c[3,4] ■ What will be the depth of the tree? ● Observation 2: Overlapping subproblems ■ Find some places where we solve the same subproblem more than once           otherwise ]) , 1 [ ], 1 , [ max( ], [ ] [ if 1 ] 1 , 1 [ ] , [ j i c j i c j y i x j i c j i c
  • 4. David Luebke 4 8/29/2022 Review: Structure of Subproblems ● For the LCS problem: ■ There are few subproblems in total ■ And many recurring instances of each (unlike divide & conquer, where subproblems unique) ● How many distinct problems exist for the LCS of x[1..m] and y[1..n]? ● A: mn
  • 5. David Luebke 5 8/29/2022 Memoization ● Memoization is another way to deal with overlapping subproblems ■ After computing the solution to a subproblem, store in a table ■ Subsequent calls just do a table lookup ● Can modify recursive alg to use memoziation: ■ There are mn subproblems ■ How many times is each subproblem wanted? ■ What will be the running time for this algorithm? The running space?
  • 6. David Luebke 6 8/29/2022 Review: Dynamic Programming ● Dynamic programming: build table bottom-up ■ Same table as memoization, but instead of starting at (m,n) and recursing down, start at (1,1) ● Least Common Subsequence: LCS easy to calculate from LCS of prefixes ○ As your homework shows, can actually reduce space to O(min(m,n)) ● Knapsack problem: we’ll review this in a bit
  • 7. David Luebke 7 8/29/2022 Review: Dynamic Programming ● Summary of the basic idea: ■ Optimal substructure: optimal solution to problem consists of optimal solutions to subproblems ■ Overlapping subproblems: few subproblems in total, many recurring instances of each ■ Solve bottom-up, building a table of solved subproblems that are used to solve larger ones ● Variations: ■ “Table” could be 3-dimensional, triangular, a tree, etc.
  • 8. David Luebke 8 8/29/2022 Greedy Algorithms ● A greedy algorithm always makes the choice that looks best at the moment ■ My everyday examples: ○ Walking to the Corner ○ Playing a bridge hand ■ The hope: a locally optimal choice will lead to a globally optimal solution ■ For some problems, it works ● Dynamic programming can be overkill; greedy algorithms tend to be easier to code
  • 9. David Luebke 9 8/29/2022 Activity-Selection Problem ● Problem: get your money’s worth out of a carnival ■ Buy a wristband that lets you onto any ride ■ Lots of rides, each starting and ending at different times ■ Your goal: ride as many rides as possible ○ Another, alternative goal that we don’t solve here: maximize time spent on rides ● Welcome to the activity selection problem
  • 10. David Luebke 10 8/29/2022 Activity-Selection ● Formally: ■ Given a set S of n activities si = start time of activity i fi = finish time of activity i ■ Find max-size subset A of compatible activities  Assume (wlog) that f1  f2  …  fn 1 2 3 4 5 6
  • 11. David Luebke 11 8/29/2022 Activity Selection: Optimal Substructure ● Let k be the minimum activity in A (i.e., the one with the earliest finish time). Then A - {k} is an optimal solution to S’ = {i  S: si  fk} ■ In words: once activity #1 is selected, the problem reduces to finding an optimal solution for activity- selection over activities in S compatible with #1 ■ Proof: if we could find optimal solution B’ to S’ with |B| > |A - {k}|, ○ Then B U {k} is compatible ○ And |B U {k}| > |A|
  • 12. David Luebke 12 8/29/2022 Activity Selection: Repeated Subproblems ● Consider a recursive algorithm that tries all possible compatible subsets to find a maximal set, and notice repeated subproblems: S 1A? S’ 2A? S-{1} 2A? S-{1,2} S’’ S’-{2} S’’ yes no no no yes yes
  • 13. David Luebke 13 8/29/2022 Greedy Choice Property ● Dynamic programming? Memoize? Yes, but… ● Activity selection problem also exhibits the greedy choice property: ■ Locally optimal choice  globally optimal sol’n ■ Them 17.1: if S is an activity selection problem sorted by finish time, then  optimal solution A  S such that {1}  A ○ Sketch of proof: if  optimal solution B that does not contain {1}, can always replace first activity in B with {1} (Why?). Same number of activities, thus optimal.
  • 14. David Luebke 14 8/29/2022 Activity Selection: A Greedy Algorithm ● So actual algorithm is simple: ■ Sort the activities by finish time ■ Schedule the first activity ■ Then schedule the next activity in sorted list which starts after previous activity finishes ■ Repeat until no more activities ● Intuition is even more simple: ■ Always pick the shortest ride available at the time
  • 15. David Luebke 15 8/29/2022 Minimum Spanning Tree Revisited ● Recall: MST problem has optimal substructure ■ Prove it ● Is Prim’s algorithm greedy? Why? ● Is Kruskal’s algorithm greedy? Why?
  • 16. David Luebke 16 8/29/2022 Review: The Knapsack Problem ● The famous knapsack problem: ■ A thief breaks into a museum. Fabulous paintings, sculptures, and jewels are everywhere. The thief has a good eye for the value of these objects, and knows that each will fetch hundreds or thousands of dollars on the clandestine art collector’s market. But, the thief has only brought a single knapsack to the scene of the robbery, and can take away only what he can carry. What items should the thief take to maximize the haul?
  • 17. David Luebke 17 8/29/2022 Review: The Knapsack Problem ● More formally, the 0-1 knapsack problem: ■ The thief must choose among n items, where the ith item worth vi dollars and weighs wi pounds ■ Carrying at most W pounds, maximize value ○ Note: assume vi, wi, and W are all integers ○ “0-1” b/c each item must be taken or left in entirety ● A variation, the fractional knapsack problem: ■ Thief can take fractions of items ■ Think of items in 0-1 problem as gold ingots, in fractional problem as buckets of gold dust
  • 18. David Luebke 18 8/29/2022 Review: The Knapsack Problem And Optimal Substructure ● Both variations exhibit optimal substructure ● To show this for the 0-1 problem, consider the most valuable load weighing at most W pounds ■ If we remove item j from the load, what do we know about the remaining load? ■ A: remainder must be the most valuable load weighing at most W - wj that thief could take from museum, excluding item j
  • 19. David Luebke 19 8/29/2022 Solving The Knapsack Problem ● The optimal solution to the fractional knapsack problem can be found with a greedy algorithm ■ How? ● The optimal solution to the 0-1 problem cannot be found with the same greedy strategy ■ Greedy strategy: take in order of dollars/pound ■ Example: 3 items weighing 10, 20, and 30 pounds, knapsack can hold 50 pounds ○ Suppose item 2 is worth $100. Assign values to the other items so that the greedy strategy will fail
  • 20. David Luebke 20 8/29/2022 The Knapsack Problem: Greedy Vs. Dynamic ● The fractional problem can be solved greedily ● The 0-1 problem cannot be solved with a greedy approach ■ As you have seen, however, it can be solved with dynamic programming