Job Sequencing Problem – Loss Minimization
Last Updated :
27 Mar, 2023
We are given N jobs numbered 1 to N. For each activity, let Ti denotes the number of days required to complete the job. For each day of delay before starting to work for job i, a loss of Li is incurred. We are required to find a sequence to complete the jobs so that overall loss is minimized. We can only work on one job at a time. If multiple such solutions are possible, then we are required to give the lexicographically least permutation (i.e earliest in dictionary order). Examples:
Input : L = {3, 1, 2, 4} and
T = {4, 1000, 2, 5}
Output : 3, 4, 1, 2
Explanation: We should first complete
job 3, then jobs 4, 1, 2 respectively.
Input : L = {1, 2, 3, 5, 6}
T = {2, 4, 1, 3, 2}
Output : 3, 5, 4, 1, 2
Explanation: We should complete jobs
3, 5, 4, 1 and then 2 in this order.
Let us consider two extreme cases and we shall deduce the general case solution from them.
- All jobs take same time to finish, i.e Ti = k for all i. Since all jobs take same time to finish we should first select jobs which have large Loss (Li). We should select jobs which have the highest losses and finish them as early as possible. Thus this is a greedy algorithm. Sort the jobs in descending order based on Li only.
- All jobs have the same penalty. Since all jobs have the same penalty we will do those jobs first which will take less amount of time to finish. This will minimize the total delay, and hence also the total loss incurred. This is also a greedy algorithm. Sort the jobs in ascending order based on Ti. Or we can also sort in descending order of 1/Ti.
From the above cases, we can easily see that we should sort the jobs not on the basis of Li or Ti alone. Instead, we should sort the jobs according to the ratio Li/Ti, in descending order.
We can get the lexicographically smallest permutation of jobs if we perform a stable sort on the jobs. An example of a stable sort is merge sort.
To get most accurate result avoid dividing Li by Ti. Instead, compare the two ratios like fractions. To compare a/b and c/d, compare ad and bc.
C++
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
#define all(c) c.begin(), c.end()
typedef pair< int , pair< int , int > > job;
bool cmp_pair(job a, job b)
{
int a_Li, a_Ti, b_Li, b_Ti;
a_Li = a.second.first;
a_Ti = a.second.second;
b_Li = b.second.first;
b_Ti = b.second.second;
return (a_Li * b_Ti) > (b_Li * a_Ti);
}
void printOptimal( int L[], int T[], int N)
{
vector<job> list;
for ( int i = 0; i < N; i++) {
int t = T[i];
int l = L[i];
list.push_back(make_pair(i + 1, make_pair(l, t)));
}
stable_sort(all(list), cmp_pair);
cout << "Job numbers in optimal sequence are\n" ;
for ( int i = 0; i < N; i++)
cout << list[i].first << " " ;
}
int main()
{
int L[] = { 1, 2, 3, 5, 6 };
int T[] = { 2, 4, 1, 3, 2 };
int N = sizeof (L) / sizeof (L[0]);
printOptimal(L, T, N);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
public static class cmp_pair implements Comparator<job>
{
@Override
public int compare(job a, job b){
int a_Li, a_Ti, b_Li, b_Ti;
a_Li = a.li;
a_Ti = a.ti;
b_Li = b.li;
b_Ti = b.ti;
return (a_Li * b_Ti) > (b_Li * a_Ti)?- 1 : 1 ;
}
}
public static void printOptimal( int L[], int T[], int N)
{
List<job> list = new ArrayList<>();
for ( int i = 0 ; i < N; i++) {
int t = T[i];
int l = L[i];
list.add( new job(i + 1 , l, t));
}
Collections.sort(list, new cmp_pair());
System.out.println( "Job numbers in optimal sequence are" );
for ( int i = 0 ; i < N; i++)
System.out.print(list.get(i).index+ " " );
}
public static void main (String[] args) {
int L[] = { 1 , 2 , 3 , 5 , 6 };
int T[] = { 2 , 4 , 1 , 3 , 2 };
int N = L.length;
printOptimal(L, T, N);
}
}
class job{
int index,ti,li;
job( int i, int l, int t){
this .index=i;
this .ti=t;
this .li=l;
}
}
|
Python3
import functools
import operator
from typing import List
class cmp_pair:
def __call__( self , a, b):
a_Li, a_Ti, b_Li, b_Ti = a.li, a.ti, b.li, b.ti
return - 1 if (a_Li * b_Ti) > (b_Li * a_Ti) else 1
class job:
def __init__( self , i, l, t):
self .index, self .ti, self .li = i, t, l
def printOptimal(L: List [ int ], T: List [ int ], N: int ) - > None :
job_list = []
for i in range (N):
t, l = T[i], L[i]
job_list.append(job(i + 1 , l, t))
job_list.sort(key = functools.cmp_to_key(cmp_pair()))
print ( "Job numbers in optimal sequence are" )
for i in range (N):
print (job_list[i].index, end = " " )
L = [ 1 , 2 , 3 , 5 , 6 ]
T = [ 2 , 4 , 1 , 3 , 2 ]
N = len (L)
printOptimal(L, T, N)
|
Javascript
function cmp_pair(a, b) {
const a_Li = a.li, a_Ti = a.ti, b_Li = b.li, b_Ti = b.ti;
return (a_Li * b_Ti) > (b_Li * a_Ti) ? -1 : 1;
}
class job {
constructor(i, l, t) {
this .index = i;
this .ti = t;
this .li = l;
}
}
function printOptimal(L, T, N) {
const job_list = [];
for (let i = 0; i < N; i++) {
const t = T[i], l = L[i];
job_list.push( new job(i + 1, l, t));
}
job_list.sort(cmp_pair);
console.log( "Job numbers in optimal sequence are" );
for (let i = 0; i < N; i++) {
process.stdout.write(job_list[i].index + " " );
}
}
const L = [1, 2, 3, 5, 6];
const T = [2, 4, 1, 3, 2];
const N = L.length;
printOptimal(L, T, N);
|
C#
<div id= "highlighter_942247" class = "syntaxhighlighter nogutter " ><table border= "0" cellpadding= "0" cellspacing= "0" ><tbody><tr><td class = "code" ><div class = "container" ><div class = "line number1 index0 alt2" ><code class = "comments" >
|
Output
Job numbers in optimal sequence are
3 5 4 1 2
Time Complexity: O(N log N)
Space Complexity: O(N)
Share your thoughts in the comments
Please Login to comment...