CSE 421 Greedy Algorithms / Interval Scheduling

Similar documents
Algorithm Design and Analysis

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.

Greedy Algorithms. Kleinberg and Tardos, Chapter 4

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

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

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

Algorithm Design and Analysis

Design and Analysis of Algorithms

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

CSE 421 Dynamic Programming

CS 580: Algorithm Design and Analysis

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

Proof methods and greedy algorithms

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

CS 580: Algorithm Design and Analysis

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

Greedy Algorithms My T. UF

Dynamic Programming: Interval Scheduling and Knapsack

Lecture 6: Greedy Algorithms I

CS781 Lecture 3 January 27, 2011

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

Algorithms: COMP3121/3821/9101/9801

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

CS 161: Design and Analysis of Algorithms

Dynamic Programming( Weighted Interval Scheduling)

Minimizing the Number of Tardy Jobs

6. DYNAMIC PROGRAMMING I

CSE 421 Weighted Interval Scheduling, Knapsack, RNA Secondary Structure

6. DYNAMIC PROGRAMMING I

Approximation Algorithms (Load Balancing)

CS 6901 (Applied Algorithms) Lecture 3

APTAS for Bin Packing

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

Lecture 2: Divide and conquer and Dynamic programming

Matching Residents to Hospitals

Dynamic Programming. Cormen et. al. IV 15

CSEP 521 Applied Algorithms. Richard Anderson Winter 2013 Lecture 1

FINAL EXAM PRACTICE PROBLEMS CMSC 451 (Spring 2016)

CSE 202 Dynamic Programming II

More Approximation Algorithms

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

CS Lunch. Dynamic Programming Recipe. 2 Midterm 2! Slides17 - Segmented Least Squares.key - November 16, 2016

Simple Dispatch Rules

Knapsack and Scheduling Problems. The Greedy Method

Aperiodic Task Scheduling

Lecture 3: Big-O and Big-Θ

Algorithm Design Strategies V

CS 6783 (Applied Algorithms) Lecture 3

This means that we can assume each list ) is

1 Some loose ends from last time

0 1 d 010. h 0111 i g 01100

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

Algorithm Design. Scheduling Algorithms. Part 2. Parallel machines. Open-shop Scheduling. Job-shop Scheduling.

Recursion: Introduction and Correctness

34 th United States of America Mathematical Olympiad

(a) Write a greedy O(n log n) algorithm that, on input S, returns a covering subset C of minimum size.

Analysis of Algorithms - Midterm (Solutions)

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

1 Ordinary Load Balancing

Undirected Graphs. V = { 1, 2, 3, 4, 5, 6, 7, 8 } E = { 1-2, 1-3, 2-3, 2-4, 2-5, 3-5, 3-7, 3-8, 4-5, 5-6 } n = 8 m = 11

Quick Sort Notes , Spring 2010

Embedded Systems 14. Overview of embedded systems design

Greedy Homework Problems

Algorithms: Lecture 12. Chalmers University of Technology

UCSD CSE 21, Spring 2014 [Section B00] Mathematics for Algorithm and System Analysis

Algorithms for Bioinformatics

Scheduling Lecture 1: Scheduling on One Machine

Static-Priority Scheduling. CSCE 990: Real-Time Systems. Steve Goddard. Static-priority Scheduling

CSE 421 Greedy: Huffman Codes

8.5 Sequencing Problems

EECS 477: Introduction to algorithms. Lecture 12

Linear Classification: Linear Programming

arxiv: v1 [cs.ds] 30 Jun 2016

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

Embedded Systems 15. REVIEW: Aperiodic scheduling. C i J i 0 a i s i f i d i

Online Interval Coloring and Variants

Algoritmiek, bijeenkomst 3

Lecture 9. Greedy Algorithm

General Methods for Algorithm Design

PGSS Discrete Math Solutions to Problem Set #4. Note: signifies the end of a problem, and signifies the end of a proof.

1 Terminology and setup

Algorithms Exam TIN093 /DIT602

Single Machine Models

Exercises. Template for Proofs by Mathematical Induction

Linear Classification: Linear Programming

Dynamic Programming. Data Structures and Algorithms Andrei Bulatov

Handout 2 (Correction of Handout 1 plus continued discussion/hw) Comments and Homework in Chapter 1

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

Recap & Interval Scheduling

Chapter 6. Dynamic Programming. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

Compatible probability measures

