Activity Selection
Activity Selection Problem: Given a set of activities, along with the starting and finishing time of each activity, find the maximum number of activities performed by a single person assuming that a person can only work on a single activity at a time.
For example,
Input: Following set of activities (1, 4), (3, 5), (0, 6), (5, 7), (3, 8), (5, 9), (6, 10), (8, 11), (8, 12), (2, 13), (12, 14) Output: (1, 4), (5, 7), (8, 11), (12, 14)
The activity selection problem is a problem concerning selecting non-conflicting activities to perform within a given time frame, given a set of activities each marked by a start and finish time. A classic application of this problem is scheduling a room for multiple competing events, each having its time requirements (start and end time).
Letโs assume there exist n
activities each being represented by a start time si and finish time fj
. Two activities i
and j
are said to be non-conflicting if si = fj
or sj = fi
.
We can solve this problem by being greedy. Using a greedy approach will always result in an optimal solution to this problem. The idea is to initially sort the activities in increasing order of their finish times and create a set S
to store the selected activities and initialize it with the first activity. Then from the second activity onward, include the activity in the activities list if the activityโs start time is greater or equal to the finish time of the last selected activity. Repeat this for each activity involved.
Output: {1, 4} {5, 7} {8, 11} {12, 14}
The time complexity of the above solution is O(n.log(n)), wheren
is the total number of activities. The auxiliary space required by the program is constant.
Last updated