Job Sequencing Problem

Distribute Education like Computer Geek

Introduction

The Job Sequencing Problem is a classic optimization problem that falls under the category of Greedy Algorithms. It is a problem related to job scheduling and aims to find the most profitable way to execute a set of jobs within a given time frame. In this article, we’ll explore the basics of the Job Sequencing Problem, how it works, its algorithm, and various aspects associated with it.

.

Problem

Given a set of ‘n’ jobs, where each job ‘i’ has a deadline ‘Di‘ by which it must be completed, a profit ‘Pi‘ associated with it if completed on time, and a processing time ‘Ti‘ find a schedule that maximizes the total profit.

.

Working

The schedule should ensure that each job is completed before or at its respective deadline, and the processing times do not overlap.

The Job Sequencing Problem is all about finding the best way to complete a bunch of tasks.

These tasks have a time limit and a reward attached to them.

You want to finish as many as possible within the time limits to get the most rewards.

It’s like a puzzle where you need to figure out the right order to work on these tasks so that you don’t miss any deadlines and collect the maximum rewards.

.

Algorithm of Job Sequencing Problem
  1. Sort the jobs in non-ascending order of their profits.
  2. Initialize an array to keep track of time slots and a variable to store the total profit.
  3. Iterate through the sorted jobs list.
  4. For each job, find the highest possible time slot that is available (i.e., not yet allocated).
  5. Allocate the job to the found time slot and update the total profit.
  6. Continue this process until all jobs are allocated or no more time slots are available.

.

Example

In university exams, there is a question which comes most of the times.

Job Sequencing Input

So, we sort the jobs in descending order of their profits.

Job Sequencing Sorted Input
Job Sequencing Sorted Input

.

1 First job is J3. (Profit = 30, Deadline = 5)

The highest possible time slot that is available (i.e., not yet allocated) is 4-5.

Job Sequencing 1st job

.

2 Next job is J9. (Profit = 25, Deadline = 3)

The highest possible time slot that is available (i.e., not yet allocated) is 2-3.

Job Sequencing 2nd job

.

3 Next job is J7. (Profit = 23, Deadline = 2)

The highest possible time slot that is available (i.e., not yet allocated) is 1-2.

Job Sequencing 3rd job

.

4 Next job is J2. (Profit = 20, Deadline = 2)

The highest possible time slot that is available (i.e., not yet allocated) is 0-1, because 1-2 time has been allocated to J7.Job Sequencing 4th job

.

5 Next job is J4. (Profit = 18, Deadline = 3)

The highest possible time slot that is available (i.e., not yet allocated) is NONE, because 0-1, 1-2 and 2-3 time has been allocated to J2, J7 and J9, respectively. This job creates an overlap, so skip this.

.

6 Next job is J5. (Profit = 18, Deadline = 4)

The highest possible time slot that is available (i.e., not yet allocated) is 3-4.

6th job

.

7 Next job is J8. (Profit = 16, Deadline = 7)

The highest possible time slot that is available (i.e., not yet allocated) is 6-7.

7th job

.

8 Next job is J1. (Profit = 15, Deadline = 7)

The highest possible time slot that is available (i.e., not yet allocated) is 5-6, because 6-7 time has been allocated to J8.

8th job

.

So, all the slots have been fulfilled. Skip the rest jobs.

The total profit is 20 + 23 + 25 + 18 + 30 + 15 + 16 = 147

.

Source Code for Job Sequencing Problem

Time complexity of Job Sequencing Problem
  1. To sort N Jobs – O(nlogn)
  2. Take each job and start where the deadline is given. Keep searching for the vacant location. So, if there are N jobs for each job, we need to search N slots. i.e O(n2)

So, total time complexity is O(n2).

.

Space Complexity of Job Sequencing Problem

The space complexity is O(n) as it primarily involves storing the jobs and their associated data.

.

Advantages of Job Sequencing Problem
  1. The greedy algorithm for job sequencing is relatively simple and efficient.
  2. It provides a quick solution to scheduling problems with the aim of maximizing profits.
  3. It can be applied to a wide range of practical situations where job scheduling is crucial.

.

Disadvantages of Job Sequencing Problem
  1. It assumes that the execution times of jobs are the same, which may not hold true in real-world scenarios.
  2. If not sorted by profit, it might lead to suboptimal solutions.
  3. It is non-pre-emptive.
  4. It has a higher average waiting time compared to other algorithms.
  5. It favours CPU bound processes over I/O bound processes.
  6. The algorithm assumes that all jobs are independent and can be scheduled without any dependencies or constraints.
  7. Once a job is skipped due to a missed deadline, it cannot be rescheduled.

.

Applications of Job Sequencing Problem
  1. Print job scheduling in computer systems.
  2. In marketing, companies may have multiple advertising campaigns with various deadlines. It can be applied to schedule these campaigns for maximum reach and profitability.
  3. It can be used in the scheduling of transport vehicles and routes to optimize the delivery of goods, reduce transportation costs, and ensure timely deliveries.
  4. Educational institutions use this problem to schedule exams to avoid conflicts, reduce idle time between exams, and ensure a fair distribution of exams for students.
  5. Farmers can use to optimize planting and harvesting schedules, taking into account factors like crop growth cycles and weather conditions.

