AOA-Lab Exp4
AOA-Lab Exp4
/Div: 60/D6AD
EXPERIMENT-4
AIM:
Write a C program to implement Job sequencing with deadlines problem.
THEORY:
Given an array of jobs where every job has a deadline and associated profit if the job is finished before the
deadline. It is also given that every job takes a single unit of time, so the minimum possible deadline for any
job is 1. How to maximize total profit if only one job can be scheduled at a time.
We are given a set of n jobs. Associated with job i is an integer deadline di ≥ 0 and a profit pi ≥ 0. For any
job i the profit p, is earned if the job is completed by its deadline. To complete a job, one has to process the
job on a machine for one unit of time. Only one machine is available for processing jobs. A feasible solution
for this problem is a subset J of jobs such that each job in this subset can be completed by its deadline. The
value of a feasible solution J is the sum of the profits of the jobs in J, or Σi∈JPi. An optimal solution is a
feasible solution with maximum value.
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.
JS(d, j, n)
// di ≥ 1,1 ≤ i ≤ n are the deadlines,n≥1.
//The jobs 3 are ordered such that p[1] ≥ p[2]… ≥p[n]
//J[i] is the ith job in the optimal solution, 1≤ i ≤k
// Also, at termination d[J[i]] ≤ d[J[i+1]], 1 ≤ i <k
{
d[0]:=J[0] := 0; // Initialize.
J[1] := 1; // Include job 1.
k := 1;
for i:= 2 to n do
{
// Consider jobs in nonincreasing order of p[i].
// Find position for i and check feasibility of insertion.
r:=k;
while ((d[J[r]] > d[i]) and (d[J[r]] ≠ r)) do r: =r=1;
if ((d[J[r]] ≤ d[i]) and (d[i]) >r)) then
{
// Insert i into J[]
for q:= k to (r+1) step -1 do J[g + 1]: = J[g];
J[r +1] := i ; k:=k + 1;
}
}
return k;
}
PROGRAM CODE:
OUTPUT:
CONCLUSION:
Thus, we have implemented the concept of greedy algorithm to solve the job sequencing with deadlines
problem.