Job Sequence Scheduling Using Greedy Algorithm
Job Sequence Scheduling Using Greedy Algorithm
Job scheduling is the problem of scheduling jobs out of a set of N jobs on a single
processor which maximizes profit as much as possible. Consider N jobs, each taking
unit time for execution. Each job is having some profit and deadline associated with
it. Profit earned only if the job is completed on or before its deadline. Otherwise, we
have to pay a profit as a penalty. Each job has deadline di ≥ 1 and profit pi ≥ 0. At a
time, only one job can be active on the processor.
The job is feasible only if it can be finished on or before its deadline. A feasible
solution is a subset of N jobs such that each job can be completed on or before its
deadline. An optimal solution is a solution with maximum profit.
The greedy approach produces an optimal result in fairly less time. As each job
takes the same amount of time, we can think of the schedule S consisting of a
sequence of job slots 1, 2, 3, …, N, where S(t) indicates job scheduled in slot t. Slot t
has a span of (t – 1) to t. S(t) = 0 implies no job is scheduled in slot t.
Schedule S is an array of slots S(t), S(t) ∈ {1, 2, 3, …, N} for each t ∈ {1, 2, 3, …, N}
Solution:
Given that,
Jobs j1 j
2 j
3 j4 j5 j
6 j7
Profit 3 5 20 18 1 6 30
Deadline 1 3 4 3 2 1 2
So, P = (30, 20, 18, 6, 5, 3, 1), J = (J7, J3, J4, J6, J2, J1, J5) and D = (2, 4, 3, 1, 3, 1, 2). We
shall select one by one job from the list of sorted jobs J, and check if it satisfies the
deadline. If so, schedule the job in the latest free slot. If no such slot is found, skip
the current job and process the next one. Initially,
Iteration 1:
Deadline for job J7 is 2. Slot 2 (t = 1 to t = 2) is free, so schedule it in slot 2. Solution
set S = {J7}, and Profit SP = {30}
Iteration 2:
Deadline for job J3 is 4. Slot 4 (t = 3 to t = 4) is free, so schedule it in slot 4. Solution
set S = {J7, J3}, and Profit SP = {30, 20}
Iteration 3:
Deadline for job J4 is 3. Slot 3 (t = 2 to t = 3) is free, so schedule it in slot 3.
Solution set S = {J7, J3, J4}, and Profit SP = {30, 20, 18}
Iteration 4:
Deadline for job J6 is 1. Slot 1 (t = 0 to t = 1) is free, so schedule it in slot 1.
Solution set S = {J7, J3, J4, J6}, and Profit
SP = {30, 20, 18, 6}
First, all four slots are occupied and none of the remaining jobs has deadline lesser
than 4. So none of the remaining jobs can be scheduled. Thus, with the greedy
approach, we will be able to schedule four jobs {J7, J3, J4, J6}, which give a profit of
(30 + 20 + 18 + 6) = 74 units.
# length of array
n = len(arr)
# Driver's Code
if __name__ == '__main__':
arr = [['a', 2, 100], # Job Array
['b', 1, 19],
['c', 2, 27],
['d', 1, 25],
['e', 3, 15]]
# Function Call
printJobScheduling(arr, 3)
Conclusion:
In this assignment we have learnt and successfully implemented Job Scheduling algorithm
using Greedy Approach