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

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

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

Algorithm Design and Analysis

1 Some loose ends from last time

Greedy Algorithms. Kleinberg and Tardos, Chapter 4

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

CSE 421 Greedy Algorithms / Interval Scheduling

Algorithm Design and Analysis

CMPSCI611: The Matroid Theorem Lecture 5

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

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.

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

CS 580: Algorithm Design and Analysis

CS781 Lecture 3 January 27, 2011

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

0 1 d 010. h 0111 i g 01100

Discrete Optimization 2010 Lecture 2 Matroids & Shortest Paths

Trees. A tree is a graph which is. (a) Connected and. (b) has no cycles (acyclic).

Recap & Interval Scheduling

Lecture 6: Greedy Algorithms I

10.3 Matroids and approximation

Proof methods and greedy algorithms

Greedy Alg: Huffman abhi shelat

Algorithm Design and Analysis

CS 580: Algorithm Design and Analysis

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

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

Homework Assignment 1 Solutions

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

Greedy Algorithms My T. UF

1 Matroid intersection

Algorithms Exam TIN093 /DIT602

Minimizing the Number of Tardy Jobs

Design and Analysis of Algorithms

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

Lecture 10 February 4, 2013

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

Knapsack and Scheduling Problems. The Greedy Method

HW Graph Theory SOLUTIONS (hbovik) - Q

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

The Matching Polytope: General graphs

Algorithm Design and Analysis

CS 161: Design and Analysis of Algorithms

CSEP 521 Applied Algorithms. Richard Anderson Winter 2013 Lecture 1

Design and Analysis of Algorithms

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

Fall 2017 November 10, Written Homework 5

NATIONAL UNIVERSITY OF SINGAPORE CS3230 DESIGN AND ANALYSIS OF ALGORITHMS SEMESTER II: Time Allowed 2 Hours

1 Ordinary Load Balancing

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

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

CSE 421 Dynamic Programming

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

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

Design and Analysis of Algorithms

Algorithms and Data Structures 2016 Week 5 solutions (Tues 9th - Fri 12th February)

Lecture 11: Generalized Lovász Local Lemma. Lovász Local Lemma

The domination game played on unions of graphs

CSE548, AMS542: Analysis of Algorithms, Fall 2016 Date: Nov 30. Final In-Class Exam. ( 7:05 PM 8:20 PM : 75 Minutes )

Algorithms and Theory of Computation. Lecture 13: Linear Programming (2)

K-center Hardness and Max-Coverage (Greedy)

Markov Decision Processes

Research Methods in Mathematics Homework 4 solutions

Lecture 5: January 30

CS 6901 (Applied Algorithms) Lecture 3

Lecture 9. Greedy Algorithm

CSE 4502/5717: Big Data Analytics

Weighted Activity Selection

Lecture 13: Spectral Graph Theory

Omega notation. Transitivity etc.

General Methods for Algorithm Design

ACO Comprehensive Exam 19 March Graph Theory

CSE 421 Greedy: Huffman Codes

5.5 Deeper Properties of Continuous Functions

HOMEWORK #2 - MATH 3260

Ring Sums, Bridges and Fundamental Sets

A necessary and sufficient condition for the existence of a spanning tree with specified vertices having large degrees

Lecture 2: Scheduling on Parallel Machines

Lexicographic Flow. Dexter Kozen Department of Computer Science Cornell University Ithaca, New York , USA. June 25, 2009

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

2. A vertex in G is central if its greatest distance from any other vertex is as small as possible. This distance is the radius of G.

Cographs; chordal graphs and tree decompositions

Analysis of Algorithms. Outline. Single Source Shortest Path. Andres Mendez-Vazquez. November 9, Notes. Notes

More Approximation Algorithms

CS2223 Algorithms D Term 2009 Exam 3 Solutions

25 Minimum bandwidth: Approximation via volume respecting embeddings

List of Theorems. Mat 416, Introduction to Graph Theory. Theorem 1 The numbers R(p, q) exist and for p, q 2,

Divide-and-Conquer Algorithms Part Two

CS1800: Mathematical Induction. Professor Kevin Gold

Clock-driven scheduling

Countable Menger s theorem with finitary matroid constraints on the ingoing edges

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

