Download the BYJU'S Exam Prep App for free GATE/ESE preparation videos & tests - Download the BYJU'S Exam Prep App for free GATE/ESE preparation videos & tests -

# Job Sequencing with Deadlines

Job sequencing with deadlines is a problem that involves scheduling a set of jobs to maximize profit while adhering to their respective deadlines. This approach assumes that each job can be completed in exactly one unit of time. If jobs have different durations, a more advanced scheduling algorithm might be necessary. Also, if the deadlines are represented as relative time (e.g., time units after job release), the algorithm would require adjustments accordingly.

In this article, we explain Job Sequencing with Deadlines through algorithms like the Greedy method.

What is Job Sequencing with Deadlines?

The prime objective of the Job Sequencing with Deadlines algorithm is to complete the given order of jobs within respective deadlines, resulting in the highest possible profit. To achieve this, we are given a number of jobs, each associated with a specific deadline, and completing a job before its deadline earns us a profit. The challenge is to arrange these jobs in a way that maximizes our total profit.

It is not always possible to complete all of the assigned jobs within the deadlines. For each job, denoted as Ji, we have a deadline di and a profit pi associated with completing it on time. Our objective is to find the best solution that maximizes profit while still ensuring that the jobs are completed within their deadlines.

Here’s how Job Sequencing with Deadlines algorithm works:

Problem Setup

You’re given a list of jobs, where each job has a unique identifier (job_id), a deadline (by which the job should be completed), and a profit value (the benefit you gain by completing the job).

Sort the Jobs by Profit

To ensure we consider jobs with higher profits first, sort the jobs in non-increasing order based on their profit values.

Initialize the Schedule and Available Time Slots

Set up an array to represent the schedule. Initialize all elements to -1, indicating that no job has been assigned to any time slot. Also, create a boolean array to represent the availability of time slots, with all elements set to true initially.

Assign Jobs to Time Slots

Go through the sorted jobs one by one. For each job, find the latest available time slot just before its deadline. If such a time slot is available, assign the job to that slot. If not, skip the job.

Calculate Total Profit and Scheduled Jobs

Sum up the profits of all the scheduled jobs to get the total profit. Additionally, keep track of which job is assigned to each time slot.

Output the Results

Finally, display the total profit achieved and the list of jobs that have been scheduled.

Job Sequencing with Deadlines algorithm

Given jobs J(i) with deadline D(i) and profit P(i) for 0â‰¤iâ‰¤1, these jobs are arranged in descending order of profit p1â©¾p2â©¾p3â©¾…â©¾pn.

Job-Sequencing-With-Deadline (D, J, n, k)

D(0) := J(0) := 0

k:= 1

J(1) := 1 // means first job is selected

for i = 2 â€¦ n do

r:= k

while D(J(r)) > D(i) and D(J(r)) â‰  r do

r:= r â€“ 1

if D(J(r)) â‰¤ D(i) and D(i) > r then

for l = k â€¦ r + 1 by -1 do

J(l + 1): = J(l)

J(r + 1): = i

k:= k + 1

Job Sequencing with Deadlines using Greedy method

Job sequencing with deadlines is often solved using a Greedy algorithm approach, where jobs are selected based on their profitability and deadline constraints. The goal is to maximize the total profit by scheduling jobs in the most optimal manner.

Here’s a C code implementation of the Job Sequencing with Deadlines using Greedy method:

#include <stdio.h>

#include <stdlib.h>

struct Job {

int profit;

};

int compare_jobs(const void* a, const void* b) {

return ((struct Job*)b)->profit – ((struct Job*)a)->profit;

}

