The Wayback Machine - https://web.archive.org/web/20240910201818/https://www.geeksforgeeks.org/job-sequencing-problem/
Open In App

Job Sequencing Problem

Last Updated : 23 Feb, 2023
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow
Companies:
Show Topics
Solve Problem
Medium
34.51%
2.5L

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 a single unit of time, so the minimum possible deadline for any job is 1. Maximize the 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

Recommended Practice

Naive Approach: To solve the problem follow the below idea:

Generate all subsets of a given set of jobs and check individual subsets for the feasibility of jobs in that subset. Keep track of maximum profit among all feasible subsets.

Greedy approach for job sequencing problem:

Greedily choose the jobs with maximum profit first, by sorting the jobs in decreasing order of their profit. This would help to maximize the total profit as choosing the job with maximum profit for every time slot will eventually maximize the total profit

Follow the given steps to solve the problem:

  • Sort all jobs in decreasing order of profit. 
  • Iterate on jobs in decreasing order of profit.For each job , do the following : 
    • Find a time slot i, such that slot is empty and i < deadline and i is greatest.Put the job in 
      this slot and mark this slot filled. 
    • If no such i exists, then ignore the job. 

Below is the implementation of the above approach:

C




// C program for the above approach
 
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
 
// A structure to represent a job
typedef struct Job {
   
    char id; // Job Id
    int dead; // Deadline of job
    int profit; // Profit if job is over before or on
                // deadline
} Job;
 
// This function is used for sorting all jobs according to
// profit
int compare(const void* a, const void* b)
{
    Job* temp1 = (Job*)a;
    Job* temp2 = (Job*)b;
    return (temp2->profit - temp1->profit);
}
 
// Find minimum between two numbers.
int min(int num1, int num2)
{
    return (num1 > num2) ? num2 : num1;
}
 
// Returns maximum profit from jobs
void printJobScheduling(Job arr[], int n)
{
    // Sort all jobs according to decreasing order of profit
    qsort(arr, n, sizeof(Job), compare);
 
    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])
            printf("%c ", arr[result[i]].id);
}
 
// Driver's code
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]);
    printf(
        "Following is maximum profit sequence of jobs \n");
 
    // Function call
    printJobScheduling(arr, n);
    return 0;
}
 
// This code is contributed by Aditya Kumar (adityakumar129)


C++




// C++ code for the above approach
 
#include <algorithm>
#include <iostream>
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
};
 
// Comparator function for sorting jobs
bool comparison(Job a, Job b)
{
    return (a.profit > b.profit);
}
 
// Returns maximum profit from jobs
void printJobScheduling(Job arr[], int n)
{
    // Sort all jobs according to decreasing order of profit
    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's code
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 jobs "
            "\n";
 
    // Function call
    printJobScheduling(arr, n);
    return 0;
}
 
// This code is contributed by Aditya Kumar (adityakumar129)


Java




// Java code for the above approach
 
import java.util.*;
 
class Job {
   
    // Each job has a unique-id,profit and deadline
    char id;
    int deadline, profit;
 
    // Constructors
    public Job() {}
 
    public Job(char id, int deadline, int profit)
    {
        this.id = id;
        this.deadline = deadline;
        this.profit = profit;
    }
 
    // Function to schedule the jobs take 2 arguments
    // arraylist and no of jobs to schedule
    void printJobScheduling(ArrayList<Job> arr, int t)
    {
        // Length of array
        int n = arr.size();
       
        // Sort all jobs according to decreasing order of
        // profit
        Collections.sort(arr,
                         (a, b) -> b.profit - a.profit);
 
        // To keep track of free time slots
        boolean result[] = new boolean[t];
 
        // To store result (Sequence of jobs)
        char job[] = new char[t];
 
        // 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
                 = Math.min(t - 1, arr.get(i).deadline - 1);
                 j >= 0; j--) {
                // Free slot found
                if (result[j] == false) {
                    result[j] = true;
                    job[j] = arr.get(i).id;
                    break;
                }
            }
        }
 
        // Print the sequence
        for (char jb : job)
            System.out.print(jb + " ");
        System.out.println();
    }
 
    // Driver's code
    public static void main(String args[])
    {
        ArrayList<Job> arr = new ArrayList<Job>();
        arr.add(new Job('a', 2, 100));
        arr.add(new Job('b', 1, 19));
        arr.add(new Job('c', 2, 27));
        arr.add(new Job('d', 1, 25));
        arr.add(new Job('e', 3, 15));
 
        System.out.println(
            "Following is maximum profit sequence of jobs");
 
        Job job = new Job();
 
        // Function call
        job.printJobScheduling(arr, 3);
    }
}
 
