Algorithm
-
Getting Started with AlgorithmWhat is an Algorithm?
-
Characteristics of Algorithm1 Topic
-
Analysis Framework
-
Performance Analysis3 Topics
-
Mathematical Analysis2 Topics
-
Sorting AlgorithmSorting Algorithm10 Topics
-
Searching Algorithm6 Topics
-
Fundamental of Data StructuresStacks
-
Queues
-
Graphs
-
Trees
-
Sets
-
Dictionaries
-
Divide and ConquerGeneral Method
-
Binary Search
-
Recurrence Equation for Divide and Conquer
-
Finding the Maximum and Minimum
-
Merge Sort
-
Quick Sort
-
Stassen’s Matrix Multiplication
-
Advantages and Disadvantages of Divide and Conquer
-
Decrease and ConquerInsertion Sort
-
Topological Sort
-
Greedy MethodGeneral Method
-
Coin Change Problem
-
Knapsack Problem
-
Job Sequencing with Deadlines
-
Minimum Cost Spanning Trees2 Topics
-
Single Source Shortest Paths1 Topic
-
Optimal Tree Problem1 Topic
-
Transform and Conquer Approach1 Topic
-
Dynamic ProgrammingGeneral Method with Examples
-
Multistage Graphs
-
Transitive Closure1 Topic
-
All Pairs Shortest Paths6 Topics
-
BacktrackingGeneral Method
-
N-Queens Problem
-
Sum of Subsets problem
-
Graph Coloring
-
Hamiltonian Cycles
-
Branch and Bound2 Topics
-
0/1 Knapsack problem2 Topics
-
NP-Complete and NP-Hard Problems1 Topic
Participants2253
Job Sequencing with Deadlines
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++.
#include <bits/stdc++.h>
using namespace std;
int main()
{
int profit[10],dead_line[10];int maximum_profit=0,n,temp;
cout<<"Enter Number Of Data"<<endl;
cin>>n;
bool box[n];
for(int i=0; i<n; i++){
cin>>profit[i]>>dead_line[i];
}
for(int i=0; i<n-1; i++){
for(int j=i+1; j<n; j++)
if(profit[i]<profit[j]){
temp = profit[i];
profit[i] = profit[j];
profit[j] = temp;
temp = dead_line[i];
dead_line[i] = dead_line[j];
dead_line[j] = temp;
}
}
cout<<"After Sorting"<<endl;
for(int i=0; i<n; i++){
cout<<profit[i]<<" "<<dead_line[i]<<endl;
}
cout<<endl<<endl;
memset(box,false,sizeof(box));
for(int i=0; i<n; i++){
for(int j=min(n,dead_line[i])-1; j>=0; j--){
if(box[j]==false){
box[j]=true;
maximum_profit +=profit[i];
break;
}
}
}
cout<<"Maximum Profit: "<<maximum_profit<<endl;
return 0;
}