Find Jobs involved in Weighted Job Scheduling in C++


Suppose we have a list of N jobs where each job has three parameters. 1. Start Time 2. Finish Time 3. Profit We have to find a subset of jobs associated with maximum profit so that no two jobs in the subset overlap.

So, if the input is like N = 4 and J = {{2, 3, 55},{4, 6, 25},{7, 20, 150},{3, 150, 250}} , then the output will be [(2, 3, 55),(3, 150, 250)] and optimal profit 305

To solve this, we will follow these steps −

  • Define a function find_no_conflict(), this will take an array jobs, index,

  • left := 0, right := index - 1

  • while left <= right, do −

    • mid := (left + right) / 2

    • if jobs[mid].finish <= jobs[index].start, then −

      • if jobs[mid + 1].finish <= jobs[index].start, then −

        • left := mid + 1

      • return mid

        • return mid

    • Otherwise

      • right := mid - 1

  • return -1

  • From the main method, do the following −

  • sort the array job_list based on finish time

  • make a table for jobs called table of size n

  • table[0].value := job_list[0].profit

  • insert job_list[0] at the end of table[0]

  • for initialize i := 1, when i < n, update (increase i by 1), do −

    • include_profit := job_list[i].profit

    • l := find_no_conflict(job_list, i)

    • if l is not equal to - 1, then −

      • include_profit := include_profit + table[l].value

    • if include_profit > table[i - 1].value, then −

      • table[i].value := include_profit

      • table[i].job := table[l].job

      • insert job_list[i] at the end of of table[i]

    • Otherwise

      • table[i] := table[i - 1]

  • display jobs from table

  • display Optimal profit := table[n - 1].value

Example (C++)

Let us see the following implementation to get better understanding −

 Live Demo

#include <bits/stdc++.h>
using namespace std;
class Job {
   public:
      int start, finish, profit;
};
struct job_with_weight {
   vector<Job> job;
   int value;
};
bool jobComparator(Job s1, Job s2) {
   return (s1.finish < s2.finish);
}
int find_no_conflict(Job jobs[], int index) {
   int left = 0, right = index - 1;
   while (left <= right) {
      int mid = (left + right) / 2;
      if (jobs[mid].finish <= jobs[index].start) {
         if (jobs[mid + 1].finish <= jobs[index].start)
            left = mid + 1;
         else
            return mid;
      }
      else
         right = mid - 1;
   }
   return -1;
}
int get_max_profit(Job job_list[], int n) {
   sort(job_list, job_list + n, jobComparator);
   job_with_weight table[n];
   table[0].value = job_list[0].profit;
   table[0].job.push_back(job_list[0]);
   for (int i = 1; i < n; i++) {
      int include_profit = job_list[i].profit;
      int l = find_no_conflict(job_list, i);
      if (l != - 1)
         include_profit += table[l].value;
      if (include_profit > table[i - 1].value){
         table[i].value = include_profit;
         table[i].job = table[l].job;
         table[i].job.push_back(job_list[i]);
      }
      else
         table[i] = table[i - 1];
   }
   cout << "[";
   for (int i=0; i<table[n-1].job.size(); i++) {
      Job j = table[n-1].job[i];
      cout << "(" << j.start << ", " << j.finish << ", " << j.profit << "),";
   }
   cout << "]\nOptimal profit: " << table[n - 1].value;
}
int main() {
   Job arr[] = {{2, 3, 55},{4, 6, 25},{7, 20, 150},{3, 150, 250}};
   int n = sizeof(arr)/sizeof(arr[0]);
   get_max_profit(arr, n);
}

Input

{{2, 3, 55},{4, 6, 25},{7, 20, 150},{3, 150, 250}}

Output

[(2, 3, 55),(3, 150, 250),]
Optimal profit: 305

Updated on: 25-Aug-2020

140 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements