0
0
DSA Typescriptprogramming~10 mins

Activity Selection Problem in DSA Typescript - 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
Select activity
Repeat
Done
Sort activities by finish time, then pick the earliest finishing activity that doesn't overlap with previously selected ones.
Execution Sample
DSA Typescript
interface Activity {
  start: number;
  finish: number;
}

function selectActivities(activities: Activity[]): Activity[] {
  activities.sort((a, b) => a.finish - b.finish);
  const selected: Activity[] = [];
  let lastFinish = 0;
  for (const act of activities) {
    if (act.start >= lastFinish) {
      selected.push(act);
      lastFinish = act.finish;
    }
  }
  return selected;
}
This code sorts activities by finish time and selects the maximum number of non-overlapping activities.
Execution Table
StepOperationCurrent Activity (start, finish)Last Selected FinishSelected ActivitiesActionVisual State
1Sort activities by finish time--[]Sorted activities[ (1,4), (3,5), (0,6), (5,7), (8,9), (5,9) ]
2Select first activity(1,4)0[]Select (1,4)[(1,4)]
3Check next activity(3,5)4[(1,4)]Skip (3,5) because 3 < 4[(1,4)]
4Check next activity(0,6)4[(1,4)]Skip (0,6) because 0 < 4[(1,4)]
5Check next activity(5,7)4[(1,4)]Select (5,7) because 5 >= 4[(1,4), (5,7)]
6Check next activity(8,9)7[(1,4), (5,7)]Select (8,9) because 8 >= 7[(1,4), (5,7), (8,9)]
7Check next activity(5,9)9[(1,4), (5,7), (8,9)]Skip (5,9) because 5 < 9[(1,4), (5,7), (8,9)]
8End of activities-9[(1,4), (5,7), (8,9)]Done selecting[(1,4), (5,7), (8,9)]
💡 All activities checked; selection complete.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5After Step 6After Step 7Final
lastFinish04447999
selected[][(1,4)][(1,4)][(1,4)][(1,4), (5,7)][(1,4), (5,7), (8,9)][(1,4), (5,7), (8,9)][(1,4), (5,7), (8,9)]
currentActivity-(1,4)(3,5)(0,6)(5,7)(8,9)(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 up time earliest, allowing more activities to fit later. See Step 1 in execution_table where activities are sorted.
Why do we skip activities that start before the last selected activity finishes?
Because they overlap and can't be done together. For example, at Step 3 and 4, activities starting before lastFinish are skipped to avoid overlap.
Why do we update lastFinish only when we select an activity?
Because lastFinish marks the end time of the last chosen activity, helping to check overlap for future activities. See variable_tracker for lastFinish updates after selection steps.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at Step 5, what is the lastFinish value after selecting activity (5,7)?
A4
B7
C5
D9
💡 Hint
Check the 'Last Selected Finish' column at Step 5 in execution_table.
At which step does the condition 'act.start >= lastFinish' become false for the first time?
AStep 3
BStep 2
CStep 5
DStep 6
💡 Hint
Look at the 'Action' column in execution_table where activities are skipped due to overlap.
If the activity (3,5) was changed to (4,5), how would the selection change at Step 3?
AIt would cause an error
BIt would still be skipped
CIt would be selected instead of skipped
DIt would be selected but lastFinish would not update
💡 Hint
Compare 'currentActivity' start time with 'lastFinish' at Step 3 in variable_tracker.
Concept Snapshot
Activity Selection Problem:
- Sort activities by finish time
- Select first activity
- For each next activity, select if start >= last selected finish
- Update last selected finish time
- Result: maximum set of non-overlapping activities
Full Transcript
The Activity Selection Problem involves choosing the maximum number of activities that don't overlap. We start by sorting activities by their finish times. Then, we pick the first activity and keep track of its finish time. For each next activity, if its start time is equal or after the last selected finish time, we select it and update the finish time. Otherwise, we skip it. This process continues until all activities are checked. The final selected list contains the maximum number of compatible activities.