Skip to content

Job sequencing with deadlines #6854

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
Changes from 16 commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
e22c1c5
completed optimised code for job sequencing with deadline problem
lavenroop5 Oct 8, 2022
d7cb01f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 8, 2022
388b978
completed optimised code for job sequencing with deadline problem
lavenroop5 Oct 9, 2022
f707dc2
completed optimized code for job sequencing with deadline problem
lakshayroop5 Oct 9, 2022
de476be
completed optimised code for job sequencing with deadline problem
lavenroop5 Oct 9, 2022
1545f2e
completed optimised code for job sequencing with deadline problem
lavenroop5 Oct 9, 2022
017b3af
completed optimised code for job sequencing with deadline problem
lavenroop5 Oct 9, 2022
51dcd9d
completed optimised code for job sequencing with deadline problem
lavenroop5 Oct 9, 2022
02b0e81
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 9, 2022
166dce9
completed optimized code for the issue "Job Scheduling with deadlines"
lakshayroop5 Oct 9, 2022
e49c8e1
completed optimized code for the issue "Job Scheduling with deadlines"
lakshayroop5 Oct 9, 2022
487829d
completed optimized code for the issue "Job Scheduling with deadlines"
lakshayroop5 Oct 9, 2022
3785de3
Update greedy_methods/job_sequencing_with_deadline.py
lakshayroop5 Oct 9, 2022
10e7276
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 9, 2022
500231b
Updated reviews
lakshayroop5 Oct 9, 2022
a0b4d52
Updated reviews
lakshayroop5 Oct 9, 2022
c577d7e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 9, 2022
0eff505
Update and rename greedy_methods/job_sequencing_with_deadline.py to s…
cclauss Oct 9, 2022
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
57 changes: 57 additions & 0 deletions greedy_methods/job_sequencing_with_deadline.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
"""
This is a pure Python implementation of the greedy-merge-sort algorithm

For doctests run following command:
python3 -m doctest -v greedy_merge_sort.py
"""


# FUNCTION
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()