Skip to content

Job sequencing with deadlines (#6854) #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 10, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions scheduling/job_sequencing_with_deadline.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
def job_sequencing_with_deadlines(num_jobs: int, jobs: list) -> list:
"""
Function to find the maximum profit by doing jobs in a given time frame

Args:
num_jobs [int]: Number of jobs
jobs [list]: A list of tuples of (job_id, deadline, profit)

Returns:
max_profit [int]: Maximum profit that can be earned by doing jobs
in a given time frame

Examples:
>>> job_sequencing_with_deadlines(4,
... [(1, 4, 20), (2, 1, 10), (3, 1, 40), (4, 1, 30)])
[2, 60]
>>> job_sequencing_with_deadlines(5,
... [(1, 2, 100), (2, 1, 19), (3, 2, 27), (4, 1, 25), (5, 1, 15)])
[2, 127]
"""

# Sort the jobs in descending order of profit
jobs = sorted(jobs, key=lambda value: value[2], reverse=True)

# Create a list of size equal to the maximum deadline
# and initialize it with -1
max_deadline = max(jobs, key=lambda value: value[1])[1]
time_slots = [-1] * max_deadline

# Finding the maximum profit and the count of jobs
count = 0
max_profit = 0
for job in jobs:
# Find a free time slot for this job
# (Note that we start from the last possible slot)
for i in range(job[1] - 1, -1, -1):
if time_slots[i] == -1:
time_slots[i] = job[0]
count += 1
max_profit += job[2]
break
return [count, max_profit]


if __name__ == "__main__":
import doctest

doctest.testmod()