greedy algorithms
play

Greedy Algorithms - Gordon Gecko (Michael Douglas) Optimization - PDF document

Greed is good. Greed is right. Greed works. Greed clarifies, cuts through, and captures the essence of the evolutionary spirit. Greedy Algorithms - Gordon Gecko (Michael Douglas) Optimization problem: Min/Max an objective. Minimize


  1. Greed is good. Greed is right. Greed works. Greed clarifies, cuts through, and captures the essence of the evolutionary spirit. Greedy Algorithms - Gordon Gecko (Michael Douglas) • Optimization problem: Min/Max an objective. – Minimize the total length of a spanning tree. – Minimize the size of a file using compression – … (The mother of all problems) Analysis of Algorithms • Greedy Algorithm – Attempt to do best at each step without consideration of future consideration Piyush Kumar • For some problems, Locally optimal choice leads to global opt. • Follows “Greed is good” philosophy (Lecture e 4: Greedy y Algorithms) • Requires “Optimal Substructure” • What examples have we already seen? Welcome to 4531 Source: K. Wayne, … Greedy Algorithms Problem of Change • For some problems, “Greed is good” works. • Vending machine has quarters, nickels, pennies and dimes. Needs to return N cents change. • For some, it finds a good solution which is not global opt • Wanted: An algorithm to return the N cents in minimum – Heuristics number of coins. – Approximation Algorithms • What do we do? • For some, it can do very bad. Interval Scheduling • 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. 4.1 Interval Scheduling a b c d e f g h Time 5 0 1 2 3 4 5 6 7 8 9 10 11 1

  2. Interval Scheduling: Greedy Interval Scheduling: Algorithms Greedy Algorithms • Greedy template. Consider jobs in some order. Take each job provided it's • Greedy template. Consider jobs in some order. Take each job provided it's compatible with the ones already taken. compatible with the ones already taken. breaks earliest start time – [Earliest start time] Consider jobs in ascending order of start time s j . – [Earliest finish time] Consider jobs in ascending order of finish time breaks shortest interval f j . – [Shortest interval] Consider jobs in ascending order of interval length f j - s j . breaks fewest conflicts – [Fewest conflicts] For each job, count the number of conflicting jobs c j . Schedule in ascending order of conflicts c j . Interval Scheduling: Interval Scheduling: Analysis Greedy Algorithm • Theorem. Greedy algorithm is optimal. • Greedy algorithm. Consider jobs in increasing order of finish time. Take • Pf. (by contradiction) each job provided it's compatible with the ones already taken. – Assume greedy is not optimal, and let's see what happens. – Let i 1 , i 2 , ... i k denote set of jobs selected by greedy. Sort jobs by finish times so that f 1  f 2  ...  f 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. jobs selected A   job i r+1 finishes before j r+1 for j = 1 to n { if (job j compatible with A) Greedy: A  A  {j} i 1 i 1 i r i r+1 } return A j r+1 . . . OPT: j 1 j 2 j r • Implementation. O(n log n). – Remember job j* that was added last to A. why not replace job j r+1 with job i r+1 ? – Job j is compatible with A if s j  f j* . Interval Scheduling: Analysis • Theorem. Greedy algorithm is optimal. • Pf. (by contradiction) – Assume greedy is not optimal, and let's see what happens. – Let i 1 , i 2 , ... i k denote set of jobs selected by greedy. 4.1 Interval – 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. Partitioning job i r+1 finishes before j r+1 Greedy: i 1 i 1 i r i r+1 . . . OPT: j 1 j 2 j r i r+1 solution still feasible and optimal, but contradicts maximality of r. 12 2

  3. Interval Partitioning Interval Partitioning • Interval partitioning. • Interval partitioning. – Lecture j starts at s j and finishes at f j . – Lecture j starts at s j and finishes at f j . – Goal: find minimum number of classrooms to schedule all – Goal: find minimum number of classrooms to schedule all lectures so that no two occur at the same time in the same lectures so that no two occur at the same time in the same room. room. • Ex: This schedule uses only 3. • Ex: This schedule uses 4 classrooms to schedule 10 lectures. e j c d g c d f j b h b g i a f 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 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 Time Interval Partitioning: Lower Bound Interval Partitioning: on Optimal Solution Greedy Algorithm • Def. The depth of a set of open intervals is the maximum number that contain any given time. • Greedy algorithm. Consider lectures in increasing order of start time: assign lecture to any compatible classroom. • Key observation. Number of classrooms needed  depth. Sort intervals by starting time so that s 1  s 2  ...  s n . • Ex: Depth of schedule below = 3  schedule below is optimal. d  0 number of allocated classrooms for j = 1 to n { a, b, c all contain 9:30 if (lecture j is compatible with some classroom k) • Q. Does there always exist a schedule equal to depth of intervals? schedule lecture j in classroom k else allocate a new classroom d + 1 schedule lecture j in classroom d + 1 c d f j d  d + 1 } b g i a e h • Implementation. O(n log n). – For each classroom k, maintain the finish time of the last job added. 9 9:30 10 10:30 11 11:30 12 12:30 1 1:30 2 2:30 3 3:30 4 4:30 – Keep the classrooms in a priority queue. Time Interval Partitioning: Greedy Analysis • Observation. Greedy algorithm never schedules two incompatible lectures in the same classroom. • Theorem. Greedy algorithm is optimal. 4.2 Scheduling to • Pf. – Let d = number of classrooms that the greedy algorithm allocates. Minimize Lateness – Classroom d is opened because we needed to schedule a job, say j, that is incompatible with all d-1 other classrooms. – Since we sorted by start time, all these incompatibilities are caused by lectures that start no later than s j . – Thus, we have d lectures overlapping at time s j +  . – Key observation  all schedules use  d classrooms. ▪ 18 3

  4. Minimizing Lateness: Scheduling to Minimizing Lateness Greedy Algorithms • Minimizing lateness problem. – Single resource processes one job at a time. – Job j requires t j units of processing time and is due at time d j . • Greedy template. Consider jobs in some order. – If j starts at time s j , it finishes at time f j = s j + t j . – Lateness:  j = max { 0, f j - d j }. – Goal: schedule all jobs to minimize maximum lateness L = max  j . – [Shortest processing time first] Consider jobs in ascending order of processing time t j . • Ex: 1 2 3 4 5 6 – [Earliest deadline first] Consider jobs in ascending order of t j 3 2 1 4 3 2 deadline d j . d j 6 8 9 9 14 15 – [Smallest slack] Consider jobs in ascending order of slack d j - lateness = 2 lateness = 0 max lateness = 6 t j . d 3 = 9 d 2 = 8 d 6 = 15 d 1 = 6 d 5 = 14 d 4 = 9 12 13 14 15 0 1 2 3 4 5 6 7 8 9 10 11 Minimizing Lateness: Greedy Minimizing Lateness: Greedy Algorithms Algorithm • Greedy template. Consider jobs in some order. – [Shortest processing time first] Consider jobs in ascending • Greedy algorithm. Earliest deadline first. order of processing time t j . 1 2 Sort n jobs by deadline so that d 1  d 2  …  d n t j 1 10 counterexample t  0 d j 100 10 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 – [Smallest slack] Consider jobs in ascending order of slack d j - output intervals [s j , f j ] t j . 1 2 max lateness = 1 t j 1 10 counterexample d 1 = 6 d 2 = 8 d 3 = 9 d 4 = 9 d 5 = 14 d 6 = 15 d j 2 10 12 13 14 15 0 1 2 3 4 5 6 7 8 9 10 11 Minimizing Lateness: Inversions Minimizing Lateness: No Idle Time • Observation. There exists an optimal schedule with no idle time. • Def. An inversion in schedule S is a pair of jobs i and j such that: i < j but j scheduled before i. inversion d = 4 d = 6 d = 12 0 1 2 3 4 5 6 7 8 9 10 11 j i before swap d = 4 d = 6 d = 12 • Observation. Greedy schedule has no inversions. 0 1 2 3 4 5 6 7 8 9 10 11 • Observation. If a schedule (with no idle time) has an inversion, it has one with a pair of inverted jobs scheduled consecutively. • Observation. The greedy schedule has no idle time. 4

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend