10_greedy.v2

# 10_greedy.v2 - CS161 Greedy Algorithms David Kauchak •...

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
This is the end of the preview. Sign up to access the rest of the 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 doesn’t, but it produces a reasonable solution that is computationally tractible. • MST was a greedy algorithm Both Kruskall’s and Prim’s 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: IntervalSchedule-Recursive ( 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 = IntervalSchedule-Recursive ( 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 !) We’ll 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 } counter-example: 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 } counter-example: one short active that conflicts with two longer – Select the activity with the smallest number of conflicts counter-example? – Select the activity that finishes first, i.e. argmin { f 1 ,f 2 ,...,f n } IntervalSchedule-Greedy ( 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

{[ snackBarMessage ]}

### Page1 / 9

10_greedy.v2 - CS161 Greedy Algorithms David Kauchak •...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online