Proof of optimality of a greedy solution to job sequencing
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
In the field of operations research and computer science, the Job Sequencing Problem is a classic optimization problem. The objective is typically to determine the sequence of executing jobs to maximize profit or minimize time. A common approach for solving the job sequencing problem involves greedy algorithms. This article focuses on proving the optimality of a greedy algorithm in the context of job sequencing with deadlines and profit maximization.
Problem Statement
The job sequencing problem consists of a set of jobs, each with a defined profit and a specified deadline. Each job requires a single unit of time to complete. The goal is to maximize the total profit by sequencing jobs within their respective deadlines.
Mathematical Formulation
• Let be a set of jobs. • Each job has: • a deadline • a profit
The objective is to find a sequence of jobs that maximizes the total profit, , such that each job is finished by its deadline .
Greedy Algorithm Approach
The greedy algorithm for the job sequence problem can be outlined in the following steps:
- Sort Jobs: Sort all jobs in descending order according to their profits, i.e., if , then job comes before .
- Loop over the Sorted Jobs: Iterate over all jobs in the sorted list and attempt to place the job in the latest free slot before its deadline.
- Assign Jobs to Slots: If a suitable slot is found, the job is added to the schedule.
Pseudocode
• Initially ordering the jobs by descending profit ensures that we choose the most profitable jobs first, maximizing the primary criteria. • When placing a job, it’s inserted into the latest possible available slot before its deadline. This leaves as many earlier slots open as possible for other jobs, maximizing flexibility. • Suppose there exists another solution, `S'`, that provides a higher profit than the solution `S` offered by the greedy algorithm. • Replace any job in `S` with a job from `S'` not in `S` that has higher profit until no such job exists. • This contradiction implies `S` must be optimal. • A is assigned to the second slot (latest possible). • C is assigned to the first slot (since slot 2 is taken by A). • B cannot be added within its deadline of 1, as it's filled by C.
Related reading
- Proof that Fowler's money allocation algorithm is correct
- Proposing an algorithm for arbitrary shape Bit Matrix Transposition with BDD-like structure
- Prove NP-Completeness clique independent set graph
- prove the algorithm that uses min-heap to merge k sorted lists
- Proper use of the IDisposable interface
- Proving that a two-pointer approach works pair sum
- Proving correctness of multithread algorithms
- Pseudocode to compare two trees

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.