SlideShare a Scribd company logo
CS 332: Algorithms Greedy Algorithms
Review: Dynamic Programming Dynamic programming  is another strategy for designing algorithms Use when problem breaks down into recurring small subproblems
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
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
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?
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
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.
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
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
Activity-Selection Formally: Given a set  S  of  n  activities s i  =  start time of activity  i f i  =  finish time of activity  i Find max-size subset  A  of compatible activities Assume (wlog) that  f 1      f 2     …     f n 1 2 3 4 5 6
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 :  s i      f k } 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|
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
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.
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
Minimum Spanning Tree Revisited Recall: MST problem has optimal substructure Prove it Is Prim’s algorithm greedy?  Why? Is Kruskal’s algorithm greedy?  Why?
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?
Review: The Knapsack Problem More formally, the  0-1 knapsack problem : The thief must choose among  n  items, where the  i th item worth  v i   dollars and weighs  w i  pounds Carrying at most  W  pounds, maximize value Note: assume  v i , w i ,  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
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  -  w j   that thief could take from museum, excluding item j
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
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

More Related Content

Viewers also liked

Web 2.0 and libraries
Web 2.0 and librariesWeb 2.0 and libraries
Web 2.0 and libraries
Andrea Marchitelli
 
Imagen y Posicionamiento El Llanquihue Puerto Montt
Imagen y Posicionamiento El Llanquihue   Puerto MonttImagen y Posicionamiento El Llanquihue   Puerto Montt
Imagen y Posicionamiento El Llanquihue Puerto Montt
matildebaeza
 
Rcreee regional renewable energy conference 2010 enabling the desertec_concept
Rcreee regional renewable energy conference 2010 enabling the desertec_conceptRcreee regional renewable energy conference 2010 enabling the desertec_concept
Rcreee regional renewable energy conference 2010 enabling the desertec_concept
RCREEE
 
New Page Layouts
New  Page  LayoutsNew  Page  Layouts
New Page Layouts
Neil Stewart
 
victoriaacevedo
victoriaacevedovictoriaacevedo
victoriaacevedo
guest2a530b7
 
Realiza Tu Video Con Los Elementos Mas Conocidos
Realiza Tu Video Con Los Elementos Mas ConocidosRealiza Tu Video Con Los Elementos Mas Conocidos
Realiza Tu Video Con Los Elementos Mas Conocidos
GERMAN MULLO
 
Amasiato Y Divorcio Entre La Historia Y Literatura
Amasiato Y Divorcio Entre La Historia Y LiteraturaAmasiato Y Divorcio Entre La Historia Y Literatura
Amasiato Y Divorcio Entre La Historia Y Literatura
switchfoot
 
Abc!
Abc!Abc!
Abc!
adon
 
Facts On Stars!
Facts On Stars!Facts On Stars!
Facts On Stars!
kphonegrl101
 
Homage To Banksy
Homage To BanksyHomage To Banksy
Homage To Banksy
Herb Williams
 
INFOGRAPHIC: Why Attend Frontline Live Chicago?
INFOGRAPHIC: Why Attend Frontline Live Chicago?INFOGRAPHIC: Why Attend Frontline Live Chicago?
INFOGRAPHIC: Why Attend Frontline Live Chicago?
Frontline Technologies
 
Histogramas Poblacion
Histogramas PoblacionHistogramas Poblacion
Histogramas Poblacion
profericardo
 
RCREEE/enerMENA professional training course for CSP announcment_EN
RCREEE/enerMENA professional training course for CSP announcment_ENRCREEE/enerMENA professional training course for CSP announcment_EN
RCREEE/enerMENA professional training course for CSP announcment_EN
RCREEE
 
Familia
FamiliaFamilia
Familia
guest816f68
 
Improving Your App Quality with Raygun Error Reporting
Improving Your App Quality with Raygun Error ReportingImproving Your App Quality with Raygun Error Reporting
Improving Your App Quality with Raygun Error Reporting
Russ Fustino
 
Progressive-2Q 06 QSR
Progressive-2Q 06 QSRProgressive-2Q 06 QSR
Progressive-2Q 06 QSR
finance18
 
Hall.Room One.Floor Layout
Hall.Room One.Floor LayoutHall.Room One.Floor Layout
Hall.Room One.Floor Layout
C3D_Designs
 

Viewers also liked (18)

Web 2.0 and libraries
Web 2.0 and librariesWeb 2.0 and libraries
Web 2.0 and libraries
 
Imagen y Posicionamiento El Llanquihue Puerto Montt
Imagen y Posicionamiento El Llanquihue   Puerto MonttImagen y Posicionamiento El Llanquihue   Puerto Montt
Imagen y Posicionamiento El Llanquihue Puerto Montt
 
Rcreee regional renewable energy conference 2010 enabling the desertec_concept
Rcreee regional renewable energy conference 2010 enabling the desertec_conceptRcreee regional renewable energy conference 2010 enabling the desertec_concept
Rcreee regional renewable energy conference 2010 enabling the desertec_concept
 
