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

Slides:



Advertisements
Similar presentations
Introduction to Algorithms
Advertisements

Unit-iv.
© 2008 Pearson Addison Wesley. All rights reserved Chapter Seven Costs.
Operations Scheduling
Copyright © 2011, Elsevier Inc. All rights reserved. Chapter 5 Author: Julia Richards and R. Scott Hawley.
1 Copyright © 2010, Elsevier Inc. All rights Reserved Fig 2.1 Chapter 2.
and 6.855J Cycle Canceling Algorithm. 2 A minimum cost flow problem , $4 20, $1 20, $2 25, $2 25, $5 20, $6 30, $
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
DIVIDING INTEGERS 1. IF THE SIGNS ARE THE SAME THE ANSWER IS POSITIVE 2. IF THE SIGNS ARE DIFFERENT THE ANSWER IS NEGATIVE.
Addition Facts
1 Student-Project Allocation with Preferences over Projects David Manlove Gregg OMalley University of Glasgow Department of Computing Science Supported.
Lecture 11 Overview Self-Reducibility. Overview on Greedy Algorithms.
Fakultät für informatik informatik 12 technische universität dortmund Classical scheduling algorithms for periodic systems Peter Marwedel TU Dortmund,
COMP 482: Design and Analysis of Algorithms
§1 Greedy Algorithms ALGORITHM DESIGN TECHNIQUES
ABC Technology Project
Prof. Swarat Chaudhuri COMP 482: Design and Analysis of Algorithms Spring 2013 Lecture 2.
Chapter 9 -- Simplification of Sequential Circuits.
COMP 482: Design and Analysis of Algorithms
Lecture 7 Paradigm #5 Greedy Algorithms
1 Undirected Breadth First Search F A BCG DE H 2 F A BCG DE H Queue: A get Undiscovered Fringe Finished Active 0 distance from A visit(A)
Priority Queues Two kinds of priority queues: Min priority queue. Max priority queue.
Great Theoretical Ideas in Computer Science
1 Breadth First Search s s Undiscovered Discovered Finished Queue: s Top of queue 2 1 Shortest path from s.
Squares and Square Root WALK. Solve each problem REVIEW:
Addition 1’s to 20.
25 seconds left…...
Week 1.
We will resume in: 25 Minutes.
1 Chapter 4 Greedy Algorithms Slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved.
Instructor Neelima Gupta Table of Contents Greedy Algorithms.
Greedy Algorithms Basic idea Connection to dynamic programming Proof Techniques.
1 Chapter 4 Greedy Algorithms Slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved.
CSE 421 Algorithms Richard Anderson Lecture 6 Greedy Algorithms.
9/3/10 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne Guest lecturer: Martin Furer Algorithm Design and Analysis L ECTURE.
Called as the Interval Scheduling Problem. A simpler version of a class of scheduling problems. – Can add weights. – Can add multiple resources – Can ask.
9/8/10 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne Adam Smith Algorithm Design and Analysis L ECTURE 6 Greedy Algorithms.
1 Greedy algorithm 叶德仕 2 Greedy algorithm’s paradigm Algorithm is greedy if it builds up a solution in small steps it chooses a decision.
CSCI-256 Data Structures & Algorithm Analysis Lecture Note: Some slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved. 8.
Lectures on Greedy Algorithms and Dynamic Programming
CSCI 256 Data Structures and Algorithm Analysis Lecture 6 Some slides by Kevin Wayne copyright 2005, Pearson Addison Wesley all rights reserved, and some.
1 Chapter 5-1 Greedy Algorithms Slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved.
1 Chapter 4 Greedy Algorithms Slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved.
Greedy Algorithms Interval Scheduling and Fractional Knapsack These slides are based on the Lecture Notes by David Mount for the course CMSC 451 at the.
CSEP 521 Applied Algorithms Richard Anderson Winter 2013 Lecture 3.
CSCI-256 Data Structures & Algorithm Analysis Lecture Note: Some slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved. 10.
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.
Greedy Algorithms Lecture 10 Asst. Prof. Dr. İlker Kocabaş 1.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 17.
Greedy Algorithms – Chapter 5
Chapter 4 Greedy Algorithms
Analysis of Algorithms CS 477/677
Chapter 4 Greedy Algorithms
Greedy Algorithms / Interval Scheduling Yin Tat Lee
Presented by Po-Chuan & Chen-Chen 2016/03/08
Chapter 4 Greedy Algorithms
Greedy Algorithms.
Lecture 21 CSE 331 Oct 21, 2016.
Lecture 21 CSE 331 Oct 20, 2017.
4.1 Interval Scheduling.
Richard Anderson Lecture 6 Greedy Algorithms
Richard Anderson Autumn 2016 Lecture 7
Richard Anderson Lecture 7 Greedy Algorithms
Chapter 4 Greedy Algorithms
Richard Anderson Winter 2019 Lecture 7
Week 2: Greedy Algorithms
Richard Anderson Autumn 2015 Lecture 7
Analysis of Algorithms CS 477/677
Richard Anderson Autumn 2019 Lecture 7
Presentation transcript:

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

4.1 Interval Scheduling

3 Interval Scheduling Interval scheduling. n Job j starts at s j and finishes at f j. n Two jobs compatible if they don't overlap. n Goal: find maximum subset of mutually compatible jobs. Time f g h e a b c d

4 Interval Scheduling: Greedy Algorithms Greedy template. Consider jobs in some order. Take each job provided it's compatible with the ones already taken. n [Earliest start time] Consider jobs in ascending order of start time s j. n [Earliest finish time] Consider jobs in ascending order of finish time f j. n [Shortest interval] Consider jobs in ascending order of interval length f j - s j. n [Fewest conflicts] For each job, count the number of conflicting jobs c j. Schedule in ascending order of conflicts c j.

5 Interval Scheduling: Greedy Algorithms Greedy template. Consider jobs in some order. Take each job provided it's compatible with the ones already taken. breaks earliest start time breaks shortest interval breaks fewest conflicts

6 Greedy algorithm. Consider jobs in increasing order of finish time. Take each job provided it's compatible with the ones already taken. Implementation. O(n log n). n Remember job j* that was added last to A. n Job j is compatible with A if s j f j*. 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 jobs selected Interval Scheduling: Greedy Algorithm

7 Interval Scheduling: Analysis Theorem. Greedy algorithm is optimal. Pf. (by contradiction) n Assume greedy is not optimal, and let's see what happens. n Let i 1, i 2,... i k denote set of jobs selected by greedy. n Let j 1, j 2,... j m denote set of jobs in the optimal solution with i 1 = j 1, i 2 = j 2,..., i r = j r for the largest possible value of r. j1j1 j2j2 jrjr i1i1 i1i1 irir i r+1... Greedy: OPT: j r+1 why not replace job j r+1 with job i r+1 ? job i r+1 finishes before j r+1

8 j1j1 j2j2 jrjr i1i1 i1i1 irir i r+1 Interval Scheduling: Analysis Theorem. Greedy algorithm is optimal. Pf. (by contradiction) n Assume greedy is not optimal, and let's see what happens. n Let i 1, i 2,... i k denote set of jobs selected by greedy. n Let j 1, j 2,... j m denote set of jobs in the optimal solution with i 1 = j 1, i 2 = j 2,..., i r = j r for the largest possible value of r.... Greedy: OPT: solution still feasible and optimal, but contradicts maximality of r. i r+1 job i r+1 finishes before j r+1

4.1 Interval Partitioning

10 Interval Partitioning Interval partitioning. n Lecture j starts at s j and finishes at f j. n Goal: find minimum number of classrooms to schedule all lectures so that no two occur at the same time in the same room. Ex: This schedule uses 4 classrooms to schedule 10 lectures. Time 99:301010:301111:301212:3011:3022:30 h c b a e d g f i j 33:3044:30

11 Interval Partitioning Interval partitioning. n Lecture j starts at s j and finishes at f j. n Goal: find minimum number of classrooms to schedule all lectures so that no two occur at the same time in the same room. Ex: This schedule uses only 3. Time 99:301010:301111:301212:3011:3022:30 h c a e f g i j 33:3044:30 d b

12 Interval Partitioning: Lower Bound on Optimal Solution Def. The depth of a set of open intervals is the maximum number that contain 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? Time 99:301010:301111:301212:3011:3022:30 h c a e f g i j 33:3044:30 d b a, b, c all contain 9:30

