Selected Solutions for Chapter 16:
Greedy Algorithms
Solution to Exercise 16.14
Let
S
be the set of
n
activities.
The “obvious” solution of using GREEDYACTIVITYSELECTOR to find a maxi
mumsize 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
N
S
1
for the
second hall, (and so on until all the activities are assigned), requires
‚.n
2
/
time
in the worst case. Moreover, it can produce a result that uses more lecture halls
than necessary. Consider activities with the intervals
f
Œ1; 4/; Œ2; 5/; Œ6; 7/; Œ4; 8/
g
.
GREEDYACTIVITYSELECTOR would choose the activities with intervals
Œ1; 4/
and
Œ6; 7/
for the first lecture hall, and then each of the activities with intervals
Œ2; 5/
and
Œ4; 8/
would have to go into its own hall, for a total of three halls used.
An optimal solution would put the activities with intervals
Œ1; 4/
and
Œ4; 8/
into one
hall and the activities with intervals
Œ2; 5/
and
Œ6; 7/
into another hall, for only two
halls used.
There is a correct algorithm, however, whose asymptotic time is just the time
needed to sort the activities by time—
O.n
lg
n/
time for arbitrary times, or pos
sibly 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
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.
 Spring '10
 .
 Order theory, WI, Monotonic function, mth lecture hall

Click to edit the document details