void job_sequencing_with_deadlines(struct Job* jobs, int n) {

qsort(jobs, n, sizeof(struct Job), compare_jobs);

int max_deadline = 0;

for (int i = 0; i < n; i++) {

}

int* schedule = (int*)calloc(max_deadline, sizeof(int));

int total_profit = 0;

for (int i = 0; i < n; i++) {

while (deadline > 0) {

if (schedule[deadline – 1] == 0) {

schedule[deadline – 1] = 1;

total_profit += jobs[i].profit;

break;

}

}

}

printf(“Total profit: %d\n”, total_profit);

free(schedule);

}

int main() {

int n;

printf(“Enter the number of jobs: “);

scanf(“%d”, &n);

struct Job* jobs = (struct Job*)malloc(n * sizeof(struct Job));

printf(“Enter job details (deadline profit) for each job:\n”);

for (int i = 0; i < n; i++)

scanf(“%d %d”, &jobs[i].deadline, &jobs[i].profit);

free(jobs);

return 0;

}

Examples of Job Sequencing with Deadlines

In the given job sequencing problem, we need to determine the optimal job sequence within their deadlines to achieve maximum profit. Each job is characterized by a deadline and a profit. Our goal is to find the most profitable combination of jobs that meet their respective deadlines.

 Job Job 1 Job 2 Job 3 Job 4 Job 5 Deadline 2 1 2 1 3 Profit 100 50 75 80 90

We want to find the optimal sequence of jobs to perform, considering their deadlines, in order to maximize the total profit. So, weâ€™ll arrange the profits in descending order:

 Job Job 1 Job 5 Job 4 Job 3 Job 2 Deadline 2 3 1 2 1 Profit 100 90 80 75 50

Job 1 (100) can be assigned to the deadline 2 (at index 1) since it’s the highest possible deadline available.

Job 5 (90) can be assigned to the deadline 3 (at index 2) since it’s the highest possible deadline available.

Job 3 (75) can be assigned to the deadline 2 (at index 1) since it’s the highest possible deadline available.

Job 4 (80) can’t be assigned as the only available slots have deadlines 2 and 3, both occupied.

Job 2 (50) can’t be assigned as all slots with deadlines 2 and 3 are occupied.

The total profit obtained by scheduling the selected jobs is 100 + 90 + 75 = 265.

The sequence of jobs that are scheduled: Job 1, Job 5, and Job 3.

The optimal sequence of jobs, considering their deadlines, that maximizes the total profit is Job 1, Job 5, and Job 3, with a total profit of 265.

## Frequently Asked Questions on Job Sequencing with Deadlines

Q1

### What is the Job Sequencing with Deadlines problem?

The Job Sequencing with Deadlines problem involves scheduling a set of jobs to maximize profit while adhering to their respective deadlines. Each job has a deadline and a profit, and the goal is to find the best arrangement of jobs that yields the highest total profit within the given time constraints.

Q2

### How is the Job Sequencing with Deadlines algorithm structured?

The algorithm can be broken down into the following steps:

• Sort the jobs in non-increasing order based on their profits.
• Initialize the schedule and available time slots.
• Assign jobs to time slots based on their deadlines and availability.
• Calculate the total profit and track the scheduled jobs.
• Output the final results, including the total profit and the list of scheduled jobs.
Q3

### What if some jobs cannot be completed within their deadlines?

The Job Sequencing with Deadlines algorithm aims to maximize profit while respecting deadlines. If some jobs cannot be completed within their deadlines, they are skipped during the scheduling process to meet the time constraints of higher-priority jobs.

Q4

### How does the Greedy method apply to Job Sequencing with Deadlines?

The Greedy method is often used to solve the Job Sequencing with Deadlines problem. It involves selecting jobs based on their profitability and deadline constraints. By arranging the jobs in descending order of profit, the Greedy method can achieve an optimal sequence of jobs that maximizes the total profit while considering deadline limitations.

Q5

### Can the Job Sequencing with Deadlines algorithm handle jobs with different durations?

The basic Job Sequencing with Deadlines algorithm assumes that each job requires exactly one unit of time to complete. If jobs have different durations, a more advanced scheduling algorithm would be necessary to accommodate varying processing times