// This code is contributed by Aditya Kumar (adityakumar129)


Python3




# Python3 code for the above approach
 
# 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's Code
if __name__ == '__main__':
    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")
 
    # Function Call
    printJobScheduling(arr, 3)
 
# This code is contributed
# by Anubhav Raj Singh


C#




// C# Program for the above approach
 
using System;
using System.Collections.Generic;
 
class GFG : IComparer<Job> {
    public int Compare(Job x, Job y)
    {
        if (x.profit == 0 || y.profit == 0) {
            return 0;
        }
 
        // CompareTo() method
        return (y.profit).CompareTo(x.profit);
    }
}
 
public class Job {
 
    // Each job has a unique-id,
    // profit and deadline
    char id;
    public int deadline, profit;
 
    // Constructors
    public Job() {}
 
    public Job(char id, int deadline, int profit)
    {
        this.id = id;
        this.deadline = deadline;
        this.profit = profit;
    }
 
    // Function to schedule the jobs take 2
    // arguments arraylist and no of jobs to schedule
    void printJobScheduling(List<Job> arr, int t)
    {
        // Length of array
        int n = arr.Count;
 
        GFG gg = new GFG();
        // Sort all jobs according to
        // decreasing order of profit
        arr.Sort(gg);
 
        // To keep track of free time slots
        bool[] result = new bool[t];
 
        // To store result (Sequence of jobs)
        char[] job = new char[t];
 
        // 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
                 = Math.Min(t - 1, arr[i].deadline - 1);
                 j >= 0; j--) {
 
                // Free slot found
                if (result[j] == false) {
                    result[j] = true;
                    job[j] = arr[i].id;
                    break;
                }
            }
        }
 
        // Print the sequence
        foreach(char jb in job) { Console.Write(jb + " "); }
        Console.WriteLine();
    }
 
    // Driver's code
    static public void Main()
    {
 
        List<Job> arr = new List<Job>();
 
        arr.Add(new Job('a', 2, 100));
        arr.Add(new Job('b', 1, 19));
        arr.Add(new Job('c', 2, 27));
        arr.Add(new Job('d', 1, 25));
        arr.Add(new Job('e', 3, 15));
 
        Console.WriteLine("Following is maximum "
                          + "profit sequence of jobs");
 
        Job job = new Job();
 
        // Function call
        job.printJobScheduling(arr, 3);
    }
}
 
// This code is contributed by avanitracchadiya2155.


Javascript




// 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
 
function printJobScheduling(arr, t){
    // length of array
    let n = arr.length;
 
    // Sort all jobs according to
    // decreasing order of profit
    for(let i=0;i<n;i++){
        for(let j = 0;j<(n - 1 - i);j++){
            if(arr[j][2] < arr[j + 1][2]){
                let temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
            }
         }
     }
 
    // To keep track of free time slots
    let result = [];
 
    // To store result (Sequence of jobs)
    let job = [];
    for(let i = 0;i<t;i++){
        job[i] = '-1';
        result[i] = false;
    }
 
    // Iterate through all given jobs
    for(let i= 0;i<arr.length;i++){
        // Find a free slot for this job
        // (Note that we start from the
        // last possible slot)
        for(let j = (t - 1, arr[i][1] - 1);j>=0;j--){
            // Free slot found
            if(result[j] == false){
                result[j] = true;
                job[j] = arr[i][0];
                break;
            }
        }
    }
 
    // print the sequence
    document.write(job);
}
 
// Driver COde
arr = [['a', 2, 100],  // Job Array
       ['b', 1, 19],
       ['c', 2, 27],
       ['d', 1, 25],
       ['e', 3, 15]];
 
document.write("Following is maximum profit sequence of jobs ");
document.write("<br>");
 
