Greedy Algorithms BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)1.

Slides:



Advertisements
Similar presentations
Introduction to Algorithms
Advertisements

CS 332: Algorithms NP Completeness David Luebke /2/2017.
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
Comp 122, Spring 2004 Greedy Algorithms. greedy - 2 Lin / Devi Comp 122, Fall 2003 Overview  Like dynamic programming, used to solve optimization problems.
1.1 Data Structure and Algorithm Lecture 6 Greedy Algorithm Topics Reference: Introduction to Algorithm by Cormen Chapter 17: Greedy Algorithm.
Greedy Algorithms Greed is good. (Some of the time)
Analysis of Algorithms
Greed is good. (Some of the time)
Greedy Algorithms Be greedy! always make the choice that looks best at the moment. Local optimization. Not always yielding a globally optimal solution.
Introduction to Algorithms Jiafen Liu Sept
Dynamic Programming.
David Luebke 1 5/4/2015 CS 332: Algorithms Dynamic Programming Greedy Algorithms.
Review: Dynamic Programming
Lecture 7: Greedy Algorithms II Shang-Hua Teng. Greedy algorithms A greedy algorithm always makes the choice that looks best at the moment –My everyday.
CPSC 411, Fall 2008: Set 4 1 CPSC 411 Design and Analysis of Algorithms Set 4: Greedy Algorithms Prof. Jennifer Welch Fall 2008.
Greedy Algorithms Reading Material: –Alsuwaiyel’s Book: Section 8.1 –CLR Book (2 nd Edition): Section 16.1.
CSE 780 Algorithms Advanced Algorithms Greedy algorithm Job-select problem Greedy vs DP.
Greedy Algorithms CIS 606 Spring Greedy Algorithms Similar to dynamic programming. Used for optimization problems. Idea – When we have a choice.
CS3381 Des & Anal of Alg ( SemA) City Univ of HK / Dept of CS / Helena Wong 5. Greedy Algorithms - 1 Greedy.
Week 2: Greedy Algorithms
Dynamic Programming 0-1 Knapsack These notes are taken from the notes by Dr. Steve Goddard at
Lecture 7: Greedy Algorithms II
CPSC 411, Fall 2008: Set 4 1 CPSC 411 Design and Analysis of Algorithms Set 4: Greedy Algorithms Prof. Jennifer Welch Fall 2008.
16.Greedy algorithms Hsu, Lih-Hsing. Computer Theory Lab. Chapter 16P An activity-selection problem Suppose we have a set S = {a 1, a 2,..., a.
David Luebke 1 8/23/2015 CS 332: Algorithms Greedy Algorithms.
David Luebke 1 10/24/2015 CS 332: Algorithms Greedy Algorithms Continued.
CSC 413/513: Intro to Algorithms Greedy Algorithms.
Introduction to Algorithms Chapter 16: Greedy Algorithms.
Greedy Methods and Backtracking Dr. Marina Gavrilova Computer Science University of Calgary Canada.
Honors Track: Competitive Programming & Problem Solving Optimization Problems Kevin Verbeek.
CSC 201: Design and Analysis of Algorithms Greedy Algorithms.
COSC 3101A - Design and Analysis of Algorithms 8 Elements of DP Memoization Longest Common Subsequence Greedy Algorithms Many of these slides are taken.
Greedy Algorithms. What is “Greedy Algorithm” Optimization problem usually goes through a sequence of steps A greedy algorithm makes the choice looks.
December 14, 2015 Design and Analysis of Computer Algorithm Pradondet Nilagupta Department of Computer Engineering.
David Luebke 1 1/14/2016 CS 332: Algorithms NP Completeness.
CS 3343: Analysis of Algorithms Lecture 19: Introduction to Greedy Algorithms.
Greedy Algorithms Interval Scheduling and Fractional Knapsack These slides are based on the Lecture Notes by David Mount for the course CMSC 451 at the.
Greedy Algorithms Chapter 16 Highlights
Data Structures and Algorithms (AT70.02) Comp. Sc. and Inf. Mgmt. Asian Institute of Technology Instructor: Prof. Sumanta Guha Slide Sources: CLRS “Intro.
1 Algorithms CSCI 235, Fall 2015 Lecture 29 Greedy Algorithms.
Greedy Algorithms Lecture 10 Asst. Prof. Dr. İlker Kocabaş 1.
A greedy algorithm is an algorithm that follows the problem solving heuristic of making the locally optimal choice at each stage with the hope of finding.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 18.
6/13/20161 Greedy A Comparison. 6/13/20162 Greedy Solves an optimization problem: the solution is “best” in some sense. Greedy Strategy: –At each decision.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 17.
CS6045: Advanced Algorithms Greedy Algorithms. Main Concept –Divide the problem into multiple steps (sub-problems) –For each step take the best choice.
CS583 Lecture 12 Jana Kosecka Dynamic Programming Longest Common Subsequence Matrix Chain Multiplication Greedy Algorithms Many slides here are based on.
Greedy Algorithms Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
Greedy Algorithms Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
Greedy Algorithms General principle of greedy algorithm
Greedy algorithms: CSC317
CSC317 Greedy algorithms; Two main properties:
Lecture on Design and Analysis of Computer Algorithm
Greedy Algorithms (Chap. 16)
Greedy Algorithm.
Algorithm Design Methods
CS 3343: Analysis of Algorithms
CS6045: Advanced Algorithms
CS4335 Design and Analysis of Algorithms/WANG Lusheng
Greedy Algorithm Enyue (Annie) Lu.
Greedy Algorithms Many optimization problems can be solved more quickly using a greedy approach The basic principle is that local optimal decisions may.
Chapter 16: Greedy algorithms Ming-Te Chi
Advanced Algorithms Analysis and Design
Lecture 6 Topics Greedy Algorithm
Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt
Chapter 16: Greedy algorithms Ming-Te Chi
Algorithms CSCI 235, Spring 2019 Lecture 29 Greedy Algorithms
Algorithm Design Techniques Greedy Approach vs Dynamic Programming
Greedy algorithms.
Advance Algorithm Dynamic Programming
Presentation transcript:

Greedy Algorithms BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)1