Scheduling Online Algorithms. Tim Nieberg

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

An analogy from Calculus: limits

Scheduling Lecture 1: Scheduling on One Machine

SPT is Optimally Competitive for Uniprocessor Flow

Bin packing and scheduling

CSE 421 Introduction to Algorithms Final Exam Winter 2005

Transcription:

CSE 421 Greedy Algorithms / Interval Scheduling Yin Tat Lee 1

Interval Scheduling Job j starts at s(j) and finishes at f(j). Two jobs compatible if they don t overlap. Goal: find maximum subset of mutually compatible jobs. a b c d e f g h 0 1 2 3 4 5 6 7 8 9 10 11 Time 2

Greedy Alg: Earliest Finish Time Consider jobs in increasing order of finish time. Take each job provided it s compatible with the ones already taken. Sort jobs by finish times so that f(1) f(2)... f(n). A for j = 1 to n { if (job j compatible with A) A A {j} } return A Implementation. O(nlog n). Remember job j that was added last to A. Job j is compatible with A if s j f(j ). 3

Correctness Theorem: Greedy algorithm is optimal. Proof: (technique: Greedy stays ahead ) Let i 1, i 2, i 3,, i k be jobs picked by greedy, j 1, j 2, j 3,, j m those in some optimal solution in order. We show f(i r ) f(j r ) for all r, by induction on r. Base Case: i 1 chosen to have min finish time, so f(i 1 ) f(j 1 ). IH: f(i r ) f j r for some r IS: Since f i r f j r s(j r+1 ), j r+1 is among the candidates considered by greedy when it picked i r+1, & it picks min finish, so f i r+1 f(j r+1 ) Observe that we must have k m, else j k+1 is among (nonempty) set of candidates for i k+1. 4

CSE 421 Greedy Algorithms / Interval Partitioning Yin Tat Lee 5

Interval Partitioning Lecture j starts at s(j) and finishes at f(j). Goal: find minimum number of classrooms to schedule all lectures so that no two occur at the same time in the same room. Room 4 e j Room 3 c d g Room 2 b h Room 1 a f i 9 9:30 10 10:30 11 11:30 12 12:30 1 1:30 2 2:30 3 3:30 4 4:30 Time 6

Interval Partitioning Note: graph coloring is hard in general, but graphs corresponding to interval intersections are simpler. Room 4 Room 3 Room 2 Room 1 C A B D E G F H J I e j c d g b h a f i 9 9:30 10 10:30 11 11:30 12 12:30 1 1:30 2 2:30 3 3:30 4 4:30 Time 7

A Better Schedule This one uses only 3 classrooms c d f j b g i a e h 9 9:30 10 10:30 11 11:30 12 12:30 1 1:30 2 2:30 3 3:30 4 4:30 Time 8

A Greedy Algorithm Greedy algorithm: Consider lectures in increasing order of finish time: assign lecture to any compatible classroom. Sort intervals by finish time so that f 1 f 2... f n. d 0 for j = 1 to n { if (lect j is compatible with some classroom k, 1 k d) schedule lecture j in classroom k else allocate a new classroom d + 1 schedule lecture j in classroom d + 1 d d + 1 } Correctness: This is wrong! 9

Example a b c In the interval scheduling problem, we want to ignore super long job. 0 1 2 3 4 5 6 d Time In this problem, we need to schedule all the jobs. Picking them tightly is important. Greedy by finish time gives: a c b a b OPT: d c 0 1 2 3 4 5 6 d Time 0 1 2 3 4 5 6 Time 10

A Greedy Algorithm Greedy algorithm: Consider lectures in increasing order of start time: assign lecture to any compatible classroom. Sort intervals by starting time so that s 1 s 2... s n. d 0 for j = 1 to n { if (lect j is compatible with some classroom k, 1 k d) schedule lecture j in classroom k else allocate a new classroom d + 1 schedule lecture j in classroom d + 1 d d + 1 } Implementation: Exercise! 11

A Structural Lower-Bound on OPT Def. The depth of a set of open intervals is the maximum number that contains any given time. Key observation. Number of classrooms needed depth. Ex: Depth of schedule below = 3 schedule below is optimal. Q. Does there always exist a schedule equal to depth of intervals? c d f j b g i a e h 9 9:30 10 10:30 11 11:30 12 12:30 1 1:30 2 2:30 3 3:30 4 4:30 Time 12

