Job Sequencing algorithm – Java
Objective: You are given n jobs along with the deadline and profit for each job. Your task is to write an algorithm to choose the jobs wisely which can maximize the profit. Also compute the maximum profit. Below are the details
- Each job duration is 1 unit.
- Name – Name of the job.
- Deadline – Job should be completed before the deadline is over. For instance if the deadline is 4 then the job has to be completed before 4 time units are over.
- Profit – Amount earned if the job has run.
Jobs Deadline Profit A 2 40 B 1 20 C 4 10 D 1 10 Output: Jobs: B A C, Total Profit: 70 Jobs Deadline Profit A 2 140 B 1 90 C 2 100 D 3 50 E 3 15 Output: Job Sequence: C A D, Total Profit: 290
Approach: Greedy Algorithm
- Sort the jobs in descending order of profit.
- Initialize the result, this will store jobs.
- Iterate through all the jobs (from highest profit to lowest), for job j
- Pick the job j if this job can be fit into the result without missing the deadline or in other words if current job j does not have any conflict with the jobs already selected in the results.(First job will not have conflict) then job j to the result. If job j has conflict then ignore this job.
Jobs: C A D , Total Profit: 290