0
0
DSA Cprogramming~10 mins

Activity Selection Problem in DSA C - Execution Trace

Choose your learning style9 modes available
Concept Flow - Activity Selection Problem
Sort activities by finish time
Select first activity
For each next activity
Check if start >= last selected finish?
NoSkip activity
Yes
Select activity
Repeat until all activities checked
Return selected activities
Sort activities by finish time, then pick the earliest finishing compatible activities one by one.
Execution Sample
DSA C
activities = [(1,4),(3,5),(0,6),(5,7),(8,9),(5,9)];
// Sort by finish time
activities.sort(key=lambda x: x[1])
selected = [activities[0]]
last_finish = activities[0][1]
for i in range(1, len(activities)):
    if activities[i][0] >= last_finish:
        selected.append(activities[i])
        last_finish = activities[i][1]
Select maximum number of non-overlapping activities by sorting and greedy selection.
Execution Table
StepOperationCurrent Activity (start, finish)Last Selected FinishSelected ActivitiesAction
1Sort activities by finish time--[]Original: [(1,4),(3,5),(0,6),(5,7),(8,9),(5,9)] sorted by finish time: [(1,4),(3,5),(0,6),(5,7),(8,9),(5,9)]
2Select first activity(1,4)-[(1,4)]Select first activity (1,4)
3Check next activity(3,5)4[(1,4)]3 < 4? No, skip (3,5)
4Check next activity(0,6)4[(1,4)]0 < 4? No, skip (0,6)
5Check next activity(5,7)4[(1,4)]5 >= 4? Yes, select (5,7)
6Check next activity(8,9)7[(1,4),(5,7)]8 >= 7? Yes, select (8,9)
7Check next activity(5,9)9[(1,4),(5,7),(8,9)]5 >= 9? No, skip (5,9)
8End-9[(1,4),(5,7),(8,9)]All activities checked
💡 All activities checked, selection complete.
Variable Tracker
VariableStartAfter Step 2After Step 5After Step 7Final
Selected Activities[][(1,4)][(1,4),(5,7)][(1,4),(5,7),(8,9)][(1,4),(5,7),(8,9)]
Last Selected Finish-4799
Current Activity-(1,4)(5,7)(5,9)-
Key Moments - 3 Insights
Why do we sort activities by their finish time first?
Sorting by finish time ensures we always pick the activity that frees the schedule earliest, allowing more activities to fit later. See Step 1 in execution_table.
Why do we compare the start time of the current activity with the finish time of the last selected activity?
To avoid overlapping activities, we only select the current activity if it starts after or exactly when the last selected one finishes. See Steps 3, 5, 6, and 7.
Why do we always select the first activity after sorting?
Because the first activity finishes earliest, selecting it maximizes room for others. This is shown in Step 2.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the selected activities list after Step 5?
A[(1,4),(3,5)]
B[(1,4)]
C[(1,4),(5,7)]
D[(5,7)]
💡 Hint
Check the 'Selected Activities' column at Step 5 in execution_table.
At which step does the condition 'start >= last finish' become false for the activity (5,9)?
AStep 4
BStep 7
CStep 6
DStep 3
💡 Hint
Look at Step 7 in execution_table under 'Action' and 'Current Activity'.
If the activity (5,7) was removed, what would be the selected activities after Step 7?
A[(1,4),(8,9)]
B[(1,4),(5,9),(8,9)]
C[(1,4),(5,9)]
D[(3,5),(8,9)]
💡 Hint
Without (5,7), the next compatible activity after (1,4) is (8,9). Check variable_tracker for selected activities.
Concept Snapshot
Activity Selection Problem:
- Sort activities by finish time
- Select first activity
- For each next activity, select if start >= last selected finish
- Repeat until all checked
- Result: max non-overlapping activities
Full Transcript
The Activity Selection Problem finds the maximum number of activities that don't overlap. First, we sort activities by their finish times. Then, we pick the first activity because it finishes earliest. Next, for each activity, we check if its start time is at or after the finish time of the last selected activity. If yes, we select it; if no, we skip it. We repeat this until all activities are checked. The selected activities form the largest set of non-overlapping activities.