Sub topic 6
Job sequencing with deadlines and profits
This chapter assumes you know the greedy-choice property and the exchange argument from the earlier chapters. It also assumes you can read Python and know basic array and union-find operations.
The problem
You have n jobs. Each job takes exactly one unit of time and has two numbers:
- A deadline
d. The job earns its profit only if it finishes at or before timed. - A profit
p. You earn it only if the job meets its deadline.
One machine runs one job at a time, so the time line is split into unit slots 1, 2, 3, and so on. Slot t means the interval from t-1 to t. You may skip jobs. The goal is a set of jobs, each assigned its own slot no later than its deadline, with maximum total profit.
Feasible sets
A set of jobs is feasible if every job in it can meet its deadline. You do not need to test every ordering. Sort the set by deadline and check that the job in position k has deadline at least k. Equivalently, for every time t, the number of jobs with deadline at most t must not exceed t.
This matters because feasibility is closed under removing jobs. If a set is feasible, every subset is feasible. That structure is why a greedy method works here.
The greedy algorithm
- Sort the jobs by profit, highest first.
- For each job, find the latest free slot at or before its deadline.
- If such a slot exists, assign the job to it. Otherwise discard the job.
Taking the latest free slot keeps earlier slots open for jobs with tighter deadlines.
Worked example
| Job | Deadline | Profit |
|---|---|---|
| a | 2 | 100 |
| b | 1 | 19 |
| c | 2 | 27 |
| d | 1 | 25 |
| e | 3 | 15 |
The sorted order is a (100), c (27), d (25), b (19), e (15). The largest deadline is 3, so there are three slots.
- a has deadline 2. Slot 2 is free, so a takes slot 2.
- c has deadline 2. Slot 2 is taken and slot 1 is free, so c takes slot 1.
- d has deadline 1. Slot 1 is taken, so d is discarded.
- b has deadline 1. Slot 1 is taken, so b is discarded.
- e has deadline 3. Slot 3 is free, so e takes slot 3.
The schedule is c, a, e with profit 27 + 100 + 15 = 142.
Implementation
The direct version scans backward from the deadline to find a free slot.
def job_sequencing(jobs):
"""jobs: list of (name, deadline, profit). Returns (schedule, total)."""
if not jobs:
return [], 0
max_d = max(d for _, d, _ in jobs)
slots = [None] * (max_d + 1) # index 0 unused
total = 0
for name, d, p in sorted(jobs, key=lambda j: -j[2]):
for t in range(min(d, max_d), 0, -1):
if slots[t] is None:
slots[t] = name
total += p
break
return slots[1:], total
jobs = [('a', 2, 100), ('b', 1, 19), ('c', 2, 27), ('d', 1, 25), ('e', 3, 15)]
print(job_sequencing(jobs)) # (['c', 'a', 'e'], 142)The backward scan can cost O(n) per job, so the total is O(n²) in the worst case. The sort costs O(n log n).
Speeding up with union-find
Use a disjoint-set structure over slot numbers. Each slot points to the latest free slot at or before it. When slot t is filled, link it to t - 1. Slot 0 is a sentinel meaning "no free slot".
def job_sequencing_dsu(jobs):
if not jobs:
return [], 0
max_d = max(d for _, d, _ in jobs)
parent = list(range(max_d + 1))
slots = [None] * (max_d + 1)
def find(x):
while parent[x] != x:
parent[x] = parent[parent[x]]
x = parent[x]
return x
total = 0
for name, d, p in sorted(jobs, key=lambda j: -j[2]):
t = find(min(d, max_d))
if t > 0:
slots[t] = name
total += p
parent[t] = t - 1
return slots[1:], total
jobs = [('a', 2, 100), ('b', 1, 19), ('c', 2, 27), ('d', 1, 25), ('e', 3, 15)]
print(job_sequencing_dsu(jobs)) # (['c', 'a', 'e'], 142)| Version | Time | Extra space |
|---|---|---|
| Backward scan | O(n²) worst case | O(max deadline) |
| Union-find | O(n log n), dominated by the sort | O(max deadline) |
Why the greedy choice is safe
Let G be the greedy set and O an optimal feasible set. Suppose the highest-profit job j that is feasible on its own is not in O.
- If O plus
jis feasible, O was not optimal, which is a contradiction. - Otherwise, add
jto O and remove one jobkfrom the conflict. Becausejwas considered first,p(j) ≥ p(k). The new set is feasible and its profit is at least that of O.
So some optimal solution contains j. Repeat the argument on the remaining jobs. This is the exchange argument. The same reasoning shows the family of feasible sets forms a matroid, so greedy by weight is optimal.
Common errors
- Taking the earliest free slot instead of the latest. This blocks jobs with later deadlines and can lose profit.
- Sorting by deadline instead of profit. Deadline order is used to check feasibility, not to choose jobs.
- Forgetting to cap the deadline at the number of available slots when deadlines are larger than
n. - Allowing jobs with different lengths. With non-unit processing times the problem becomes NP-hard and this greedy method fails.
Exercise
Jobs J1 (deadline 4, profit 20), J2 (deadline 1, profit 10), J3 (deadline 1, profit 40), J4 (deadline 1, profit 30). Each takes one unit. What is the maximum profit and which jobs are scheduled?
Show answer
Sort by profit: J3 (40), J4 (30), J1 (20), J2 (10). J3 takes slot 1. J4 has deadline 1 and slot 1 is full, so it is discarded. J1 takes slot 4. J2 is discarded for the same reason as J4. The scheduled jobs are J3 and J1, with maximum profit 60.
Exercise
Why does the union-find version reduce the running time after the sort from O(n²) to nearly linear?
Show answer
Finding the latest free slot at or before a deadline is a find operation. Filling a slot is a union with the slot just below it. With path compression, each operation takes nearly constant amortized time, so the scan over all jobs costs close to O(n) and the sort's O(n log n) dominates.