This preview shows pages 1–3. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: CS161  Greedy Algorithms David Kauchak Greedy approach Make locally optimal decisions. Ideally, this would create a globally optimal solution. Sometimes, it doesnt, but it produces a reasonable solution that is computationally tractible. MST was a greedy algorithm Both Kruskalls and Prims algorithm made a greedy selection for the next edge to add to the MST. Interval scheduling We are given a set of n activities A = [ a 1 ,a 2 ,...,a n ] where each activity has a start time s i and a finish time f i . We would like to schedule as many of these activities for a shared resource. The only constraint is that no two activities can be scheduled at the same time. Example A recursive solution: IntervalScheduleRecursive ( A ) 1 if A = {} 2 return 3 else 4 max = 5 for all a A 6 A prime A minus a and all conflicting activites with a 7 s = IntervalScheduleRecursive ( A prime ) 8 if s > max 9 max = s 10 return 1 + max 1 Is it correct? The algorithm tries all possible sets and returns max. Runtime Worst case, how many recursive calls are made? O ( n !) Well see next week that we can do better with dynamic program ming methods O ( n 2 ) Can we do better than this? Rather than trying to examine all possible solutions, can we make some locally greedy solution and still end up at the optimal solution? Some ideas: Select the activity that starts the earliest, i.e. argmin { s 1 ,s 2 ,...,s n } counterexample: one long activity that starts the earliest Select the shortest activity, i.e. argmin { f 1 s 1 ,f 2 s 2 ,...,f n s n } counterexample: one short active that conflicts with two longer Select the activity with the smallest number of conflicts counterexample? Select the activity that finishes first, i.e. argmin { f 1 ,f 2 ,...,f n } IntervalScheduleGreedy ( A ) 1 sort A based on finish times f i 2 for i 1 to n 3 add a i to R 4 finish f i 5 while s i < finish 6 i i + 1 7 return R Is it correct?...
View Full
Document
 Summer '09
 Algorithms

Click to edit the document details