greedy algorithms
play

Greedy Algorithms Lecture 17 March 19, 2015 Chandra & Lenny - PowerPoint PPT Presentation

CS 374: Algorithms & Models of Computation, Spring 2015 Greedy Algorithms Lecture 17 March 19, 2015 Chandra & Lenny (UIUC) CS374 1 Spring 2015 1 / 46 Part I Problems and Terminology Chandra & Lenny (UIUC) CS374 2 Spring


  1. Smallest Processing Time Process jobs in the order of processing time, starting with jobs that require the shortest processing. Figure : Counter example for smallest processing time Back Counter Chandra & Lenny (UIUC) CS374 13 Spring 2015 13 / 46

  2. Fewest Conflicts Process jobs in that have the fewest “conflicts” first. Back Counter Chandra & Lenny (UIUC) CS374 14 Spring 2015 14 / 46

  3. Fewest Conflicts Process jobs in that have the fewest “conflicts” first. Back Counter Chandra & Lenny (UIUC) CS374 14 Spring 2015 14 / 46

  4. Fewest Conflicts Process jobs in that have the fewest “conflicts” first. Back Counter Chandra & Lenny (UIUC) CS374 14 Spring 2015 14 / 46

  5. Fewest Conflicts Process jobs in that have the fewest “conflicts” first. Back Counter Chandra & Lenny (UIUC) CS374 14 Spring 2015 14 / 46

  6. Fewest Conflicts Process jobs in that have the fewest “conflicts” first. Back Counter Chandra & Lenny (UIUC) CS374 14 Spring 2015 14 / 46

  7. Fewest Conflicts Process jobs in that have the fewest “conflicts” first. Figure : Counter example for fewest conflicts Back Counter Chandra & Lenny (UIUC) CS374 14 Spring 2015 14 / 46

  8. Fewest Conflicts Process jobs in that have the fewest “conflicts” first. Figure : Counter example for fewest conflicts Back Counter Chandra & Lenny (UIUC) CS374 14 Spring 2015 14 / 46

  9. Fewest Conflicts Process jobs in that have the fewest “conflicts” first. Figure : Counter example for fewest conflicts Back Counter Chandra & Lenny (UIUC) CS374 14 Spring 2015 14 / 46

  10. Fewest Conflicts Process jobs in that have the fewest “conflicts” first. Figure : Counter example for fewest conflicts Back Counter Chandra & Lenny (UIUC) CS374 14 Spring 2015 14 / 46

  11. Earliest Finish Time Process jobs in the order of their finishing times, beginning with those that finish earliest. Chandra & Lenny (UIUC) CS374 15 Spring 2015 15 / 46

  12. Earliest Finish Time Process jobs in the order of their finishing times, beginning with those that finish earliest. Chandra & Lenny (UIUC) CS374 15 Spring 2015 15 / 46

  13. Earliest Finish Time Process jobs in the order of their finishing times, beginning with those that finish earliest. Chandra & Lenny (UIUC) CS374 15 Spring 2015 15 / 46

  14. Earliest Finish Time Process jobs in the order of their finishing times, beginning with those that finish earliest. Chandra & Lenny (UIUC) CS374 15 Spring 2015 15 / 46

  15. Earliest Finish Time Process jobs in the order of their finishing times, beginning with those that finish earliest. Chandra & Lenny (UIUC) CS374 15 Spring 2015 15 / 46

  16. Earliest Finish Time Process jobs in the order of their finishing times, beginning with those that finish earliest. Chandra & Lenny (UIUC) CS374 15 Spring 2015 15 / 46

  17. Earliest Finish Time Process jobs in the order of their finishing times, beginning with those that finish earliest. Chandra & Lenny (UIUC) CS374 15 Spring 2015 15 / 46

  18. Optimal Greedy Algorithm R is the set of all requests X is empty (* X will store all the jobs that will be scheduled while R is not empty choose i ∈ R such that finishing time of i is least add i to X remove from R all requests that overlap with i return X Theorem The greedy algorithm that picks jobs in the order of their finishing times is optimal. Chandra & Lenny (UIUC) CS374 16 Spring 2015 16 / 46

  19. Proving Optimality Correctness: Clearly the algorithm returns a set of jobs that does 1 not have any conflicts Chandra & Lenny (UIUC) CS374 17 Spring 2015 17 / 46

  20. Proving Optimality Correctness: Clearly the algorithm returns a set of jobs that does 1 not have any conflicts For a set of requests R , let O be an optimal set and let X be the 2 set returned by the greedy algorithm. Then O = X ? Chandra & Lenny (UIUC) CS374 17 Spring 2015 17 / 46

  21. Proving Optimality Correctness: Clearly the algorithm returns a set of jobs that does 1 not have any conflicts For a set of requests R , let O be an optimal set and let X be the 2 set returned by the greedy algorithm. Then O = X ?Not likely! Chandra & Lenny (UIUC) CS374 17 Spring 2015 17 / 46

  22. Proving Optimality Correctness: Clearly the algorithm returns a set of jobs that does 1 not have any conflicts For a set of requests R , let O be an optimal set and let X be the 2 set returned by the greedy algorithm. Then O = X ?Not likely! Chandra & Lenny (UIUC) CS374 17 Spring 2015 17 / 46

  23. Proving Optimality Correctness: Clearly the algorithm returns a set of jobs that does 1 not have any conflicts For a set of requests R , let O be an optimal set and let X be the 2 set returned by the greedy algorithm. Then O = X ?Not likely! Chandra & Lenny (UIUC) CS374 17 Spring 2015 17 / 46

  24. Proving Optimality Correctness: Clearly the algorithm returns a set of jobs that does 1 not have any conflicts For a set of requests R , let O be an optimal set and let X be the 2 set returned by the greedy algorithm. Then O = X ?Not likely! Instead we will show that | O | = | X | Chandra & Lenny (UIUC) CS374 17 Spring 2015 17 / 46

  25. Proof of Optimality: Key Lemma Lemma Let i 1 be first interval picked by Greedy. There exists an optimum solution that contains i 1 . Proof. Let O be an arbitrary optimum solution. If i 1 ∈ O we are done. Chandra & Lenny (UIUC) CS374 18 Spring 2015 18 / 46

  26. Proof of Optimality: Key Lemma Lemma Let i 1 be first interval picked by Greedy. There exists an optimum solution that contains i 1 . Proof. Let O be an arbitrary optimum solution. If i 1 ∈ O we are done. Claim: If i 1 �∈ O then there is exactly one interval j 1 ∈ O that conflicts with i 1 . (proof later) Chandra & Lenny (UIUC) CS374 18 Spring 2015 18 / 46

  27. Proof of Optimality: Key Lemma Lemma Let i 1 be first interval picked by Greedy. There exists an optimum solution that contains i 1 . Proof. Let O be an arbitrary optimum solution. If i 1 ∈ O we are done. Claim: If i 1 �∈ O then there is exactly one interval j 1 ∈ O that conflicts with i 1 . (proof later) Form a new set O ′ by removing j 1 from O and adding i 1 , that is 1 O ′ = (O − { j 1 } ) ∪ { i 1 } . From claim, O ′ is a feasible solution (no conflicts). 2 Since | O ′ | = | O | , O ′ is also an optimum solution and it 3 contains i 1 . Chandra & Lenny (UIUC) CS374 18 Spring 2015 18 / 46

  28. Proof of Claim Claim If i 1 �∈ O , there is exactly one interval j 1 ∈ O that conflicts with i 1 . Proof. If no j ∈ O conflicts with i 1 then O is not optimal! 1 Suppose j 1 , j 2 ∈ O such that j 1 � = j 2 and both j 1 and j 2 conflict 2 with i 1 . Since i 1 has earliest finish time, j 1 and i 1 overlap at f(i 1 ) . 3 For same reason j 2 also overlaps with i 1 at f(i 1 ) . 4 Implies that j 1 , j 2 overlap at f(i 1 ) but intervals in O cannot 5 overlap. See figure in next slide. Chandra & Lenny (UIUC) CS374 19 Spring 2015 19 / 46

  29. Figure for proof of Claim j 2 j 1 i 1 f ( i 1 ) f ( j 1 ) f ( j 2 ) time Figure : Since i 1 has the earliest finish time, any interval that conflicts with it does so at f(i 1 ) . This implies j 1 and j 2 conflict. Chandra & Lenny (UIUC) CS374 20 Spring 2015 20 / 46

  30. Proof of Optimality of Earliest Finish Time First Proof by Induction on number of intervals. Base Case: n = 1 . Trivial since Greedy picks one interval. Induction Step: Assume theorem holds for i < n . Let I be an instance with n intervals I ′ : I with i 1 and all intervals that overlap with i 1 removed G(I) , G(I ′ ) : Solution produced by Greedy on I and I ′ From Lemma, there is an optimum solution O to I and i 1 ∈ O . Let O ′ = O − { i 1 } . O ′ is a solution to I ′ . | G(I) | = 1 + | G(I ′ ) | ( from Greedy description ) 1 + | O ′ | ≥ ( By induction, G(I ′ ) is optimum for I ′ ) = | O | Chandra & Lenny (UIUC) CS374 21 Spring 2015 21 / 46

  31. Implementation and Running Time Initially R is the set of all requests X is empty (* X will store all the jobs that will be scheduled while R is not empty choose i ∈ R such that finishing time of i is least if i does not overlap with requests in X add i to X remove i from R return the set X Chandra & Lenny (UIUC) CS374 22 Spring 2015 22 / 46

  32. Implementation and Running Time Initially R is the set of all requests X is empty (* X will store all the jobs that will be scheduled while R is not empty choose i ∈ R such that finishing time of i is least if i does not overlap with requests in X add i to X remove i from R return the set X Chandra & Lenny (UIUC) CS374 22 Spring 2015 22 / 46

  33. Implementation and Running Time Initially R is the set of all requests X is empty (* X will store all the jobs that will be scheduled while R is not empty choose i ∈ R such that finishing time of i is least if i does not overlap with requests in X add i to X remove i from R return the set X Presort all requests based on finishing time. O(n log n) time 1 Now choosing least finishing time is O(1) 2 Chandra & Lenny (UIUC) CS374 22 Spring 2015 22 / 46

  34. Implementation and Running Time Initially R is the set of all requests X is empty (* X will store all the jobs that will be scheduled while R is not empty choose i ∈ R such that finishing time of i is least if i does not overlap with requests in X add i to X remove i from R return the set X Presort all requests based on finishing time. O(n log n) time 1 Now choosing least finishing time is O(1) 2 Keep track of the finishing time of the last request added to A . 3 Then check if starting time of i later than that Thus, checking non-overlapping is O(1) 4 Chandra & Lenny (UIUC) CS374 22 Spring 2015 22 / 46

  35. Implementation and Running Time Initially R is the set of all requests X is empty (* X will store all the jobs that will be scheduled while R is not empty choose i ∈ R such that finishing time of i is least if i does not overlap with requests in X add i to X remove i from R return the set X Presort all requests based on finishing time. O(n log n) time 1 Now choosing least finishing time is O(1) 2 Keep track of the finishing time of the last request added to A . 3 Then check if starting time of i later than that Thus, checking non-overlapping is O(1) 4 Total time O(n log n + n) = O(n log n) 5 Chandra & Lenny (UIUC) CS374 22 Spring 2015 22 / 46

  36. Comments Interesting Exercise: smallest interval first picks at least half the 1 optimum number of intervals. All requests need not be known at the beginning. Such online 2 algorithms are a subject of research Chandra & Lenny (UIUC) CS374 23 Spring 2015 23 / 46

  37. Weighted Interval Scheduling Suppose we are given n jobs. Each job i has a start time s i , a finish time f i , and a weight w i . We would like to find a set S of compatible jobs whose total weight is maximized. Which of the following greedy algorithms finds the optimum schedule? (A) Earliest start time first. (B) Earliest finish time fist. (C) Highest weight first. (D) None of the above. (E) IDK. Chandra & Lenny (UIUC) CS374 24 Spring 2015 24 / 46

  38. Scheduling all Requests Input A set of lectures, with start and end times Goal Find the minimum number of classrooms, needed to schedule all the lectures such two lectures do not occur at the same time in the same room. j e g c d b h a f i Figure : A schedule requiring 3 classrooms Figure : A schedule requiring 4 classrooms Chandra & Lenny (UIUC) CS374 25 Spring 2015 25 / 46

  39. Scheduling all Requests Input A set of lectures, with start and end times Goal Find the minimum number of classrooms, needed to schedule all the lectures such two lectures do not occur at the same time in the same room. j e j c d f g c d g b i b h a e h a f i Figure : A schedule requiring 3 Figure : A schedule requiring 4 classrooms classrooms Chandra & Lenny (UIUC) CS374 25 Spring 2015 25 / 46

  40. Scheduling all Requests Input A set of lectures, with start and end times Goal Find the minimum number of classrooms, needed to schedule all the lectures such two lectures do not occur at the same time in the same room. j e j c d f g c d g b i b h a e h a f i Figure : A schedule requiring 3 Figure : A schedule requiring 4 classrooms classrooms Chandra & Lenny (UIUC) CS374 25 Spring 2015 25 / 46

  41. Greedy Algorithm Initially R is the set of all requests d = 0 (* number of classrooms *) while R is not empty do choose i ∈ R if i can be scheduled in some class-room k ≤ d schedule lecture i in class-room k else allocate a new class-room d + 1 and schedule lecture i in d + 1 d = d + 1 What order should we process requests in? Chandra & Lenny (UIUC) CS374 26 Spring 2015 26 / 46

  42. Greedy Algorithm Initially R is the set of all requests d = 0 (* number of classrooms *) while R is not empty do choose i ∈ R such that start time of i is earliest if i can be scheduled in some class-room k ≤ d schedule lecture i in class-room k else allocate a new class-room d + 1 and schedule lecture i in d + 1 d = d + 1 What order should we process requests in? According to start times (breaking ties arbitrarily) Chandra & Lenny (UIUC) CS374 26 Spring 2015 26 / 46

  43. Example of algorithm execution “Few things are harder to put up with than a good example.” – Mark Twain f i j e g c d b h a Chandra & Lenny (UIUC) CS374 27 Spring 2015 27 / 46

  44. Example of algorithm execution “Few things are harder to put up with than a good example.” – Mark Twain a Chandra & Lenny (UIUC) CS374 27 Spring 2015 27 / 46

  45. Example of algorithm execution “Few things are harder to put up with than a good example.” – Mark Twain a Chandra & Lenny (UIUC) CS374 27 Spring 2015 27 / 46

  46. Example of algorithm execution “Few things are harder to put up with than a good example.” – Mark Twain b a Chandra & Lenny (UIUC) CS374 27 Spring 2015 27 / 46

  47. Example of algorithm execution “Few things are harder to put up with than a good example.” – Mark Twain b a Chandra & Lenny (UIUC) CS374 27 Spring 2015 27 / 46

  48. Example of algorithm execution “Few things are harder to put up with than a good example.” – Mark Twain c b a a Chandra & Lenny (UIUC) CS374 27 Spring 2015 27 / 46

  49. Example of algorithm execution “Few things are harder to put up with than a good example.” – Mark Twain c b a Chandra & Lenny (UIUC) CS374 27 Spring 2015 27 / 46

  50. Example of algorithm execution “Few things are harder to put up with than a good example.” – Mark Twain c d b a Chandra & Lenny (UIUC) CS374 27 Spring 2015 27 / 46

  51. Example of algorithm execution “Few things are harder to put up with than a good example.” – Mark Twain c d b a Chandra & Lenny (UIUC) CS374 27 Spring 2015 27 / 46

  52. Example of algorithm execution “Few things are harder to put up with than a good example.” – Mark Twain e c d b a Chandra & Lenny (UIUC) CS374 27 Spring 2015 27 / 46

  53. Example of algorithm execution “Few things are harder to put up with than a good example.” – Mark Twain c d b a e Chandra & Lenny (UIUC) CS374 27 Spring 2015 27 / 46

  54. Example of algorithm execution “Few things are harder to put up with than a good example.” – Mark Twain f c d b a e Chandra & Lenny (UIUC) CS374 27 Spring 2015 27 / 46

  55. Example of algorithm execution “Few things are harder to put up with than a good example.” – Mark Twain c d b f a e Chandra & Lenny (UIUC) CS374 27 Spring 2015 27 / 46

  56. Example of algorithm execution “Few things are harder to put up with than a good example.” – Mark Twain g c d b f a e Chandra & Lenny (UIUC) CS374 27 Spring 2015 27 / 46

  57. Example of algorithm execution “Few things are harder to put up with than a good example.” – Mark Twain g c d b f a e Chandra & Lenny (UIUC) CS374 27 Spring 2015 27 / 46

  58. Example of algorithm execution “Few things are harder to put up with than a good example.” – Mark Twain g c d b f h a e Chandra & Lenny (UIUC) CS374 27 Spring 2015 27 / 46

  59. Example of algorithm execution “Few things are harder to put up with than a good example.” – Mark Twain g c d b f a e h Chandra & Lenny (UIUC) CS374 27 Spring 2015 27 / 46

  60. Example of algorithm execution “Few things are harder to put up with than a good example.” – Mark Twain i g c d b f a e h Chandra & Lenny (UIUC) CS374 27 Spring 2015 27 / 46

  61. Example of algorithm execution “Few things are harder to put up with than a good example.” – Mark Twain g c d b f i a e h Chandra & Lenny (UIUC) CS374 27 Spring 2015 27 / 46

  62. Example of algorithm execution “Few things are harder to put up with than a good example.” – Mark Twain j g c d b f i a e h Chandra & Lenny (UIUC) CS374 27 Spring 2015 27 / 46

  63. Example of algorithm execution “Few things are harder to put up with than a good example.” – Mark Twain g j c d b f i a e h Chandra & Lenny (UIUC) CS374 27 Spring 2015 27 / 46

  64. Depth of Lectures Definition For a set of lectures R , k are said to be in conflict if there is 1 some time t such that there are k lectures going on at time t . Chandra & Lenny (UIUC) CS374 28 Spring 2015 28 / 46

  65. Depth of Lectures Definition For a set of lectures R , k are said to be in conflict if there is 1 some time t such that there are k lectures going on at time t . The depth of a set of lectures R is the maximum number of 2 lectures in conflict at any time. Chandra & Lenny (UIUC) CS374 28 Spring 2015 28 / 46

  66. Depth of Lectures Definition For a set of lectures R , k are said to be in conflict if there is 1 some time t such that there are k lectures going on at time t . The depth of a set of lectures R is the maximum number of 2 lectures in conflict at any time. j c d f g b i a e h Chandra & Lenny (UIUC) CS374 28 Spring 2015 28 / 46

Recommend


More recommend