13 Interval Partitioning: Greedy Algorithm Greedy algorithm. Consider lectures in increasing order of start time: assign lecture to any compatible classroom. Implementation. O(n log n). n For each classroom k, maintain the finish time of the last job added. n Keep the classrooms in a priority queue. Sort intervals by starting time so that s 1 s 2... s n. d 0 for j = 1 to n { if (lecture j is compatible with some classroom k) schedule lecture j in classroom k else allocate a new classroom d + 1 schedule lecture j in classroom d + 1 d d + 1 } number of allocated classrooms

14 Interval Partitioning: Greedy Analysis Observation. Greedy algorithm never schedules two incompatible lectures in the same classroom. Theorem. Greedy algorithm is optimal. Pf. n Let d = number of classrooms that the greedy algorithm allocates. n Classroom d is opened because we needed to schedule a job, say j, that is incompatible with all d-1 other classrooms. n Since we sorted by start time, all these incompatibilities are caused by lectures that start no later than s j. n Thus, we have d lectures overlapping at time s j +. n Key observation all schedules use d classrooms.

4.2 Scheduling to Minimize Lateness

16 Scheduling to Minimizing Lateness Minimizing lateness problem. n Single resource processes one job at a time. n Job j requires t j units of processing time and is due at time d j. n If j starts at time s j, it finishes at time f j = s j + t j. n Lateness: j = max { 0, f j - d j }. n Goal: schedule all jobs to minimize maximum lateness L = max j. Ex: d 5 = 14d 2 = 8d 6 = 15d 1 = 6d 4 = 9d 3 = 9 lateness = 0lateness = 2 djdj 6 tjtj max lateness = 6

17 Minimizing Lateness: Greedy Algorithms Greedy template. Consider jobs in some order. n [Shortest processing time first] Consider jobs in ascending order of processing time t j. n [Earliest deadline first] Consider jobs in ascending order of deadline d j. n [Smallest slack] Consider jobs in ascending order of slack d j - t j.

18 Greedy template. Consider jobs in some order. n [Shortest processing time first] Consider jobs in ascending order of processing time t j. n [Smallest slack] Consider jobs in ascending order of slack d j - t j. counterexample djdj tjtj djdj tjtj Minimizing Lateness: Greedy Algorithms

d 5 = 14d 2 = 8d 6 = 15d 1 = 6d 4 = 9d 3 = 9 max lateness = 1 Sort n jobs by deadline so that d 1 d 2 … d n t 0 for j = 1 to n Assign job j to interval [t, t + t j ] s j t, f j t + t j t t + t j output intervals [s j, f j ] Minimizing Lateness: Greedy Algorithm Greedy algorithm. Earliest deadline first.

20 Minimizing Lateness: No Idle Time Observation. There exists an optimal schedule with no idle time. Observation. The greedy schedule has no idle time d = 4d = d = d = 4d = d = 12

21 Minimizing Lateness: Inversions Def. An inversion in schedule S is a pair of jobs i and j such that: i < j but j scheduled before i. Observation. Greedy schedule has no inversions. Observation. If a schedule (with no idle time) has an inversion, it has one with a pair of inverted jobs scheduled consecutively. ij before swap inversion

22 Minimizing Lateness: Inversions Def. An inversion in schedule S is a pair of jobs i and j such that: i < j but j scheduled before i. Claim. Swapping two adjacent, inverted jobs reduces the number of inversions by one and does not increase the max lateness. Pf. Let be the lateness before the swap, and let ' be it afterwards. n ' k = k for all k i, j n ' i i n If job j is late: ij ij before swap after swap f' j fifi inversion

23 Minimizing Lateness: Analysis of Greedy Algorithm Theorem. Greedy schedule S is optimal. Pf. Define S* to be an optimal schedule that has the fewest number of inversions, and let's see what happens. n Can assume S* has no idle time. n If S* has no inversions, then S = S*. n If S* has an inversion, let i-j be an adjacent inversion. – swapping i and j does not increase the maximum lateness and strictly decreases the number of inversions – this contradicts definition of S*

24 Greedy Analysis Strategies Greedy algorithm stays ahead. Show that after each step of the greedy algorithm, its solution is at least as good as any other algorithm's. Exchange argument. Gradually transform any solution to the one found by the greedy algorithm without hurting its quality. Structural. Discover a simple "structural" bound asserting that every possible solution must have a certain value. Then show that your algorithm always achieves this bound.