Algorithm analysis
and design
Job sequencing with deadlines.
Algorithm stated
1. Algorithm JS(d,j,n)
2. {
3. D[0] : =T[0] : =0; / * This fictious job allows easy insertion into position 1 of time slot*/
4. T[1] : =1; / *assigning job 1 to time slot*/
5. k:=1;
6. For i:=2 to n do
7. {
8. r:=k;
9. while((D[T[r]]>D[i]) and (D[T[r]]!=r))do r:=r-1;
10. if ((D[T[r]]<=D[i]) and (D[i]>r))then
11. {
12. for q:=k to (r+1) step-1 do T[q+1]:=T[q]; /*”step-1” implies making the
consecutive slots empty*/
13. T[r+1]:= i; k:=k+1;
14. }
15. }
16. Return k;
17. }
Example:
Q. Let n=5;p=(20,15,10,5,1);d=(2,2,1,3,3)
Ans:-
Given:
Jobs J1 J2 J3 J4 J5
Profits 20 15 10 5 1
Deadline 2 2 1 3 3
Now , we give index to jobs: n=5
i
Index 1 2 3 4 5
Jobs J1 J2 J3 J4 J5
Profits 20 15 10 5 1
Deadline 2 2 1 3 3
The maximum deadline given for a job in the egs is:
3.
Hence, Dmax=3.
So we can get a maximum of 3 time slots(initially-1 job-
1 unit of time).
Henceforth , we create a table for time slot ,T:
Timeslot 1 2 3
Status
From the algo we are solving the problem:
 Look at index 1 of job array , its deadline is 2 ,tf it has to
come in the first slot.
Now, assigning i=1 =>J1 to timeslot array, T[].
Then , assigning other slots to be Empty[-1].
T[1] : =1; / *assigning job 1 to time slot*/
for q:=k to (r+1) step-1 do T[q+1]:=T[q]; /*”step-1” implies making
the consecutive slots empty*/
T[r+1]:= i; k:=k+1;
Table will be now,
Now , i becomes 2
Formula of k is min(Dmax, deadline(i))
k checks which is the min among the values
 k =min(3,2)=2;
Checking if k>=1.
The solution is a yes .
Hence, checking timeslot(k)==-1or empty
If yes fill it with the corresponding job.
Now , i becomes 3
Formula of k is min(Dmax, deadline(i))
k checks which is the min among the values
 k =min(3,1)=1;
Checking if k>=1.
The solution is a yes .
Hence, checking timeslot(k)==-1or empty
It’s a no,hence decrementing k by 1 and j3 rejected.
Now , i becomes 4
Formula of k is min(Dmax, deadline(i))
k checks which is the min among the values
 k =min(3,3)=3;
Checking if k>=1.
The solution is a yes .
Hence, checking timeslot(k)==-1or empty
If yes fill it with the corresponding job.(j4)
Now , i becomes 5
Formula of k is min(Dmax, deadline(i))
k checks which is the min among the values
 k =min(3,3)=3;
