1. Gyanastra
  2. Algorithms
  3. Activity Selection, Job Sequencing and Greedy Versus Dynamic Programming
  4. Activity Selection Problem
Free lesson, no account needed 4 min read
Asked in past papers2 times
  • UGC NET20162015

Algorithms · Activity Selection, Job Sequencing and Greedy Versus Dynamic Programming

Activity Selection Problem

2.1 Statement and notation

Given nn activities, each with start time sis_i and finish time fif_i with 0≤si<fi0 \le s_i < f_i. Two activities i,ji,j are compatible if their intervals do not overlap: fi≤sjf_i \le s_j or fj≤sif_j \le s_i. (Touching endpoints — one finishes exactly when the next starts — counts as compatible; this is the standard convention in CLRS and in GATE.) Goal: select a maximum-size set of mutually compatible activities.

Notation: nn = number of activities; si,fis_i, f_i = start, finish of activity ii; AA = chosen set; ∣A∣|A| = number of activities selected.

2.2 The greedy rule and why "earliest finish" wins

Rule: sort activities by finish time ascending, then scan once, picking an activity if its start time is ≥\ge the finish time of the last picked activity.

Why earliest finish and not earliest start or shortest duration? Earliest finish leaves the maximum remaining time for all later activities. Formally (exchange argument): let a1a_1 be the activity with earliest finish time in the whole set, and let OO be an optimal solution. If OO's first activity is ak≠a1a_k \ne a_1, then f1≤fkf_1 \le f_k, so replacing aka_k by a1a_1 keeps every other activity in OO compatible (they all start at or after fk≥f1f_k \ge f_1). So there is an optimal solution starting with a1a_1; recurse on the activities starting after f1f_1. Hence greedy is optimal.

Counter-rules to remember: choosing earliest start or shortest duration first is not optimal in general.

2.3 Algorithm and complexity

ACTIVITY-SELECTION(s, f, n)          // f already sorted ascending
1  A = {1}
2  last = 1
3  for i = 2 to n
4      if s[i] >= f[last]
5          A = A ∪ {i}
6          last = i
7  return A

Sorting costs O(nlog⁡n)O(n \log n); the scan is O(n)O(n). Total O(nlog⁡n)O(n \log n) (or O(n)O(n) if input is already sorted by finish time — a favourite one-mark question).

2.4 Full trace on 8 activities

ii12345678
sis_i13053568
fif_i4567991011

Step 1 — sort by finish time. Finish times are already 4,5,6,7,9,9,10,114,5,6,7,9,9,10,11 in index order, so the sorted order is 1,2,3,4,5,6,7,81,2,3,4,5,6,7,8 (ties between 5 and 6 broken arbitrarily; both have f=9f=9).

Step 2 — scan.

StepConsidersis_iflastf_{\text{last}}si≥flasts_i \ge f_{\text{last}}?Actionlast
init11——pick 11 (f=4f=4)
12343≥43 \ge 4? Noskip1
23040≥40 \ge 4? Noskip1
34545≥45 \ge 4? Yespick 44 (f=7f=7)
45373≥73 \ge 7? Noskip4
56575≥75 \ge 7? Noskip4
67676≥76 \ge 7? Noskip4
78878≥78 \ge 7? Yespick 88 (f=11f=11)

Answer: A={1,4,8}A = \{1, 4, 8\}, maximum count =3= 3.

Check compatibility: activity 1 runs [1,4)[1,4), activity 4 runs [5,7)[5,7), activity 8 runs [8,11)[8,11) — no overlaps. Can we do 4? The timeline [0,11][0,11] with these intervals: after picking 1 (ends 4) the only activities starting ≥4\ge 4 are 4 (5–7), 6 (5–9), 7 (6–10), 8 (8–11). Picking 4 (ends 7) leaves only 8 (starts 8). Picking 6 (ends 9) leaves nothing. So 3 is indeed maximum.


Pages 1–2 of 2