Greedy Algorithms A greedy algorithm always makes the choice that looks best at the moment –Everyday examples: Walking to the school 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 BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)2

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 This is an activity selection problem BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)3

Activity-Selection Problem Input: a set S ={1, 2, …, n} of n activities –si = Start time of activity i, –fi = Finish time of activity i –Activity i takes place in [s i, f i ) Aim: Find max-size subset A of mutually compatible activities –Max number of activities, not max time spent in activities –Activities i and j are compatible if intervals [s i, f i ) and [s j, f j ) do not overlap, i.e., either s i ≥ f j or s j ≥ f i BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)4

Activity-Selection Problem - Example BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)5

Activity Selection: Optimal Substructure Theorem: Let k be the activity with the earliest finish time in an optimal solution A ⊆ S then A-{k} is an optimal solution to sub-problem S k ´ = {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 (by contradiction): Let B´ be an optimal solution to S k ´ and |B´| > | A-{k}| = |A| - 1 Then, B = B´ ∪ {k} is compatible and |B| = |B´|+1 > |A| Contradiction to the optimality of A Q.E.D. BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)6

Activity Selection: Repeated Subproblems Consider a recursive algorithm that tries all possible compatible subsets to find a maximal set, and notice repeated subproblems: BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)7 S 1  A? S’ 2  A? S-{1} 2  A? S-{1,2}S’’S’-{2}S’’ yesno yes

Greedy Choice Property Repeated sub-problems and optimal substructure properties hold in activity selection problem Greed choice property: a sequence of locally optimal (greedy) choices ⇒ an optimal solution Some problems (such as activity selection) has greedy choice property, we can use a greedy algorithm for those problems. –We may use dynamic programming (or memoize) approch for them too, but it will be cumbersome. If a problem does not have greedy choice property, we may not use a greedy algorithm for that problem, but we can still use a dynamic programming approach. Assume (without loss of generality) f 1 ≤ f 2 ≤ … ≤ f n – If not sort activities according to their finish times in nondecreasing order BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)8

Greedy Choice Property in Activity Selection BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)9

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 BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)10

Activity Selection Problem: An Example S={[1, 4 ), [5, 7 ), [2, 8 ), [3, 11 ), [8, 15 ), [13, 18 )}  A={1, 2, 5} BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)11

Greedy vs Dynamic Programming Optimal substructure property exploited by both Greedy and DP strategies Greedy Choice Property: A sequence of locally optimal choices ⇒ an optimal solution – We make the choice that seems best at the moment – Then solve the subproblem arising after the choice is made DP: We also make a choice/decision at each step, but the choice may depend on the optimal solutions to subproblems Greedy: The choice may depend on the choices made so far, but it cannot depend on any future choices or on the solutions to subproblems DP is a bottom-up strategy Greedy is a top-down strategy –each greedy choice in the sequence iteratively reduces each problem to a similar but smaller problem BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)12

Proof of Correctness of Greedy Algorithms Examine a globally optimal solution Show that this solution can be modified so that 1) A greedy choice is made as the first step 2) This choice reduces the problem to a similar but smaller problem Apply induction to show that a greedy choice can be used at every step Showing (2) reduces the proof of correctness to proving that the problem exhibits optimal substructure property BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)13

