# Job Sequencing Problem | Set 1 (Greedy Algorithm)

Given an array of jobs where every job has a deadline and associated profit if the job is finished before the deadline. It is also given that every job takes single unit of time, so the minimum possible deadline for any job is 1. How to maximize total profit if only one job can be scheduled at a time.

**Examples:**

Input: Four Jobs with following deadlines and profits JobID Deadline Profit a 4 20 b 1 10 c 1 40 d 1 30 Output: Following is maximum profit sequence of jobs c, a Input: Five Jobs with following deadlines and profits JobID Deadline Profit a 2 100 b 1 19 c 2 27 d 1 25 e 3 15 Output: Following is maximum profit sequence of jobs c, a, e

A **Simple Solution** is to generate all subsets of given set of jobs and check individual subset for feasibility of jobs in that subset. Keep track of maximum profit among all feasible subsets. The time complexity of this solution is exponential.

This is a standard Greedy Algorithm problem. Following is algorithm.

1) Sort all jobs in decreasing order of profit. 2) Initialize the result sequence as first job in sorted jobs. 3) Do following for remaining n-1 jobs .......a) If the current job can fit in the current result sequence without missing the deadline, add current job to the result. Else ignore the current job.

The Following is the implementation of above algorithm.

## C++

`// Program to find the maximum profit job sequence from a given array ` `// of jobs with deadlines and profits ` `#include<iostream> ` `#include<algorithm> ` `using` `namespace` `std; ` ` ` `// A structure to represent a job ` `struct` `Job ` `{ ` ` ` `char` `id; ` `// Job Id ` ` ` `int` `dead; ` `// Deadline of job ` ` ` `int` `profit; ` `// Profit if job is over before or on deadline ` `}; ` ` ` `// This function is used for sorting all jobs according to profit ` `bool` `comparison(Job a, Job b) ` `{ ` ` ` `return` `(a.profit > b.profit); ` `} ` ` ` `// Returns minimum number of platforms reqquired ` `void` `printJobScheduling(Job arr[], ` `int` `n) ` `{ ` ` ` `// Sort all jobs according to decreasing order of prfit ` ` ` `sort(arr, arr+n, comparison); ` ` ` ` ` `int` `result[n]; ` `// To store result (Sequence of jobs) ` ` ` `bool` `slot[n]; ` `// To keep track of free time slots ` ` ` ` ` `// Initialize all slots to be free ` ` ` `for` `(` `int` `i=0; i<n; i++) ` ` ` `slot[i] = ` `false` `; ` ` ` ` ` `// Iterate through all given jobs ` ` ` `for` `(` `int` `i=0; i<n; i++) ` ` ` `{ ` ` ` `// Find a free slot for this job (Note that we start ` ` ` `// from the last possible slot) ` ` ` `for` `(` `int` `j=min(n, arr[i].dead)-1; j>=0; j--) ` ` ` `{ ` ` ` `// Free slot found ` ` ` `if` `(slot[j]==` `false` `) ` ` ` `{ ` ` ` `result[j] = i; ` `// Add this job to result ` ` ` `slot[j] = ` `true` `; ` `// Make this slot occupied ` ` ` `break` `; ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` ` ` `// Print the result ` ` ` `for` `(` `int` `i=0; i<n; i++) ` ` ` `if` `(slot[i]) ` ` ` `cout << arr[result[i]].id << ` `" "` `; ` `} ` ` ` `// Driver program to test methods ` `int` `main() ` `{ ` ` ` `Job arr[] = { {` `'a'` `, 2, 100}, {` `'b'` `, 1, 19}, {` `'c'` `, 2, 27}, ` ` ` `{` `'d'` `, 1, 25}, {` `'e'` `, 3, 15}}; ` ` ` `int` `n = ` `sizeof` `(arr)/` `sizeof` `(arr[0]); ` ` ` `cout << ` `"Following is maximum profit sequence of jobsn"` `; ` ` ` `printJobScheduling(arr, n); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Program to find the maximum profit ` `# job sequence from a given array ` `# of jobs with deadlines and profits ` ` ` `# function to schedule the jobs take 2 ` `# arguments array and no of jobs to schedule ` `def` `printJobScheduling(arr, t): ` ` ` ` ` `# length of array ` ` ` `n ` `=` `len` `(arr) ` ` ` ` ` `# Sort all jobs according to ` ` ` `# decreasing order of profit ` ` ` `for` `i ` `in` `range` `(n): ` ` ` `for` `j ` `in` `range` `(n ` `-` `1` `-` `i): ` ` ` `if` `arr[j][` `2` `] < arr[j ` `+` `1` `][` `2` `]: ` ` ` `arr[j], arr[j ` `+` `1` `] ` `=` `arr[j ` `+` `1` `], arr[j] ` ` ` ` ` `# To keep track of free time slots ` ` ` `result ` `=` `[` `False` `] ` `*` `t ` ` ` ` ` `# To store result (Sequence of jobs) ` ` ` `job ` `=` `[` `'-1'` `] ` `*` `t ` ` ` ` ` `# Iterate through all given jobs ` ` ` `for` `i ` `in` `range` `(` `len` `(arr)): ` ` ` ` ` `# Find a free slot for this job ` ` ` `# (Note that we start from the ` ` ` `# last possible slot) ` ` ` `for` `j ` `in` `range` `(` `min` `(t ` `-` `1` `, arr[i][` `1` `] ` `-` `1` `), ` `-` `1` `, ` `-` `1` `): ` ` ` ` ` `# Free slot found ` ` ` `if` `result[j] ` `is` `False` `: ` ` ` `result[j] ` `=` `True` ` ` `job[j] ` `=` `arr[i][` `0` `] ` ` ` `break` ` ` ` ` `# print the sequence ` ` ` `print` `(job) ` ` ` `# Driver COde ` `arr ` `=` `[[` `'a'` `, ` `2` `, ` `100` `], ` `# Job Array ` ` ` `[` `'b'` `, ` `1` `, ` `19` `], ` ` ` `[` `'c'` `, ` `2` `, ` `27` `], ` ` ` `[` `'d'` `, ` `1` `, ` `25` `], ` ` ` `[` `'e'` `, ` `3` `, ` `15` `]] ` ` ` ` ` `print` `(` `"Following is maximum profit sequence of jobs"` `) ` `printJobScheduling(arr, ` `3` `) ` `# Function Call ` ` ` `# This code is contributed ` `# by Anubhav Raj Singh ` |

