0% found this document useful (0 votes)
2 views4 pages

Weighted

The document presents a C++ implementation of the weighted interval scheduling problem, which aims to maximize profit from a set of jobs with given start and finish times. It includes a structure to define jobs, a function to find the latest non-conflicting job, and a main function that calculates the maximum profit using dynamic programming. The example provided demonstrates the algorithm with a set of jobs and outputs the optimal profit.

Uploaded by

asthya44
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views4 pages

Weighted

The document presents a C++ implementation of the weighted interval scheduling problem, which aims to maximize profit from a set of jobs with given start and finish times. It includes a structure to define jobs, a function to find the latest non-conflicting job, and a main function that calculates the maximum profit using dynamic programming. The example provided demonstrates the algorithm with a set of jobs and outputs the optimal profit.

Uploaded by

asthya44
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

weighted interval scheduling

#include <algorithm>

#include <iostream>

using namespace std;

// A job has start time, finish time and profit.

struct Job {

int start, finish, profit;

};

bool jobComparator(Job s1, Job s2)

return (s1.finish < s2.finish);

// Find the latest job (in sorted array) that doesn't

// conflict with the job[i]

int latestNonConflict(Job arr[], int i)

for (int j = i - 1; j >= 0; j--) {

if (arr[j].finish <= arr[i].start)


return j;

return -1;

// The main function that returns the maximum possible

// profit from given array of jobs

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.

// table[i] stores the profit for jobs till arr[i]

// (including arr[i])

int* table = new int[n];

table[0] = arr[0].profit;

// Fill entries in M[] 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 += table[l];

// Store maximum of including and excluding

table[i] = max(inclProf, table[i - 1]);

// Store result and free dynamic memory allocated for

// table[]

int result = table[n - 1];

delete[] table;

return result;

// Driver program

int main()

Job arr[] = { { 3, 10, 20 },

{ 1, 2, 50 },

{ 6, 19, 100 },
{ 2, 100, 200 } };

int n = sizeof(arr) / sizeof(arr[0]);

cout << "The optimal profit is "

<< findMaxProfit(arr, n);

return 0;

You might also like