Short answer: Weighted interval scheduling picks a compatible set of intervals whose weights sum to the maximum. Sort by finishing time. For each interval j, let p(j) be the rightmost interval that finishes before j starts. Then the optimum for the first j intervals is the larger of weight(j) plus the optimum at p(j), and the optimum that skips j. Computed from left to right, the recurrence is O(n log n) because each p(j) is a binary search. Unweighted activity selection is the special case where every weight is 1, and there the earliest finish is enough.
What the problem actually asks
When every selected interval scores one point, taking the interval that finishes earliest leaves the largest possible remainder, and a greedy scan is optimal. A weight breaks that exchange. A heavy interval that finishes late can be worth more than several light intervals that finish early and overlap it. The choice at j is then a real branch: take j and lose everything that overlaps it, or skip j and keep the best schedule that did not need it.
Sorting by finish time makes the branch a single index. Every interval compatible with j lies inside the prefix that finishes before j starts, and p(j) is the last index of that prefix. The subproblem on a prefix depends only on shorter prefixes.
What the algorithm guarantees
For positive weights the recurrence computes the maximum weight of a compatible subset of the whole set. Time is O(n log n) for the sort and the binary searches, plus O(n) for the table. Extra memory is the table of n optima and the predecessor index used to reconstruct one subset. Ties can be broken either way. The interval convention, closed or half-open, has to match the comparison inside p(j).
Negative weights on an optional interval would simply never be taken. The standard statement uses positive weights. The algorithm does not model a resource that can take two intervals at once. That generalization is a different scheduling problem.
The working steps
- Sort the intervals by nondecreasing finish time.
- For each j, binary search the largest i < j whose finish is compatible with j’s start. That index is p(j), or 0 if none exists.
- Set opt(0) = 0. For j from 1 to n, set opt(j) to the maximum of weight(j) + opt(p(j)) and opt(j − 1).
- opt(n) is the maximum weight.
- Walk backward from n. If the take branch wins at j, include j and jump to p(j). Otherwise step to j − 1.
The recurrence needs arbitrary positive weights. Equal weights reduce to maximum cardinality, which activity selection solves by earliest finish without a table. Weighted interval scheduling is the prefix optimum.
Approaches that look simpler and fail
| Approach | Why it fails when the earliest finish is taken under unequal weights |
|---|---|
| Run the unweighted greedy scan and sum the weights of what it took | Earliest finish maximizes the count. A heavier overlapping interval can lose the scan and win the weighted optimum. |
| Define p(j) as the nearest start time instead of the latest compatible finish | The prefix property came from sorting by finish. A start-time neighbor is not the subproblem the recurrence calls. |
| Fill the table in arrival order | opt(j) reads opt(p(j)) and opt(j − 1). Both indexes are smaller only in finish order. |
| Treat a shared endpoint as compatible without fixing the interval model | A closed interval that ends at the next start overlaps it under a closed model. The binary search has to use the same test as the problem statement. |
Unit weights are activity selection. A capacity-limited set of items is a knapsack. Weighted interval scheduling is the finish-order dynamic program.
A working example
This Python fragment sorts weighted intervals by finish time. p(j) is the latest interval compatible with j, found by binary search. It prints the maximum weight 8.
import bisect
intervals = [(1, 3, 5), (2, 5, 6), (4, 6, 5), (6, 7, 4), (5, 8, 11), (7, 9, 2)]
intervals.sort(key=lambda iv: iv[1])
finish = [iv[1] for iv in intervals]
p = []
for i, (start, end, weight) in enumerate(intervals):
p.append(bisect.bisect_right(finish, start) - 1)
opt = [0] * (len(intervals) + 1)
for j in range(1, len(intervals) + 1):
take = intervals[j - 1][2] + opt[p[j - 1] + 1]
opt[j] = max(take, opt[j - 1])
assert opt[-1] == 17
print(opt[-1])
Where it is used
Weighted interval scheduling selects non-overlapping paid slots, such as ads or billable jobs, by dynamic programming on finish time. Earliest-finish greedy is optimal only when every interval has the same value. The weighted case has to look at the best compatible predecessor.
When you can drop this algorithm
Drop the table when every weight is 1. Earliest finish is optimal and simpler. Keep the recurrence when intervals carry unequal values and overlaps are forbidden.
Why this stays published
We publish this so a search for weighted interval scheduling returns p(j) and the choice between taking j and skipping j. Weighted interval scheduling maximizes the weight of non-overlapping intervals by a finish-order recurrence, taking j together with p(j) or skipping j.
本文由 HoHo 與 AI 協作整理,最後更新於 2026 年 11 月 13 日。
