|
68 | 68 |
|
69 | 69 | // Implementation of the greedy algorithm using priority_queue AKA max_heap.
|
70 | 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 |
| 71 | +#include <algorithm> // for sort function. |
| 72 | +#include <cassert> // for assert funtion in testing |
| 73 | +#include <iostream> // for standard in/out |
| 74 | +#include <queue> // for priority queue data structure |
| 75 | +#include <vector> // for vector data structure |
| 76 | + |
| 77 | +/** |
| 78 | + * @namespace |
| 79 | + * @brief Greedy Algorithms |
| 80 | + */ |
| 81 | +namespace greedy_algorithms { |
| 82 | + |
| 83 | +/** |
| 84 | + * @brief A structure to represent a job |
| 85 | + */ |
79 | 86 | struct Job {
|
80 | 87 | char id; // Job Id
|
81 | 88 | int dead; // Deadline of job
|
82 | 89 | int profit; // Profit earned if job is completed before deadline
|
83 |
| -}; |
| 90 | +}; // namespace greedy_algorithmsstruct Job |
84 | 91 |
|
85 | 92 | // Custom sorting helper struct which is used for sorting
|
86 | 93 | // all jobs according to profit
|
| 94 | + |
| 95 | +/** |
| 96 | + * @brief Utility function that finds Custom sorting helper |
| 97 | + * struct which is used for sorting all jobs according to profit |
| 98 | + * |
| 99 | + * @param a first job struct |
| 100 | + * @param b second job struct |
| 101 | + * @returns true if the proft of a less than profit of b, else false. |
| 102 | + */ |
87 | 103 | struct jobProfit {
|
88 | 104 | bool operator()(Job const& a, Job const& b) {
|
89 | 105 | return (a.profit < b.profit);
|
90 | 106 | }
|
91 | 107 | };
|
92 | 108 |
|
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; }); |
| 109 | +/** |
| 110 | + * @brief function that get the maximum profit from jobs |
| 111 | + * |
| 112 | + * @param arr array fo Jobs |
| 113 | + * @param n number of the jobs in the array |
| 114 | + * @returns vector of 'char' including the IDs of jobs |
| 115 | + * that has the maximum profit from jobs |
| 116 | + * |
| 117 | + */ |
| 118 | +std::vector<char> getJobScheduling(Job arr[], int n) { |
| 119 | + std::vector<Job> result; |
| 120 | + std::sort(arr, arr + n, [](Job a, Job b) { return a.dead < b.dead; }); |
97 | 121 |
|
98 | 122 | // set a custom priority queue
|
99 |
| - priority_queue<Job, vector<Job>, jobProfit> pq; |
| 123 | + std::priority_queue<Job, std::vector<Job>, jobProfit> pq; |
100 | 124 |
|
101 | 125 | for (int i = n - 1; i >= 0; i--) {
|
102 | 126 | int slot_available;
|
@@ -129,20 +153,47 @@ void printJobScheduling(Job arr[], int n) {
|
129 | 153 | sort(result.begin(), result.end(),
|
130 | 154 | [&](Job a, Job b) { return a.dead < b.dead; });
|
131 | 155 |
|
132 |
| - // print the result |
133 |
| - for (int i = 0; i < result.size(); i++) cout << result[i].id << ' '; |
134 |
| - cout << endl; |
| 156 | + std::vector<char> answer; |
| 157 | + |
| 158 | + // extract the job IDs only to be returned |
| 159 | + for (int i = 0; i < result.size(); i++) answer.push_back(result[i].id); |
| 160 | + |
| 161 | + // return the job IDs |
| 162 | + return answer; |
135 | 163 | }
|
| 164 | +} // namespace greedy_algorithms |
136 | 165 |
|
137 |
| -// main function to test the code |
138 |
| -int main() { |
139 |
| - Job arr[] = { |
| 166 | +/** |
| 167 | + * @brief Self-test implementations |
| 168 | + * @returns void |
| 169 | + */ |
| 170 | +void tests() { |
| 171 | + greedy_algorithms::Job jobs1[] = { |
140 | 172 | {'a', 2, 100}, {'b', 1, 19}, {'c', 2, 27}, {'d', 1, 25}, {'e', 3, 15}};
|
| 173 | + int n = sizeof(jobs1) / sizeof(jobs1[0]); |
| 174 | + |
| 175 | + // 1st test |
| 176 | + assert(greedy_algorithms::getJobScheduling(jobs1, n)[0] == 'a'); |
| 177 | + assert(greedy_algorithms::getJobScheduling(jobs1, n)[1] == 'c'); |
| 178 | + assert(greedy_algorithms::getJobScheduling(jobs1, n)[2] == 'e'); |
141 | 179 |
|
142 |
| - int n = sizeof(arr) / sizeof(arr[0]); |
143 |
| - cout << "Following is maximum profit sequence of jobs \n"; |
| 180 | + greedy_algorithms::Job jobs2[] = { |
| 181 | + {'x', 1, 50}, {'y', 2, 60}, {'z', 2, 20}, {'w', 3, 30}}; |
| 182 | + n = sizeof(jobs2) / sizeof(jobs2[0]); |
144 | 183 |
|
145 |
| - // Function call |
146 |
| - printJobScheduling(arr, n); |
| 184 | + // 2nd test |
| 185 | + assert(greedy_algorithms::getJobScheduling(jobs2, n)[0] == 'x'); |
| 186 | + assert(greedy_algorithms::getJobScheduling(jobs2, n)[1] == 'y'); |
| 187 | + assert(greedy_algorithms::getJobScheduling(jobs2, n)[2] == 'w'); |
| 188 | + |
| 189 | + std::cout << "All tests have successfully passed!\n"; |
| 190 | +} |
| 191 | + |
| 192 | +/** |
| 193 | + * @brief Main function |
| 194 | + * @returns 0 on exit |
| 195 | + */ |
| 196 | +int main() { |
| 197 | + tests(); |
147 | 198 | return 0;
|
148 | 199 | }
|
0 commit comments