Activity Selection Problem in DSA C++ - Time & Space Complexity
We want to understand how the time needed to pick the most activities grows as the number of activities increases.
How does the program's work change when we have more activities to choose from?
Analyze the time complexity of the following code snippet.
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int activitySelection(vector<pair<int,int>> &activities) {
sort(activities.begin(), activities.end(), [](auto &a, auto &b) { return a.second < b.second; });
int count = 1, last_end = activities[0].second;
for (int i = 1; i < activities.size(); i++) {
if (activities[i].first >= last_end) {
count++;
last_end = activities[i].second;
}
}
return count;
}
This code selects the maximum number of activities that don't overlap by sorting them by finish time and then picking compatible ones.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Sorting the activities by their finish time.
- How many times: Sorting runs once over all activities, then a single loop checks each activity once.
Sorting takes more time as the number of activities grows, and the loop checks each activity once.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 log 10 + 10 = 40 operations |
| 100 | About 100 log 100 + 100 = 700 operations |
| 1000 | About 1000 log 1000 + 1000 = 10,000 operations |
Pattern observation: The work grows a bit faster than the number of activities because of sorting, but the loop grows linearly.
Time Complexity: O(n log n)
This means the time needed grows a little faster than the number of activities because sorting takes the most time.
[X] Wrong: "The loop checking activities is the slowest part, so the complexity is O(n)."
[OK] Correct: Sorting the activities takes more time than the loop, so it dominates the total time.
Understanding this problem helps you practice sorting and greedy choices, skills often needed to solve real-world scheduling tasks efficiently.
"What if the activities were already sorted by finish time? How would the time complexity change?"
Practice
Activity Selection Problem?Solution
Step 1: Understand the problem goal
The Activity Selection Problem aims to choose the largest set of activities that do not overlap in their time intervals.Step 2: Identify the correct goal
Among the options, only selecting the maximum number of non-overlapping activities matches the problem's goal.Final Answer:
Select the maximum number of activities that don't overlap in time -> Option DQuick Check:
Goal = Max non-overlapping activities [OK]
- Confusing longest duration with maximum count
- Sorting by start time only
- Summing all activity durations
std::sort?Solution
Step 1: Identify sorting criteria for Activity Selection
Activities must be sorted by their finish time to apply the greedy approach correctly.Step 2: Match sorting code to finish time
std::sort(activities.begin(), activities.end(), [](auto &a, auto &b){ return a.finish < b.finish; }); sorts activities by their finish time using a lambda comparator, which is the correct syntax.Final Answer:
std::sort(activities.begin(), activities.end(), [](auto &a, auto &b){ return a.finish < b.finish; }); -> Option BQuick Check:
Sort by finish time = std::sort(activities.begin(), activities.end(), [](auto &a, auto &b){ return a.finish < b.finish; }); [OK]
- Sorting by start time instead of finish time
- Using default sort without comparator on custom structs
- Sorting by duration which is not needed
struct Activity {
int start, finish;
};
Activity activities[] = {{1, 3}, {2, 5}, {4, 7}, {6, 9}};
int n = 4;
std::sort(activities, activities + n, [](Activity a, Activity b) { return a.finish < b.finish; });
int count = 1, last_finish = activities[0].finish;
for (int i = 1; i < n; i++) {
if (activities[i].start >= last_finish) {
count++;
last_finish = activities[i].finish;
}
}
std::cout << count;
Solution
Step 1: Sort activities by finish time
Sorted activities by finish time: (1,3), (2,5), (4,7), (6,9)Step 2: Select activities greedily
Start with first activity (1,3). Next, (2,5) starts at 2 < 3, skip. (4,7) starts at 4 >= 3, select, last_finish=7. (6,9) starts at 6 < 7, skip. Total selected = 3.Final Answer:
3 -> Option AQuick Check:
Max non-overlapping activities = 3 [OK]
- Counting overlapping activities
- Not updating last_finish correctly
- Assuming all activities can be selected
struct Activity {
int start, finish;
};
Activity activities[] = {{1, 2}, {3, 4}, {0, 6}, {5, 7}};
int n = 4;
std::sort(activities, activities + n, [](Activity a, Activity b) { return a.start < b.start; });
int count = 1, last_finish = activities[0].finish;
for (int i = 1; i < n; i++) {
if (activities[i].start >= last_finish) {
count++;
last_finish = activities[i].finish;
}
}
std::cout << count;
Solution
Step 1: Check sorting criteria
The code sorts activities by start time, but the greedy approach requires sorting by finish time.Step 2: Understand impact of wrong sorting
Sorting by start time can cause incorrect selection of activities, leading to wrong count.Final Answer:
Sorting by start time instead of finish time -> Option CQuick Check:
Sort by finish time needed, not start time [OK]
- Sorting by start time
- Incorrect initialization of count
- Wrong loop boundaries
activities = [(1, 4), (3, 5), (0, 6), (5, 7), (8, 9), (5, 9)]
Using the Activity Selection Problem approach, which set of activities represents the maximum number of non-overlapping activities?
Solution
Step 1: Sort activities by finish time
Sorted by finish time: (1,4), (3,5), (0,6), (5,7), (8,9), (5,9)Step 2: Select activities greedily
Select (1,4). Next, (3,5) overlaps with (1,4), skip. (0,6) overlaps, skip. (5,7) starts after 4, select. (8,9) starts after 7, select. (5,9) overlaps with (5,7), skip.Final Answer:
[(1, 4), (5, 7), (8, 9)] -> Option AQuick Check:
Max non-overlapping set = [(1, 4), (5, 7), (8, 9)] [OK]
- Choosing overlapping activities
- Not sorting before selection
- Selecting activities with later finish times first
Ready to prove it?
Pass the test and this pattern moves unknown
