This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: ECE608, Homework #9 Solution (1) CLR 16.13 Let S be the set of n activities. The obvious solution of using GreedyActivity Selector to find a maximumsize set S 1 of compatible activities from S for the first lecture hall, then using it again to find a maximumsize 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 eventtime 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 activityselection 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 neverused hall. (This can be done by always working at the front of the freehall 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 mth 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 activitystarts/activityends events. (In the sorted order, an activity ending event should precede an activitystarting 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.
 Fall '11
 PAI

Click to edit the document details