Skip to content

Commit 446eac3

Browse files
Merge pull request #213 from abhisheks008/main
Job Sequencing Problem using Greedy Approach
2 parents 586da8c + acadd23 commit 446eac3

File tree

5 files changed

+181
-0
lines changed

5 files changed

+181
-0
lines changed
11.1 KB
Loading
10.6 KB
Loading
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# Job Sequencing Problem using Greedy Approach
2+
🔴 Language used : **Python 3**
3+
4+
## 🎯 Aim
5+
The aim of this script is to find the maximum profit earned by executing the tasks within the specified deadlines.
6+
7+
## 👉 Purpose
8+
The main purpose of this script is to show the implementation of Greedy Approach to find the maximum profit earned by executing the tasks within the specified deadlines.
9+
10+
## 📄 Description
11+
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.
12+
13+
🔴 Examples:
14+
15+
```
16+
Constraints:
17+
arr[] -> Set of datapoints including JobID, Deadline and Profit into a single array.
18+
t -> No. of jobs to be scheduled.
19+
20+
Input:
21+
Five Jobs with following deadlines and profits
22+
23+
JobID Deadline Profit
24+
a 2 100
25+
b 1 19
26+
c 2 27
27+
d 1 25
28+
e 3 15
29+
30+
Output:
31+
Following is maximum profit sequence of jobs:
32+
c, a, e
33+
```
34+
35+
36+
## 🧮 Algorithm
37+
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.
38+
- This problem is basically solved using the `printJobScheduling(arr, t)` funtion, which is basically the main function. `arr` signifies the job array and `t` signifies the number of jobs to be scheduled.
39+
- Sort all jobs in decreasing order of profit.
40+
- Iterate on jobs in decreasing order of profit.For each job , do the following :
41+
- 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.
42+
- If no such `i` exists, then ignore the job.
43+
- This whole process will be repeated until and unless the funtion got the `-1` value. Once it got the `-1` value it will stop the program and provide the output.
44+
45+
## 💻 Input and Output
46+
- **Test Case 1 :**
47+
```
48+
Input Given :
49+
arr = [['a', 2, 100],
50+
['b', 1, 19],
51+
['c', 2, 27],
52+
['d', 1, 25],
53+
['e', 3, 15]]
54+
```
55+
56+
![](https://github.com/abhisheks008/PyAlgo-Tree/blob/main/Greedy/Job%20Sequencing%20Problem/Images/jsp-1.png)
57+
58+
- **Test Case 2 :**
59+
```
60+
Input Given :
61+
arr = [['a', 4, 20],
62+
['b', 1, 10],
63+
['c', 1, 40],
64+
['d', 1, 30]]
65+
```
66+
![](https://github.com/abhisheks008/PyAlgo-Tree/blob/main/Greedy/Job%20Sequencing%20Problem/Images/jsp-2.png)
67+
68+
## ⏰ Time and Space complexity
69+
- **Time Complexity :** `O(n^2)`.
70+
- **Space Complexity :** `O(n)`.
71+
72+
---------------------------------------------------------------
73+
## 🖋️ Author
74+
**Code contributed by, _Abhishek Sharma_, 2022 [@abhisheks008](github.com/abhisheks008)**
75+
76+
[![forthebadge made-with-python](http://ForTheBadge.com/images/badges/made-with-python.svg)](https://www.python.org/)
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
# Problem Name: Job Sequencing Problem
2+
# Approach used: Greedy Method
3+
# Language used: Python
4+
5+
# Problem Statement: Given an array of jobs where every job has a deadline
6+
# and associated profit if the job is finished before the
7+
# deadline. It is also given that every job takes a single
8+
# unit of time, so the minimum possible deadline for any
9+
# job is 1. Maximize the total profit if only one job can
10+
# be scheduled at a time.
11+
12+
# -----------------------------------------------------------------------------
13+
14+
# Constraints:
15+
# arr[] -> Set of datapoints including JobID, Deadline and Profit into a single array.
16+
# t -> No. of jobs to be scheduled.
17+
18+
# -----------------------------------------------------------------------------
19+
20+
21+
def printJobScheduling(arr, t):
22+
# This is the main funtion for determining the final result
23+
# function to schedule the jobs take 2 arguments array and no of jobs to schedule
24+
25+
# length of array
26+
n = len(arr)
27+
28+
# Sort all jobs according to
29+
# decreasing order of profit
30+
for i in range(n):
31+
for j in range(n - 1 - i):
32+
if arr[j][2] < arr[j + 1][2]:
33+
arr[j], arr[j + 1] = arr[j + 1], arr[j]
34+
35+
# To keep track of free time slots
36+
result = [False] * t
37+
38+
# To store result (Sequence of jobs)
39+
job = ['-1'] * t
40+
41+
# Iterate through all given jobs
42+
for i in range(len(arr)):
43+
44+
# Find a free slot for this job
45+
# (Note that we start from the
46+
# last possible slot)
47+
for j in range(min(t - 1, arr[i][1] - 1), -1, -1):
48+
49+
# Free slot found
50+
if result[j] is False:
51+
result[j] = True
52+
job[j] = arr[i][0]
53+
break
54+
55+
# print the sequence
56+
print(job)
57+
58+
59+
# Driver's Code
60+
if __name__ == '__main__':
61+
arr = [['a', 2, 100], # Job Array
62+
['b', 1, 19],
63+
['c', 2, 27],
64+
['d', 1, 25],
65+
['e', 3, 15]]
66+
67+
print ("-- Job Sequencing Problem using Greedy Approach --")
68+
print ()
69+
print ("Job ID Deadline Profit")
70+
for i in range(0, len(arr)):
71+
for j in range (0, 3):
72+
print (arr[i][j], end = " ")
73+
print()
74+
75+
print ("\nOutput --")
76+
print("Following is maximum profit sequence of jobs")
77+
78+
# Function Call
79+
printJobScheduling(arr, 3)
80+
81+
82+
# -------------------------------------------------------------------------
83+
84+
# Output
85+
# -- Job Sequencing Problem using Greedy Approach --
86+
87+
# Job ID Deadline Profit
88+
# a 2 100
89+
# b 1 19
90+
# c 2 27
91+
# d 1 25
92+
# e 3 15
93+
94+
# Output --
95+
# Following is maximum profit sequence of jobs
96+
# c a e
97+
98+
# -------------------------------------------------------------------------
99+
100+
# Code contributed by, Abhishek Sharma, 2022
101+
102+
# -------------------------------------------------------------------------

Greedy/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
1+
## Implementations of Greedy Algorithms :computer:
12

3+
- [**Activity Selection Problem**](https://github.com/abhisheks008/PyAlgo-Tree/tree/main/Greedy/Activity%20Selection%20Problem)
4+
- [**Job Sequencing Problem**](https://github.com/abhisheks008/PyAlgo-Tree/tree/main/Greedy/Job%20Sequencing%20Problem)

0 commit comments

Comments
 (0)