New Page Layouts
New  Page  LayoutsNew  Page  Layouts
New Page Layouts
 
victoriaacevedo
victoriaacevedovictoriaacevedo
victoriaacevedo
 
Realiza Tu Video Con Los Elementos Mas Conocidos
Realiza Tu Video Con Los Elementos Mas ConocidosRealiza Tu Video Con Los Elementos Mas Conocidos
Realiza Tu Video Con Los Elementos Mas Conocidos
 
Amasiato Y Divorcio Entre La Historia Y Literatura
Amasiato Y Divorcio Entre La Historia Y LiteraturaAmasiato Y Divorcio Entre La Historia Y Literatura
Amasiato Y Divorcio Entre La Historia Y Literatura
 
Abc!
Abc!Abc!
Abc!
 
Facts On Stars!
Facts On Stars!Facts On Stars!
Facts On Stars!
 
Homage To Banksy
Homage To BanksyHomage To Banksy
Homage To Banksy
 
INFOGRAPHIC: Why Attend Frontline Live Chicago?
INFOGRAPHIC: Why Attend Frontline Live Chicago?INFOGRAPHIC: Why Attend Frontline Live Chicago?
INFOGRAPHIC: Why Attend Frontline Live Chicago?
 
Histogramas Poblacion
Histogramas PoblacionHistogramas Poblacion
Histogramas Poblacion
 
RCREEE/enerMENA professional training course for CSP announcment_EN
RCREEE/enerMENA professional training course for CSP announcment_ENRCREEE/enerMENA professional training course for CSP announcment_EN
RCREEE/enerMENA professional training course for CSP announcment_EN
 
Familia
FamiliaFamilia
Familia
 
Improving Your App Quality with Raygun Error Reporting
Improving Your App Quality with Raygun Error ReportingImproving Your App Quality with Raygun Error Reporting
Improving Your App Quality with Raygun Error Reporting
 
Progressive-2Q 06 QSR
Progressive-2Q 06 QSRProgressive-2Q 06 QSR
Progressive-2Q 06 QSR
 
Hall.Room One.Floor Layout
Hall.Room One.Floor LayoutHall.Room One.Floor Layout
Hall.Room One.Floor Layout
 
Noutati in programele de pregatire CIM!
Noutati in programele de pregatire CIM!Noutati in programele de pregatire CIM!
Noutati in programele de pregatire CIM!
 

Similar to Lecture34

lecture 27
lecture 27lecture 27
lecture 27
sajinsc
 
Greedy algorithms
Greedy algorithmsGreedy algorithms
Greedy algorithms
Rajendran
 
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programming
Melaku Bayih Demessie
 
Analysis and Design of Algorithms notes
Analysis and Design of Algorithms  notesAnalysis and Design of Algorithms  notes
Analysis and Design of Algorithms notes
Prof. Dr. K. Adisesha
 
Greedy algorithms -Making change-Knapsack-Prim's-Kruskal's
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 Patel
 
Greedy
GreedyGreedy
Greedy
koralverma
 
Algorithms Design Patterns
Algorithms Design PatternsAlgorithms Design Patterns
Algorithms Design Patterns
Ashwin Shiv
 
Greedy1.ppt
Greedy1.pptGreedy1.ppt
Greedy1.ppt
PallaviDhade1
 
DynamicProgramming.pptx
DynamicProgramming.pptxDynamicProgramming.pptx
DynamicProgramming.pptx
SaimaShaheen14
 
Mastering Greedy Algorithms: Optimizing Solutions for Efficiency"
Mastering Greedy Algorithms: Optimizing Solutions for Efficiency"Mastering Greedy Algorithms: Optimizing Solutions for Efficiency"
Mastering Greedy Algorithms: Optimizing Solutions for Efficiency"
22bcs058
 
Ms nikita greedy agorithm
Ms nikita greedy agorithmMs nikita greedy agorithm
Ms nikita greedy agorithm
Nikitagupta123
 
Optimization problems
Optimization problemsOptimization problems
Optimization problems
Ruchika Sinha
 
Greedy method1
Greedy method1Greedy method1
Greedy method1
Rajendran
 
Fractional Knapsack Problem
Fractional Knapsack ProblemFractional Knapsack Problem
Fractional Knapsack Problem
harsh kothari
 
Parallel_Algorithms_In_Combinatorial_Optimization_Problems.ppt
Parallel_Algorithms_In_Combinatorial_Optimization_Problems.pptParallel_Algorithms_In_Combinatorial_Optimization_Problems.ppt
Parallel_Algorithms_In_Combinatorial_Optimization_Problems.ppt
dakccse
 
Parallel_Algorithms_In_Combinatorial_Optimization_Problems.ppt
Parallel_Algorithms_In_Combinatorial_Optimization_Problems.pptParallel_Algorithms_In_Combinatorial_Optimization_Problems.ppt
Parallel_Algorithms_In_Combinatorial_Optimization_Problems.ppt
BinayakMukherjee4
 
