# Find Jobs involved in Weighted Job Scheduling

Given N jobs where every job is represented by following three elements of it.

1. Start Time
2. Finish Time
3. Profit or Value Associated

Find the subset of jobs associated with maximum profit such that no two jobs in the subset overlap.

Examples:

```Input:
Number of Jobs n = 4
Job Details {Start Time, Finish Time, Profit}
Job 1:  {1, 2, 50}
Job 2:  {3, 5, 20}
Job 3:  {6, 19, 100}
Job 4:  {2, 100, 200}

Output:
Jobs involved in maximum profit are
Job 1:  {1, 2, 50}
Job 4:  {2, 100, 200}
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

In previous post, we have discussed about Weighted Job Scheduling problem. However, the post only covered code related to finding maximum profit. In this post, we will also print the jobs invloved in maximum profit.

Let arr[0..n-1] be the input array of Jobs. We define an array DP[] such that DP[i] stores Jobs involved to achieve maximum profit of array arr[0..i]. i.e. DP[i] stores solution to subproblem arr[0..i]. The rest of algorithm remains same as discussed in previous post.

Below is its C++ implementation –

 `// C++ program for weighted job scheduling using Dynamic ` `// Programming and Binary Search ` `#include ` `using` `namespace` `std; ` ` `  `// A job has start time, finish time and profit. ` `struct` `Job ` `{ ` `    ``int` `start, finish, profit; ` `}; ` ` `  `// to store subset of jobs ` `struct` `weightedJob ` `{ ` `    ``// vector of Jobs ` `    ``vector job; ` ` `  `    ``// find profit associated with included Jobs ` `    ``int` `value; ` `}; ` ` `  `// A utility function that is used for sorting events ` `// according to finish time ` `bool` `jobComparator(Job s1, Job s2) ` `{ ` `    ``return` `(s1.finish < s2.finish); ` `} ` ` `  `// A Binary Search based function to find the latest job ` `// (before current job) that doesn't conflict with current ` `// job. "index" is index of the current job. This function ` `// returns -1 if all jobs before index conflict with it. The ` `// array jobs[] is sorted in increasing order of finish time ` `int` `latestNonConflict(Job jobs[], ``int` `index) ` `{ ` `    ``// Initialize 'lo' and 'hi' for Binary Search ` `    ``int` `lo = 0, hi = index - 1; ` ` `  `    ``// Perform binary Search iteratively ` `    ``while` `(lo <= hi) ` `    ``{ ` `        ``int` `mid = (lo + hi) / 2; ` `        ``if` `(jobs[mid].finish <= jobs[index].start) ` `        ``{ ` `            ``if` `(jobs[mid + 1].finish <= jobs[index].start) ` `                ``lo = mid + 1; ` `            ``else` `                ``return` `mid; ` `        ``} ` `        ``else` `            ``hi = mid - 1; ` `    ``} ` ` `  `    ``return` `-1; ` `} ` ` `  `// The main function that finds the subset of jobs ` `// associated with maximum profit such that no two ` `// jobs in the subset overlap. ` `int` `findMaxProfit(Job arr[], ``int` `n) ` `{ ` `    ``// Sort jobs according to finish time ` `    ``sort(arr, arr + n, jobComparator); ` ` `  `    ``// Create an array to store solutions of subproblems. ` `    ``// DP[i] stores the Jobs involved and their total profit ` `    ``// till arr[i] (including arr[i]) ` `    ``weightedJob DP[n]; ` ` `  `    ``// initialize DP to arr ` `    ``DP.value = arr.profit; ` `    ``DP.job.push_back(arr); ` ` `  `    ``// Fill entries in DP[] using recursive property ` `    ``for` `(``int` `i = 1; i < n; i++) ` `    ``{ ` `        ``// Find profit including the current job ` `        ``int` `inclProf = arr[i].profit; ` `        ``int` `l = latestNonConflict(arr, i); ` `        ``if` `(l != - 1) ` `            ``inclProf += DP[l].value; ` ` `  `        ``// Store maximum of including and excluding ` `        ``if` `(inclProf > DP[i - 1].value) ` `        ``{ ` `            ``DP[i].value = inclProf; ` ` `  `            ``// including previous jobs and current job ` `            ``DP[i].job = DP[l].job; ` `            ``DP[i].job.push_back(arr[i]); ` ` `  `        ``} ` `        ``else` `            ``// excluding the current job ` `            ``DP[i] = DP[i - 1]; ` `    ``} ` ` `  `    ``// DP[n - 1] stores the result ` `    ``cout << ````"Optimal Jobs for maximum profits are "``` `; ` `    ``for` `(``int` `i=0; i

Output:

```Optimal Jobs for maximum profits are
(1, 2, 50)
(2, 100, 200)

Total Optimal profit is 250
```

## tags:

Dynamic Programming Dynamic Programming