DSA Notes
Master the Job Sequencing Problem — scheduling jobs with deadlines and profits to maximize total profit, greedy strategy of sorting by profit, DSU-based efficient scheduling, complete implementations, proof of correctness, and variants including minimize lateness.
Problem Statement
Given n jobs, each with:
- A deadline d[i] (must finish by this time)
- A profit p[i] (earned if completed on time)
Each job takes exactly 1 unit of time. Find the sequence that maximizes total profit.
| Jobs | (id, deadline, profit) |
| J1 | (1, d=2, p=100) |
| J2 | (2, d=1, p=19) |
| J3 | (3, d=2, p=27) |
| J4 | (4, d=1, p=25) |
| J5 | (5, d=3, p=15) |
| Greedy by profit (descending) | J1, J3, J4, J2, J5 |
| Place J1 (p=100, d=2) | schedule at slot 2. Available: [slot 1, slot 3] |
| Place J3 (p=27, d=2) | slot 2 taken, try slot 1. Available: [slot 3] |
| Place J4 (p=25, d=1) | slot 1 taken, no slot ≤ 1 available. Skip. |
| Place J2 (p=19, d=1) | slot 1 taken, skip. |
| Place J5 (p=15, d=3) | schedule at slot 3. |
| Result | J3 (slot 1), J1 (slot 2), J5 (slot 3), profit = 27+100+15 = 142 |
Efficient Implementation with DSU
The naive O(n²) approach can be improved to O(n log n) using Union-Find to quickly find the latest available slot:
Minimize Total Lateness (Related Problem)
A related greedy problem: Schedule all jobs to minimize total lateness (where lateness = max(0, finish_time - deadline)).
Why EDF works for minimizing lateness? Exchange argument: If job i with earlier deadline d_i is scheduled AFTER job j with later deadline d_j, swapping them reduces total lateness.
Variants
Minimize Number of Late Jobs
Complexity Summary
| Variant | Greedy Criterion | Time | Algorithm |
|---|---|---|---|
| Max profit with deadlines | Sort by profit desc | O(n²) | Linear search |
| Max profit (efficient) | Sort by profit desc | O(n log n) | DSU |
| Minimize total lateness | Sort by deadline (EDF) | O(n log n) | Simple |
| Minimize late jobs | EDF + remove longest | O(n log n) | Priority queue |
Summary
Job Sequencing:
- Greedy choice: Process jobs in decreasing profit order, assign latest available slot ≤ deadline
- Why correct: Taking the most profitable jobs first, using latest available slots preserves flexibility for lower-profit jobs
- DSU optimization:
find(d)gives latest available slot ≤ d in O(α(n)) - Minimize lateness variant: Sort by earliest deadline (EDF) — classic greedy for scheduling
*Next Lesson: Greedy Interview Problems*
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Job Sequencing Problem — Greedy Scheduling with Deadlines.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Data Structures & Algorithms topic.
Search Terms
data-structures-algorithms, data structures & algorithms, data, structures, algorithms, greedy, job, sequencing
Related Data Structures & Algorithms Topics