*chevron_right*

*filter_none*

**Output:**

Following is maximum profit sequence of jobs c a e

**Time Complexity** of the above solution is O(n^{2}). It can be optimized using Disjoint Set Data Structure. Please refer below post for details.

Job Sequencing Problem | Set 2 (Using Disjoint Set)

This article is contributed by **Shubham**. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

## Recommended Posts:

- K Centers Problem | Set 1 (Greedy Approximate Algorithm)
- Set Cover Problem | Set 1 (Greedy Approximate Algorithm)
- Job Sequencing Problem | Set 2 (Using Disjoint Set)
- Job Sequencing Problem - Loss Minimization
- Activity Selection Problem | Greedy Algo-1
- Greedy Algorithm for Egyptian Fraction
- Boruvka's algorithm | Greedy Algo-9
- Graph Coloring | Set 2 (Greedy Algorithm)
- Greedy Algorithm to find Minimum number of Coins
- Dijkstra's shortest path algorithm | Greedy Algo-7
- Kruskal’s Minimum Spanning Tree Algorithm | Greedy Algo-2
- Dijkstra’s Algorithm for Adjacency List Representation | Greedy Algo-8
- Correctness of Greedy Algorithms
- Top 20 Greedy Algorithms Interview Questions
- Huffman Coding | Greedy Algo-3