What is the main goal of the Activity Selection Problem?
To select the maximum number of activities that don't overlap in time from a given set of activities, each with a start and finish time.
Click to reveal answer
beginner
Which algorithmic approach is commonly used to solve the Activity Selection Problem efficiently?
A greedy algorithm that selects activities based on earliest finish time first.
Click to reveal answer
intermediate
Why do we sort activities by their finish times in the Activity Selection Problem?
Sorting by finish times helps to always pick the activity that leaves the most room for the remaining activities, maximizing the total number selected.
Click to reveal answer
intermediate
In the Activity Selection Problem, after selecting an activity, what is the next step?
Skip all activities that start before the selected activity finishes, then select the next activity that starts after the current one finishes.
Click to reveal answer
intermediate
What is the time complexity of the greedy solution to the Activity Selection Problem after sorting?
O(n log n) due to sorting the activities by finish time; the selection process itself is O(n).
Click to reveal answer
What is the first step in solving the Activity Selection Problem using a greedy approach?
ASort activities by their finish times
BSort activities by their start times
CSelect the longest activity first
DSelect activities randomly
✗ Incorrect
Sorting activities by finish times ensures we pick the activity that finishes earliest, allowing more activities to fit afterward.
After selecting an activity, which activities are considered next?
AAll activities that start before the selected activity finishes
BAll activities that start after the selected activity finishes
CAll activities with the earliest start time
DAll activities with the longest duration
✗ Incorrect
Only activities that start after the current selected activity finishes can be chosen next to avoid overlap.
What does the Activity Selection Problem maximize?
ATotal duration of selected activities
BSum of finish times
CSum of start times
DNumber of selected activities
✗ Incorrect
The goal is to select the maximum number of non-overlapping activities.
Which of these is NOT true about the greedy solution to the Activity Selection Problem?
AIt always picks the activity with the earliest finish time next
BIt guarantees the optimal solution
CIt requires checking all subsets of activities
DIt runs efficiently after sorting
✗ Incorrect
The greedy solution does not check all subsets; it makes a local optimal choice at each step.
What is the main reason the greedy algorithm works for the Activity Selection Problem?
ABecause the problem has the greedy-choice property and optimal substructure
BBecause activities are independent
CBecause all activities have the same duration
DBecause it tries all possible combinations
✗ Incorrect
The problem has the greedy-choice property and optimal substructure, allowing greedy algorithms to find the optimal solution.
Explain step-by-step how the greedy algorithm solves the Activity Selection Problem.
Think about how sorting helps and how you pick activities one by one.
You got /5 concepts.
Why does sorting activities by finish time lead to an optimal solution in the Activity Selection Problem?
Consider how choosing the earliest finishing activity affects the rest.
You got /4 concepts.
Practice
(1/5)
1. What is the main goal of the Activity Selection Problem?
easy
A. Calculate the total time of all activities
B. Find the activity with the longest duration
C. Sort activities by their start time
D. Select the maximum number of activities that don't overlap
Solution
Step 1: Understand the problem objective
The Activity Selection Problem aims to choose the largest set of activities that do not overlap in time.
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 -> Option D
Quick Check:
Goal = Max non-overlapping activities [OK]
Hint: Focus on maximizing non-overlapping activities [OK]
Common Mistakes:
Confusing longest duration with maximum count
Sorting by start time only
Summing all activity durations
2. Which of the following Java code snippets correctly sorts activities by their finish time for the Activity Selection Problem?
easy
A. Arrays.sort(activities, (a, b) -> a.finish - b.finish);
B. Arrays.sort(activities, (a, b) -> a.start - b.start);
C. Arrays.sort(activities, (a, b) -> b.finish - a.finish);
D. Arrays.sort(activities, (a, b) -> b.start - a.start);
Solution
Step 1: Identify sorting criteria for Activity Selection
Activities must be sorted by their finish time in ascending order to apply the greedy approach.
Step 2: Match code to sorting by finish time ascending
Arrays.sort(activities, (a, b) -> a.finish - b.finish); sorts by finish time ascending (a.finish - b.finish), which is correct.
Final Answer:
Arrays.sort(activities, (a, b) -> a.finish - b.finish); -> Option A
Quick Check:
Sort by finish time ascending = Arrays.sort(activities, (a, b) -> a.finish - b.finish); [OK]
Hint: Sort activities by finish time ascending [OK]
Common Mistakes:
Sorting by start time instead of finish time
Sorting in descending order
Using incorrect comparator syntax
3. What is the output of the following Java code snippet for the Activity Selection Problem?
class Activity {
int start, finish;
Activity(int s, int f) { start = s; finish = f; }
}
Activity[] activities = {
new Activity(1, 4),
new Activity(3, 5),
new Activity(0, 6),
new Activity(5, 7),
new Activity(8, 9),
new Activity(5, 9)
};
Arrays.sort(activities, (a, b) -> a.finish - b.finish);
int count = 1;
int lastFinish = activities[0].finish;
for (int i = 1; i < activities.length; i++) {
if (activities[i].start >= lastFinish) {
count++;
lastFinish = activities[i].finish;
}
}
System.out.println(count);
medium
A. 5
B. 3
C. 4
D. 6
Solution
Step 1: Sort activities by finish time ascending
The activities in sorted order (stable sort preserves order of ties): (1,4), (3,5), (0,6), (5,7), (8,9), (5,9)
Step 2: Trace the greedy selection
Pick (1,4), count=1, lastFinish=4 Skip (3,5) since 3 < 4 Skip (0,6) since 0 < 4 Pick (5,7) since 5 >= 4, count=2, lastFinish=7 Pick (8,9) since 8 >= 7, count=3, lastFinish=9 Skip (5,9) since 5 < 9
Step 3: Check if any other activities can be selected
All activities considered, total count = 4 (including the first selected activity)
Final Answer:
4 -> Option C
Quick Check:
Greedy selection count = 4 [OK]
Hint: Sort by finish time, pick next starting after last finish [OK]
Common Mistakes:
Not sorting activities before selection
Selecting overlapping activities
Incorrectly updating last finish time
4. Identify the error in the following Java code snippet for the Activity Selection Problem:
Arrays.sort(activities, (a, b) -> a.finish + b.finish);
int count = 1;
int lastFinish = activities[0].finish;
for (int i = 1; i < activities.length; i++) {
if (activities[i].start >= lastFinish) {
count++;
lastFinish = activities[i].finish;
}
}
System.out.println(count);
medium
A. Loop starts from index 0 instead of 1
B. Comparator uses addition instead of subtraction for sorting
C. Condition should check start > lastFinish, not >= lastFinish
D. Count variable is not initialized
Solution
Step 1: Analyze the comparator in Arrays.sort
The comparator uses a.finish + b.finish, which does not correctly compare finish times. It should use subtraction (a.finish - b.finish) to sort ascending.
Step 2: Understand impact of incorrect comparator
Using addition returns a positive number regardless of order, causing incorrect sorting and wrong activity selection.
Final Answer:
Comparator uses addition instead of subtraction for sorting -> Option B
Quick Check:
Comparator must subtract finish times [OK]
Hint: Use subtraction in comparator, not addition [OK]
Common Mistakes:
Using addition in comparator instead of subtraction
Starting loop at wrong index
Misunderstanding condition operator
5. Given the following activities with start and finish times: