Activity Selection
The Activity Selection problem is a classic example of a greedy algorithm application. It is concerned with selecting the maximum number of activities that don't overlap in time from a given set of activities.
Problem Statement
Given n activities with their start and finish times, select the maximum number of activities that a single person can attend without any overlap. Each activity is represented by a start time s[i] and a finish time f[i].
Greedy Strategy
The optimal greedy strategy for the activity selection problem involves selecting activities based on their finishing times. Here are the steps:
- Sort the activities based on their finish times.
- Select the first activity from the sorted list and add it to the result as it finishes the earliest.
- Iterate through the remaining activities and select the next activity if its start time is greater than or equal to the finish time of the last selected activity.
Algorithm
- Sort all the activities by their finishing time.
- Initialize
lastSelectedFinishTimeto 0 (or negative infinity if using time). - Initialize
selectedActivitiesto an empty list. - For each activity in the sorted list:
- If the start time of the activity is greater than or equal to
lastSelectedFinishTime:- Add the activity to
selectedActivities. - Update
lastSelectedFinishTimeto the finish time of the current activity.
- Add the activity to
- If the start time of the activity is greater than or equal to
- Return
selectedActivitiesas the list of chosen activities.
Greedy-Activity-Selector(A, s, f):
Sort A by finish times stored in f
S = {A[1]} # Select the first activity
k = 1
for i = 2 to n:
if s[i] >= f[k]:
S = S U {A[i]} # Select the activity
k = i
return S
Complexity
- Time Complexity: due to the sorting step, followed by an pass to select activities.
- Space Complexity: , not counting the space needed for output; this is due to the inplace sorting and a constant number of extra variables.
Example of the Activity Selection Problem
Suppose we have the following set of activities (labeled from A to E) with their respective start and finish times:
| Activity | Start Time | Finish Time |
|---|---|---|
| A | 1 | 4 |
| B | 3 | 5 |
| C | 0 | 6 |
| D | 5 | 7 |
| E | 8 | 9 |
Applying the Greedy Algorithm
-
Step 1: Sort Activities by Finish Time
- Sorted order of activities based on their finish times:
A, B, D, E, C
- Sorted order of activities based on their finish times:
-
Step 2: Select Activities
- Start with the first activity in the sorted list.
- Activity
A(1, 4) - Next, we skip
Bbecause it overlaps withA(it starts beforeAfinishes). - The next activity that doesn't overlap with
AisD(5, 7), so we select it. - Finally, activity
E(8, 9) does not overlap withDand can be selected.
Selected Activities
- A (Starts at 1, finishes at 4)
- D (Starts at 5, finishes at 7)
- E (Starts at 8, finishes at 9)
These activities are chosen because they allow for the maximum number of non-overlapping activities (three in this case). If you choose any activity like C that spans a long duration (0, 6), it would block out multiple other shorter activities, thus reducing the overall count.
Proof of Optimality
Step 1: Definition of Sets
- Let be the set of activities selected by the greedy algorithm.
- Let be any other set of activities that also do not overlap.
Step 2: Assumptions
- The greedy algorithm selects activities based on the earliest finishing times.
- Assume that activities in both and are sorted by their finish times. Let be the activities in and be the activities in , where and denote their respective finish times.
Step 3: Method of Substitution
- The goal is to show that the number of activities in (|A|) is at least as large as the number of activities in (|B|), i.e., .
Step 4: Proof by Induction
- Base Case: Compare and . Since is selected by the greedy algorithm, it has the earliest finish time among all activities and thus .
- Inductive Step:
- Assume that for the first activities in , they can be replaced by activities in such that for all .
- Consider the next activity . Since does not overlap with , we have .
- From the greedy selection, is the next activity selected after with , and thus . Therefore, must also be less than or equal to because is the earliest finishing activity available after .
Step 5: Conclusion
- By induction, each activity in can be matched with an activity in that finishes no later than itself, which implies .
- Since was any arbitrary set of non-overlapping activities, must be the largest possible set of non-overlapping activities.
- Therefore, the set selected by the greedy algorithm is optimal.