Checking if k>=1.
The solution is a yes .
Hence, checking timeslot(k)==-1or empty
It’s a no, hence decrement k by 1 and reject j5 and exit for
loop.
Final timeslot table results give a sequece of jobs with max
profit which meet the deadlines.
Therefore the sequence of jobs is :
J1->J2->J4
•Algorithm that can
be prepared from
the example(for
easy
implementation)
1. Algorithm JS(D,J,n)
2. {
3. D[0] :=J[0] :=0;
4. for i:=1 to n
5. {
6. T[i]=-1;
7. }
8. J[1]:=1;
9. for i:=2 to n
10. {
11. k:= min(Dmax,D(i));
12. if(k>=1) then
13. {
14. if(T[k]==-1) then
15. {
16. T[k]:=J[k];
17. }
18. else
19. k--;
20. }
21. }

Job sequencing with deadlines(with example)

  • 1.
    Algorithm analysis and design Jobsequencing with deadlines.
  • 2.
    Algorithm stated 1. AlgorithmJS(d,j,n) 2. { 3. D[0] : =T[0] : =0; / * This fictious job allows easy insertion into position 1 of time slot*/ 4. T[1] : =1; / *assigning job 1 to time slot*/ 5. k:=1; 6. For i:=2 to n do 7. { 8. r:=k; 9. while((D[T[r]]>D[i]) and (D[T[r]]!=r))do r:=r-1; 10. if ((D[T[r]]<=D[i]) and (D[i]>r))then 11. { 12. for q:=k to (r+1) step-1 do T[q+1]:=T[q]; /*”step-1” implies making the consecutive slots empty*/ 13. T[r+1]:= i; k:=k+1; 14. } 15. } 16. Return k; 17. }
  • 3.
    Example: Q. Let n=5;p=(20,15,10,5,1);d=(2,2,1,3,3) Ans:- Given: JobsJ1 J2 J3 J4 J5 Profits 20 15 10 5 1 Deadline 2 2 1 3 3
  • 4.
    Now , wegive index to jobs: n=5 i Index 1 2 3 4 5 Jobs J1 J2 J3 J4 J5 Profits 20 15 10 5 1 Deadline 2 2 1 3 3
  • 5.
    The maximum deadlinegiven for a job in the egs is: 3. Hence, Dmax=3. So we can get a maximum of 3 time slots(initially-1 job- 1 unit of time). Henceforth , we create a table for time slot ,T: Timeslot 1 2 3 Status
  • 6.
    From the algowe are solving the problem:  Look at index 1 of job array , its deadline is 2 ,tf it has to come in the first slot. Now, assigning i=1 =>J1 to timeslot array, T[]. Then , assigning other slots to be Empty[-1]. T[1] : =1; / *assigning job 1 to time slot*/ for q:=k to (r+1) step-1 do T[q+1]:=T[q]; /*”step-1” implies making the consecutive slots empty*/ T[r+1]:= i; k:=k+1; Table will be now,
  • 7.
    Now , ibecomes 2 Formula of k is min(Dmax, deadline(i)) k checks which is the min among the values  k =min(3,2)=2; Checking if k>=1. The solution is a yes . Hence, checking timeslot(k)==-1or empty If yes fill it with the corresponding job. Now , i becomes 3 Formula of k is min(Dmax, deadline(i)) k checks which is the min among the values  k =min(3,1)=1; Checking if k>=1. The solution is a yes . Hence, checking timeslot(k)==-1or empty It’s a no,hence decrementing k by 1 and j3 rejected.
  • 8.
    Now , ibecomes 4 Formula of k is min(Dmax, deadline(i)) k checks which is the min among the values  k =min(3,3)=3; Checking if k>=1. The solution is a yes . Hence, checking timeslot(k)==-1or empty If yes fill it with the corresponding job.(j4) Now , i becomes 5 Formula of k is min(Dmax, deadline(i)) k checks which is the min among the values  k =min(3,3)=3; Checking if k>=1. The solution is a yes . Hence, checking timeslot(k)==-1or empty It’s a no, hence decrement k by 1 and reject j5 and exit for loop.
  • 9.
    Final timeslot tableresults give a sequece of jobs with max profit which meet the deadlines. Therefore the sequence of jobs is : J1->J2->J4
  • 10.
    •Algorithm that can beprepared from the example(for easy implementation)
  • 11.
    1. Algorithm JS(D,J,n) 2.{ 3. D[0] :=J[0] :=0; 4. for i:=1 to n 5. { 6. T[i]=-1; 7. } 8. J[1]:=1; 9. for i:=2 to n 10. { 11. k:= min(Dmax,D(i)); 12. if(k>=1) then 13. { 14. if(T[k]==-1) then 15. { 16. T[k]:=J[k]; 17. } 18. else 19. k--; 20. } 21. }