Greedy Algorithms. CSE 101: Design and Analysis of Algorithms Lecture 9

Similar documents
Greedy Algorithms. CSE 101: Design and Analysis of Algorithms Lecture 10

Algorithm Design and Analysis

CSE 421 Greedy Algorithms / Interval Scheduling

Greedy Algorithms. Kleinberg and Tardos, Chapter 4

Algorithm Design and Analysis

Chapter 4. Greedy Algorithms. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

CS 374: Algorithms & Models of Computation, Spring 2017 Greedy Algorithms Lecture 19 April 4, 2017 Chandra Chekuri (UIUC) CS374 1 Spring / 1

CSE101: Design and Analysis of Algorithms. Ragesh Jaiswal, CSE, UCSD

CSE 417. Chapter 4: Greedy Algorithms. Many Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

Chapter 4. Greedy Algorithms. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

Chapter 4. Greedy Algorithms. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

CS 6901 (Applied Algorithms) Lecture 3

Lecture 6: Greedy Algorithms I

Design and Analysis of Algorithms

CS 580: Algorithm Design and Analysis

Announcements. CompSci 102 Discrete Math for Computer Science. Chap. 3.1 Algorithms. Specifying Algorithms

0 1 d 010. h 0111 i g 01100

Knapsack and Scheduling Problems. The Greedy Method

CS781 Lecture 3 January 27, 2011

CS 580: Algorithm Design and Analysis

Recap & Interval Scheduling

CMSC 451: Lecture 7 Greedy Algorithms for Scheduling Tuesday, Sep 19, 2017

1 Basic Definitions. 2 Proof By Contradiction. 3 Exchange Argument

CS 6783 (Applied Algorithms) Lecture 3

Greedy Algorithms My T. UF

More Approximation Algorithms

CSE 421 Dynamic Programming

The Greedy Method. Design and analysis of algorithms Cs The Greedy Method

Dynamic Programming( Weighted Interval Scheduling)

1 Some loose ends from last time

Proof methods and greedy algorithms

Outline / Reading. Greedy Method as a fundamental algorithm design technique

Divide and Conquer Algorithms. CSE 101: Design and Analysis of Algorithms Lecture 14

CSEP 521 Applied Algorithms. Richard Anderson Winter 2013 Lecture 1

CS 161: Design and Analysis of Algorithms

This means that we can assume each list ) is

Dynamic Programming: Interval Scheduling and Knapsack

Discrete Optimization 2010 Lecture 2 Matroids & Shortest Paths

Real Analysis on Metric Spaces

Design and Analysis of Algorithms

Algorithms Exam TIN093 /DIT602

Proving languages to be nonregular

10-704: Information Processing and Learning Fall Lecture 10: Oct 3

Fall 2017 November 10, Written Homework 5

Algorithm Design Strategies V

CSE 421 Algorithms. Richard Anderson Autumn 2015 Lecture 1

CS 2110: INDUCTION DISCUSSION TOPICS

A lower bound for scheduling of unit jobs with immediate decision on parallel machines

Discrete Wiskunde II. Lecture 5: Shortest Paths & Spanning Trees

cse547, math547 DISCRETE MATHEMATICS Professor Anita Wasilewska

Minimizing the Number of Tardy Jobs

NP-complete problems. CSE 101: Design and Analysis of Algorithms Lecture 20

6. DYNAMIC PROGRAMMING I

CMPSCI611: The Matroid Theorem Lecture 5

Definitions. Notations. Injective, Surjective and Bijective. Divides. Cartesian Product. Relations. Equivalence Relations

Algorithm Design and Analysis

A n = A N = [ N, N] A n = A 1 = [ 1, 1]. n=1

Algorithms and Theory of Computation. Lecture 9: Dynamic Programming

CS 561, Lecture: Greedy Algorithms. Jared Saia University of New Mexico

IS 709/809: Computational Methods in IS Research Fall Exam Review

3 Greedy Algorithms. 3.1 An activity-selection problem

Today s Outline. CS 561, Lecture 15. Greedy Algorithms. Activity Selection. Greedy Algorithm Intro Activity Selection Knapsack

CS261: Problem Set #3

CSC 344 Algorithms and Complexity. Proof by Mathematical Induction

Convexity in R N Supplemental Notes 1

Welcome to CSE21! Lecture B Miles Jones MWF 9-9:50pm PCYN 109. Lecture D Russell (Impagliazzo) MWF 4-4:50am Center 101

Definition 2.3. We define addition and multiplication of matrices as follows.

Homework 1 (revised) Solutions

CSE 421 Greedy: Huffman Codes

