Given an array of jobs where every job has a deadline and a profit. Profit can be earned only if the job is finished before the deadline. It is also given that every job takes a single unit of time, so the minimum possible deadline for any job is 1. How to maximize total profit if only one job can be scheduled at a time. Print the sequence of jobID order to maximize total profit.
Input example
JobID |
Deadline |
Profit |
1 |
4 |
30 |
2 |
2 |
20 |
3 |
2 |
60 |
4 |
2 |
30 |
5 |
1 |
10 |
6 |
4 |
80 |
Output example: 4 3 1 6
At time 0:
Select maximum profit one with deadline at most 2
Job id: 3, deadline: 2, valid choice, process the job
Profit at time 0 : 60
At time 1:
Select maximum profit one from remaining jobswith deadline at most 2
Job id: 4, deadline: 2, valid choice, process the job
Profit at time 1 : 60+30
That’s why can’t choose job with ID 5 & 2
At time 2:
Select maximum from remaining one with deadline greater than 2
Job id: 6, deadline: 4, valid choice, process the job
Profit at time 2 : 60+30+80
At time 3:
Select job
With job id: 1, deadline : 4, valid choice, process the job
Job sequence : 3 4 6 1
Finally total profit= 60+30+80+30=200
No other choice could have resulted better (you can check it!!!!). Thus the solution is optimized and we have found the maximum solution.
Now, revise what we have done. We have actually sorted the job table according to max profit & then have made the local best choice at each iteration to reduce the problem size & ultimately to reach the goal. Simply speaking, we have used the greedy technique intuitively & greedy algorithm has successfully solved the job sequencing problem.
Now to code simply follow the below steps which are nothing but what we did solving the previous example:
To do this we have defined our own compare function & used STL sort.
Create slot[max] to mark occupied slots
C++ implementation of Job sequencing problem
Output
need an explanation for this answer? contact us directly to get an explanation for this answer