// Function Call
printJobScheduling(arr, 3) ;


Output

Following is maximum profit sequence of jobs 
c a e 

Time Complexity: O(N2)
Auxiliary Space: O(N)

Job sequencing problem using Priority-Queue (Max-Heap):

Sort the jobs in the increasing order of their deadlines and then calculate the available slots between every two consecutive deadlines while iterating from the end. Include the profit of the job at the root of the Max-Heap while the empty slots are available and Heap is not empty, as this would help to choose the jobs with maximum profit for every set of available slots.

Follow the given steps to solve the problem:

  • Sort the jobs based on their deadlines.
  • Iterate from the end and calculate the available slots between every two consecutive deadlines. Insert the profit, deadline, and job ID of ith job in the max heap.
  • While the slots are available and there are jobs left in the max heap, include the job ID with maximum profit and deadline in the result.
  • Sort the result array based on their deadlines.

Below is the implementation of the above approach:

C++




// C++ code for the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// A structure to represent a job
struct Job {
   
    char id; // Job Id
    int dead; // Deadline of job
    int profit; // Profit earned if job is completed before
                // deadline
};
 
// Custom sorting helper struct which is used for sorting
// all jobs according to profit
struct jobProfit {
    bool operator()(Job const& a, Job const& b)
    {
        return (a.profit < b.profit);
    }
};
 
// Returns maximum profit from jobs
void printJobScheduling(Job arr[], int n)
{
    vector<Job> result;
    sort(arr, arr + n,
         [](Job a, Job b) { return a.dead < b.dead; });
   
    // set a custom priority queue
    priority_queue<Job, vector<Job>, jobProfit> pq;
   
    for (int i = n - 1; i >= 0; i--) {
        int slot_available;
       
        // we count the slots available between two jobs
        if (i == 0) {
            slot_available = arr[i].dead;
        }
        else {
            slot_available = arr[i].dead - arr[i - 1].dead;
        }
       
        // include the profit of job(as priority),
        // deadline and job_id in maxHeap
        pq.push(arr[i]);
       
        while (slot_available > 0 && pq.size() > 0) {
           
            // get the job with the most profit
            Job job = pq.top();
            pq.pop();
           
            // reduce the slots
            slot_available--;
           
            // add it to the answer
            result.push_back(job);
        }
    }
   
    // sort the result based on the deadline
    sort(result.begin(), result.end(),
         [&](Job a, Job b) { return a.dead < b.dead; });
   
    // print the result
    for (int i = 0; i < result.size(); i++)
        cout << result[i].id << ' ';
    cout << endl;
}
 
// Driver's code
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 jobs "
            "\n";
 
    // Function call
    printJobScheduling(arr, n);
    return 0;
}
 
// This code is contributed By Reetu Raj Dubey


Java




// Java implementation of above approach
 
// Program to find the maximum profit
// job sequence from a given array
// of jobs with deadlines and profits
import java.util.*;
 
public class GFG {
 
    // a class to represent job
    static class Job {
        char job_id;
        int deadline;
        int profit;
        Job(char job_id, int deadline, int profit)
        {
            this.deadline = deadline;
            this.job_id = job_id;
            this.profit = profit;
        }
    }
 
    static void printJobScheduling(ArrayList<Job> arr)
    {
        int n = arr.size();
 
        // sorting the array on the
        // basis of their deadlines
        Collections.sort(arr, (a, b) -> {
            return a.deadline - b.deadline;
        });
 
        // initialise the result array and maxHeap
        ArrayList<Job> result = new ArrayList<>();
        PriorityQueue<Job> maxHeap = new PriorityQueue<>(
            (a, b) -> { return b.profit - a.profit; });
 
        // starting the iteration from the end
        for (int i = n - 1; i > -1; i--) {
            int slot_available;
           
            // calculate slots between two deadlines
            if (i == 0) {
                slot_available = arr.get(i).deadline;
            }
            else {
                slot_available = arr.get(i).deadline
                                 - arr.get(i - 1).deadline;
            }
 
            // include the profit of job(as priority),
            // deadline and job_id in maxHeap
            maxHeap.add(arr.get(i));
 
            while (slot_available > 0
                   && maxHeap.size() > 0) {
 
                // get the job with max_profit
                Job job = maxHeap.remove();
 
                // reduce the slots
                slot_available--;
 
                // include the job in the result array
                result.add(job);
            }
        }
 
        // jobs included might be shuffled
        // sort the result array by their deadlines
        Collections.sort(result, (a, b) -> {
            return a.deadline - b.deadline;
        });
       
        for (Job job : result) {
            System.out.print(job.job_id + " ");
        }
       
        System.out.println();
    }
 