Today s exercises. 5.17: Football Pools. 5.18: Cells of Line and Hyperplane Arrangements. Inclass: PPZ on the formula F

Exercises. Template for Proofs by Mathematical Induction

University of Toronto Department of Electrical and Computer Engineering. Final Examination. ECE 345 Algorithms and Data Structures Fall 2016

CS325: Analysis of Algorithms, Fall Final Exam

8. INTRACTABILITY I. Lecture slides by Kevin Wayne Copyright 2005 Pearson-Addison Wesley. Last updated on 2/6/18 2:16 AM

Lecture 04: Secret Sharing Schemes (2) Secret Sharing

CSE 431/531: Analysis of Algorithms. Dynamic Programming. Lecturer: Shi Li. Department of Computer Science and Engineering University at Buffalo

Aditya Bhaskara CS 5968/6968, Lecture 1: Introduction and Review 12 January 2016

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Matroids and Greedy Algorithms Date: 10/31/16

Math 421, Homework #6 Solutions. (1) Let E R n Show that = (E c ) o, i.e. the complement of the closure is the interior of the complement.

Greedy Homework Problems

Impossibility of Distributed Consensus with One Faulty Process

Chapter 2. Mathematical Reasoning. 2.1 Mathematical Models

Activity selection. Goal: Select the largest possible set of nonoverlapping (mutually compatible) activities.

Lecture 9. Greedy Algorithm

Recursion: Introduction and Correctness

Weighted Activity Selection

1 Markov decision processes

12. LOCAL SEARCH. gradient descent Metropolis algorithm Hopfield neural networks maximum cut Nash equilibria

Dynamic Programming. Cormen et. al. IV 15

Advanced Analysis of Algorithms - Midterm (Solutions)

Impagliazzo s Hardcore Lemma

Coin Changing: Give change using the least number of coins. Greedy Method (Chapter 10.1) Attempt to construct an optimal solution in stages.

To hand in: (a) Prove that a group G is abelian (= commutative) if and only if (xy) 2 = x 2 y 2 for all x, y G.

CS583 Lecture 11. Many slides here are based on D. Luebke slides. Review: Dynamic Programming

Fall 2015 Lecture 14: Modular congruences. cse 311: foundations of computing

CS2223 Algorithms D Term 2009 Exam 3 Solutions

Greedy. Outline CS141. Stefano Lonardi, UCR 1. Activity selection Fractional knapsack Huffman encoding Later:

10.3 Matroids and approximation

Operations and Supply Chain Management Prof. G. Srinivasan Department of Management Studies Indian Institute of Technology, Madras

Clock-driven scheduling

Transcription:

Greedy Algorithms CSE 101: Design and Analysis of Algorithms Lecture 9

CSE 101: Design and analysis of algorithms Greedy algorithms Reading: Kleinberg and Tardos, sections 4.1, 4.2, and 4.3 Homework 4 is due Oct 30, 11:59 PM CSE 101, Fall 2018 2

Event scheduling 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 Based on slides courtesy of Miles Jones CSE 101, Fall 2018 3

Event scheduling Your goal is to schedule the most events possible that day such that no two events overlap Exponential is too slow. Let s try some greedy strategies: Shortest duration Earliest start time Fewest conflicts (take away most conflicts) Earliest end time CSE 101, Fall 2018 4

Shortest duration 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 CSE 101, Fall 2018 5

Shortest duration 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 CSE 101, Fall 2018 6

Earliest start time 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 CSE 101, Fall 2018 7

Earliest start time 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 CSE 101, Fall 2018 8

Fewest conflicts 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 CSE 101, Fall 2018 9

Counterexample for fewest conflicts 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 CSE 101, Fall 2018 10

Counterexample for fewest conflicts 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 7 conflicts 4 conflicts 8 conflicts 8 conflicts 8 conflicts 8 conflicts 7 conflicts CSE 101, Fall 2018 11

Earliest end time 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 CSE 101, Fall 2018 12

Event scheduling Your goal is to schedule the most events possible that day such that no two events overlap Exponential is too slow. Let s try some greedy strategies: Shortest duration Earliest start time Fewest conflicts Earliest end time (we cannot find a counterexample!) CSE 101, Fall 2018 13

Problem specification Design an algorithm that uses the greedy choice of picking the next available event with the earliest end time Instance: events E 1,, E n each with a start time s and end time f ; E i = (s i, f i ) Solution format: list of events Constraints: events cannot overlap Objective: maximize the number of events CSE 101, Fall 2018 14

