1 / 19

Algorithm Design and Analysis

L ECTURE 5 Greedy Algorithms Interval Scheduling Interval Partitioning. Algorithm Design and Analysis. CSE 565. Guest lecturer: Martin Furer. Review. In a DFS tree of an un directed graph, can there be an edge ( u,v ) where v is an ancestor of u ? (“back edge”)

toki
Télécharger la présentation

Algorithm Design and Analysis

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. LECTURE 5 • Greedy Algorithms • Interval Scheduling • Interval Partitioning Algorithm Design and Analysis CSE 565 Guest lecturer: Martin Furer A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne

  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? A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne

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

  4. 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 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne

  5. Interval Scheduling Problem • Job j starts at sj and finishes at fj. • Two jobs are compatibleif they do not overlap. • Find: maximum subset of mutually compatible jobs. a b c d e f g h Time 0 1 2 3 4 5 6 7 8 9 10 11 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne

  6. 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 sj. • Earliest finish time: ascending order of fj. • Shortest interval: ascending order of (fj – sj). • Fewest conflicts: For each job j, count the number of conflicting jobs cj. Schedule in ascending order of cj. A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne

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

  8. Formulating Algorithm • Arrays of start and finishing times • s1, s2, …,sn • f1, f2,…, fn • Input length? • 2n = ϴ(n) A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne

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

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

  11. 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. Analysis: Greedy Stays Ahead • Theorem. Greedy algorithm is optimal. • Pf (by contradiction):Suppose greedy is not optimal. • Let i1, i2, ... ik denote set of jobs selected by greedy. • Let j1, j2, ... jm be set of jobs in the optimal solution withi1 = j1, i2 = j2, ..., ir = jr for the largest possible value of r. job ir+1 finishes before jr+1 Greedy: i1 i2 ir ir+1 j1 j2 jr jr+1 . . . OPT: why not replace job jr+1with job ir+1? A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne

  13. Analysis: Greedy Stays Ahead • Theorem. Greedy algorithm is optimal. • Pf (by contradiction):Suppose greedy is not optimal. • Let i1, i2, ... ik denote set of jobs selected by greedy. • Let j1, j2, ... jm be set of jobs in the optimal solution withi1 = j1, i2 = j2, ..., ir = jr for the largest possible value of r. job ir+1 finishes before jr+1 Greedy: i1 i2 ir ir+1 j1 j2 jr ir+1 . . . OPT: solution still feasible and optimal, but contradicts maximality of r. A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne

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

  15. Interval Partitioning • Lecture j starts at sj and finishes at fj. • 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. e j 4 g c d 3 b h 2 a f i 1 3 3:30 4 4:30 9 9:30 10 10:30 11 11:30 12 12:30 1 1:30 2 2:30 Time A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne

  16. Interval Partitioning • Lecture j starts at sj and finishes at fj. • 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. f c d j 3 i b g 2 a h e 1 3 3:30 4 4:30 9 9:30 10 10:30 11 11:30 12 12:30 1 1:30 2 2:30 Time A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne

  17. 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 f c d j 3 i b g 2 a h e 1 3 3:30 4 4:30 9 9:30 10 10:30 11 11:30 12 12:30 1 1:30 2 2:30 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne

  18. 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 nlog(d) time) Sort intervals by starting time so that s1 s2 ...  sn. 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 } A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne

  19. 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 sj. • Since we sorted by start time, they start no later than sj. • Thus, we have d lectures overlapping at time sj + . • Key observation  all schedules use  d classrooms. ▪ A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne

More Related