    // Driver's Code
    public static void main(String[] args)
    {
        ArrayList<Job> arr = new ArrayList<Job>();
 
        arr.add(new Job('a', 2, 100));
        arr.add(new Job('b', 1, 19));
        arr.add(new Job('c', 2, 27));
        arr.add(new Job('d', 1, 25));
        arr.add(new Job('e', 3, 15));
       
        System.out.println("Following is maximum "
                           + "profit sequence of jobs");
 
        // Function call
        printJobScheduling(arr);
    }
}
 
// This code is contributed by Karandeep Singh


Python3




# Python3 program for the above approach
import heapq
 
 
def printJobScheduling(arr):
    n = len(arr)
 
    # arr[i][0] = job_id, arr[i][1] = deadline, arr[i][2] = profit
 
    # sorting the array on the
    # basis of their deadlines
    arr.sort(key=lambda x: x[1])
 
    # initialise the result array and maxHeap
    result = []
    maxHeap = []
 
    # starting the iteration from the end
    for i in range(n - 1, -1, -1):
 
        # calculate slots between two deadlines
        if i == 0:
            slots_available = arr[i][1]
        else:
            slots_available = arr[i][1] - arr[i - 1][1]
 
        # include the profit of job(as priority), deadline
        # and job_id in maxHeap
        # note we push negative value in maxHeap to convert
        # min heap to max heap in python
        heapq.heappush(maxHeap, (-arr[i][2], arr[i][1], arr[i][0]))
 
        while slots_available and maxHeap:
 
            # get the job with max_profit
            profit, deadline, job_id = heapq.heappop(maxHeap)
 
            # reduce the slots
            slots_available -= 1
 
            # include the job in the result array
            result.append([job_id, deadline])
 
    # jobs included might be shuffled
    # sort the result array by their deadlines
    result.sort(key=lambda x: x[1])
 
    for job in result:
        print(job[0], end=" ")
    print()
 
 
# Driver's Code
if __name__ == '__main__':
    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")
 
    # Function Call
    printJobScheduling(arr)
 
# This code is contributed
# by Shivam Bhagat


C#




// C# implementation of the above approach
using System;
using System.Collections.Generic;
 
namespace GFG
{
   
  // A class to represent a job
  public class Job
  {
    public char JobId { get; set; }
    public int Deadline { get; set; }
    public int Profit { get; set; }
    public Job(char jobId, int deadline, int profit)
    {
      this.Deadline = deadline;
      this.JobId = jobId;
      this.Profit = profit;
    }
  }
 
  class Scheduling
  {
    static void PrintJobScheduling(List<Job> arr)
    {
      int n = arr.Count;
 
      // Sorting the array based on their deadlines
      arr.Sort((a, b) => b.Deadline.CompareTo(a.Deadline));
 
      // Initializing the result array
      List<Job> result = new List<Job>();
 
      // Starting the iteration from the end
      for (int i = n - 1; i >= 0; i--)
      {
        int slot_available;
 
        // Calculating the slots between two deadlines
        if (i == 0)
        {
          slot_available = arr[i].Deadline;
        }
        else
        {
          slot_available = arr[i].Deadline - arr[i - 1].Deadline;
        }
 
        // Including the job with max profit
        Job job = null;
        int maxProfit = -1;
        for (int j = i; j >= 0; j--)
        {
          if (arr[j].Deadline >= slot_available && arr[j].Profit > maxProfit)
          {
            job = arr[j];
            maxProfit = arr[j].Profit;
          }
        }
 
        if (job != null)
        {
          slot_available--;
          result.Add(job);
          arr.Remove(job);
          i--;
        }
      }
 
      // Jobs included might be shuffled
      // Sorting the result array based on their deadlines
      result.Sort((a, b) => a.Deadline.CompareTo(b.Deadline));
 
      foreach (Job job in result)
      {
        Console.Write(job.JobId + " ");
      }
      Console.WriteLine();
    }
 
