Cs333/cutler Greedy1 Introduction to Greedy Algorithms The greedy technique Problems explored –The coin changing problem –Activity selection.

Slides:



Advertisements
Similar presentations
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
Advertisements

Greedy Algorithms.
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.
Chapter 5 Fundamental Algorithm Design Techniques.
Greedy Algorithms Greed is good. (Some of the time)
Greedy Algorithms Clayton Andrews 2/26/08. What is an algorithm? “An algorithm is any well-defined computational procedure that takes some value, or set.
Analysis of Algorithms
Greedy Algorithms Be greedy! always make the choice that looks best at the moment. Local optimization. Not always yielding a globally optimal solution.
Greedy Algorithms Basic idea Connection to dynamic programming
Greedy Algorithms Basic idea Connection to dynamic programming Proof Techniques.
CSE115/ENGR160 Discrete Mathematics 02/28/12
Merge Sort 4/15/2017 6:09 PM The Greedy Method The Greedy Method.
Greedy vs Dynamic Programming Approach
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.
CSE115/ENGR160 Discrete Mathematics 03/03/11 Ming-Hsuan Yang UC Merced 1.
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.
Week 2: Greedy Algorithms
Lecture 7: Greedy Algorithms II
1 The Greedy Method CSC401 – Analysis of Algorithms Lecture Notes 10 The Greedy Method Objectives Introduce the Greedy Method Use the greedy method to.
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.
10/31/02CSE Greedy Algorithms CSE Algorithms Greedy Algorithms.
Called as the Interval Scheduling Problem. A simpler version of a class of scheduling problems. – Can add weights. – Can add multiple resources – Can ask.
Greedy Algorithms and Matroids Andreas Klappenecker.
Algorithms April-May 2013 Dr. Youn-Hee Han The Project for the Establishing the Korea ㅡ Vietnam College of Technology in Bac Giang.
Algorithmics - Lecture 101 LECTURE 10: Greedy technique.
CompSci 102 Discrete Math for Computer Science
Greedy Algorithms. Surprisingly, many important and practical computational problems can be solved this way. Every two year old knows the greedy algorithm.
Chapter Algorithms 3.2 The Growth of Functions 3.3 Complexity of Algorithms 3.4 The Integers and Division 3.5 Primes and Greatest Common Divisors.
Section 3.1. Section Summary Properties of Algorithms Algorithms for Searching and Sorting Greedy Algorithms Halting Problem.
The Greedy Method. The Greedy Method Technique The greedy method is a general algorithm design paradigm, built on the following elements: configurations:
CSC 201: Design and Analysis of Algorithms Greedy Algorithms.
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.
Dynamic Programming.  Decomposes a problem into a series of sub- problems  Builds up correct solutions to larger and larger sub- problems  Examples.
Greedy Algorithms BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)1.
CS 3343: Analysis of Algorithms Lecture 19: Introduction to Greedy Algorithms.
Greedy Algorithms Chapter 16 Highlights
Spring 2008The Greedy Method1. Spring 2008The Greedy Method2 Outline and Reading The Greedy Method Technique (§5.1) Fractional Knapsack Problem (§5.1.1)
CS 361 – Chapter 10 “Greedy algorithms” It’s a strategy of solving some problems –Need to make a series of choices –Each choice is made to maximize current.
Copyright © 2014 Curt Hill Algorithms From the Mathematical Perspective.
1 Algorithms CSCI 235, Fall 2015 Lecture 29 Greedy Algorithms.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 17.
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
Greedy Algorithms.
Lecture on Design and Analysis of Computer Algorithm
Merge Sort 7/29/ :21 PM The Greedy Method The Greedy Method.
Greedy Algorithms Basic idea Connection to dynamic programming
Greedy Algorithms / Interval Scheduling Yin Tat Lee
CSCE350 Algorithms and Data Structure
Merge Sort 11/28/2018 2:18 AM The Greedy Method The Greedy Method.
The Greedy Method Spring 2007 The Greedy Method Merge Sort
Merge Sort 11/28/2018 8:16 AM The Greedy Method The Greedy Method.
Chapter 16: Greedy algorithms Ming-Te Chi
Discrete Mathematics CMP-101 Lecture 12 Sorting, Bubble Sort, Insertion Sort, Greedy Algorithms Abdul Hameed
Advanced Algorithms Analysis and Design
Greedy Algorithms.
Merge Sort 1/17/2019 3:11 AM The Greedy Method The Greedy Method.
Chapter 16: Greedy algorithms Ming-Te Chi
Algorithms CSCI 235, Spring 2019 Lecture 29 Greedy Algorithms
Greedy Algorithms Comp 122, Spring 2004.
Algorithm Design Techniques Greedy Approach vs Dynamic Programming
Merge Sort 5/2/2019 7:53 PM The Greedy Method The Greedy Method.
Advance Algorithm Dynamic Programming
Presentation transcript:

cs333/cutler Greedy1 Introduction to Greedy Algorithms The greedy technique Problems explored –The coin changing problem –Activity selection

