Problem statement:
问题陈述:
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.
给定一系列工作,每个工作都有截止日期和利润。 只有在截止日期之前完成工作,才能赚取利润。 还假定每个工作都占用一个单位时间,因此任何工作的最小可能截止期限为1。如果一次只能安排一个工作,那么如何使总利润最大化。 打印jobID订单的顺序以最大化总利润。
Input example
输入例
JobID | Deadline | Profit |
---|---|---|
1 | 4 | 30 |
2 | 2 | 20 |
3 | 2 | 60 |
4 | 2 | 30 |
5 | 1 | 10 |
6 | 4 | 80 |
职位编号 | 最后期限 | 利润 |
---|---|---|
1个 | 4 | 30 |
2 | 2 | 20 |
3 | 2 | 60 |
4 | 2 | 30 |
5 | 1个 | 10 |
6 | 4 | 80 |
Output example: 4 3 1 6
输出示例:4 3 1 6
Problem explanation:
问题说明:
First of all, forget about the greedy algorithm. Let’s just solve with our intuition. Since the problem is to maximize profit intuition says to select jobs in decreasing order according to their profit. That means to select the maximum profit one first, then the 2nd maximum profit one and so on. While selecting jobs we only need to keep track whether it can be finished in deadline.
首先,忘掉贪婪算法 。 让我们凭直觉来解决。 由于问题在于最大化利润,因此直觉表示要根据他们的利润以降序选择工作。 这意味着首先选择一个最大利润,然后选择第二个最大利润,依此类推。 在选择工作时,我们只需要跟踪它是否可以在截止日期之前完成。
So let’s start...
所以我们开始吧...
It can be seen from the job table that, there are four jobs with the scheduled deadline at most 2. Thus we can only select a maximum of 2 jobs from these 4 jobs since each job take 1 unit time to process. (local observation)
从作业表中可以看到,有四个作业的排定的截止日期最多为2个。因此,由于每个作业需要1个单位时间来处理,因此我们只能从这4个作业中最多选择2个作业。 (当地观察)
At time 0:
在时间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
选择最大利润之一,截止日期最多为2
职位编号:3,截止日期:2,有效选择,处理职位
时间0:60的利润
At time 1:
在时间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
从剩余工作中选择最大利润之一,截止日期最多为2
职位编号:4,期限:2,有效选择,处理职位
时间1的利润:60 + 30
这就是为什么不能选择ID 5和2的工作
At time 2:
在时间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
从截止日期大于2的剩余一项中选择最大值
职位编号:6,截止日期:4,有效选择,处理职位
时间2的利润:60 + 30 + 80
At time 3:
在时间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
选择工作
职位编号:1,截止日期:4,有效选择,处理职位
作业顺序:3 4 6 1
最终总利润= 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:
现在,只需按照以下步骤进行编码,这些步骤不过是我们解决上一个示例所做的工作:
Create a class to define jobs
创建一个类来定义作业
class job { public: int jobid; //job id int deadline; //deadline int profit; //profit of the job };
To take input we have used the concept of array of pointers to the job objects. job *obj[n]; //array of pointer to jobs(jobs namely)
为了接受输入,我们使用了指向作业对象的指针数组的概念。 工作* obj [n]; //指向作业的指针数组(即作业)
maxProfit function()
maxProfit函数()
- Sort all jobs in decreasing order of profit.
bool mycompare(job *x,job *y)//boolean function { //sort as per decreasing profite return x->profit>y->profit; } sort(obj,obj+n,mycompare);
Find the maximum deadline, let it be max.
找到最大截止日期,使其为max 。
Create store[max] to store job sequence
创建store [max]以存储作业序列
Create slot[max] to mark occupied slots
创建slot [max]以标记占用的插槽
For i=0:no of jobs
对于i = 0:没有工作
// now pick the job with max deadline from // that deadline traverse array backto find an empty slot for(int j=(obj[i]->deadline)-1;j>=0;j--) { if(slot[j]==false){ // slot is empty // count the total profit profit+=obj[i]->profit; store[j]=obj[i]->jobid; slot[j]=true; break; } }
Print the store array to find job sequence and print profit which is maximum profit output
打印商店数组以查找工作顺序并打印利润 (最大利润输出)
C ++实现作业排序问题 (C++ implementation of Job sequencing problem)
#include<bits/stdc++.h>
using namespace std;
// define the class for the job
class job
{
public:
int jobid;
int deadline;
int profit;
};
// our compare function to sort
bool mycompare(job *x,job *y)//boolean function
{
//sort as per decreasing profit
return x->profit>y->profit;
}
int maxProfit(job** obj,int n){
int max=0,profit=0;;
//find maximum deadline
for(int i=0;i<n;i++){
if(i==0){
max=obj[i]->deadline;
}
else{
if(obj[i]->deadline>max)
max=obj[i]->deadline;
}
}
sort(obj,obj+n,mycompare);
// create array of max deadline size
int store[max]={0}; // empty array initially
bool slot[max]={false}; //all slots empty initially
for(int i=0;i<n;i++)
{
// now pick the job with max deadline and from
// that deadline traverse array back to find an empty slot
for(int j=(obj[i]->deadline)-1;j>=0;j--)
{
if(slot[j]==false) // slot is empty
{
// count the total profit
profit+=obj[i]->profit;
store[j]=obj[i]->jobid;
slot[j]=true;
break;
}
}
}
// printing the job sequence
cout<<"jobs sequence is:"<<"\t";
for(int i=0;i<max;i++)
{
if(slot[i])
cout<<store[i]<<" ";
}
return profit; //return profit
}
int main()
{
int n,size,max,totalprofit=0;
cout<<"enter the no. of jobs:";
cin>>n;
job *obj[n]; //array of pointer to jobs(jobs namely)
// user input and finding maximum deadline
for(int i=0;i<n;i++)
{
obj[i]=(job*)malloc(sizeof(struct job));
cout<<"enter jobid,deadline and profit of job "<<i+1<<endl;
cin>>obj[i]->jobid;
cin>>obj[i]->deadline;
cin>>obj[i]->profit;
}
totalprofit=maxProfit(obj,n); //total profit
cout<<"\ntotal profit is "<<totalprofit<<"\n";
return 0;
}
Output
输出量
enter the no. of jobs:6
enter jobid,deadline and profit of job 1
1 4 30
enter jobid,deadline and profit of job 2
2 2 20
enter jobid,deadline and profit of job 3
3 2 60
enter jobid,deadline and profit of job 4
4 2 30
enter jobid,deadline and profit of job 5
5 1 10
enter jobid,deadline and profit of job 6
6 4 80
jobs sequence is: 4 3 1 6
total profit is 200
翻译自: https://www.includehelp.com/icp/job-sequencing-problem.aspx