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}
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!
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
The time complexity of this Job Sequencing with Deadlines problem is O(n2).
#greedy-algorithm #job-sequencing-with-deadline #job-sequence-with-deadline #source-code