Test Yourself

Q1- How does the Job Sequencing Problem differ from other scheduling algorithms, such as Round Robin or Shortest Job First (SJF)?

Unlike Round Robin or SJF, the Job Sequencing Problem focuses on job profits and deadlines. It doesn’t consider job durations or prioritize based on time remaining to completion.

Q2- Explain the concept of non-pre-emptive scheduling in the context of the Job Sequencing Problem.

Non-pre-emptive scheduling means once a job is started, it cannot be interrupted or stopped. In the Job Sequencing Problem, it implies that if a job is allocated to a time slot, it must be completed within that time frame.

Q3- 10 jobs are given. The execution of each job takes 1 unit of time. Jobs are given as (Job, Profit, Deadline)
(J1, 25, 4), (J2, 35, 7), (J3, 40, 4), (J4, 30, 7), (J5, 18, 1), (J6, 25, 4), (J7, 33, 6), (J8, 43, 5), (J9, 19, 3), (J10, 25, 6)
The maximum profit earned is ____________ ?

We have to first sort the jobs in non-ascending order of their profits.

(J8, 43, 5), (J3, 40, 4), (J2, 35, 7), (J7, 33, 6), (J4, 30, 7), (J1, 25, 4), (J6, 25, 4), (J10, 25, 6), (J9, 19, 3), (J5, 18, 1).

Total Jobs execute = J8, J3, J2, J7, J4, J1, J6

Maximum Profit = 43 + 40 + 35 + 33 + 30 + 25 + 25 = 231

Q4- Jobs – (J1, J2, J3, J4) Processing Time – (1, 3, 2, 1) Deadline – (3, 3, 2, 4). Maximum jobs can be done is
  1. 1
  2. 2
  3. 3
  4. All

Ans – (3)

Explanation – In this type of problems, we sort the processing time of jobs into non-descending order.

Sorted Jobs – (J1, 1, 3), (J4, 1, 4), (J3, 2, 2), (J2, 3, 3)

Maximum Jobs can be done is 3. (J1, J4, J3)

Q5- You have 8 jobs with their respective profits and deadlines:
(J1, 50, 2), (J2, 35, 3), (J3, 60, 1), (J4, 30, 3), (J5, 45, 5), (J6, 20, 4), (J7, 55, 2), (J8, 40, 4).
What is the maximum profit that can be earned by scheduling these jobs?

To maximize profit, we first sort the jobs in non-ascending order of their profits:

(J3, 60, 1), (J7, 55, 2), (J1, 50, 2), (J5, 45, 5), (J8, 40, 4), (J2, 35, 3), (J4, 30, 3), (J6, 20, 4).

.

Now, let’s schedule the jobs to maximize profit:

Execute J3 within its deadline (Profit = 60).

Execute J7 within its deadline (Profit = 60 + 55 = 115).

Job J1 can’t be executed within its deadline.

Execute J5 within its deadline (Profit = 115 + 45 = 160).

Execute J8 within its deadline (Profit = 160 + 40 = 200).

Execute J2 within its deadline (Profit = 200 + 35 = 235).

Job J4 can’t be executed within its deadline.

Job J6 can’t be executed within its deadline.

.

The maximum profit can be earned is 235.

Q6- 5 jobs are given, and each job takes 2 units of time to complete. The jobs are represented as (Job, Profit, Deadline):
(J1, 15, 2), (J2, 25, 3), (J3, 10, 1), (J4, 20, 3), (J5, 18, 2).
 
What is the maximum profit that can be earned using the Job Sequencing Problem algorithm?

To maximize the profit, we first need to sort the jobs in non-ascending order of their profits. After sorting, the job sequence becomes:

(J2, 25, 3), (J4, 20, 4), (J5, 18, 5), (J1, 15, 2), (J3, 10, 7).

 

Each job takes 2 units of time to complete.

Now, we can execute the jobs one by one to maximize the profit. The profit is obtained by executing (J2, J5, J3) is 25 + 18 + 10 = 53.

 

But, if you see by some other angle, the profit is not maximized. We can do some other jobs with in the timeline.

Executing J2 in the first 2 units of time allows J4 to be executed in the second 2 units of time. After you execute J3. The maximum profit is now to be 25 + 20 + 10 = 55 (which is more than 53).

.

But this is a greedy method and a Greedy algorithm is an approach to solving problems by making locally optimal (best) choices at each step with the hope of finding a global optimum (best) or near-best solution.

The maximum profit is made possible by dynamic programming methodology by which we can create every possible solution to maximize profit.

So, the maximum profit that can be earned in Greedy approach is 53 units.

BOOKS

Algorithm T H CoremanAlgorithm by Udit AgarwalAlgorithm Ellis HorowitzData Structure & Algorithm Made Easy Narasimha