An activity-selection problem
Let set S = {a1, a2, . . .
,an}is of ‘n’ proposed activities that wish to use a
resource, which can serve only one activity at a time.
Each
activity ai has a start time si and a finish
time fi, where 0 ≤ si < fi
< ∞. Assume
that the activities are sorted in monotonically increasing order of finish
time: f1 ≤ f2 ≤ .
. . ≤ fn-1 ≤ fn.
In
activity-selection problem, aim is to select a maximum-size subset
of mutually compatible activities. Activities ai and aj
are compatible if the intervals [si, fi )
and [sj, fj ) do not overlap.
The
optimal substructure of the activity-selection problem
Let
us denote by Sij the set of activities that start after activity ai
finishes and that finish before activity aj starts.
To find a maximum set of mutually
compatible activities in Sij , suppose that Aij is a
maximum set , which includes some activity ak.
By
including ak in an optimal solution, there are two
sub problems:
i) finding mutually compatible activities in the set Sik (activities
that start after activity ai finishes and that finish before activity ak
starts) and,
ii) finding mutually compatible activities in the set Skj (activities
that start after activity ak finishes and that finish before activity aj
starts).
Let Aik = Aij ∩ Sik
and Akj = Aij ∩ Skj , so that Aik
contains the activities in Aij that finish before ak starts
and Akj contains the activities in Aij that start after ak
finishes.
Thus,
we have Aij = Aik U {ak} U Akj ,
and so the maximum-size set Aij of mutually compatible
activities
in Sij consists of |Aij | = |Aik| + |Akj
| + 1 activities.
If
we denote the size of an optimal solution for the set Sij by c[i, j
], then we would have the recurrence
If an optimal solution for the set Sij includes activity ak
A recursive algorithm can be developed and
memoize it, or work bottom-up and fill in table entries.
But another important characteristic of the activity-selection problem that is overlooked is to use the greedy choice.
Making the
greedy choice
The greedy choice for the activity-selection is
to choose an activity that leaves the resource available for as many other
activities as possible.
The
choice is to choose the activity in S with the earliest finish time, since that
would leave the resource available for as many of the activities that follow it
as possible. Since
the activities are sorted in monotonically increasing order by finish time, the
greedy choice is activity a1. If greedy choice is made, only one
remaining sub problem to solve is finding activities that start after a1
finishes. There is no need to consider activities that finish before a1 starts.
The algorithm works like :
1) Select the first activity from the sorted
array and put in a set A.
2) Do following for remaining activities in the sorted array.
2) Do following for remaining activities in the sorted array.
a)
If the start time of second activity is greater than or equal to the finish
time of previously selected activity then
select this activity and put it in a set A.
The procedure GREEDY-ACTIVITY-SELECTOR assumes that the input activities are
ordered by monotonically increasing finish time. It collects selected
activities into a set A and returns this set when it is done. GREEDY-ACTIVITY-SELECTOR schedules a set of n activities
in θ (n) time.
Example:
In above figure highlighted activities a1,a3,a6,a8 form the maximum subset of mutually compatible activities since there start finish times are greater than start times of already selected activities.
No comments:
Post a Comment