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.
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
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.
The Following is C++ implementation of above algorithm.
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.
This article is contributed by Shubham. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Job Sequencing Problem - Loss Minimization
- Top 20 Greedy Algorithms Interview Questions
- Job Sequencing Problem | Set 2 (Using Disjoint Set)
- Fractional Knapsack Problem
- Find minimum time to finish all jobs with given constraints
- Greedy Algorithm to find Minimum number of Coins
- K Centers Problem | Set 1 (Greedy Approximate Algorithm)
- Greedy Algorithm for Egyptian Fraction
- Minimum Number of Platforms Required for a Railway/Bus Station
- Dijkstra's shortest path algorithm | Greedy Algo-7
- Prim’s Minimum Spanning Tree (MST) | Greedy Algo-5
- Efficient Huffman Coding for Sorted Input | Greedy Algo-4
- Huffman Coding | Greedy Algo-3
- Kruskal’s Minimum Spanning Tree Algorithm | Greedy Algo-2
- Activity Selection Problem | Greedy Algo-1