L14 - Activity Selection
L14 - Activity Selection
6
Activity Selection Problem
• Input: A set of activities S = {a1,…, an}
• Each activity has start time (si) and a finish time (fi) ai=(si, fi)
• Two activities are compatible if and only if their interval does not
overlap.
7
Example: Activity Selection
• Here are a set of start and finish times:
9
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Solution
17
Algorithm: Activity Selection
Sort the input activities by increasing finishing time (f1 ≤ f2 ≤ . . . ≤ fn)
n = length [s]
A={i}
j=1
For i = 2 to n
do if si ≥ fj
then A= AU{i}
j=i
Return A
18
Complexity Analysis
• Sorting the activities on the basis of finishing line = O(nlgn)
• For loop in the algorithm = Ɵ(n)
• Overall Complexity:
When activities are sorted by their finish time: O(N)
When activities are not sorted by their finish time, the time
complexity is O(N log N) due to complexity of sorting.
19
In Class Practice
Ques: Look at the following table containing activities, and their start
and end time.
Here, si and fi are the starting and the finishing time of the activity ai.
20
Solution
21
Problem:
In the table below, we have 6 activities with corresponding start and end time, the objective is to compute an execution schedule
having maximum number of non-conflicting activities: