How activity selection takes the earliest finish

Short answer: Activity selection chooses a largest set of mutually compatible intervals. Sort the activities by finishing time and take the next activity that starts at or after the finish of the last chosen one. The greedy choice is optimal: some maximum set uses an activity that finishes first, and after it is chosen the same problem remains on the activities that start later. The sort dominates, at O(n log n). A scan of an already sorted list is linear.


What the problem actually asks

Two activities conflict when their open intervals overlap. The goal is the maximum number of activities, not the maximum total length and not the maximum weight. Weighted interval scheduling is a different problem and needs dynamic programming. Unweighted selection has the greedy structure because taking the activity that frees the resource soonest leaves the largest possible remainder.

A sort by start time, or by duration, does not have that exchange argument. A short activity that starts first can block several later activities that would have fit beside a slightly later short one. Earliest finish is the sort key that works.


What the algorithm guarantees

The set produced by earliest-finish selection has the maximum cardinality. Time is O(n log n) for the sort plus a linear scan. Extra memory is the sorted array. Ties in finish time can be broken arbitrarily. An activity that touches another only at an endpoint is compatible if the model treats the intervals as half-open. The writeup has to match the problem’s closed or open convention. A fully closed interval that ends when the next starts is a conflict under a closed model and compatible under a half-open model.

The algorithm does not maximize a sum of weights. A heavy activity that finishes late can be the right weighted choice and the wrong cardinality choice.


The working steps

  1. Sort the activities by nondecreasing finish time. Break ties arbitrarily.
  2. Pick the first activity. Set the current finish to its finish time.
  3. Scan the rest. When an activity starts at or after the current finish, under the interval convention in use, pick it and update the current finish.
  4. The picked set is a maximum compatible subset.
  5. If the objective is a weighted sum, stop and use the weighted interval DP instead of this scan.

Cardinality is the objective. Weighted interval scheduling sorts by finish time too and then runs a binary search plus a DP, and it can reject the earliest-finishing activity. A room-assignment problem that minimizes the number of resources is interval graph coloring, a different greedy algorithm that sorts by start time.


Approaches that look simpler and fail

Approach Why it fails when activities are sorted by duration
Pick the shortest remaining activity A short activity in the middle of several others can block two activities the earliest-finish rule would have kept. Shortest-first has no optimality proof for cardinality.
Sort by start time and take the same scan The first activity can be the one that runs the longest and blocks everything. Earliest start is not the greedy choice.
Apply the scan to weighted values and claim the maximum weight The exchange argument used equal weights. A weighted optimum can skip the earliest finish.
Treat a shared endpoint as compatible without stating the interval model Closed intervals that touch conflict in some statements and not in others. The comparison has to match the statement.

Weighted non-overlapping intervals are a dynamic program. Coloring an interval graph to assign rooms sorts by start time. Activity selection is the earliest-finish cardinality greedy algorithm.


A working example

The Bash script and the Python script sort activities by finish time and take the next one that starts at or after the previous finish. Half-open intervals. Both print the three chosen starts 1 5 8.

# Parallel arrays, already sorted by finish time.
start=(1 3 0 5 5 8)
end=(4 5 6 7 9 9)
n=${#start[@]}
finish=-1
out=()
for ((i=0; i<n; i++)); do
  if (( start[i] >= finish )); then
    out+=("${start[i]}")
    finish=${end[i]}
  fi
done
echo "${out[*]}"
acts = [(1, 4), (3, 5), (0, 6), (5, 7), (5, 9), (8, 9)]
acts.sort(key=lambda iv: iv[1])
chosen = []
finish = -10**9
for start, end in acts:
    if start >= finish:
        chosen.append((start, end))
        finish = end
assert chosen == [(1, 4), (5, 7), (8, 9)]
print(*(s for s, _ in chosen))

Where it is used

Earliest-finish selection is the greedy scheduler for non-overlapping jobs: classrooms, broadcast slots, and a single machine that cannot preempt. Weighted intervals need the dynamic program instead, because the earliest finish is no longer the best value.


When you can drop this algorithm

Drop earliest-finish when activities have values. Use the weighted DP. Keep the sort by finish time when every selected activity counts as one and the goal is as many as possible.


Why this stays published

We publish this so a search for activity selection returns the earliest-finish sort and the reason a shortest-first sort is not optimal. Activity selection maximizes the number of compatible intervals by always taking the one that finishes earliest among those that do not conflict with the ones already taken.

本文由 HoHo 與 AI 協作整理,最後更新於 2026 年 11 月 13 日。

Leave a Reply

Your email address will not be published. Required fields are marked *