|
| 1 | +/** |
| 2 | + * @file |
| 3 | + * @brief [Job Sequencing Problem] |
| 4 | + * (https://www.geeksforgeeks.org/job-sequencing-problem/) |
| 5 | + * Algorithm explanantion and implementation |
| 6 | + * |
| 7 | + * @details |
| 8 | + * Given an array of jobs where every job has a deadline and associated profit |
| 9 | + * if the job is finished before the deadline. |
| 10 | + * It is also given that every job takes |
| 11 | + * a single unit of time, so the minimum possible deadline for any job is 1. |
| 12 | + * Maximize the total profit if only one job can be scheduled at a time. |
| 13 | + * |
| 14 | + * Examples: |
| 15 | + * |
| 16 | + * @example |
| 17 | + * Input: Four Jobs with following deadlines and profits |
| 18 | + * JobID | Deadline | Profit |
| 19 | + * ----------------------------- |
| 20 | + * a | 4 | 20 |
| 21 | + * b | 1 | 10 |
| 22 | + * c | 1 | 40 |
| 23 | + * d | 1 | 30 |
| 24 | + * |
| 25 | + * Output: Following is maximum profit sequence of jobs: c, a |
| 26 | + * |
| 27 | + * @example |
| 28 | + * Input: Five Jobs with following deadlines and profits |
| 29 | + * JobID | Deadline | Profit |
| 30 | + * --------------------------- |
| 31 | + * a | 2 | 100 |
| 32 | + * b | 1 | 19 |
| 33 | + * c | 2 | 27 |
| 34 | + * d | 1 | 25 |
| 35 | + * e | 3 | 15 |
| 36 | + * Output: Following is maximum profit sequence of jobs: c, a, e |
| 37 | + * |
| 38 | + * |
| 39 | + * |
| 40 | + * Naive Approach: |
| 41 | + * Generate all subsets of a given set of jobs and check individual subsets for |
| 42 | + * the feasibility of jobs in that subset. Keep track of maximum profit among |
| 43 | + * all feasible subsets. |
| 44 | + * The above approach's time complexity is: O(N^2) with auxliary space O(N) |
| 45 | + * N refers to the number of jobs. |
| 46 | + * |
| 47 | + * |
| 48 | + * Greedy Approach: |
| 49 | + * Greedily choose the jobs with maximum profit first, by sorting the jobs in |
| 50 | + * decreasing order of their profit. This would help to maximize the total |
| 51 | + * profit as choosing the job with maximum profit for every time slot will |
| 52 | + * eventually maximize the total profit. |
| 53 | + * The above approaches's time complexity is: O(NlogN) with auxliary space o(N) |
| 54 | + * N refers to the number of jobs. |
| 55 | + * |
| 56 | + * |
| 57 | + * Greedy approach psuedo-code: |
| 58 | + - Sort all jobs in decreasing order of profit. |
| 59 | + - Iterate on jobs in decreasing order of profit. |
| 60 | + - For each job , do the following: |
| 61 | + - Find a time slot i, such that slot is empty and i < deadline and i |
| 62 | + is greatest. |
| 63 | + - Put the job in this slot and mark this slot filled. |
| 64 | + - If no such i exists, then ignore the job. |
| 65 | + * |
| 66 | + * @author [Mahmoud Elkholy](https://github.com/maahmoudezzat) |
| 67 | + */ |
| 68 | + |
| 69 | +// Implementation of the greedy algorithm using priority_queue AKA max_heap. |
| 70 | +// C++ code for the above approach |
| 71 | +#include <algorithm> |
| 72 | +#include <cassert> |
| 73 | +#include <iostream> |
| 74 | +#include <queue> |
| 75 | +#include <vector> |
| 76 | +using namespace std; |
| 77 | + |
| 78 | +// A structure to represent a job |
| 79 | +struct Job { |
| 80 | + char id; // Job Id |
| 81 | + int dead; // Deadline of job |
| 82 | + int profit; // Profit earned if job is completed before deadline |
| 83 | +}; |
| 84 | + |
| 85 | +// Custom sorting helper struct which is used for sorting |
| 86 | +// all jobs according to profit |
| 87 | +struct jobProfit { |
| 88 | + bool operator()(Job const& a, Job const& b) { |
| 89 | + return (a.profit < b.profit); |
| 90 | + } |
| 91 | +}; |
| 92 | + |
| 93 | +// Returns maximum profit from jobs |
| 94 | +void printJobScheduling(Job arr[], int n) { |
| 95 | + vector<Job> result; |
| 96 | + sort(arr, arr + n, [](Job a, Job b) { return a.dead < b.dead; }); |
| 97 | + |
| 98 | + // set a custom priority queue |
| 99 | + priority_queue<Job, vector<Job>, jobProfit> pq; |
| 100 | + |
| 101 | + for (int i = n - 1; i >= 0; i--) { |
| 102 | + int slot_available; |
| 103 | + |
| 104 | + // we count the slots available between two jobs |
| 105 | + if (i == 0) { |
| 106 | + slot_available = arr[i].dead; |
| 107 | + } else { |
| 108 | + slot_available = arr[i].dead - arr[i - 1].dead; |
| 109 | + } |
| 110 | + |
| 111 | + // include the profit of job(as priority), |
| 112 | + // deadline and job_id in maxHeap |
| 113 | + pq.push(arr[i]); |
| 114 | + |
| 115 | + while (slot_available > 0 && pq.size() > 0) { |
| 116 | + // get the job with the most profit |
| 117 | + Job job = pq.top(); |
| 118 | + pq.pop(); |
| 119 | + |
| 120 | + // reduce the slots |
| 121 | + slot_available--; |
| 122 | + |
| 123 | + // add it to the answer |
| 124 | + result.push_back(job); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + // sort the result based on the deadline |
| 129 | + sort(result.begin(), result.end(), |
| 130 | + [&](Job a, Job b) { return a.dead < b.dead; }); |
| 131 | + |
| 132 | + // print the result |
| 133 | + for (int i = 0; i < result.size(); i++) cout << result[i].id << ' '; |
| 134 | + cout << endl; |
| 135 | +} |
| 136 | + |
| 137 | +// main function to test the code |
| 138 | +int main() { |
| 139 | + Job arr[] = { |
| 140 | + {'a', 2, 100}, {'b', 1, 19}, {'c', 2, 27}, {'d', 1, 25}, {'e', 3, 15}}; |
| 141 | + |
| 142 | + int n = sizeof(arr) / sizeof(arr[0]); |
| 143 | + cout << "Following is maximum profit sequence of jobs \n"; |
| 144 | + |
| 145 | + // Function call |
| 146 | + printJobScheduling(arr, n); |
| 147 | + return 0; |
| 148 | +} |
0 commit comments