Greedy Algorithms CLRS Laura Toma, csci2200, Bowdoin College

Similar documents
1 Non greedy algorithms (which we should have covered

Design and Analysis of Algorithms

Greedy algorithms is another useful way for solving optimization problems.

Applied Algorithm Design Lecture 3

Greedy Algorithms. CLRS Chapters Introduction to greedy algorithms. Design of data-compression (Huffman) codes

Greedy Algorithms CHAPTER 16

Analysis of Algorithms - Greedy algorithms -

Introduction to Algorithms / Algorithms I Lecturer: Michael Dinitz Topic: Dynamic Programming I Date: 10/6/16

LECTURE NOTES OF ALGORITHMS: DESIGN TECHNIQUES AND ANALYSIS

Greedy Algorithms. Informal Definition A greedy algorithm makes its next step based only on the current state and simple calculations on the input.

Design and Analysis of Algorithms

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Approximation algorithms Date: 11/27/18

Introduction to Algorithms / Algorithms I Lecturer: Michael Dinitz Topic: Approximation algorithms Date: 11/18/14

Design and Analysis of Algorithms

Algorithms Dr. Haim Levkowitz

Scribe: Virginia Williams, Sam Kim (2016), Mary Wootters (2017) Date: May 22, 2017

CS473-Algorithms I. Lecture 11. Greedy Algorithms. Cevdet Aykanat - Bilkent University Computer Engineering Department

We ve done. Now. Next

Dynamic Programming Assembly-Line Scheduling. Greedy Algorithms

Elements of Dynamic Programming. COSC 3101A - Design and Analysis of Algorithms 8. Discovering Optimal Substructure. Optimal Substructure - Examples

ECE608 - Chapter 16 answers

Chapter 16: Greedy Algorithm

Analysis of Algorithms Prof. Karen Daniels

Algorithms for Data Science

Greedy Algorithms. Previous Examples: Huffman coding, Minimum Spanning Tree Algorithms

CS141: Intermediate Data Structures and Algorithms Greedy Algorithms

Chapter 16. Greedy Algorithms

Greedy Algorithms. Algorithms

Problem Strategies. 320 Greedy Strategies 6

Main approach: always make the choice that looks best at the moment. - Doesn t always result in globally optimal solution, but for many problems does

Greedy Algorithms. Alexandra Stefan

Main approach: always make the choice that looks best at the moment.

16.Greedy algorithms

CSE 421 Applications of DFS(?) Topological sort

CSci 231 Final Review

Design and Analysis of Algorithms 演算法設計與分析. Lecture 7 April 15, 2015 洪國寶

Greedy Algorithms 1. For large values of d, brute force search is not feasible because there are 2 d

CSE541 Class 2. Jeremy Buhler. September 1, 2016

Longest Common Subsequence, Knapsack, Independent Set Scribe: Wilbur Yang (2016), Mary Wootters (2017) Date: November 6, 2017

IN101: Algorithmic techniques Vladimir-Alexandru Paun ENSTA ParisTech

Efficient Sequential Algorithms, Comp309. Problems. Part 1: Algorithmic Paradigms

Solving NP-hard Problems on Special Instances

4.1 Interval Scheduling

Treaps. 1 Binary Search Trees (BSTs) CSE341T/CSE549T 11/05/2014. Lecture 19

Subset sum problem and dynamic programming

Framework for Design of Dynamic Programming Algorithms

CMPSCI611: Approximating SET-COVER Lecture 21

Optimization I : Brute force and Greedy strategy

1 i n (p i + r n i ) (Note that by allowing i to be n, we handle the case where the rod is not cut at all.)

MergeSort, Recurrences, Asymptotic Analysis Scribe: Michael P. Kim Date: April 1, 2015

Dijkstra s algorithm for shortest paths when no edges have negative weight.

Planar convex hulls (I) Computational Geometry [csci 3250] Laura Toma Bowdoin College

Tutorial 6-7. Dynamic Programming and Greedy

Sankalchand Patel College of Engineering - Visnagar Department of Computer Engineering and Information Technology. Assignment

CS246: Mining Massive Datasets Jure Leskovec, Stanford University

Greedy Algorithms 1 {K(S) K(S) C} For large values of d, brute force search is not feasible because there are 2 d {1,..., d}.

15.4 Longest common subsequence

Announcements. CSEP 521 Applied Algorithms. Announcements. Polynomial time efficiency. Definitions of efficiency 1/14/2013

8 Matroid Intersection

Exam 3 Practice Problems

CSC 373: Algorithm Design and Analysis Lecture 3

Dynamic Programming. Introduction, Weighted Interval Scheduling, Knapsack. Tyler Moore. Lecture 15/16

Subsequence Definition. CS 461, Lecture 8. Today s Outline. Example. Assume given sequence X = x 1, x 2,..., x m. Jared Saia University of New Mexico

CMSC 451: Lecture 10 Dynamic Programming: Weighted Interval Scheduling Tuesday, Oct 3, 2017

Some Extra Information on Graph Search

1 Format. 2 Topics Covered. 2.1 Minimal Spanning Trees. 2.2 Union Find. 2.3 Greedy. CS 124 Quiz 2 Review 3/25/18

Computer Science 210 Data Structures Siena College Fall Topic Notes: Complexity and Asymptotic Analysis

Dynamic Programmming: Activity Selection

Design and Analysis of Algorithms 演算法設計與分析. Lecture 7 April 6, 2016 洪國寶

CS 473: Fundamental Algorithms, Spring Dynamic Programming. Sariel (UIUC) CS473 1 Spring / 42. Part I. Longest Increasing Subsequence

MergeSort, Recurrences, Asymptotic Analysis Scribe: Michael P. Kim Date: September 28, 2016 Edited by Ofir Geri

11.1 Facility Location

Application of Dynamic Programming to Calculate Denominations Used in Changes Effectively

CMSC 451: Dynamic Programming

CS 161 Computer Security

15-451/651: Design & Analysis of Algorithms November 4, 2015 Lecture #18 last changed: November 22, 2015

1 Achieving IND-CPA security

ESO207A: Data Structures and Algorithms End-semester exam

Algorithm Design Methods. Some Methods Not Covered

Table of Laplace Transforms

EECS 203 Spring 2016 Lecture 8 Page 1 of 6

Lecture 24: More Reductions (1997) Steven Skiena. skiena

COT 6405 Introduction to Theory of Algorithms

Algorithms Lab 3. (a) What is the minimum number of elements in the heap, as a function of h?

CS F-10 Greedy Algorithms 1

1 Greedy algorithms and dynamic programming

Objec&ves. Review. Directed Graphs: Strong Connec&vity Greedy Algorithms

Lecture 7: the quotient topology

Theorem 2.9: nearest addition algorithm

CIS 121 Data Structures and Algorithms Minimum Spanning Trees

Critical Analysis of Computer Science Methodology: Theory

Polynomial-Time Approximation Algorithms

Heaps. Heapsort. [Reading: CLRS 6] Laura Toma, csci2000, Bowdoin College

Unit-5 Dynamic Programming 2016

CS246: Mining Massive Datasets Jure Leskovec, Stanford University

(Refer Slide Time: 0:19)

16 Greedy Algorithms

Greedy algorithms part 2, and Huffman code

Exercises Optimal binary search trees root

Algorithms for Integer Programming

Transcription:

Greedy Algorithms CLRS 16.1-16.2 Laura Toma, csci2200, Bowdoin College Overview. Sometimes we can solve optimization problems with a technique called greedy. A greedy algorithm picks the option that looks best according to a quick evaluation criterion, without evaluating each choice recursively. Greedy algorithms are intuitive and seem (much?) easier to understand than dynamic programming. We ll see that it s quite easy to come up with different heuristic greedy strategies. However, many times (most of the time?) they do not lead to optimal solutions. Before we look at a specific example we underline the similarities and differences between greedy and dynamic programming. Dynamic programming: The problem must have the optimal substructure property: the optimal solution to the problem contains within it optimal solutions to subproblems. This allows for a recursive solution. Dynamic programming: Evaluate all options recursively, pick the best and recurse. An option is evaluated recursively, meaning all its options are evaluated and all their options are evaluated, and so on; basically to evaluate an option, you have to go through the whole recursion tree for that option. Dynamic programming is essentially smart recursion (recursion without repetition). The subproblems are stored in a table to ensure that they are computed at most once. Typically the number of different subproblems is small, but the recursive algorithm implementing the recursive formulation of the problem is exponential. This is because of overlapping calls to same subproblem. So if you don t use the table to cache partial solutions you incur a significant penalty (Often the difference is polynomial vs. exponential). Sometimes we might want to take dynamic programming a step further, and eliminate the recursion - this is purely for eliminating the overhead of recursion, and does not change the Θ() of the running time. We can think of dynamic programming as filling the table bottom-up, without recursion. The greedy technique: As with dynamic programming, in order to be solved with the greedy technique, the problem must have optimal substructure. The problems that can be solved with the greedy method are a (very small!) subset of those that can be solved with dynamic programming. 1

The idea of greedy technique is the following: Instead of evaluating all options recursively and picking the best one, we use a quick way to pick what looks locally like the best choice, add it to the solution, and repeat. So basically a greedy algorithm picks the locally optimal choice at each step, hoping to a solution that is globally optimal. Coming up with greedy heuristics is easy, but proving that a heuristic gives the optimal solution can be tricky. Let s look at an example. 1 Interval scheduling (Activity selection) Problem: Given a set A = {A 1, A 2,, A n } of n activities with start and finish times (s i, f i ), 1 i n, find a maximal set S of non-overlapping activities. This is a special case of the weighted-interval scheduling problem, where all intervals have the same weight. A possible practical interpretation: corresponds to scheduling the maximal number of classes (given their start and finish times) in one classroom. Or more exciting: get your money s worth at Disney Land! you are in the park, you have lots of rides to chose from, you know start and end time for each ride, you want to ride as many rides as possible. Take Algorithms! This can be solved with dynamic programming: Look at the first activity, try a solution with it and one without it, and see which one is better. We can make it run in O(n lg n). Note that with dynamic programming we evaluate recursively both options (current activity is in or out), and pick the best. With a greedy solution, we would find a quick way to pick one or the other option. Here are some possible greedy algorithms for activity selection: Greedy 1: Pick the shortest activity, eliminate all activities that overlap with it, and recurse. Clearly all we need to do is sort the activities, so this would run in O(n lg n) time. Does this work? Yeah it works in the sense that it comes up with a set of activities that are non-overlapping, but is this set of activities optimal (i.e. largest set possible)? Either we need to argue that this algorithm always gives an optimal solution; or, we need to give a counter-example (an instance where this strategy does NOT give the optimal solution). In this case it is fairly simple to find a counter-example: 2

Greedy 2: Pick the activity that starts first (eliminate all activities that overlap with it, and recurse). Counter-example: Greedy 3: Pick the activity that ends first (eliminate all activities that overlap with it, and recurse). Counter-example: 2 A greedy solution for Activity Selection Turns out that picking activities in order of their finish time gives the correct optimal solution. We ll argue below why. The intuition is that by picking what ends first, we maximize the remaining time. First let s spell out the idea a bit more: 1. Sort activity by finish time and renumber the activities so that A 1, A 2,, A n denotes sorted sequence. 2. Pick first activity A 1. 3. Compute B = set of activities in A that do not overlap with A 1. 4. Recursively solve problem on B. 2.1 Correctness Before we think about running time, we need to argue that this algorithm is correct. Greedy outputs a set of activities. 1. Are they non-overlapping? 2. Is this set the largest possible? It s easy to see that 1 is true. For 2, the crux of the proof is the following: Claim: Let A 1 be the activity with earliest finish time. Then there exists an optimal solution that includes A 1. Proof: Suppose O is an optimal solution (a non-overlapping subset of A of max size). If A 1 O, we are done If A 1 / O: Let first activity in O be A k 3

Make new solution O {A k } + {A 1 } by removing A k and using A 1 instead This is valid solution (because f 1 < f k ) of maximal size ( O 1 + 1 = O So this tells us that the first greedy choice is correct. What about the second one? Once the first greedy choice is made, we find an optimal solution for the remaining problem (the activities that don t conflict with A 1 ); this is the optimal substructure of the problem. The second activity chosen by the greedy solution is the activity that finishes first among all the activities that don t conflict with A 1, so applying the claim again to the remaining problem we know there must exist an optimal solution that includes this second greedy choice. And so on, it follows that at every step, the greedy choice stays ahead, and there exists an optimal solution that consists entirely of greedy choices. 2.2 Implementing the greedy idea The greedy idea above can be implemented in quadratic time: Sorting takes O(n lg n) time; step 2 takes O(1) time; step 3 can be implemented in O(n) time, and in step 4 we may recurse on n 1 activities, so overall this can be implemented in O(n 2 ) time. However, if we are a little more careful, once the activities are sorted, we can implement the greedy idea in O(n) time. The crucial observation is that the solution consists of greedy choices that are compatible with previous greedy choices; so we pick the first activity A 1, then we traverse A 2, A 3,.. in order, and discard all those that conflict with A 1 ; we pick the first activity that does not conflict with A 1. And so on. This leads to the following algorithm: Sort A by finish time. Schedule the first activity Then go through remaining activities in order and schedule the first activity that starts after the previously scheduled activity finishes. Repeat. GreedyActivitySelection (A[1..n]) Sort A by finish time S = {A 1 } j = 1 FOR i = 2 to n DO IF s i f j THEN S = S {A i } j = i 4

Analysis: Running time is O(n lg n) + O(n) = O(n lg n). Example: Trace the algorithm on the following 11 activities (already sorted by finish time): (1, 4), (3, 5), (0, 6), (5, 7), (3, 8), (5, 9), (6, 10), (8, 11), (8, 12), (2, 13), (12, 14) 5

3 Comments, etc A complete greedy correctness proof has three parts: 1. Prove that there exists an optimal solution which contains the first greedy choice. 2. Prove optimal sub-structure. 3. Induction on the solution size to prove that there exists an optimal solution that consists entirely of greedy choices. Part 2 and 3 are usually omitted (because it s a fairly similar proof for all problems). To prove that a greedy algorithm is correct it suffices to prove part 1, namely that there exists an optimal solution which contains the first greedy choice. A greedy algorithm chooses what looks like the best solution at any given moment; its choice is local and does not depend on solution to subproblems. (Greediness is shortsightedness: Always go for seemingly next best thing, optimizing the present without regard for the future, and never change past choices). In theory, we are only interested in greedy algorithms that are provably optimal. Most optimization problems cannot be solved greedily. Or put differently, the number of problems that have greedy solutions is very small (so chances are that if you came up with a greedy algorithms its probably wrong). The greedy technique can be applied to pretty much any optimization problem and is very popular in AI. There one deals with exponential problems or infinite search spaces, and one cannot solve these problems optimally, so the expectations are different. To emphasize that they are not optimal they are usually referred to as greedy heuristics. It is often hard to figure out when being greedy gives the optimal solution! Problems that look very similar may have very different solutions. Example: 0 1 knapsack problem: Given n items, with item i being worth $ v i and having weight w i pounds, fill knapsack of capacity w pounds with maximal value. Fractional knapsack problem it s the same, but we can take fractions of items. fractional knapsack can be solved greedily: Compute value per pound v i w i for each item Sort items by value per pound. Fill knapsack greedily (take objects in order) Runs in O(n log n) time Can be shown that solution is optimal. 6