greedy algorithms
play

Greedy Algorithms Chapter 16 1 CPTR 430 Algorithms Greedy - PowerPoint PPT Presentation

Greedy Algorithms Chapter 16 1 CPTR 430 Algorithms Greedy Algorithms Greedy Algorithms For some optimization problems, dynamic programming algorithms are overkill A greedy algorithm always makes a choice that looks best at the moment


  1. Greedy Algorithms Chapter 16 1 CPTR 430 Algorithms Greedy Algorithms

  2. Greedy Algorithms ■ For some optimization problems, dynamic programming algorithms are overkill ■ A greedy algorithm always makes a choice that looks best at the moment ■ The strategy: A locally optimal choice will ultimately lead to a globally optimal solution ■ Greedy algorithms do not always yield optimal solutions ■ For some kinds of problems, though, greedy algorithms always yield optimal solutions ❚ Dijkstra’s shortest path in a graph ❚ Prim’s minimum spanning tree of a graph ❚ Plus many others . . . 2 CPTR 430 Algorithms Greedy Algorithms

  3. ☎ ✄ ✂ ✆ ☎ ✆ ✄ ✂ ☎ ✄ ✂ ✁ ✁ � Example: Activity Selection ■ Need to schedule several activities that compete for a common resource to which they require exclusive access For example, a classroom in which only one class can be conducted at a time ■ Each activity a i has a start time s i and a finish time f i , where ∞ 0 s i f i ■ Activity a i takes place during the half-open interval s i f i ■ Activities a i and a j are compatible if the intervals s i f i and s j f j do not overlap That is, s i f j or s j f i 3 CPTR 430 Algorithms Greedy Algorithms

  4. ✄ � ✄ ✄ ✁ ✁ ✄ � ✄ ✄ � ✄ ✄ ✁ The Activity Selection Problem ■ Select a maximum size subset of mutually compatible activities ■ Example: Consider the set of activities (sorted in monotonically increasing order of finish time): i 1 2 3 4 5 6 7 8 9 10 11 s i 1 3 0 5 3 5 6 8 8 2 12 f i 4 5 6 7 8 9 10 11 12 13 14 a 3 a 9 a 11 ■ The subset contains mutually compatible activities but is not maximal ■ The subsets a 1 a 4 a 8 a 11 and a 1 a 4 a 9 a 11 are both larger (and both are maximal) 4 CPTR 430 Algorithms Greedy Algorithms

  5. ✂ � ✁ � � ✂ ✁ ☎ � � ✁ � � � ✄ ✄ ✂ A Dynamic Programming Solution ■ Find the optimal substructure ■ Define set S i j : S i j a i j f i s k f k s j S i j is the set of activities in S that can start after activity a i finishes and finish before activity a j starts ■ S i j is the set of all activities that are compatible with both a i and a j ■ To make a nice, regular algorithm, create dummy activities a 0 and a n 1 (to bracket the real activities in which we are interested) ∞ 0 and s n f 0 1 ■ S , the set of all activities, can thus be written S 0 1 , n ■ 0 1 i j n 5 CPTR 430 Algorithms Greedy Algorithms

  6. ✁ ✂ ✁ � � ✁ ✁ � � ☎ ✂ ✆ ☎ � ✄ ✄ � � � ✂ � � � Finding Subproblems ■ Sort the activities in monotonically increasing order of finish time: f 0 f 1 f 2 f n f n � ✁� 1 ■ i j S i j ❚ Prove by contradiction ❚ Suppose a k S i j for some i j , where a i follows a j in sorted order (MIOFT) ❚ This means f i s k f k s j f j f i f j , a contradiction to the assumption that a i follows a j in sorted order (MIOFT) ■ Thus, for subproblems we chose a maximal-size subset of mutually compatible activities from S i j where 0 1 i j n All other S i j will be empty 6 CPTR 430 Algorithms Greedy Algorithms

  7. � ✁ � � ✁ ✁ ✁ � ✁ ✁ � Substructure ■ Let a solution to S i j contain activity a k ■ f i s k f k s j ■ Use a k to split the problem into two subproblems: ❚ S ik , the set of activities that start after a i finishes and before a k starts ❚ S k j , the set of activities that start after a k finishes and before a j starts ❚ Let the optimal solution to S pq be expressed as A pq This means that no other set A pq contains more activities than A pq ❚ The solution for S i j , A i j , is then A ik a k A k j : A i j A ik a k A k j 7 CPTR 430 Algorithms Greedy Algorithms

  8. � ✁ ✁ ✁ � ✁ � � ✁ � � ✁ ✁ � ✄ � ✆ ✁ Optimal Substructure A ik is an optimal solution to S ik ■ a k A i j A k j is an optimal solution to S k j ■ To show, suppose otherwise ■ Let A A ik ( A ik contains more activities than A ik ) ik Then A i j is not optimal, since substituting A ik for A ik yields a more optimal solution to S i j ■ Let A A k j ( A k j contains more activities than A k j ) k j Then A i j is not optimal, since substituting A k j for A k j yields a more optimal solution to S i j 8 CPTR 430 Algorithms Greedy Algorithms

  9. ✄ ✂ ✄ ✂ ✁ ✁ � ✁ � DP Solution Build a maximum-size subset of mutually compatible activities in S i j by: ■ Choose an activity, a k , that is a member of A i j , an optimal solution ■ Find the optimal solutions to S ik and S k j (that is, A ik and A k j ) ■ Combine the results: A i j A ik a k A k j ■ To solve the entire problem, S 0 1 , find A 0 1 n n 9 CPTR 430 Algorithms Greedy Algorithms

  10. � � � � ☎ � � ✂ � ✄ ✄ � ✂ ✄ ✂ ✁ ✄ ✁ � ☎ ✁ ☎ � ✂ � ✂ ✂ ☎ ✂ ✂ ☎ ✄ � � ✄ � ✄ � ☎ ✄ ✂ ✂ ☎ ✄ ☎ � � � ✄ ✂ ✂ ✄ � ✄ Recursive Solution ■ Let c i j be the number of activities in a maximal-size subset of mutually compatible activities in S i j 0 ■ S i j c i j 0 ■ i j c i j Why? 1 ■ c i j c i k c k j ■ What is k ? 1 1 ❚ i k j 1 possibilities) ❚ Check all these possible values for k (all j i 0 if S i j ❚ c i j max 1 c i k c k j if S i j i k j 10 CPTR 430 Algorithms Greedy Algorithms

  11. ✁ � ✆ ✁ ✁ ✆ ✄ ✁ � ✆ � � � � ☎ � ✄ ☎ � Making the Choice of k Easy Theorem 16.1: ■ Let S i j , and let a m be the activity with the earliest finish time in S i j min f m f k a k S i j 1 a m A i j min ■ f m f k a k S i j 2 Subproblem S im ❚ The first item means that a m is in a maximal-size subset of mutually compatible activities in S i j ❚ The second item means that of the two subproblems formed by choosing a m , only S m j is nonempty 11 CPTR 430 Algorithms Greedy Algorithms

  12. ✁ ✁ � ✁ � � � � ✁ � ✄ � ✁ ✆ ✁ ✄ � � ✁ ✁ ✆ � ✁ Proof of Item 1 ■ Let A i j be a maximal-size subset of mutually compatible activities in S i j ■ Order the elements of A i j in monotonically increasing order of finish time ■ Let a k be the first element in A i j : ❚ a k a m a m A i j ❚ If a k a m , then make a new set A A i j a k a m i j ❚ The activities in A i j are mutually compatible because: ❙ The activities in A i j are mutually compatible ❙ a k is the first activity to finish in A i j ❙ f m f k ❚ Since A A i j , A i j must also be a maximal-size subset of mutually i j compatible activities in S i j , and a m A i j 12 CPTR 430 Algorithms Greedy Algorithms

  13. ✆ � ✄ ✁ ✁ � ✁ ✁ ✄ � ✁ ✄ ☎ � ✄ � ☎ � ✄ � Proof of Item 2 ■ Suppose S im ■ S im there exists activity a k such that f i s k f k s m f m min ■ f k f m f m f k a k S i j , contradicting our choice of a m 13 CPTR 430 Algorithms Greedy Algorithms

  14. ✁ ✄ ✄ ✄ ✄ ✁ Value of Theorem 16.1 ■ DP solution two subproblems 1 choices for solving subproblem S i j ■ DP solution j i ■ Greedy solution one subproblem ■ Greedy solution one choice for selecting the subproblem 14 CPTR 430 Algorithms Greedy Algorithms

  15. Greedy Approach is Top-down ■ DP is naturally bottom-up: Recursively solve the subproblems to determine the best choice for a m , then and combine the results to obtain the solution to the overall problem ■ The greedy approach is naturally top-down: To solve S i j , choose a m first, then solve the subproblem S m j 15 CPTR 430 Algorithms Greedy Algorithms

  16. Being Greedy ■ In the Activity scheduling problem, we always choose the activity with the earliest finish time that is mutually compatible with activities already in the solution ■ The choice is “greedy” since it leaves more activities left to be considered ■ This maximizes the amount of unscheduled time so that more activities may be crammed into the solution 16 CPTR 430 Algorithms Greedy Algorithms

  17. Recursive Activity Scheduler First, some of the supporting code: public class ActivitySelector { private static class Time { public int start; public int finish; public Time(int s, int f) { start = s; finish = f; } public String toString() { return "[" + start + "," + finish + "]"; } } private static final int INFINITY = Integer.MAX_VALUE; // . . . } 17 CPTR 430 Algorithms Greedy Algorithms

  18. More Supporting Code public class ActivitySelector { // . . . private static Time[] concat(Time[] array1, Time[] array2) { Time[] result = null; // Default value if ( array1 == null ) { result = array2; } else if ( array2 == null ) { result = array1; } else { result = new Time[array1.length + array2.length]; System.arraycopy(array1, 0, result, 0, array1.length); System.arraycopy(array2, 0, result, array1.length, array2.length); } return result; // . . . } } 18 CPTR 430 Algorithms Greedy Algorithms

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