0% found this document useful (0 votes)
3K views5 pages

Job Scheduling with Greedy Algorithm

Uploaded by

Abubaker Qureshi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3K views5 pages

Job Scheduling with Greedy Algorithm

Uploaded by

Abubaker Qureshi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
  • Introduction to Job Scheduling
  • Example Iterations
  • Python Code for Job Scheduling
  • Time Complexity and Conclusion

Job Scheduling using Greedy Algorithm

Whai is Job scheduling?

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}

Schedule S is feasible if,


▪ S(t) = i, then t ≤ di (Scheduled job must meet its deadline)
▪ Each job can be scheduled at max once.
Our goal is to find a feasible schedule S which maximizes the profit of scheduled
job. The goal can be achieved as follow: Sort all jobs in decreasing order of profit.
Start with the empty schedule, select one job at a time and if it is feasible then
schedule it in the latest possible slot.

Algorithm for Job Scheduling

Problem: Solve the following instance of “job scheduling with deadlines”


problem : n = 7, profits (p1, p2, p3, p4, p5, p6, p7) = (3, 5, 20, 18, 1, 6, 30) and
deadlines (d1, d2, d3, d4, d5, d6, d7) = (1, 3, 4, 3, 2, 1, 2). Schedule the jobs in
such a way to get maximum profit.

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

Sort all jobs in descending order of profit.

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,

Profit of scheduled jobs, SP = 0

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.

# Python3 code for the above approach

# function to schedule the jobs take 2


# arguments array and no of jobs to schedule

def printJobScheduling(arr, t):

# length of array
n = len(arr)

# Sort all jobs according to


# decreasing order of profit
for i in range(n):
for j in range(n - 1 - i):
if arr[j][2] < arr[j + 1][2]:
arr[j], arr[j + 1] = arr[j + 1], arr[j]

# To keep track of free time slots


result = [False] * t
# To store result (Sequence of jobs)
job = ['-1'] * t

# Iterate through all given jobs


for i in range(len(arr)):

# Find a free slot for this job


# (Note that we start from the
# last possible slot)
for j in range(min(t - 1, arr[i][1] - 1), -1, -1):

# Free slot found


if result[j] is False:
result[j] = True
job[j] = arr[i][0]
break

# print the sequence


print(job)

# Driver's Code
if __name__ == '__main__':
arr = [['a', 2, 100], # Job Array
['b', 1, 19],
['c', 2, 27],
['d', 1, 25],
['e', 3, 15]]

print("Following is maximum profit sequence of jobs")

# Function Call
printJobScheduling(arr, 3)

Time Complexity: O(N2)


Application:
A job scheduler is a computer application for controlling unattended background
program execution of jobs.

Conclusion:
In this assignment we have learnt and successfully implemented Job Scheduling algorithm
using Greedy Approach

Common questions

Powered by AI

The greedy algorithm's success in achieving maximum profit in the job scheduling scenario indicates its effectiveness in making locally optimal decisions that lead to a good global outcome. Its strategy of prioritizing immediate gains makes it efficient and straightforward. However, this same strategy can be a shortcoming in situations where future gains require short-term sacrifices or complex interactions between jobs exist. Thus, while effective in simple, constrained contexts, the greedy algorithm might not provide optimal solutions in more complex scenarios that require global optimization considerations .

The primary goal of job scheduling using a greedy algorithm is to maximize profit by scheduling jobs on a single processor, such that each job is completed on or before its deadline. This is achieved by sorting all jobs in decreasing order of profit and scheduling them one-by-one in the latest available free time slots while ensuring that each scheduled job meets its deadline .

Sorting jobs in descending order of profit is necessary to ensure that the most profitable jobs are considered first for scheduling. By prioritizing jobs that offer the highest profit, the algorithm maximizes the overall profit within the constrained scheduling environment, where each job must also meet its deadline. This approach follows the greedy strategy of making locally optimal choices that lead to a globally optimal solution .

The job scheduling problem demonstrates the efficacy of the greedy algorithm by showing how it systematically selects and schedules jobs to maximize profit, even under constraints. For the given data set, the greedy approach quickly identifies high-profit jobs and schedules them within appropriate slots, achieving a maximum profit solution without the need for exhaustive search or complex computation, thereby exemplifying the algorithm's efficiency and applicability in time-critical applications .

The greedy approach ensures that a feasible solution is achieved by iteratively selecting the most profitable job and attempting to schedule it in the latest possible time slot that meets its deadline. A feasible solution is defined as a subset of jobs where each job is scheduled at most once, and all scheduled jobs are completed on or before their respective deadlines, thereby adhering to all constraints .

Real-world applications of job scheduling algorithms include controlling unattended background program execution in computer systems. These algorithms help in optimizing the use of processor resources by efficiently managing the scheduling and execution of jobs, thereby maximizing profit and minimizing idle time in various computational settings .

The job scheduling problem is solved using the following steps in Python: 1) Sort jobs in descending order of profit. 2) Initialize an array to track free slots and another to store the job sequence. 3) Iterate through the sorted jobs, for each job, find the latest possible free slot within its deadline. 4) Assign the job to this slot and update the free slot tracker. 5) The job sequence reflects the order of scheduled jobs, thereby providing the maximum profit arrangement .

The time complexity of the job scheduling algorithm is determined to be O(N²), which implies that the algorithm's runtime increases quadratically with the number of jobs. This complexity arises from the need to sort the jobs by profit and iteratively check for scheduling feasibility. As the number of jobs grows, computational resources increase due to repeated evaluations needed for each job against the potential slots .

One limitation of using a greedy algorithm for job scheduling is that it does not consider the global context of job deadlines and might miss optimal schedules that require lower profit jobs to be scheduled early to accommodate more high-profit jobs later. In the provided example, scheduling is limited by the available free slots before the deadlines, leading to a solution that maximizes immediate profit without reevaluating future scheduling possibilities like combinatorial interactions of deadlines and profits .

Once all slots are occupied in a job scheduling problem, it implies that no further jobs can be scheduled unless they can fit into existing slots while meeting their deadlines. This situation necessitates skipping of jobs that could not be scheduled due to slot unavailability, thus potentially impacting the total profit. The greedy approach might lead to suboptimal global profit maximization as it prioritizes immediate gains without considering future job scheduling possibilities or potential trade-offs .

You might also like