# Job Sequencing Problem with Deadline

### Greedy Algorithm

Share

In this tutorial we will learn about Job Sequencing Problem with Deadline. This problem consists of n jobs each associated with a deadline and profit and our objective is to earn maximum profit. We will earn profit only when job is completed on or before deadline. We assume that each job will take unit time to complete.

## Points to remember

• In this problem we have n jobs j1, j2, … jn each has an associated deadline d1, d2, … dn and profit p1, p2, ... pn.
• Profit will only be awarded or earned if the job is completed on or before the deadline.
• We assume that each job takes unit time to complete.
• The objective is to earn maximum profit when only one job can be scheduled or processed at any given time.

## Problem

Consider the following 5 jobs and their associated deadline and profit.

 index 1 2 3 4 5 JOB j1 j2 j3 j4 j5 DEADLINE 2 1 3 2 1 PROFIT 60 100 20 40 20

## Sort the jobs according to their profit in descending order

Note! If two or more jobs are having the same profit then sort them as per their entry in the job list.

 index 1 2 3 4 5 JOB j2 j1 j4 j3 j5 DEADLINE 1 2 2 3 1 PROFIT 100 60 40 20 20

## Find the maximum deadline value

Looking at the jobs we can say the max deadline value is 3.
So, dmax = 3

As dmax = 3 so we will have THREE slots to keep track of free time slots. Set the time slot status to EMPTY

 time slot 1 2 3 status EMPTY EMPTY EMPTY

Total number of jobs is 5.
So we can write n = 5

Note!

If we look at job j2, it has a deadline 1. This means we have to complete job j2 in time slot 1 if we want to earn its profit.

Similarly, if we look at job j1 it has a deadline 2. This means we have to complete job j1 on or before time slot 2 in order to earn its profit.

Similarly, if we look at job j3 it has a deadline 3. This means we have to complete job j3 on or before time slot 3 in order to earn its profit.

Our objective is to select jobs that will give us higher profit.

## Pseudo code

``````
for i = 1 to n do

while k >= 1 do
if timeslot[k] is EMPTY then
timeslot[k] = job(i)
break
endif

Set k = k - 1

endwhile

endfor
``````

## Code in C

In the code we are saving jobs from 0 index.

``````#include <stdio.h>

#define MAX 100

typedef struct Job {
char id;
int profit;
} Job;

int minValue(int x, int y) {
if(x < y) return x;
return y;
}

int main(void) {
//variables
int i, j;

Job jobs = {
{"j1", 2,  60},
{"j2", 1, 100},
{"j3", 3,  20},
{"j4", 2,  40},
{"j5", 1,  20},
};

//temp
Job temp;

//number of jobs
int n = 5;

//sort the jobs profit wise in descending order
for(i = 1; i < n; i++) {
for(j = 0; j < n - i; j++) {
if(jobs[j+1].profit > jobs[j].profit) {
temp = jobs[j+1];
jobs[j+1] = jobs[j];
jobs[j] = temp;
}
}
}

printf("%10s %10s %10s\n", "Job", "Deadline", "Profit");
for(i = 0; i < n; i++) {
printf("%10s %10i %10i\n", jobs[i].id, jobs[i].deadline, jobs[i].profit);
}

return 0;
}

void jobSequencingWithDeadline(Job jobs[], int n) {
//variables
int i, j, k, maxprofit;

//free time slots
int timeslot[MAX];

//filled time slots
int filledTimeSlot = 0;

int dmax = 0;
for(i = 0; i < n; i++) {
}
}

//free time slots initially set to -1 [-1 denotes EMPTY]
for(i = 1; i <= dmax; i++) {
timeslot[i] = -1;
}

printf("dmax: %d\n", dmax);

for(i = 1; i <= n; i++) {
k = minValue(dmax, jobs[i - 1].deadline);
while(k >= 1) {
if(timeslot[k] == -1) {
timeslot[k] = i-1;
filledTimeSlot++;
break;
}
k--;
}

//if all time slots are filled then stop
if(filledTimeSlot == dmax) {
break;
}
}

//required jobs
printf("\nRequired Jobs: ");
for(i = 1; i <= dmax; i++) {
printf("%s", jobs[timeslot[i]].id);

if(i < dmax) {
printf(" --> ");
}
}

//required profit
maxprofit = 0;
for(i = 1; i <= dmax; i++) {
maxprofit += jobs[timeslot[i]].profit;
}
printf("\nMax Profit: %d\n", maxprofit);
}
``````

## Output

``````
j2          1        100
j1          2         60
j4          2         40
j3          3         20
j5          1         20
dmax: 3

Required Jobs: j2 --> j1 --> j3
Max Profit: 180
``````

## Time complexity

The time complexity of this problem is O(n2).

Share