3 Greedy Algorithms. 3.1 An activity-selection problem

University of California Berkeley CS170: Efficient Algorithms and Intractable Problems November 19, 2001 Professor Luca Trevisan. Midterm 2 Solutions

Advanced Combinatorial Optimization September 22, Lecture 4

CSE 421 Algorithms. Richard Anderson Autumn 2015 Lecture 1

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

Analysis of Algorithms - Midterm (Solutions)

CSE 591 Foundations of Algorithms Homework 4 Sample Solution Outlines. Problem 1

Transcription:

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

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 today, 11:59 PM Homework 5 will be assigned today Due Nov 6, 11:59 PM CSE 101, Fall 2018 2

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 3

Problem specification Design an algorithm that uses the greedy choice of picking the next available event with the earliest end time Instance: n 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 4

Greedy stays ahead Instead of just first greedy choice, compare all of the greedy algorithm s solution to all of the other algorithm s solution OS GS J 1 J 2 J 3 J k E 1 E 2 E 3 E L What to show: L k, but indirectly by comparing some progress measure of GS to OS In what way is E1 better than J1, E2 better than J2, etc.? CSE 101, Fall 2018 5

Greedy stays ahead Suppose input is a set of n events {A 1,, A n } for some n 1 Given: an arbitrary solution OS = {J 1,, J k } ordered by finish time and greedy solution GS = {E 1,, E L } ordered by finish time Claim: Finish(E i ) Finish(J i ) for all i 1 OS GS J 1 J 2 J 3 J k E 1 E 2 E 3 E L CSE 101, Fall 2018 6

Greedy stays ahead OS GS J 1 J 2 J 3 J k E 1 E 2 E 3 Claim: Finish (E i ) Finish(J i ) for all i 1 Base case: Finish(E 1 ) Finish(J 1 ) because of greedy choice Inductive hypothesis: Suppose that for some i 1, Finish(E i ) Finish(J i ) What to show: Finish(E i+1 ) Finish(J i+1 ) E L CSE 101, Fall 2018 7

Greedy stays ahead OS GS J 1 J 2 J 3 J k E 1 E 2 E 3 Claim: Finish (E i ) Finish(J i ) for all i 1 What to show: Finish(E i+1 ) Finish(J i+1 ) Finish(J i ) Start(J i+1 ) Finish(E i ) Finish(J i ), inductive hypothesis So, Finish(E i ) Finish(J i ) Start(J i+1 ) E i+1 is the first to finish after Finish(E i ), definition of greedy J i+1 is in the set of available events, so Finish(E i+1 ) Finish(J i+1 ) E L CSE 101, Fall 2018 8

Greedy stays ahead Suppose by contradiction that OS has more events than GS OS = k, GS = L In other words, L < k E L is the final greedy choice, so there are no other events that end after E L that do not conflict with E L By inductive argument: Finish(E L ) Finish(J L ) Finish (J L ) Start(J L+1 ) Then greedy would not end with E L because J L+1 is still available. Contradiction CSE 101, Fall 2018 9

Greedy stays ahead template Define progress measure Order the decisions in OS to line up with GS Prove by induction that the progress after the i-th decision in GS is at least as big as that in OS Assume that OS is strictly better than GS Use progress argument to arrive at contradiction CSE 101, Fall 2018 10

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 11

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 12

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? Instance: Start and end times of n events Solution Format: an assignment of each event to a room Constraints: No two events that overlap are assigned to the same room Objective: minimize the number of rooms used CSE 101, Fall 2018 13

Implementation Sort both start and finish times of events Keep priority queue of available rooms ordered by room number Go through sorted lists of times When an event starts, assign it to the smallest room in the priority queue, and delete that room from the priority queue When an event finishes, insert the room it is scheduled in back into the priority queue N inserts, N deletes, O(N log N) time to sort: O(N log N) time total CSE 101, Fall 2018 14

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 15

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 E i1,, E i B t 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 L = max B(t). Then, L is the lower bound on the number of t rooms needed. CSE 101, Fall 2018 16

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 17

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 18

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 L = 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 t CSE 101, Fall 2018 19

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 20

KRUSKAL S ALGORITHM, PROOF OF CORRECTNESS CSE 101, Fall 2018 21

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