Elements of Greedy Strategy How can you judge whether a greedy algorithm will solve a particular optimization problem? Two key ingredients – Greedy choice property – Optimal substructure property Greedy Choice Property: A globally optimal solution can be arrived at by making locally optimal (greedy) choices In DP,we make a choice at each step but the choice may depend on the solutions to subproblems In Greedy Algorithms, we make the choice that seems best at that moment then solve the subproblems arising after the choice is made – The choice may depend on choices so far, but it cannot depend on any future choice or on the solutions to subproblems DP solves the problem bottom-up Greedy usually progresses in a top-down fashion by making one greedy choice after another reducing each given problem instance to a smaller one BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)14

Key Ingredients: Greedy Choice Property We must prove that a greedy choice at each step yields a globally optimal solution The proof examines a globally optimal solution Shows that the soln can be modified so that a greedy choice made as the first step reduces the problem to a similar but smaller subproblem Then induction is applied to show that a greedy choice can be used at each step Hence, this induction proof reduces the proof of correctness to demonstrating that an optimal solution must exhibit optimal substructure property BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)15

Key Ingredients: Optimal Substructure A problem exhibits optimal substructure if an optimal solution to the problem contains within it optimal solutions to subproblems Example: Activity selection problem S If an optimal solution A to S begins with activity 1 then the set of activities A´ = A-{1} is an optimal solution to the activity selection problem S´ = {i ∈ S: s i ≥ f 1 } BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)16

Key Ingredients: Optimal Substructure Optimal substructure property is exploited by both greedy and dynamic programming strategies Hence one may – Try to generate a dynamic programming solution to a problem when a greedy strategy suffices – Or, may mistakenly think that a greedy solution works when in fact a DP solution is required Example: Knapsack Problems(S, w) BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)17

Knapsack Problems The 0-1 Knapsack Problem(S, W) – A thief robbing a store finds n items S ={I 1, I 2, …, I n }, the i th item is worth v i dollars and weighs w i pounds, where v i and w i are integers – He wants to take as valuable a load as possible, but he can carry at most W pounds in his knapsack, where W is an integer – The thief cannot take a fractional amount of an item The Fractional Knapsack Problem (S, W) – The scenario is the same – But, the thief can take fractions of items rather than having to make binary (0-1) choice for each item BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)18

0-1 and Fractional Knapsack Problems Both knapsack problems exhibit the optimal substructure property The 0-1Knapsack Problem(S, W) – Consider a most valuable load L where W L ≤W – If we remove item j from this optimal load L The remaining load L j ´ = L -{I j } must be a most valuable load weighing at most W j ´ = W - w j pounds that the thief can take from S j ´ = S -{I j } – That is, L j ´ should be an optimal solution to the 0-1 Knapsack Problem(S j ´, W j ´) BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)19

0-1 and Fractional Knapsack Problems The Fractional Knapsack Problem(S, W) – Consider a most valuable load L where W L ≤ W – If we remove a weight 0< w ≤ w j of item j from optimal load L The remaining load L j ´ = L -{w pounds of I j } must be a most valuable load weighing at most W j ´ = W - w pounds that the thief can take from S j ´ = S -{I j } ∪ {w j - w pounds of I j } – That is, L j ´ should be an optimal solution to the Fractional Knapsack Problem(S j ´, W j ´) BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)20

Knapsack Problems Although the problems are similar, and both knapsack problems exhibit the optimal substructure property The Fractional Knapsack Problem is solvable by Greedy strategy –The Fractional Knapsack Problem has a greedy search propert y. Whereas, the 0-1 Knapsack Problem is NOT solvable by Greedy strategy –T he 0-1 Knapsack Problem does not have a greedy search property. BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)21

Greedy Solution to Fractional Knapsack BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)22 1) Compute the value per pound vi /wi for each item 2) The thief begins by taking, as much as possible, of the item with the greatest value per pound 3) If the supply of that item is exhausted before filling the knapsack he takes, as much as possible, of the item with the next greatest value per pound 4) Repeat (2-3) until his knapsack becomes full Thus, by sorting the items by value per pound the greedy algorithm runs in O(n lg n) time

0-1 Knapsack Problem BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)23

0-1 Knapsack Problem BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)24

0-1 Knapsack Problem BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)25

0-1 Knapsack Problem BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)26

0-1 Knapsack Problem BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)27

0-1 Knapsack Problem BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)28

0-1 Knapsack Problem BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)29

DP Solution to 0-1 Knapsack Problem BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)30

Finding the Set S of Articles in an Optimal Load BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)31 SOLKNAP0-1(a, v, w, n,W,c) i ← n ; ω ← W S ← ∅ while i > 0 do if c[i, ω] = c[i -1, ω] then i ← i -1 else S ← S ∪ {a i } ω ← ω - w i i ← i -1 return S