Sub topic 10
Minimizing Lateness: Earliest Deadline First
This chapter assumes you can read Python with sorting and tuples, and that you have seen the exchange argument from the earlier chapter on proof techniques.
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 runs one job at a time, starting at time 0. If job j finishes at time f_j, its lateness is max(0, f_j - d_j). The goal is to order the jobs so that the maximum lateness over all jobs is as small as possible.
Every job runs exactly once, so the only decision is the order.
Rules That Look Right but Fail
Two natural rules turn out to be wrong:
| Rule | Jobs (t, d) | Rule's result | Better order |
|---|---|---|---|
| Shortest job first | (1, 100), (10, 10) | Short job first, long job ends at 11, lateness 1 | Long job first, lateness 0 |
| Smallest slack (d − t) first | (1, 2), (10, 10) | Slack-0 job first, short job ends at 11, lateness 9 | Short job first, lateness 1 |
Shortest-job-first ignores deadlines. Smallest-slack-first ignores how long the other jobs make you wait.
The Greedy Rule: Earliest Deadline First
Sort the jobs by deadline and run them back to back with no gaps. The processing times are not used to choose the order at all.
def schedule_edf(jobs):
time = 0
max_late = 0
schedule = []
for name, length, deadline in sorted(jobs, key=lambda j: j[2]):
start = time
time += length
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)]
schedule, worst = schedule_edf(jobs)
for row in schedule:
print(row)
print("max lateness:", worst)
# ('a', 0, 3, 0)
# ('b', 3, 5, 0)
# ('c', 5, 6, 0)
# ('d', 6, 10, 1)
# ('e', 10, 13, 0)
# ('f', 13, 15, 0)
# max lateness: 1The sort dominates, so the running time is O(n log n).
Why It Is Optimal: An Exchange Argument
An inversion is a pair of jobs where i runs before j but di > dj. An EDF schedule has no inversions and no idle time. The proof transforms any optimal schedule into the EDF schedule without making it worse.
- Idle time never helps. Shifting jobs earlier can only reduce finish times.
- An inversion implies an adjacent inversion. If the deadlines are not in sorted order, some neighboring pair i, j has di > dj.
- Swapping that pair does no harm. After the swap, j finishes earlier, so its lateness cannot grow. Job i now finishes at the time j used to finish, f. Its new lateness is
f - d_i, which is less thanf - d_j, the old lateness of j. Other jobs keep their finish times. So the maximum does not increase, and the number of inversions drops by one. - Ties do not matter. Schedules without inversions differ only in how they order jobs with equal deadlines. Those jobs run consecutively, and the last of them always finishes at the same time, so the maximum lateness is the same.
By induction on the number of inversions, the EDF schedule is optimal.
Common Errors
- Measuring lateness from the start time instead of the finish time.
- Sorting by slack or by length because it "feels" more informed.
- Leaving gaps between jobs. Without idle time, each finish time is just a running sum of processing times.
Exercise
Jobs as (name, t, d): x(2, 2), y(1, 3), z(3, 4). Give the EDF order, each finish time, and the maximum lateness.
Show answer
The order is x, y, z. The finish times are 2, 3 and 6. The latenesses are 0, 0 and 2, so the maximum lateness is 2.
Exercise
In the swap step, why is it enough to consider only adjacent inverted pairs?
Show answer
Any schedule with an inversion has at least one adjacent inversion. Swapping neighbors changes only those two jobs' finish times, which keeps the analysis local, and each swap removes exactly one inversion. Repeating the swap therefore reaches an inversion-free schedule after a finite number of steps.