Correctness Theorem: Greedy algorithm is optimal. Proof (exploit structural property). Let d = # classrooms greedy allocates. Classroom d is opened because there are d 1 classrooms are in use. So, d lectures overlapping at time s j, i.e. depth d. So, all schedules use d classrooms, So, greedy is optimal 13

CSE 421 Greedy Algorithms / Minimizing Lateness Yin Tat Lee 14

Scheduling to Minimizing Lateness Similar to interval scheduling. Instead of start and finish times, request i has Time Requirement t i which must be scheduled in a contiguous block Deadline d i by which time the request would like to be finished Requests are scheduled into time intervals [s i, f i ] s.t. t i = f i s i. Lateness for request i is If d i < f i then request i is late by L i = f i d i otherwise its lateness L i = 0 Goal: Find a schedule that minimize the Maximum lateness L = max i L i 1 t j 3 d j 6 lateness = 2 lateness = 0 max lateness = 6 2 2 8 3 1 9 4 4 9 5 3 14 6 2 15 d 3 = 9 d 2 = 8 d 6 = 15 d 1 = 6 d 5 = 14 d 4 = 9 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Minimizing Lateness: Greedy Algorithms Greedy template. Consider jobs in some order. [Shortest processing time first] Consider jobs in ascending order of processing time t j. 1 2 t j 1 100 d [Smallest slack] j 10 counterexample Consider jobs in ascending order of slack d j t j. 1 10 2 1 10 t j 2 [Earliest deadline first] Consider jobs in ascending order of deadline d j. d j 10 counterexample 16

Greedy Algorithm: Earliest Deadline First Sort deadlines in increasing order (d 1 d 2 d n ) f 0 for i 1 to n to s i f f i s i + t i end for f f i 1 2 3 4 5 6 t j 3 2 1 4 3 2 d j 6 8 9 9 14 15 max lateness = 1 d 1 = 6 d 2 = 8 d 3 = 9 d 4 = 9 d 5 = 14 d 6 = 15 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 17

Minimizing Lateness: No Idle Time Observation. There exists an optimal schedule with no idle time. d = 4 d = 6 0 1 2 3 4 5 6 d = 12 7 8 9 10 11 d = 4 d = 6 d = 12 0 1 2 3 4 5 6 7 8 9 10 11 Observation. The greedy schedule has no idle time. 18

Proof for Greedy Algorithm: Exchange Argument We will show that if there is another schedule O (think optimal schedule) then we can gradually change O so that at each step the maximum lateness in O never gets worse. it eventually becomes the same cost as A. 19

Minimizing Lateness: Inversions Definition An adjacent inversion in schedule S is a pair of jobs i and j such that d i < d j Job i is scheduled immediately after Job j inversion before swap j i Observation Greedy schedule has no adjacent inversions. 20

Minimizing Lateness: Inversions Definition An adjacent inversion in schedule S is a pair of jobs i and j such that d i < d j Job i is scheduled immediately after Job j inversion f i before swap j i Claim after swap i Swapping two adjacent, inverted jobs reduces the number of inversions by one and does not increase the max lateness. j f j 21

Minimizing Lateness: Inversions Lemma: Swapping two adjacent, inverted jobs does not increase the maximum lateness. inversion f i before swap j i after swap Proof: Let O be the schedule after swapping. Lateness L i L i since i is scheduled earlier in O than in O Requests i and j together occupy the same total time slot in both schedules All other requests k i, j have L k = L k i j f j f j = f i so L j = f j d j = f i d j < f i d i = L i Maximum lateness has not increased! 22

Optimal schedules and inversions Claim: There is an optimal schedule with no idle time and no inversions Proof: By previous argument there is an optimal schedule O with no idle time If O has an inversion then it has a consecutive pair of requests in its schedule that are inverted and can be swapped without increasing lateness Eventually these swaps will produce an optimal schedule with no inversions Each swap decreases the number of inversions by 1 There are at most n(n 1)/2 inversions. (we only care that this is finite.) 14

Idleness and Inversions are the only issue Claim: All schedules with no inversions and no idle time have the same maximum lateness Proof: Schedules can differ only in how they order requests with equal deadlines Consider all requests having some common deadline d Maximum lateness of these jobs is based only on the finish time of the last of these jobs but the set of these requests occupies the same time segment in both schedules Last of these requests finishes at the same time in any such schedule. 24