Event scheduling implementation Initialize a queue Sort the intervals by end time Put the first event in Set For 2 : If : enqueue(, Return F is finish time of last event put in S Else, s i < F, E i conflicts with last event put in S CSE 101, Fall 2018 15

Proving optimization algorithms correct Remember what we need to show Let I be any instance of our problem, GS the greedy algorithm s solution, and OS any other solution If minimization: Cost (OS) Cost(GS) If maximization: Value(GS) Value (OS) CSE 101, Fall 2018 16

Proving optimality What does it mean that the greedy algorithm solves an optimization problem? I: problem instance GS: greedy solution to I OS: other solution to I For the greedy solution to be optimal, this means that Value(OS) Value (GS) for maximization (or Cost(GS) Cost (OS) for minimization) We need to show For every instance I, let GS be the greedy algorithm s solution to I. Let OS be any other solution for I. Then, Value(OS) Value (GS) (or Cost(GS) Cost (OS) for minimization) Tricky part: OS is arbitrary solution, not one that makes sense. We do not know much about it. CSE 101, Fall 2018 17

Techniques to prove optimality We will look at a number of general methods to prove optimality Greedy modify the solution (also referred to as greedy exchange): most general Greedy stays ahead: more intuitive Greedy achieves the bound: also comes up in approximation, linear programming, network flow Which one to use is up to you, but modify the solution applies almost universally. Others can be easier, but only work in special cases. CSE 101, Fall 2018 18

Greedy modify the solution General structure of modify the solution 1. Let be the first greedy choice the algorithm makes 2. Let be a solution achieved by not choosing 3. Show how to transform into some solution that chooses, and that is at least as good as Must show that is a valid solution and that is better than or equal to 4. Use 1 3 in an inductive argument Must show that is a valid solution and that is better than or equal to Base Case: the greedy strategy works for an instance of size 1 Assume the greedy strategy works for all instances of size Let be any solution for instance,. Then, there is another solution such that and includes the first greedy choice CSE 101, Fall 2018 19

Earliest end time, greedy modify the solution Correctness: Let, be the set of all events with the start time and finish time of Greedy modify the solution: Say is the event with the earliest finish time ( is the first greedy choice) Let be a legal schedule that does not include Claim: there is a schedule O that does include such that Proof: (greedy modify the solution) Let the events in be,,, ordered by start and finish times ( ) Define from CSE 101, Fall 2018 20

Earliest end time, greedy modify the solution What we have to start OS First greedy decision Earlier finish time than J 1 Our to do list Define must have no overlaps must include CSE 101, Fall 2018 21

Define OS First greedy decision = OS events in that conflict with is valid (there are no conflicts, by design) What to show: CSE 101, Fall 2018 22

Define OS First greedy decision = OS, if intersects with = OS, otherwise CSE 101, Fall 2018 23

has no overlaps = OS, if intersects with = OS, otherwise Only new place for overlaps: with, so we need to show CSE 101, Fall 2018 24

has no overlaps = OS, if intersects with = OS, otherwise Only new place for overlaps: with, so we need to show CSE 101, Fall 2018 25

is at least as good as = OS, if intersects with = OS, otherwise Either of the above = OS +1-1= OS, or = OS +1 > OS CSE 101, Fall 2018 26

Induction There is an optimal solution that always picks the greedy choice Proof by strong induction on, the number of events Base case: 0or 1. The greedy (actually, any) choice works. Inductive hypothesis (strong) Assume that the greedy algorithm is optimal for any events for 0 1(i.e., if,then for any solution, ) Goal: greedy is optimal for any events CSE 101, Fall 2018 27

Induction Inductive Step: Let be any solution of the set of events,,. Then, there exists a solution such that and includes the first greedy choice. Let be the set of events that do not conflict with Then,,where is some solution of Since, by the inductive hypothesis, And, by definition, Conclusion The greedy solution GS is optimal for every set of events CSE 101, Fall 2018 28

Induction Inductive Step: Let be any solution of the set of events,,. Then, there exists a solution such that and includes the first greedy choice. Let be the set of events that do not conflict with Then,,where is some solution of Since, by the inductive hypothesis, And, by definition, Conclusion The greedy solution GS is optimal for every set of events CSE 101, Fall 2018 29

General greedy modify the solution template Lemma: Let g be the first greedy decision. Let be any legal solution that does not pick g. Then, there is a solution that does pick g and is at least as good as. 1. State what we know: Definition of g. meets constraints. 2. Define from, g 3. Prove that meets constraints (use 1, 2) 4. Compare value/cost of to (use 2, sometimes 1) CSE 101, Fall 2018 30

General greedy modify the solution template, proof of lemma Lemma: Let g be the first greedy decision. Let be any legal solution that does not pick g. Then, there is a solution that does pick g and is at least as good as. 1. State what we know: Definition of g. meets constraints. 2. Define from, g This requires creativity 3. Prove that meets constraints (use 1, 2) 4. Compare value/cost of to (use 2, sometimes 1) CSE 101, Fall 2018 31

General greedy modify the solution template, induction Lemma: Let g be the first greedy decision. Let be any legal solution that does not pick g. Then, there is a solution that does pick g and is at least as good as. Prove by strong induction on instance size that GS is optimal Induction step 1. Let g be first greedy decision. Let I be rest of problem given g 2. GS = g + GS(I ) 3. OS is any legal solution 4. OS is defined from OS by the modify the solution argument (if OS does not include g) 5. OS = g + some solution on I 6. Induction: GS(I ) at least as good as some solution on I 7. GS is at least as good as OS, which is at least as good as OS CSE 101, Fall 2018 32

Event scheduling implementation Initialize a queue Sort the intervals by end time Put the first event in Set For 2 : If : enqueue(, Return Another method: greedy achieves the bound CSE 101, Fall 2018 33

Greedy achieves the bound This is a proof technique that does not work in all cases The way it works is to argue that when the greedy solution reaches its peak cost, it reveals a bound Then, show this bound is also a lower bound on the cost of any other solution So we are showing: Cost(GS) Bound Cost (OS) Allows the two inequalities to be separated CSE 101, Fall 2018 34

Event scheduling with multiple rooms Suppose you have a conference to plan with n events and you have an unlimited supply of rooms. How can you assign events to rooms in such a way as to minimize the number of rooms? Greedy choice: Run previous algorithm and put results in room 1. Repeat on remainder and put results in room 2. Repeat until all events have a room. CSE 101, Fall 2018 35

Event scheduling with multiple rooms Suppose you have a conference to plan with n events and you have an unlimited supply of rooms. How can you assign events to rooms in such a way as to minimize the number of rooms? Greedy choice: Run previous algorithm and put results in room 1 Repeat on remainder and put results in room 2 Repeat until all events have a room CSE 101, Fall 2018 36

Counterexample 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 We only need 2 rooms CSE 101, Fall 2018 37

Event scheduling with multiple rooms Suppose you have a conference to plan with n events and you have an unlimited supply of rooms. How can you assign events to rooms in such a way as to minimize the number of rooms? Greedy choice: Number the rooms from 1 to n Sort the events by earliest start time Put the first event in room 1 For events 2,, n, put each event in the smallest numbered room that is available CSE 101, Fall 2018 38

When does GS reach peak cost? Room 1 2 3 4 Why did we have to use four rooms? What happened at the time we reached "peak cost"? CSE 101, Fall 2018 39

Defining the lower bound Let t be a certain time during the conference Let B(t) be the set of all events E such that t E Let R be the number of rooms you need for a valid schedule Then, R B(t) for all t Proof For any time t, let,, be all the events in B(t) Then, since they all are happening at time t, they all have to be in different rooms, so R B(t) Let max B(t). Then, L is the lower bound on the number of rooms needed. CSE 101, Fall 2018 40

Greedy achieves the bound Let k be the number of rooms picked by the greedy algorithm. Then, at some point t, B(t) k (i.e., there are at least k events happening at time t). Proof Let t be the starting time of the first event to be scheduled in room k Then, by the greedy choice, room k was the least number room available at that time This means at time t there was an event happening in room 1, room 2,, room k 1. And, an additional event happening in room k Therefore, B(t) k at some point t CSE 101, Fall 2018 41

Conclusion: greedy is optimal The greedy algorithm uses the minimum number of rooms Let GS be the greedy solution, k = Cost(GS) the number of rooms used in the greedy solution Let k be the number of rooms the greedy algorithm uses and let R be any valid schedule of rooms. There exists a t such that at all time, k events are happening simultaneously. So R uses at least k rooms. So, R uses at least as many rooms as the greedy solution. Therefore, the greedy solution is optimal. CSE 101, Fall 2018 42

Conclusion: greedy is optimal Let GS be the greedy solution, k = Cost(GS) the number of rooms used in the greedy solution Let OS be any other schedule, R= Cost(OS) the number of rooms used in OS By the bounding lemma, R max B(t) By the achieves the bound lemma, k = B(t) L for some t Putting the two together, Cost(GS) =k R= Cost(OS) Thus, the greedy solution is optimal CSE 101, Fall 2018 43

Greedy algorithms Next lecture Reading: Kleinberg and Tardos, sections 4.1, 4.2, and 4.3 CSE 101, Fall 2018 44