Data Analysis and Algorithms Lecture 1: Introduction
 Data Analysis and Algorithms Lecture 1: Introduction Data Analysis and Algorithms Lecture 1: Introduction
Data Analysis and Algorithms Lecture 1: Introduction
TayyabSattar5
 
Greedy Algorithms WITH Activity Selection Problem.ppt
Greedy Algorithms WITH Activity Selection Problem.pptGreedy Algorithms WITH Activity Selection Problem.ppt
Greedy Algorithms WITH Activity Selection Problem.ppt
Ruchika Sinha
 
BackTracking Algorithm: Technique and Examples
BackTracking Algorithm: Technique and ExamplesBackTracking Algorithm: Technique and Examples
BackTracking Algorithm: Technique and Examples
Fahim Ferdous
 
Back tracking
Back trackingBack tracking

Similar to Lecture34 (20)

lecture 27
lecture 27lecture 27
lecture 27
 
Greedy algorithms
Greedy algorithmsGreedy algorithms
Greedy algorithms
 
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programming
 
Analysis and Design of Algorithms notes
Analysis and Design of Algorithms  notesAnalysis and Design of Algorithms  notes
Analysis and Design of Algorithms notes
 
Greedy algorithms -Making change-Knapsack-Prim's-Kruskal's
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
 
Greedy
GreedyGreedy
Greedy
 
Algorithms Design Patterns
Algorithms Design PatternsAlgorithms Design Patterns
Algorithms Design Patterns
 
Greedy1.ppt
Greedy1.pptGreedy1.ppt
Greedy1.ppt
 
DynamicProgramming.pptx
DynamicProgramming.pptxDynamicProgramming.pptx
DynamicProgramming.pptx
 
Mastering Greedy Algorithms: Optimizing Solutions for Efficiency"
Mastering Greedy Algorithms: Optimizing Solutions for Efficiency"Mastering Greedy Algorithms: Optimizing Solutions for Efficiency"
Mastering Greedy Algorithms: Optimizing Solutions for Efficiency"
 
Ms nikita greedy agorithm
Ms nikita greedy agorithmMs nikita greedy agorithm
Ms nikita greedy agorithm
 
Optimization problems
Optimization problemsOptimization problems
Optimization problems
 
Greedy method1
Greedy method1Greedy method1
Greedy method1
 
Fractional Knapsack Problem
Fractional Knapsack ProblemFractional Knapsack Problem
Fractional Knapsack Problem
 
Parallel_Algorithms_In_Combinatorial_Optimization_Problems.ppt
Parallel_Algorithms_In_Combinatorial_Optimization_Problems.pptParallel_Algorithms_In_Combinatorial_Optimization_Problems.ppt
Parallel_Algorithms_In_Combinatorial_Optimization_Problems.ppt
 
Parallel_Algorithms_In_Combinatorial_Optimization_Problems.ppt
Parallel_Algorithms_In_Combinatorial_Optimization_Problems.pptParallel_Algorithms_In_Combinatorial_Optimization_Problems.ppt
Parallel_Algorithms_In_Combinatorial_Optimization_Problems.ppt
 
Data Analysis and Algorithms Lecture 1: Introduction
 Data Analysis and Algorithms Lecture 1: Introduction Data Analysis and Algorithms Lecture 1: Introduction
Data Analysis and Algorithms Lecture 1: Introduction
 
Greedy Algorithms WITH Activity Selection Problem.ppt
Greedy Algorithms WITH Activity Selection Problem.pptGreedy Algorithms WITH Activity Selection Problem.ppt
Greedy Algorithms WITH Activity Selection Problem.ppt
 
BackTracking Algorithm: Technique and Examples
BackTracking Algorithm: Technique and ExamplesBackTracking Algorithm: Technique and Examples
BackTracking Algorithm: Technique and Examples
 
Back tracking
Back trackingBack tracking
Back tracking
 

Lecture34

  • 1. CS 332: Algorithms Greedy Algorithms
  • 2. Review: Dynamic Programming Dynamic programming is another strategy for designing algorithms Use when problem breaks down into recurring small subproblems
  • 3. 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
  • 4. 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. 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. 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. 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. 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. 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. Activity-Selection Formally: Given a set S of n activities s i = start time of activity i f i = finish time of activity i Find max-size subset A of compatible activities Assume (wlog) that f 1  f 2  …  f n 1 2 3 4 5 6
  • 11. 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 : s i  f k } 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. 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. 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. 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. 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. 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. Review: The Knapsack Problem More formally, the 0-1 knapsack problem : The thief must choose among n items, where the i th item worth v i dollars and weighs w i pounds Carrying at most W pounds, maximize value Note: assume v i , w i , 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. 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 - w j that thief could take from museum, excluding item j
  • 19. 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. 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