    // Driver Code
    static void Main(string[] args)
    {
      List<Job> arr = new List<Job>
      {
        new Job('a', 2, 100),
        new Job('b', 1, 19),
        new Job('c', 2, 27),
        new Job('d', 1, 25),
        new Job('e', 3, 15)
        };
 
      Console.WriteLine("Following is maximum profit sequence of jobs");
 
      // Function call
      PrintJobScheduling(arr);
    }
  }
}
 
// This code is contributed by phasing17.


Javascript




// JS implementation of the above approach
 
// A class to represent a job
class Job {
constructor(jobId, deadline, profit) {
this.JobId = jobId;
this.Deadline = deadline;
this.Profit = profit;
}
}
 
function PrintJobScheduling(arr) {
  let n = arr.length;
 
  // Sorting the array based on their deadlines
  arr.sort((a, b) => b.Deadline - a.Deadline);
 
  // Initializing the result array
  let result = [];
 
  // Starting the iteration from the end
  let i;
  for (i = n - 1; i >= 0; i--) {
    let slot_available;
 
    // Calculating the slots between two deadlines
    if (i == 0) {
      slot_available = arr[i].Deadline;
    } else {
      slot_available = arr[i].Deadline - arr[i - 1].Deadline;
    }
 
    // Including the job with max profit
    let job ;
    let maxProfit = -1;
    for (let j = i; j >= 0; j--) {
      if (arr[j].Deadline >= slot_available && arr[j].Profit > maxProfit) {
        job = arr[j];
        maxProfit = arr[j].Profit;
      }
    }
 
    if (job != null) {
      slot_available--;
      result.push(job);
      arr.splice(arr.indexOf(job), 1);
      i--;
      // Remove the job from the input list
    }
  }
 
  // Jobs included might be shuffled
  // Sorting the result array based on their deadlines
  result.sort((a, b) => a.Deadline - b.Deadline);
 
  const output = result.map(job => job.JobId).join(" ");
  console.log(output);
}
 
 
 
// Driver Code
let arr = [
new Job('a', 2, 100),
new Job('b', 1, 19),
new Job('c', 2, 27),
new Job('d', 1, 25),
new Job('e', 3, 15)
];
 
console.log("Following is maximum profit sequence of jobs");
 
// Function call
PrintJobScheduling(arr);
 
// This code is contributed by phasing17.


Output

Following is maximum profit sequence of jobs
a c e 

Time Complexity: O(N log N)
Auxiliary Space: O(N)

It can also be optimized using Disjoint Set Data Structure. Please refer to the below post for details.
Job Sequencing Problem | Set 2 (Using Disjoint Set)
 



Previous Article
Next Article

Similar Reads

