Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 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 5 Greedy Algorithms Interval Scheduling Interval Partitioning

2 Review In a DFS tree of an undirected graph, can there be an edge (u,v) –where v is an ancestor of u? (“back edge”) –where v is a sibling of u? (“cross edge”) Same questions with a directed graph? Same questions with a BFS tree –directed? –undirected? 9/10/2008 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne

3 Design technique #1: Greedy Algorithms 9/3/10 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne

4 9/3/10 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne Greedy Algorithms Build up a solution to an optimization problem at each step shortsightedly choosing the option that currently seems the best. –Sometimes good –Often does not work

5 9/3/10 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne Interval Scheduling Problem Job j starts at s j and finishes at f j. Two jobs are compatible if they do not overlap. Find: maximum subset of mutually compatible jobs. 01234567891011 f g h e a b c d Time

6 9/3/10 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne Possible Greedy Strategies Consider jobs in some natural order. Take next job if it is compatible with the ones already taken. Earliest start time: ascending order of s j. Earliest finish time: ascending order of f j. Shortest interval: ascending order of (f j – s j ). Fewest conflicts: For each job j, count the number of conflicting jobs c j. Schedule in ascending order of c j.

7 9/3/10 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne Greedy: Counterexamples for earliest start time for shortest interval for fewest conflicts

8 Formulating Algorithm Arrays of start and finishing times –s 1, s 2, …,s n –f 1, f 2,…, f n Input length? –2n = (n) 9/3/10 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne

9 9/3/10 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne Earliest finish time: ascending order of f j. Implementation. –Remember job j* that was added last to A. –Job j is compatible with A if s j  f j*. Greedy Algorithm Sort jobs by finish times so that f 1  f 2 ...  f n. A   M Set of selected jobs for j = 1 to n { if (job j compatible with A) A  A  {j} } return A O(n log n) time; O(n) space.

10 9/3/10 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne Running time: O(n log n) Sort jobs by finish times so that f 1  f 2 ...  f n. A  (empty) M Queue of selected jobs j*  0 for j = 1 to n { if (f j* <= s j ) enqueue(j onto A) } return A O(n log n) O(1) n × O(1)

11 9/3/10 Analysis: Greedy Stays Ahead Theorem. Greedy algorithm is optimal. Proof strategy (by contradiction): –Suppose greedy is not optimal. –Consider an optimal strategy… which one? Consider the optimal strategy that agrees with the greedy strategy for as many initial jobs as possible – Look at first place in list where optimal strategy differs from greedy strategy – Show a new optimal strategy that agrees more w/ greedy A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne

12 9/3/10 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne Analysis: Greedy Stays Ahead Theorem. Greedy algorithm is optimal. Pf (by contradiction): Suppose greedy is not optimal. –Let i 1, i 2,... i k denote set of jobs selected by greedy. –Let j 1, j 2,... j m be 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 i2i2 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

13 9/3/10 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne Analysis: Greedy Stays Ahead Theorem. Greedy algorithm is optimal. Pf (by contradiction): Suppose greedy is not optimal. –Let i 1, i 2,... i k denote set of jobs selected by greedy. –Let j 1, j 2,... j m be 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. i1i1 i2i2 irir i r+1 Greedy: job i r+1 finishes before j r+1 solution still feasible and optimal, but contradicts maximality of r. j1j1 j2j2 jrjr... OPT: i r+1

14 Interval Partitioning 9/3/10 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne

15 9/3/10 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne Interval Partitioning Lecture j starts at s j and finishes at f j. Find: minimum number of classrooms to schedule all lectures so that no two occur at the same time in the same room. E.g.: 10 lectures are scheduled in 4 classrooms. Time 99:301010:301111:301212:3011:3022:30 h c b a e d g f i j 33:3044:30 1 2 3 4

16 9/3/10 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne Interval Partitioning Lecture j starts at s j and finishes at f j. Find: minimum number of classrooms to schedule all lectures so that no two occur at the same time in the same room. E.g.: Same lectures are scheduled in 3 classrooms. Time 99:301010:301111:301212:3011:3022:30 h c a e f g i j 33:3044:30 d b 1 2 3

17 9/3/10 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne Lower Bound Definition. The depth of a set of open intervals is the maximum number that contain any given time. Key observation. Number of classrooms needed  depth. E.g.: Depth of this schedule = 3  this schedule is optimal. Q: Is it always sufficient to have number of classrooms = depth? a, b, c all contain 9:30 99:301010:301111:301212:3011:3022:30 h c a e f g i j 33:3044:30 d b 1 2 3

18 9/3/10 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne Greedy Algorithm Consider lectures in increasing order of start time: assign lecture to any compatible classroom. Implementation. O(n log n) time; O(n) space. –For each classroom, maintain the finish time of the last job added. –Keep the classrooms in a priority queue (main loop n log(d) time) Sort intervals by starting time so that s 1  s 2 ...  s n. d  0 M Number of allocated classrooms 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 }

19 9/3/10 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne Analysis: Structural Argument Observation. Greedy algorithm never schedules two incompatible lectures in the same classroom. Theorem. Greedy algorithm is optimal. Proof: Let d = number of classrooms allocated by greedy. –Classroom d is opened because we needed to schedule a lecture, say j, that is incompatible with all d-1 last lectures in other classrooms. –These d lectures each end after s j. –Since we sorted by start time, they start no later than s j. –Thus, we have d lectures overlapping at time s j + . –Key observation  all schedules use  d classrooms. ▪


Download ppt "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."

Similar presentations


Ads by Google