cs333/cutler Greedy2 Optimization problems An optimization problem: –Given a problem instance, a set of constraints and an objective function. –Find a feasible solution for the given instance for which the objective function has an optimal value –either maximum or minimum depending on the problem being solved. A feasible solution satisfies the problem’s constraints The constraints specify the limitations on the required solutions. For example in the knapsack problem we require that the items in the knapsack will not exceed a given weight

cs333/cutler Greedy3 The Greedy Technique(Method) Greedy algorithms make good local choices in the hope that they result in an optimal solution. –They result in feasible solutions. –Not necessarily an optimal solution. A proof is needed to show that the algorithm finds an optimal solution. A counter example shows that the greedy algorithm does not provide an optimal solution.

cs333/cutler Greedy4 Pseudo-code for Greedy Algorithm set Greedy (Set Candidate){ solution= new Set( ); while (Candidate.isNotEmpty()) { next = Candidate.select(); //use selection criteria, //remove from Candidate and return value if (solution.isFeasible( next)) //constraints satisfied solution.union( next); if (solution.solves()) return solution} //No more candidates and no solution return null }

cs333/cutler Greedy5 Pseudo code for greedy cont. select() chooses a candidate based on a local selection criteria, removes it from Candidate, and returns its value. isFeasible() checks whether adding the selected value to the current solution can result in a feasible solution (no constraints are violated). solves() checks whether the problem is solved.

cs333/cutler Greedy6 (12,D,N,P/15) Coin changing problem Problem: Return correct change using a minimum number of coins. Greedy choice: coin with highest coin value A greedy solution (next slide): American money The amount owed = 37 cents. The change is: 1 quarter, 1 dime, 2 cents. Solution is optimal. Is it optimal for all sets of coin sizes? Is there a solution for all sets of coin sizes?

cs333/cutler Greedy7 A greedy solution: Input: Set of coins of different denominations, amount- owed change = {} while (more coin-sizes && valueof(change)<amount- owed) Choose the largest remaining coin-size // Selection // feasibility check while (adding the coin does not make the valueof(change) exceed the amount-owed ) then add coin to change //check if solved if ( valueof(change) equals amount-owed) return change else delete coin-size return “failed to compute change”

cs333/cutler Greedy8 Elements of the Greedy Strategy Cast problem as one in which we make a greedy choice and are left with one subproblem to solve. To show optimality: 1.Prove there is always an optimal solution to original problem that makes the greedy choice.

cs333/cutler Greedy9 Elements of the Greedy Strategy 2.Demonstrate that what remains is a subproblem with property: If we combine the optimal solution of the subproblem with the greedy choice we have an optimal solution to original problem.

cs333/cutler Greedy10 Activity Selection Given a set S of n activities with start time s i and finish time f i of activity i Find a maximum size subset A of compatible activities (maximum number of activities). Activities are compatible if they do not overlap Can you suggest a greedy choice?

cs333/cutler Greedy11 Example Time Activities

cs333/cutler Greedy12 Counter Example 1 Select by start time Time Activities 1 2 3

cs333/cutler Greedy13 Counter Example 2 Select by minimum duration Time Activities 1 2 3

cs333/cutler Greedy14 Select by finishing time Time Activities

cs333/cutler Greedy15 Activity Selection Assume without loss of generality that we number the intervals in order of finish time. So f 1 ...  f n. Greedy choice: choose activity with minimum finish time The following greedy algorithm starts with A={1} and then adds all compatible jobs. (Theta(n)) Theta(nlogn) when including sort

cs333/cutler Greedy16 Greedy-Activity-Selector(s,f) n <- length[s] // number of activities A <- {1} j <- 1 //last activity added for i <- 2 to n //select if s i >= f j then //compatible (feasible) add {i} to A j <- i //save new last activity return A

cs333/cutler Greedy17 Proof that greedy solution is optimal 1.It is easy to see that there is an optimal solution to the problem that makes the greedy choice. Proof of 1. Let A be an optimal solution. Let activity 1 be the greedy choice. If 1  A the proof is done. If 1  A, we will show that A’=A-{a}+{1} is another optimal solution that includes 1. Let a be the activity with minimum finish time in A. Since activities are sorted by finishing time in the algorithm, f(1)  f(a). If f(1)  s(a) we could add 1 to A and it could not be optimal. So s(1) < f(a), and 1 and a overlap. Since f(1)  f(a), if we remove a and add 1 we get another compatible solution A’=A-{a}+{1} and |A’|=|A|

cs333/cutler Greedy18 Proof that greedy solution is optimal 2. If we combine the optimal solution of the remaining subproblem with the greedy choice we have an optimal solution to the original problem. Proof of 2. Let activity 1 be the greedy choice. Let S’ be the subset of activities that do not overlap with 1. S’={i|i =1,…,n and s i  f(1)}. Let B be an optimal solution for S’. From the definition of S’, A’={1}+B is compatible, and a solution to the original problem.

cs333/cutler Greedy19 Proof that greedy solution is optimal 2. If we combine the optimal solution of the remaining subproblem with the greedy choice we have an optimal solution to the original problem. Proof of 2 continued. The proof is by contradiction. Assume that A’ is not an optimal solution to the original problem. Let A be an optimal solution that contains 1. So |A’| |A’-{1}|=|B|. But A-{1} is also a solution to the problem of S’, contradicting the assumption that B is an optimal solution to S’.