Job and Activity Selection: Greedy Rules, Worked Examples and Exam Traps

Learn why activity selection sorts by finish time while unit-time job sequencing sorts by profit. Trace both algorithms, test their invariants, and avoid the standard traps.

KnowledgeGate Team

Exam prep & CS education

Updated 23 Nov 20266 min read
Job and Activity Selection: Greedy Rules, Worked Examples and Exam Traps

Activity selection and job sequencing are both greedy selection problems, yet they optimise different objectives and need different sorting keys. Mixing their rules gives plausible but wrong answers. Exchange arguments justify both choices, and concrete counterexamples break tempting alternatives.

Job and activity selection are two different greedy problems

In activity selection, activity i has start time s_i and finish time f_i. Choose the largest pairwise compatible set for one resource. Here, a new activity is compatible when start >= last_finish, so it may start exactly when the previous one finishes.

In job sequencing with deadlines and profits, every job takes one time slot. Job i has deadline d_i and profit p_i, earned only if it finishes by its deadline. Maximise total profit, not job count.

Feature

Activity selection

Job sequencing

Input

Start and finish times

Deadline and profit

Objective

Maximum compatible activity count

Maximum total profit

Greedy key

Non-decreasing finish time

Descending profit

Placement rule

Accept if start >= last_finish

Use the latest free slot at or before the deadline

Critical assumption

One resource, unweighted activities

One machine, unit-time jobs

Weighted interval scheduling, variable-duration jobs, and multiple-machine scheduling are different problems. Keep those boundaries visible while studying Algorithms through the GATE CS exam preparation courses and test series.

Activity selection uses the earliest-finish greedy choice

Sort by non-decreasing finish time and accept the first activity. Scan left to right, accepting activity i exactly when s_i >= last_finish, then set last_finish = f_i. A deterministic tie-break such as input order makes tied output repeatable without changing optimal cardinality.

Why earliest finish? In an optimal solution, replace the first choice with the globally earliest finisher. It finishes no later, so no later chosen activity becomes incompatible and the count does not fall. Repeat this exchange on the remaining suffix.

Sorting costs O(n log n) and the scan costs O(n), giving O(n log n) time overall. If the input is already sorted by finish time, the scan alone is O(n). The earlier Greedy Algorithms: Strategy and Problems guide introduces exchange arguments and summarises this classic interval set. Here, the same eleven intervals are expanded into every accept/reject state so you can compare activity selection directly with unit-time job sequencing.

Activity selection worked example: trace all eleven intervals

The activities are already sorted by finish time.

Activity

Interval

Decision

Reason or new state

A1

(1,4)

Select

First choice, last_finish = 4

A2

(3,5)

Reject

3 < 4

A3

(0,6)

Reject

0 < 4

A4

(5,7)

Select

5 >= 4, so last_finish = 7

A5

(3,9)

Reject

3 < 7

A6

(5,9)

Reject

5 < 7

A7

(6,10)

Reject

6 < 7

A8

(8,11)

Select

8 >= 7, so last_finish = 11

A9

(8,12)

Reject

8 < 11

A10

(2,14)

Reject

2 < 11

A11

(12,16)

Select

12 >= 11, so last_finish = 16

Therefore the selected sequence is [A1, A4, A8, A11], and the selected count is 4. This is a maximum-size compatible set. No profit was given or maximised.

Activity selection timeline with chosen intervals A1, A4, A8, and A11 highlighted.

Job sequencing uses profit order and the latest free slot

Sort unit-time jobs by descending profit and create slots 1..D, where D is the maximum deadline. Place each job in the latest free slot at or before its deadline, rejecting it only when no such slot exists. If jobs are optional, skip non-positive-profit jobs.

Profit order protects valuable rewards. Latest-slot placement preserves early slots for tighter deadlines. Moving a chosen job from an earlier slot to a later valid slot keeps it feasible without reducing other jobs' options.

A direct backward slot scan takes O(n log n + nD) time and O(D) space. A disjoint-set predecessor structure gives near-constant amortised lookup and O(n log n + n alpha(D)) overall. The simple scan suffices by hand.

Job sequencing worked example: three-slot schedule and greedy placements

Use J1(deadline=2, profit=100), J2(deadline=1, profit=19), J3(deadline=2, profit=27), J4(deadline=1, profit=25), and J5(deadline=3, profit=15). The maximum deadline is 3, so begin with free slots [1,2,3].

Profit order is J1(100), J3(27), J4(25), J2(19), J5(15).

  1. Put J1 in its latest available slot, slot 2.

  2. For J3, slot 2 is occupied, so put it in slot 1.

  3. Reject J4 because its only valid slot, slot 1, is occupied.

  4. Reject J2 for the same reason.

  5. Put J5 in slot 3.

The chronological schedule is [J3, J1, J5]. Its total profit is 27 + 100 + 15 = 142. Notice that profit-sorted consideration order and chronological execution order are different lists.

Job sequencing board showing J3, J1, and J5 scheduled in three slots; J4 and J2 rejected.

Job and activity selection traps need concrete counterexamples

Earliest start is wrong for activity selection. Consider L=(0,10), S1=(1,2), S2=(2,3), S3=(3,4), and S4=(4,5). Earliest start chooses L and then cannot choose anything else, giving count 1. Earliest finish chooses [S1,S2,S3,S4], a compatible set of 4 activities.

A profitable job should use its latest available slot. Let JA(deadline=2, profit=100), JB(deadline=1, profit=99), and JC(deadline=2, profit=1). If JA takes slot 1, JB is forced out and the best remaining addition is JC, giving 100 + 1 = 101. Put JA in slot 2 instead, and JB fits in slot 1, giving 100 + 99 = 199.

Use this fast trap checklist:

  • Do not sort activities by start time or profit.

  • Do not treat a deadline as an interval finish time.

  • Do not apply unit-time job sequencing to variable-duration jobs.

  • Do not add the profit of rejected jobs.

  • Do not confuse profit-sorted consideration order with chronological slot order.

Job and activity selection exam patterns test the trace and invariant

Conceptual problems may ask you to trace a table, identify the sorting key, compute count or profit, analyse complexity, fill a compatibility or slot condition, disprove a heuristic, or spot a broken assumption. KnowledgeGate has over 10 live Job and Activity Selection questions in its practice bank for practising the reasoning beyond one table.

Try two quick variations:

  1. If A11 becomes (10,16), then after selecting A8 the value of last_finish is 11. Reject the changed A11 because 10 < 11. The result is [A1,A4,A8], with count 3.

  2. If J5's profit becomes 30, profit order becomes J1,J5,J3,J4,J2. Place J1 in slot 2, J5 in slot 3, and J3 in slot 1. The schedule remains [J3,J1,J5], while profit becomes 27 + 100 + 30 = 157.

When weights and arbitrary interval lengths appear together, earliest finish alone is no longer enough. That boundary is easier to see after studying Dynamic Programming Explained: 0/1 Knapsack. For optional timed practice across such distinctions, use the GATE Test Series.

Job and activity selection: the short decision checklist

  • Intervals plus maximum count: sort by earliest finish.

  • Unit-time jobs plus deadlines and profits: sort by profit, then place each job in the latest free slot.

  • Weights with arbitrary interval lengths, or variable job durations: stop and reformulate instead of forcing either greedy rule.

The two checksums are activity set [A1,A4,A8,A11] with count 4, and job schedule [J3,J1,J5] with profit 142. A learner building wider Algorithm foundations can use GATE Guidance by Sanchit Sir for a broader subject plan. If you only needed this distinction, rework both tables once from blank paper.