Python Implementation of Job Sequencing problem














































Python Implementation of Job Sequencing problem



Python Implementation of Job Sequencing problem


    In this article 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.

    Points to remember
  • In this problem we have n jobs j1, j2, %u2026 jn each has an associated deadline d1, d2, %u2026 dn and profit             p1, p2, ... pn.
  • Profit will only be awarded or earned if the job is completed on or before the deadline.
  • We assume that each job takes unit time to complete.
  • The objective is to earn maximum profit when only one job can be scheduled or processed at any             given time.

    Problem :-    Consider the following 5 jobs and their associated deadline and profit.

index12345
JOBj1j2j3j4j5
DEADLINE21321
PROFIT60100204020

    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.

index12345
JOBj2j1j4j3j5
DEADLINE12231
PROFIT10060402020

    Find the maximum deadline value 
    Looking at the jobs we can say the max deadline value is 3. So, dmax = 3
    Total number of jobs is 5. So we can write n = 5

    If we look at job j2, it has a deadline 1. This means we have to complete job j2 in time slot 1 
    if we want to earn its profit.
                                       Similarly, if we look at job j1 it has a deadline 2. This means we have to                    complete job j1 on or before time slot 2 in order to earn its profit.
                                                                             Similarly, if we look at job j3 it has a deadline 3.                  This means we have to complete job j3 on or before time slot 3 in order to earn its profit.
    
    Code :-    def printJobSequencing(arr,t):
              n=len(arr)
              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]
              result = [False]*t
              job = ['-1']*t
              for i in range(len(arr)):
                  for j in range(min(t-1,arr[i][1]-1),-1,-1):
                      if(result[j] is False):
                          result[j] = True
                          job[j] = arr[i][0]
                          break
              print(job)

          arr = [['j1',2,60],
                 ['j2',1,100],
                 ['j3',3,20],
                 ['j4',2,40],
                 ['j5',1,20]]    

          print("Following is the maximum profit sequence of jobs")
          printJobSequencing(arr,3)

  Output :-    Following is the maximum profit sequence of jobs
                       j2 -> j1 -> j3

Comments