Job Sequencing Problem - Loss Minimization
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
8 min read
Job Sequencing Problem using Disjoint Set
Given a set of n jobs where each job i has a deadline di &gt;=1 and profit pi&gt;=0. Only one job can be scheduled at a time. Each job takes 1 unit of time to complete. We earn the profit if and only if the job is completed by its deadline. The task is to find the subset of jobs that maximizes profit. Examples: Input: Four Jobs with following deadl
15 min read
Maximizing merit points by sequencing activities
There are n activities that a person can perform. Each activity has a corresponding merit point for each of the n days. However, the person cannot perform the same activity on two consecutive days. The goal is to find a sequence of activities that maximizes the total merit points earned over the n days. Input: n = 3, point= [[1, 2, 5], [3, 1, 1], [
12 min read
Sum of count of persons still performing the job whenever a person finishes job
Given two integers P denoting the number of people and a positive integer K. Each person is assigned with the same job which takes exactly K hours to finish. A condition is given that all of them can start performing their jobs exactly in X hours of interval. Count the number of persons still performing the job whenever a prior person finishes his/
6 min read
Job Selection Problem - Loss Minimization Strategy | Set 2
We have discussed one loss minimization strategy before: Job Sequencing Problem – Loss Minimization. In this article, we will look at another strategy that applies to a slightly different problem.We are given a sequence of N goods of production numbered from 1 to N. Each good has a volume denoted by (Vi). The constraint is that once a good has been
9 min read
Job Assignment Problem using Branch And Bound
Let there be N workers and N jobs. Any worker can be assigned to perform any job, incurring some cost that may vary depending on the work-job assignment. It is required to perform all jobs by assigning exactly one worker to each job and exactly one job to each agent in such a way that the total cost of the assignment is minimized. Let us explore al
15+ min read
Job Selection Problem in Python
Given N jobs, numbered from 1 to N, let T[i] represent the total number of days needed to finish each task and L[i] is lost for each day that is spent waiting to begin work on job i. The task is to determine the sequence to complete the jobs such that the overall loss is minimized. We can only work on one job at a time. If multiple sequences yield
3 min read
Secretary Problem (A Optimal Stopping Problem)
The Secretary Problem also known as marriage problem, the sultan's dowry problem, and the best choice problem is an example of Optimal Stopping Problem. This problem can be stated in the following form: Imagine an administrator who wants to hire the best secretary out of n rankable applicants for a position. The applicants are interviewed one by on
12 min read
Transportation Problem | Set 7 ( Degeneracy in Transportation Problem )
Please go through this article first. This article will discuss degeneracy in transportation problem through an explained example. Solution: This problem is balanced transportation problem as total supply is equal to total demand. Initial basic feasible solution: Least Cost Cell Method will be used here to find the initial basic feasible solution.
3 min read
Nuts & Bolts Problem (Lock & Key problem) using Quick Sort
Given a set of n nuts of different sizes and n bolts of different sizes. There is a one-one mapping between nuts and bolts. Match nuts and bolts efficiently. Constraint: Comparison of a nut to another nut or a bolt to another bolt is not allowed. It means a nut can only be compared with a bolt and a bolt can only be compared with a nut to see which
12 min read
Nuts & Bolts Problem (Lock & Key problem) using Hashmap
Given a set of n nuts of different sizes and n bolts of different sizes. There is a one-one mapping between nuts and bolts. Match nuts and bolts efficiently. Constraint: Comparison of a nut to another nut or a bolt to another bolt is not allowed. It means nut can only be compared with bolt and bolt can only be compared with a nut to see which one i
5 min read
Difference between 0/1 Knapsack problem and Fractional Knapsack problem
What is Knapsack Problem?Suppose you have been given a knapsack or bag with a limited weight capacity, and each item has some weight and value. The problem here is that "Which item is to be placed in the knapsack such that the weight limit does not exceed and the total value of the items is as large as possible?". Consider the real-life example. Su
13 min read
Implementation of Non-Preemptive Shortest Job First using Priority Queue
Read here for Shortest Job First Scheduling algorithm for same arrival times.Shortest job first (SJF) or shortest job next, is a scheduling policy that selects the waiting process with the smallest execution time to execute next.In this article, we will implement the Shortest Job First Scheduling algorithm (SJF) using a priority queue, so that we c
12 min read
Weighted Job Scheduling
Given N jobs where every job is represented by following three elements of it. Start TimeFinish TimeProfit or Value Associated (&gt;= 0) Find the maximum profit subset of jobs such that no two jobs in the subset overlap. Example: 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,
15 min read
Weighted Job Scheduling in O(n Log n) time
Given N jobs where every job is represented by following three elements of it. Start TimeFinish TimeProfit or Value Associated Find the maximum profit subset of jobs such that no two jobs in the subset overlap.Example: 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} J
11 min read
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 AssociatedFind 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
11 min read
Weighted Job Scheduling | Set 2 (Using LIS)
Given N jobs where every job is represented by following three elements of it.1. Start Time 2. Finish Time 3. Profit or Value AssociatedFind the maximum profit subset of jobs 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,
10 min read
Program for Shortest Job First (or SJF) CPU Scheduling | Set 1 (Non- preemptive)
The shortest job first (SJF) or shortest job next, is a scheduling policy that selects the waiting process with the smallest execution time to execute next. SJN, also known as Shortest Job Next (SJN), can be preemptive or non-preemptive. Characteristics of SJF Scheduling: Shortest Job first has the advantage of having a minimum average waiting time
13 min read
Job Scheduling with two jobs allowed at a time
We are given N jobs, and their starting and ending times. We can do two jobs simultaneously at a particular moment. If one job ends at the same moment some other show starts then we can't do them. We need to check if it is possible to complete all the jobs or not.Examples: Input : Start and End times of Jobs 1 2 2 3 4 5 Output : Yes By the time thi
7 min read
Shortest Job First (or SJF) CPU Scheduling Non-preemptive algorithm using Segment Tree
Shortest job first (SJF) or shortest job next, is a scheduling policy that selects the waiting process with the smallest execution time to execute next. SJN is a non-preemptive algorithm.   Shortest Job first has the advantage of having a minimum average waiting time among all scheduling algorithms.It is a Greedy Algorithm.It may cause starvation i
15+ min read
How can competitive programming help you get a job?
Competitive programming is a brain game that takes place on the Internet or a local network in which programmers have to code according to the given constraints. Here programmers are referred to as competitive coders. Many top-notch companies like Google, and Facebook host contests like Codejam and Hackercup respectively. Those who perform well in
4 min read
Implementation of Shortest Job First (SJF) Scheduling in Python
Shortest Job First (SJF) scheduling is a non-preemptive CPU scheduling algorithm where the process with the smallest burst time is selected for execution from the ready queue. The objective is to minimize the average waiting time of processes. Example 1: Consider the following processes with their burst times: ProcessBurst TimeP16P28P37P43Output: A
2 min read
Minimum time taken by each job to be completed given by a Directed Acyclic Graph
Given a Directed Acyclic Graph having V vertices and E edges, where each edge {U, V} represents the Jobs U and V such that Job V can only be started only after completion of Job U. The task is to determine the minimum time taken by each job to be completed where each Job takes unit time to get completed.Examples: Input: N = 10, E = 13, Below is the
12 min read
Learn Data Structures and Algorithms for Your Dream Job
According to a study by employability assessment company Aspiring Minds in 2023, only 4.77 percent of candidates can write the correct logic for a program — a minimum requirement for any programming job. Another survey shows that only 7% of the engineering graduates in India are suitable for core engineering jobs. What could be the root cause of th
8 min read
8 queen problem
The eight queens problem is the problem of placing eight queens on an 8×8 chessboard such that none of them attack one another (no two are in the same row, column, or diagonal). More generally, the n queens problem places n queens on an n×n chessboard. There are different solutions for the problem. Backtracking | Set 3 (N Queen Problem) Branch and
8 min read
The Great Tree-List Recursion Problem.
Asked by Varun Bhatia. Question: Write a recursive function treeToList(Node root) that takes an ordered binary tree and rearranges the internal pointers to make a circular doubly linked list out of the tree nodes. The”previous” pointers should be stored in the “small” field and the “next” pointers should be stored in the “large” field. The list sho
1 min read
Custom Tree Problem
You are given a set of links, e.g. a ---&gt; b b ---&gt; c b ---&gt; d a ---&gt; e Print the tree that would form when each pair of these links that has the same character as start and end point is joined together. You have to maintain fidelity w.r.t. the height of nodes, i.e. nodes at height n from root should be printed at same row or column. For
13 min read
Stable Marriage Problem
The Stable Marriage Problem states that given N men and N women, where each person has ranked all members of the opposite sex in order of preference, marry the men and women together such that there are no two people of opposite sex who would both rather have each other than their current partners. If there are no such people, all the marriages are
15+ min read
Approximate solution for Travelling Salesman Problem using MST
We introduced Travelling Salesman Problem and discussed Naive and Dynamic Programming Solutions for the problem in the previous post,. Both of the solutions are infeasible. In fact, there is no polynomial time solution available for this problem as the problem is a known NP-Hard problem. There are approximate algorithms to solve the problem though.
3 min read
Channel Assignment Problem
There are M transmitter and N receiver stations. Given a matrix that keeps track of the number of packets to be transmitted from a given transmitter to a receiver. If the (i; j)-th entry of the matrix is k, it means at that time the station i has k packets for transmission to station j. During a time slot, a transmitter can send only one packet and
14 min read
Article Tags :
Practice Tags :