Skip to content

Commit 0152956

Browse files
authored
Create job_sequencing_problem.py
1 parent d92bbad commit 0152956

File tree

1 file changed

+102
-0
lines changed

1 file changed

+102
-0
lines changed
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+
# -------------------------------------------------------------------------

0 commit comments

Comments
 (0)