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 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.

Examples:

Input: Four Jobs with following deadlines and profits
  JobID    Deadline      Profit
    a        4            20   
    b        1            10
    c        1            40  
    d        1            30
Output: Following is maximum profit sequence of jobs
        c, a   


Input:  Five Jobs with following deadlines and profits
   JobID     Deadline     Profit
     a         2           100
     b         1           19
     c         2           27
     d         1           25
     e         3           15
Output: Following is maximum profit sequence of jobs
        c, a, e
[ad type=”banner”]

We strongly recommend to minimize your browser and try this yourself first.

A Simple Solution is to generate all subsets of given set of jobs and check individual subset for feasibility of jobs in that subset. Keep track of maximum profit among all feasible subsets. The time complexity of this solution is exponential.

This is a standard Greedy Algorithm problem. Following is algorithm.

1) Sort all jobs in decreasing order of profit.
2) Initialize the result sequence as first job in sorted jobs.
3) Do following for remaining n-1 jobs
.......a) If the current job can fit in the current result sequence 
          without missing the deadline, add current job to the result.
          Else ignore the current job.
[pastacode lang=”c” manual=”%2F%2F%20Program%20to%20find%20the%20maximum%20profit%20job%20sequence%20from%20a%20given%20array%0A%2F%2F%20of%20jobs%20with%20deadlines%20and%20profits%0A%23include%3Ciostream%3E%0A%23include%3Calgorithm%3E%0Ausing%20namespace%20std%3B%0A%20%0A%2F%2F%20A%20structure%20to%20represent%20a%20job%0Astruct%20Job%0A%7B%0A%20%20%20char%20id%3B%20%20%20%20%20%20%2F%2F%20Job%20Id%0A%20%20%20int%20dead%3B%20%20%20%20%2F%2F%20Deadline%20of%20job%0A%20%20%20int%20profit%3B%20%20%2F%2F%20Profit%20if%20job%20is%20over%20before%20or%20on%20deadline%0A%7D%3B%0A%20%0A%2F%2F%20This%20function%20is%20used%20for%20sorting%20all%20jobs%20according%20to%20profit%0Abool%20comparison(Job%20a%2C%20Job%20b)%0A%7B%0A%20%20%20%20%20return%20(a.profit%20%3E%20b.profit)%3B%0A%7D%0A%20%0A%2F%2F%20Returns%20minimum%20number%20of%20platforms%20reqquired%0Avoid%20printJobScheduling(Job%20arr%5B%5D%2C%20int%20n)%0A%7B%0A%20%20%20%20%2F%2F%20Sort%20all%20jobs%20according%20to%20decreasing%20order%20of%20prfit%0A%20%20%20%20sort(arr%2C%20arr%2Bn%2C%20comparison)%3B%0A%20%0A%20%20%20%20int%20result%5Bn%5D%3B%20%2F%2F%20To%20store%20result%20(Sequence%20of%20jobs)%0A%20%20%20%20bool%20slot%5Bn%5D%3B%20%20%2F%2F%20To%20keep%20track%20of%20free%20time%20slots%0A%20%0A%20%20%20%20%2F%2F%20Initialize%20all%20slots%20to%20be%20free%0A%20%20%20%20for%20(int%20i%3D0%3B%20i%3Cn%3B%20i%2B%2B)%0A%20%20%20%20%20%20%20%20slot%5Bi%5D%20%3D%20false%3B%0A%20%0A%20%20%20%20%2F%2F%20Iterate%20through%20all%20given%20jobs%0A%20%20%20%20for%20(int%20i%3D0%3B%20i%3Cn%3B%20i%2B%2B)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%2F%2F%20Find%20a%20free%20slot%20for%20this%20job%20(Note%20that%20we%20start%0A%20%20%20%20%20%20%20%2F%2F%20from%20the%20last%20possible%20slot)%0A%20%20%20%20%20%20%20for%20(int%20j%3Dmin(n%2C%20arr%5Bi%5D.dead)-1%3B%20j%3E%3D0%3B%20j–)%0A%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%2F%2F%20Free%20slot%20found%0A%20%20%20%20%20%20%20%20%20%20if%20(slot%5Bj%5D%3D%3Dfalse)%0A%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20result%5Bj%5D%20%3D%20i%3B%20%20%2F%2F%20Add%20this%20job%20to%20result%0A%20%20%20%20%20%20%20%20%20%20%20%20%20slot%5Bj%5D%20%3D%20true%3B%20%2F%2F%20Make%20this%20slot%20occupied%0A%20%20%20%20%20%20%20%20%20%20%20%20%20break%3B%0A%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F%2F%20Print%20the%20result%0A%20%20%20%20for%20(int%20i%3D0%3B%20i%3Cn%3B%20i%2B%2B)%0A%20%20%20%20%20%20%20if%20(slot%5Bi%5D)%0A%20%20%20%20%20%20%20%20%20cout%20%3C%3C%20arr%5Bresult%5Bi%5D%5D.id%20%3C%3C%20%22%20%22%3B%0A%7D%0A%20%0A%2F%2F%20Driver%20program%20to%20test%20methods%0Aint%20main()%0A%7B%0A%20%20%20%20Job%20arr%5B%5D%20%3D%20%7B%20%7B’a’%2C%202%2C%20100%7D%2C%20%7B’b’%2C%201%2C%2019%7D%2C%20%7B’c’%2C%202%2C%2027%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B’d’%2C%201%2C%2025%7D%2C%20%7B’e’%2C%203%2C%2015%7D%7D%3B%0A%20%20%20%20int%20n%20%3D%20sizeof(arr)%2Fsizeof(arr%5B0%5D)%3B%0A%20%20%20%20cout%20%3C%3C%20%22Following%20is%20maximum%20profit%20sequence%20of%20jobs%5Cn%22%3B%0A%20%20%20%20printJobScheduling(arr%2C%20n)%3B%0A%20%20%20%20return%200%3B%0A%7D” message=”” highlight=”” provider=”manual”/]

Output:

Following is maximum profit sequence of jobs
c a e

Time Complexity of the above solution is O(n2). It can be optimized using Disjoint Set Data Structure. Please refer below post for details.

[ad type=”banner”]

Categorized in:

Tagged in:

, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,