# A Greedy Algorithm for Job Sequencing with Deadlines and Profits

In this chapter we will see greedy algorithm examples. 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.

The setting is that we have n jobs, each of which takes unit time, and a processor on which we would like to schedule them in as profitable a manner as possible. Each job has a profit associated with it, as well as a deadline; if the job is not scheduled by the deadline, then we don't get the profit.

**Definition of job sequence with deadline**

A schedule S is an array: S(1),S(2).....S(n) where S(t) € { 0, 1, 2......n} for each t € { 1, 2......n}

**Job sequence with deadline strategy**

- Are all tasks completed ?
- What is the maximum profit ?

**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.

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.

**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`

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 of Jobs Scheduling with Deadlines**

```
for i = 1 to n do
Set k = min(dmax, DEADLINE(i)) //where DEADLINE(i) denotes deadline of ith job
while k >= 1 do
if timeslot[k] is EMPTY then
timeslot[k] = job(i)
break
endif
Set k = k - 1
endwhile
endfor
```

**Job Sequencing with Deadlines Algorithms Source Code**

See the source code of Job Sequencing with Deadlines problems with c++.

Read more : Divide and Conquer Algorithms with Source Code

**Time complexity**

The time complexity of this Job Sequencing with Deadlines problem is O(n^{2}).

Was this article helpful?