Presentation is loading. Please wait.

Presentation is loading. Please wait.

Prof. Swarat Chaudhuri COMP 482: Design and Analysis of Algorithms Spring 2013 Lecture 7.

Similar presentations


Presentation on theme: "Prof. Swarat Chaudhuri COMP 482: Design and Analysis of Algorithms Spring 2013 Lecture 7."— Presentation transcript:

1 Prof. Swarat Chaudhuri COMP 482: Design and Analysis of Algorithms Spring 2013 Lecture 7

2 2 Recap: 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

3 3 Recap: Interval Scheduling Algorithm. Greedy algorithm; objective is Earliest Finish Time. Argument for optimality: Greedy stays ahead Also applicable to Truck Drivers problem (selection of breakpoints) 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

4 Variant: 24-7 Interval Scheduling You have a processor that can operate People submit requests to run daily jobs on the processor. Each such job comes with a start time and an end time; if the job is accepted it must run continuously for the period between the start and end times, EVERY DAY. (Note that some jobs can start before midnight and end after midnight.) Given a list of n such jobs, your goal is to accept as many jobs as possible (regardless of length), subject to the constraint that the processor can run at most one job at any given point of time. Give an algorithm to do this. For example, here you have four jobs (6pm, 6am), (9pm, 4am), (3am, 2pm), (1pm, 7pm). The optimal solution is to pick the second and fourth jobs. 4

5 Solution Let I 1,…,I n be the n intervals. We call an I j -restricted solution one that contains the interval I j. Heres an algorithm, for fixed j, to compute an I j -restricted solution of maximum size. Let x be a point in I j. First delete I j and all intervals that overlap it. The remaining intervals do not contain the point x, so we can cut the timeline at x and produce an instance of the Interval Scheduling Problem from class. This takes O(n) time assuming intervals are sorted by ending time. Now, the algorithm for the full problem is to compute an I j -restricted solution of maximum size for each j = 1,…,n. This takes a total of O(n 2 ) time. We now pick the largest of these solutions and claim that it is the optimal. Why? Consider the optimal solution to the full problem. Suppose this produces a set of intervals S. There must be SOME I j in S, so the solution is an optimal I j -restricted solution. But then our algorithm would find it. 5

6 6 Recap: 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

7 7 Recap: 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. Greedy algorithm. Consider lectures in increasing order of start time. Assign lecture to any compatible existing classroom. If not possible, open new classroom. Time 99:301010:301111:301212:3011:3022:30 h c a e f g i j 33:3044:30 d b

8 8 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 by any algorithm depth. Argument for optimality. Show that depth number of classrooms opened by greedy algorithm. Time 99:301010:301111:301212:3011:3022:30 h c a e f g i j 33:3044:30 d b

9 9 Interval Partitioning: Greedy Analysis Observation. Greedy algorithm never schedules two incompatible lectures in the same classroom. Argument for optimality. 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.

10 10 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. 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. Exchange argument. Gradually transform any solution to the one found by the greedy algorithm without hurting its quality.

11 11 Q1: Coin Changing Goal. Given currency denominations: 1, 5, 10, 25, 100, devise a method to pay amount to customer using fewest number of coins. Ex: 34¢. Cashier's algorithm. At each iteration, add coin of the largest value that does not take us past the amount to be paid. Ex: $2.89.

12 12 Q1: Coin-Changing: Greedy Algorithm Cashier's algorithm. At each iteration, add coin of the largest value that does not take us past the amount to be paid. Q1. Is cashier's algorithm optimal? Sort coins denominations by value: c 1 < c 2 < … < c n. S while (x 0) { let k be largest integer such that c k x if (k = 0) return "no solution found" x x - c k S S {k} } return S coins selected

13 13 Coin-Changing: Analysis of Greedy Algorithm Theorem. Greed is optimal for U.S. coinage: 1, 5, 10, 25, 100. Pf. (by induction on x) n Consider optimal way to change c k x < c k+1 : greedy takes coin k. n We claim that any optimal solution must also take coin k. – if not, it needs enough coins of type c 1, …, c k-1 to add up to x – table below indicates no optimal solution can do this n Problem reduces to coin-changing x - c k cents, which, by induction, is optimally solved by greedy algorithm. 1 ckck P 4 All optimal solutions must satisfy N + D 2 Q 3 5N 1 no limit k Max value of coins 1, 2, …, k-1 in any OPT = = = 99

14 14 Coin-Changing: Analysis of Greedy Algorithm Observation. Greedy algorithm is sub-optimal for US postal denominations: 1, 10, 21, 34, 70, 100, 350, 1225, Counterexample. 140¢. n Greedy: 100, 34, 1, 1, 1, 1, 1, 1. n Optimal: 70, 70.

15 15 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. 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. Exchange argument. Gradually transform any solution to the one found by the greedy algorithm without hurting its quality.

16 4.2 Scheduling to Minimize Lateness

17 17 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

18 18 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.

19 19 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

20 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.

21 21 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

22 22 Minimizing Lateness: Inversions Def. An inversion in schedule S is a pair of jobs i and j such that: d i < d j but j scheduled before i. Observation. Greedy schedule has no inversions. Observation. All schedules with no idle time and no inversions have same maximum lateness. 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

23 23 Minimizing Lateness: Inversions Def. An inversion in schedule S is a pair of jobs i and j such that: d i < d 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

24 24 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*

25 Q2: Subsequences Suppose you have a collection of possible events (e.g., possible transactions) and a sequence S of n events. A given event may occur multiple timese.g., you could have an event buy Google stock multiple times in a log of transactions. A sequence S is a subsequence of a sequence S if theres a way to delete certain events from S such that the remaining sequence equals S. For example, the reason to do this could be pattern- matching. Give an algorithm that takes two sequences of eventsS of length m and S of length nand decides in time O(m+n) whether S is a subsequence of S. 25

26 Solution Greedy algorithm: Let the i-th event of S be S(i). Find the first event in S that matches S(1), then the second event in S that matches S(2), and so on. The running time is O(m+n). It is easy to show that if the algorithm finds a match, then S is in fact a subsequence of S. More difficult direction: if the algorithm does not find a match, then no match exists. The proof of this is by contradiction. Suppose S matches the subsequence S(l 1 ).S(l 2 )…S(l m ). Suppose GREEDY produces the sequence S(k 1 ).S(k 2 )…. Show that greedy can produce a match all the way up to S(k m ) and also k i l i for all i. This is done in a way similar to the proof in interval scheduling. 26


Download ppt "Prof. Swarat Chaudhuri COMP 482: Design and Analysis of Algorithms Spring 2013 Lecture 7."

Similar presentations


Ads by Google