Correctness proof, greedy modify the solution The first greedy choice is the smallest weight edge. Let e be the smallest weight edge and let OT be any spanning tree that does not contain e. Construct OT by adding e to OT then removing any other edge e in the cycle that was created We must show that 1. OT is a spanning tree 2. w OT w(ot) e CSE 101, Fall 2018 23

Correctness proof, greedy modify the solution The first greedy choice is the smallest weight edge. Let e be the smallest weight edge and let OT be any spanning tree that does not contain e. Construct OT by adding e to OT then removing any other edge e in the cycle that was created We must show that 1. OT is a spanning tree 2. w OT w(ot) e e CSE 101, Fall 2018 24

Correctness proof, greedy modify the solution The first greedy choice is the smallest weight edge. Let e be the smallest weight edge and let OT be any spanning tree that does not contain e. Construct OT by adding e to OT then removing any other edge e in the cycle that was created We must show that 1. OT is a spanning tree 2. w OT w(ot) e e CSE 101, Fall 2018 25

Correctness proof, greedy modify the solution We must show that 1. OT is a spanning tree 2. w OT w(ot) 1. We have taken out an edge and added in an edge. We have a graph on n vertices with n-1 edges that does not contain a cycle. Therefore, the graph is a spanning tree. 2. w OT = w OT + w e w e w(ot) e e CSE 101, Fall 2018 26

Correctness proof, greedy modify the solution We must show that 1. OT is a spanning tree 2. w OT w(ot) 1. We have taken out an edge and added in an edge. We have a graph on n vertices with n-1 edges that does not contain a cycle. Therefore, the graph is a spanning tree. 2. w OT = w OT + w e w e w(ot) e e CSE 101, Fall 2018 27

Correctness proof, greedy modify the solution We must show that 1. OT is a spanning tree 2. w OT w(ot) 1. We have taken out an edge and added in an edge. We have a graph on n vertices with n-1 edges that does not contain a cycle. Therefore, the graph is a spanning tree. 2. w OT = w OT + w e w e w(ot) e e CSE 101, Fall 2018 28

Correctness proof, greedy modify the solution We must show that 1. OT is a spanning tree 2. w OT w(ot) 1. We have taken out an edge and added in an edge. We have a graph on n vertices with n-1 edges that does not contain a cycle. Therefore, the graph is a spanning tree. 2. w OT = w OT + w e w e w(ot) 0 e e CSE 101, Fall 2018 29

General greedy modify the solution template, induction Lemma: Let g be the first greedy decision. Let OS be any legal solution that does not pick g. Then, there is a solution OS that does pick g and OS is at least as good as OS. 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 30

Induction step If G has at most two vertices, any solution is optimal Assume Kruskal s algorithm is optimal for any graph with n-1 vertices Let e be the smallest weight edge G : Contract the edge e in G, treating its two vertices as one vertex CSE 101, Fall 2018 31

Contraction e CSE 101, Fall 2018 32

Contraction Contracted graph is not necessarily simple single vertex CSE 101, Fall 2018 33

Induction on number of vertices Base case: Kruskal s algorithm generates a minimum spanning tree on any graph with at most two vertices Inductive Hypothesis: Suppose Kruskal s algorithm generates a minimum spanning tree for every graph of k vertices for some k 2 CSE 101, Fall 2018 34

Induction on number of vertices Inductive step: Let G be an arbitrary graph with k+1 vertices. Let OT be any spanning tree of G. Then, by the greedy modify the solution lemma, there exists a spanning tree OT that uses the lightest edge e and w OT w(ot) Contract the two endpoints of e into one vertex and call the resulting graph G Then, since G has k vertices, kruskal(g ) will generate a minimum spanning tree of G w OT w OT = w e + w S G w e + w(kruskal G ) = w(kruskal G ) Greedy modify the solution lemma By definition of OT By inductive hypothesis By definition of Kruskal s algorithm CSE 101, Fall 2018 35

Greedy algorithms Next lecture Reading: Kleinberg and Tardos, sections 4.1, 4.2, and 4.3 Divide and conquer algorithms Reading: Sections 2.1 and 2.2 CSE 101, Fall 2018 36