Algorithms · Job Sequencing and Huffman Coding
Job Sequencing with Deadlines
Overview
Job Sequencing with Deadlines is a greedy optimization problem: each job has a profit and a deadline, every job takes exactly one unit of time, and the goal is to choose and schedule jobs so that total profit is maximized while every scheduled job finishes within its deadline.
The correct greedy places the highest-profit jobs as late as possible, which preserves early slots for jobs with tighter deadlines.
Key concepts
- Job Sequencing with Deadlines: a set of jobs (job 1, job 2, job 3, job 4, ...) is given; each job has an associated profit (e.g., performing J1 gives profit 6, performing J2 gives profit 8) and an associated deadline — the profit is earned only if the job is finished within its deadline.
- Unit time per job: every job takes exactly one unit of time to complete. Time is measured from the starting point 0, so slots are 0, 1, 2, 3, ...
- One job per slot: since every job takes 1 unit of time, only one job can occupy each slot. If J1 is placed in a slot, J2 cannot be placed in the same slot.
- Goal: maximize the total profit by choosing which jobs to perform and in which slots to schedule them, such that every scheduled job finishes within its deadline.
- Naive idea (highest profit first, earliest slot): take the jobs with the highest profits and place them in the earliest available slots.
This can fail: a high-profit job placed early can block a lower-profit job with a tighter deadline, so the lower-profit job misses its deadline and earns nothing. - Correct greedy choice: always pick the job with the highest remaining profit and schedule it in the latest free slot not exceeding its deadline.
Placing each job as far to the right (as late) as possible leaves earlier slots free for jobs with tighter deadlines.
Drawing the diagram…
How it works
- Find the maximum deadline among all jobs and create an array (Gantt chart) of that size.
Slots are numbered ( = maximum deadline);
a job with deadline may use any slot . Only as many jobs as the maximum deadline can be completed. - Sort all jobs in descending order of profit.
- Take jobs one by one in this sorted order. For each job, start from the slot equal to its deadline and scan leftwards (from the deadline slot towards slot 1) looking for the first empty slot.
- If an empty slot is found, place the job there. If no empty slot exists before its deadline, discard the job (it cannot be finished in time, so it earns no profit).
- Placing each job as far to the right (as late) as possible leaves earlier slots free for jobs with tighter deadlines.
Pseudocode:
sort jobs in descending order of profit
maxDeadline = maximum deadline among all jobs
slots[1..maxDeadline] = empty
profit = 0
for each job j in sorted order:
for t = deadline[j] down to 1:
if slots[t] is empty:
slots[t] = j
profit = profit + profit[j]
break
(if no empty slot found, discard j)
Formulas
- Number of jobs that can be completed at most , where is the maximum deadline among all jobs.
- Array (Gantt chart) size (the maximum deadline).
- Total time complexity [sorting] [slot search] .
- For each of the jobs, the leftward scan can examine up to slots in the worst case, giving .
- Space complexity for the slot array, which is when deadlines are close to .
Worked examples
Example 1 (motivating example, jobs J1, J2, J4): Profits: J1 = 6, J2 = 8, J4 = 10 (highest). Deadlines: J1 = 2, J2 = 1, J4 = 2.
Bad order (highest profit in earliest slot):
- Place J4 in slot 1. Next highest is J2 (deadline 1) — slot 1 is taken; placing J2 in slot 2 crosses its deadline, so J2 earns nothing. Then place J1 in slot 2 (deadline 2, met).
- Total profit = 10 + 6 = 16.
Correct order (highest profit as far right as possible):
- J4 (profit 10, deadline 2) → placed as far right as possible = slot 2.
- J2 (profit 8, deadline 1) → placed as far right as possible = slot 1.
- Both are within their deadlines; total profit = 10 + 8 = 18, which is the maximum.
Example 2 (6 jobs, max deadline 5): Jobs J1–J6; profits: J1 = 200, J2 = 180, J3 = 190, J4 = 300, J5 = 120, J6 = 100. Deadlines: J1 = 5, J2 = 3, J3 = 3, J4 = 2, J5 = 4, J6 = 2. Gantt chart slots: 1, 2, 3, 4, 5 (time axis 0 to 5; only 5 jobs can be done).
Bad order (earliest slots):
- J4 (300, deadline 2) → slot 1.
- J1 (200, deadline 5) → slot 2.
- J3 (190, deadline 3) → slot 3.
- J2 (180, deadline 3): slots 1–3 all occupied → cannot be placed.
- J5 (120, deadline 4) → slot 4.
- J6 (100, deadline 2): slots 1–2 occupied → cannot be placed.
- Jobs done: J4, J1, J3, J5. Total profit = 300 + 200 + 190 + 120 = 810.
Correct order (descending profit, latest slot):
- J4 (300, deadline 2) → slot 2 (latest possible).
- J1 (200, deadline 5) → slot 5.
- J3 (190, deadline 3) → slot 3.
- J2 (180, deadline 3): slots 3 and 2 occupied, but slot 1 is free → slot 1.
- J5 (120, deadline 4) → slot 4.
- J6 (100, deadline 2): no free slot ≤ 2 → discarded.
- Jobs done: J4, J1, J3, J2, J5. Total profit = 300 + 200 + 190 + 180 + 120 = 990 — more jobs and more profit than the bad order.
Example 3 (5 jobs, max deadline 4): Profits: J1 = 2, J2 = 4, J3 = 3, J4 = 1, J5 = 10. Deadlines: J1 = 3, J2 = 3, J3 = 3, J4 = 2, J5 = 4. Array size = 4 (slots 1–4); only 4 jobs can be done.
Sorted by profit descending: J5 (10), J2 (4), J3 (3), J1 (2), J4 (1).
| Step | Job (profit, deadline) | Scan | Slot chosen | Slots state (1,2,3,4) |
|---|---|---|---|---|
| 1 | J5 (10, 4) | 4 → left | 4 | (_, _, _, J5) |
| 2 | J2 (4, 3) | 3 → left | 3 | (_, _, J2, J5) |
| 3 | J3 (3, 3) | 3 → left | 2 | (_, J3, J2, J5) |
| 4 | J1 (2, 3) | 3 → left | 1 | (J1, J3, J2, J5) |
| 5 | J4 (1, 2) | 2 → left | none (full) | discarded |
Total profit = 2 + 3 + 4 + 10 = 19 (maximum possible).
Example 4 (9 jobs, max deadline 7): Jobs J1–J9; only 7 jobs can be done. Profits: J1 = 15, J2 = 20, J3 = 30, J4 = 18, J5 = 18, J6 = 15, J7 = 23, J8 = 16, J9 = 25. Deadlines: J1 = 7, J2 = 2, J3 = 5, J4 = 3, J5 = 4, J6 = 5, J7 = 2, J8 = 7, J9 = 3. Array size = 7 (slots 1–7).
Sorted by profit descending: J3 (30), J9 (25), J7 (23), J2 (20), J4 (18), J5 (18), J8 (16), J1 (15), J6 (15).
| Step | Job (profit, deadline) | Scan | Slot chosen | Slots state (1..7) |
|---|---|---|---|---|
| 1 | J3 (30, 5) | 5 → left | 5 | (,,,,J3,,) |
| 2 | J9 (25, 3) | 3 → left | 3 | (,,J9,,J3,,_) |
| 3 | J7 (23, 2) | 2 → left | 2 | (,J7,J9,,J3,,) |
| 4 | J2 (20, 2) | 2 → left | 1 | (J2,J7,J9,,J3,,_) |
| 5 | J4 (18, 3) | 3 → left | none (full ≤ 3) | discarded |
| 6 | J5 (18, 4) | 4 → left | 4 | (J2,J7,J9,J5,J3,,) |
| 7 | J8 (16, 7) | 7 → left | 7 | (J2,J7,J9,J5,J3,_,J8) |
| 8 | J1 (15, 7) | 7 → left | 6 | (J2,J7,J9,J5,J3,J1,J8) |
| 9 | J6 (15, 5) | 5 → left | none (all full) | discarded |
All 7 slots are filled. Total profit = 30 (J3) + 25 (J9) + 23 (J7) + 20 (J2) + 18 (J5) + 16 (J8) + 15 (J1) = 147.
Comparison
| Strategy | Placement rule | Result on Example 2 |
|---|---|---|
| Highest profit in earliest slot | Place each job in the first free slot | 4 jobs, profit 810 (J2 and J6 missed) |
| Highest profit in latest slot (correct greedy) | Place each job in the latest free slot ≤ deadline | 5 jobs, profit 990 (only J6 missed) |
Revision summary
- Every job takes 1 unit of time; profit is earned only if the job finishes within its deadline.
- Sort jobs in descending order of profit; for each job scan from its deadline slot leftwards and place it in the first empty slot (as late as possible).
- Placing a job as far right as possible preserves early slots for jobs with tighter deadlines — this is the key to the greedy being optimal.
- The Gantt chart array size = maximum deadline; at most (maximum deadline) jobs can be completed.
- Time complexity: for sorting + for slot searching = ;
space for the slot array. - Shortcut: if a deadline is much larger than the number of jobs, there is no need to create an array of that size — limit the array size to (the number of jobs), since deadlines far beyond are meaningless.
- A job that finds no free slot at or before its deadline is discarded; performing it after its deadline earns nothing.
Previous-year exam insights
- Huffman coding is a greedy (not dynamic-programming, not divide-and-conquer) algorithm: repeatedly merge the two smallest-probability nodes; it yields an optimal prefix code, and no codeword is a prefix of another (the code is a full binary tree, so the two least frequent symbols get the longest, equal-length codewords).
Asked inUGC NET 2017GATE 2021 - Average (expected) code length = sum over symbols of (probability × codeword length);
for a message of N characters the expected encoded length in bits is N times this average.
Asked inGATE 2017GATE 2007UGC NET 2017UGC NET 2016 - Huffman codes are not unique: swapping left/right children or breaking ties between equal frequencies differently gives other valid optimal codes, so several answer options can be correct codes — verify prefix-freeness and lengths rather than one fixed assignment.
Asked inGATE 2007GATE 2021 - Decoding a Huffman bit string: walk the tree from the root, output a symbol whenever a leaf is reached and restart at the root; the code must be read with the exact tree built from the given frequencies.
Asked inISRO 2020GATE 2006 - Job sequencing: a set of jobs is feasible iff, for every deadline value d, the number of jobs chosen with deadline ≤ d is at most d; the greedy by descending profit with latest-free-slot placement yields the maximum profit, and at most D (maximum deadline) jobs can ever be scheduled.
Asked inGATE 2005 - Job sequencing can be implemented in overall by sorting jobs by decreasing profit and using a disjoint-set (union-find) 'latest free slot' structure instead of the linear scan for a free slot.