Sub topic 10
Minimizing Lateness: Earliest Deadline First
This chapter assumes you can read Python with sorting and tuples, follow an O(n log n) cost argument, and read a proof by contradiction. It also builds on the exchange argument from the earlier chapter on proof techniques. That chapter showed the idea in general terms. Here you apply it to a scheduling problem where the obvious greedy rules are wrong and one less obvious rule is right.
The Problem
You have one machine and a list of jobs. Job j needs t_j units of processing time and has a deadline d_j. The machine starts at time 0 and runs one job at a time without interruption. Every job must be scheduled, so you only choose the order.
If job j finishes at time f_j, its lateness is L_j = max(0, f_j - d_j). The goal is to minimize the maximum lateness, L = max_j L_j. You are not minimizing how many jobs are late or the total lateness. You are minimizing the worst single overrun.
This objective fits situations where the worst delay matters most, such as a batch of client deliverables or a set of build steps with a hard release date. You cannot skip work, so you want the largest slip to be as small as possible.
Candidate Greedy Rules
Several rules sound reasonable. Each candidate in the table except the last fails on a two-job input.
| Rule | Intuition | Counterexample (t, d) | Greedy result | Optimal |
|---|---|---|---|---|
| Shortest processing time first | Clear quick jobs fast | A=(1, 100), B=(10, 10) | A then B: lateness 1 | B then A: 0 |
| Smallest slack `d - t` first | Start the most urgent work | A=(1, 2), B=(10, 10) | B then A: lateness 9 | A then B: 1 |
| Earliest deadline first | Due soonest goes first | none exists | optimal | optimal |
Shortest-first ignores deadlines entirely. Smallest-slack looks smarter because it uses both numbers, but it lets a long job with a slightly tighter slack push a short, urgent job far past its deadline. Earliest deadline first (EDF), also called Jackson's rule, ignores processing times completely. That seems careless, yet it turns out to be the correct rule.
The Algorithm
Sort jobs by deadline and run them back to back with no idle time.
def schedule_edf(jobs):
"""jobs: list of (name, duration, deadline). Returns (schedule, max_lateness)."""
time = 0
schedule = []
max_late = 0
for name, duration, deadline in sorted(jobs, key=lambda j: j[2]):
start = time
time += duration
lateness = max(0, time - deadline)
max_late = max(max_late, lateness)
schedule.append((name, start, time, lateness))
return schedule, max_late
jobs = [("a", 3, 6), ("b", 2, 8), ("c", 1, 9),
("d", 4, 9), ("e", 3, 14), ("f", 2, 15)]
plan, worst = schedule_edf(jobs)
for name, start, finish, late in plan:
print(f"{name}: [{start}, {finish}) lateness {late}")
print("max lateness:", worst)Output:
a: [0, 3) lateness 0
b: [3, 5) lateness 0
c: [5, 6) lateness 0
d: [6, 10) lateness 1
e: [10, 13) lateness 0
f: [13, 15) lateness 0
max lateness: 1The cost is O(n log n) for the sort plus O(n) for one pass. Python's sort is stable, so jobs with equal deadlines keep their input order. As the proof shows, that order does not affect the answer.
Proving EDF Optimal with an Exchange Argument
The proof turns any optimal schedule into the EDF schedule one step at a time without ever making it worse.
Step 1: There is an optimal schedule with no idle time
If an optimal schedule has a gap, slide every later job left to close it. Finish times only decrease, so no job's lateness increases. From here on, assume no idle time. The EDF schedule has none either.
Step 2: Define inversions
An inversion is a pair of jobs i and j where i runs before j but d_i > d_j. The EDF schedule has no inversions. Any schedule with an inversion also has an adjacent inversion, meaning two consecutive jobs out of deadline order. To see why, walk from i to j through the schedule. Deadlines must drop somewhere along the way, and the first place they drop is an adjacent inverted pair.
Step 3: Swapping an adjacent inversion does not increase max lateness
Let i run immediately before j with d_i > d_j, and let f be the time j finishes. Swap the two jobs.
- Jobs other than i and j keep the same finish times.
- Job j now finishes earlier, so its lateness cannot grow.
- Job i now finishes at
f, so its new lateness ismax(0, f - d_i). Sinced_i > d_j, this is at mostmax(0, f - d_j), which was j's lateness before the swap.
Every new lateness is bounded by some old lateness, so the maximum does not increase. Each swap removes exactly one inversion, so after at most n(n−1)/2 swaps the schedule has no inversions and is still optimal.
Step 4: All inversion-free, idle-free schedules have the same max lateness
Two schedules with no inversions can differ only in how they order jobs that share a deadline. Jobs with the same deadline d sit in one contiguous block, and the block ends at the same time F whatever their internal order. The largest lateness inside that block is max(0, F - d), which comes from whichever job runs last, so the order inside the block does not matter. Therefore the EDF schedule has the same max lateness as the optimal schedule from Step 3.
Checking It Empirically
A brute-force checker over all permutations catches mistakes in both the code and your reasoning. It is a useful habit for any greedy algorithm you write.
import random
from itertools import permutations
def max_lateness(order):
time, worst = 0, 0
for _, duration, deadline in order:
time += duration
worst = max(worst, time - deadline)
return worst
def edf_value(jobs):
return max_lateness(sorted(jobs, key=lambda j: j[2]))
random.seed(1)
for trial in range(2000):
n = random.randint(1, 6)
jobs = [(k, random.randint(0, 5), random.randint(0, 15)) for k in range(n)]
best = min(max_lateness(p) for p in permutations(jobs))
assert edf_value(jobs) == best, jobs
print("EDF matched brute force on 2000 random instances")The generator deliberately includes zero-length jobs, deadlines of 0, and repeated deadlines. Those are the inputs where a hand-written proof usually skips a case.
Edge Cases and Where EDF Stops Working
Small changes to the problem can break the result, so check which version you actually have.
- Ties. Any order among equal-deadline jobs is optimal (Step 4). You do not need a tie-breaker for correctness. You might still want one so the output is the same on every run.
- Deadlines already passed. A deadline of 0 or a negative deadline is fine. That job is late no matter what, and EDF puts it first.
- Total lateness instead of maximum. EDF does not minimize the sum. Take A=(10, 1), B=(1, 2), C=(1, 2). EDF runs A, B, C for lateness 9, 9, 10: maximum 10, total 28. The order B, C, A gives 0, 0, 11: maximum 11, total 11. Minimizing total tardiness on one machine is NP-hard.
- Release times. If job j cannot start before time
r_j, minimizing max lateness without preemption becomes strongly NP-hard. With preemption allowed, the rule "always run the available job with the earliest deadline" is optimal again. - Multiple machines. Assigning jobs to machines adds a partitioning problem, and plain EDF is no longer guaranteed optimal.
- Weighted lateness. If lateness on some jobs costs more than on others, EDF ignores the weights and can lose.
Exercises
Exercise
Jobs are (t, d): A=(4, 5), B=(2, 3), C=(3, 9), D=(1, 6). Give the EDF order, each finish time, and the maximum lateness.
Show answer
Sorted by deadline the order is B(3), A(5), D(6), C(9). The finish times are B=2, A=6, D=7, C=10. The latenesses are 0, 1, 1, 1, so the maximum lateness is 1.
Exercise
In Step 3 of the proof, why is it important that the swapped jobs are adjacent?
Show answer
When i and j are adjacent, swapping them changes only their own finish times. Every other job keeps its slot, and i lands exactly at j's old finish time f, which gives the bound f - di < f - dj. If other jobs sat between them, those jobs would move when i and j trade places, and their lateness could increase.
Exercise
Someone proposes "largest processing time first among jobs with the earliest deadline, then continue." Is this different from EDF in terms of the max lateness it achieves?
Show answer
No. It only reorders jobs that share a deadline. By Step 4, any order within a block of equal deadlines gives the same max lateness, so this rule is still optimal.