hw9s.f11

# hw9s.f11 - ECE608 Homework#9 Solution(1 CLR 16.1-3 Let S be...

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: ECE608, Homework #9 Solution (1) CLR 16.1-3 Let S be the set of n activities. The obvious solution of using Greedy-Activity- Selector to find a maximum-size set S 1 of compatible activities from S for the first lecture hall, then using it again to find a maximum-size set S 2 of compatible activities from S − S 1 for the second hall, and so on until all the activities are assigned, requires Θ( n 2 ) time in the worst case. However, there is a better algorithm, whose asymptotic time is just the time needed to sort the activities by time (i.e., O ( n lg n ) time), or possibly as fast as O ( n ) if the times are small integers. The general idea is to go through the activities in order of start time, assigning each to any hall that is available at that time. To do this, move through the set of events consisting of activities starting and activities finishing, in order of event time. Maintain two lists of lecture halls: Halls that are busy at the current event-time t (because they have been assigned an activity i that started at s i ≤ t but won’t finish until f i > t ) and halls that are free at time t . (As in the activity-selection problem in Section 16.1, we are assuming that activity time intervals are half open—that is, if s i ≥ f j , activities i and j are compatible.) When t is equal to the start time of some activity, assign that activity to a free hall and move the hall from the free list to the busy list. When t is equal to the finish time of some activity, move the activity’s hall from the busy list to the free list. (The activity is certainly in some hall, because the event times are processed in order and the activity must have started before its finish time t , hence must have been assigned to a hall.) To avoid using more halls than necessary, always pick a hall that has already had an activity assigned to it, if possible, before picking a never-used hall. (This can be done by always working at the front of the free-hall list—putting freed halls onto the front of the list and taking halls from the front of the list—so that a new hall doesn’t come to the front and get chosen if there are previously used halls.) This guarantees that the algorithm uses as few lecture halls as possible: The algorithm will terminate with a schedule requiring m ≤ n lecture halls. Let activity i be the first activity scheduled in lecture hall m . The reason that i was put in the m-th lecture hall is that the first m − 1 lecture halls were busy at time s i . So at this time, there are m activities occurring simultaneously. Therefore any schedule must use at least m lecture halls, so the schedule returned by the algorithm is optimal. Run time: (i) Sort the 2 n activity-starts/activity-ends events. (In the sorted order, an activity- ending event should precede an activity-starting event at the same time.) O ( n lg n ) 1 time for arbitrary times, possibly O ( n ) if the times are restricted (e.g., to small integers)....
View Full Document

## This note was uploaded on 01/10/2012 for the course ECE 565 taught by Professor Pai during the Fall '11 term at Purdue.

### Page1 / 9

hw9s.f11 - ECE608 Homework#9 Solution(1 CLR 16.1-3 Let S be...

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

View Full Document
Ask a homework question - tutors are online