Skip to content

[Add] : Job Sequence program under GREEDY methods #10482

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 23 commits into from
Oct 15, 2023
Merged
Changes from 12 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
2320d90
to add job seq program
kosuri-indu Oct 15, 2023
3b3f3a8
to add job seq program
kosuri-indu Oct 15, 2023
83394f4
Merge branch 'add/job_seq.py' of https://github.com/kosuri-indu/Pytho…
kosuri-indu Oct 15, 2023
8bc4629
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 15, 2023
4a20527
to add definitions in parameters
kosuri-indu Oct 15, 2023
9663d5d
Merge branch 'add/job_seq.py' of https://github.com/kosuri-indu/Pytho…
kosuri-indu Oct 15, 2023
f6c1223
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 15, 2023
3aacb09
to add definitions in parameters
kosuri-indu Oct 15, 2023
4f30ae4
Merge branch 'add/job_seq.py' of https://github.com/kosuri-indu/Pytho…
kosuri-indu Oct 15, 2023
7fe25bf
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 15, 2023
c220965
to add definitions in parameters
kosuri-indu Oct 15, 2023
349c324
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 15, 2023
58856c9
changes as recommended
kosuri-indu Oct 15, 2023
a036fd6
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 15, 2023
688fd27
type hint error resolved
kosuri-indu Oct 15, 2023
0a651e4
Merge branch 'add/job_seq.py' of https://github.com/kosuri-indu/Pytho…
kosuri-indu Oct 15, 2023
37ad111
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 15, 2023
82877b3
removed lambda
kosuri-indu Oct 15, 2023
4697ade
Merge branch 'add/job_seq.py' of https://github.com/kosuri-indu/Pytho…
kosuri-indu Oct 15, 2023
f2ed330
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 15, 2023
148a08d
import stmts order
kosuri-indu Oct 15, 2023
c261d83
Merge branch 'add/job_seq.py' of https://github.com/kosuri-indu/Pytho…
kosuri-indu Oct 15, 2023
20f0dd0
Update and rename job_sequence.py to job_sequence_with_deadline.py
cclauss Oct 15, 2023
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
60 changes: 60 additions & 0 deletions greedy_methods/job_sequence.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
"""
Given a list of tasks, each with a deadline and reward,
The maximum number of tasks that can be scheduled to maximize reward.
We can only complete one task at a time, and each task takes 1 unit
of time to complete. Once a task has passed its deadline, it can no
longer be scheduled.

Example :
tasks_info = [(4, 20), (1, 10), (1, 40), (1, 30)]
max_tasks will return (2, [2, 0]) -
which is by scheduling the tasks with rewards 40, 20

This problem can be solved using the concept of "GREEDY ALGORITHM".
Time Complexity - O(n log n)

We iterate over the tasks array once, sorting it in descending order of reward.
Then we iterate over the sorted tasks array once more, scheduling each
task if its deadline is greater than the current time.The greedy choice at
each point is to either schedule the current task if its deadline is greater
than the current time, or skip it otherwise.
"""

from typing import Any


class Task:
def __init__(self, task_id: Any, deadline: int, reward: int) -> None:
self.task_id = task_id
self.deadline = deadline
self.reward = reward


def max_tasks(tasks_info: list[tuple[int]]) -> int:
"""
>>> max_tasks([(4, 20), (1, 10), (1, 40), (1, 30)])
(2, [2, 0])
>>> max_tasks([(1, 10), (2, 20), (3, 30), (2, 40)])
(2, [3, 2])
"""
tasks = [Task(i, d, p) for i, (d, p) in enumerate(tasks_info)]

tasks.sort(key=lambda task: task.reward, reverse=True)

schedule = []
current_time = 0

for task in tasks:
if task.deadline > current_time:
schedule.append(task.task_id)
current_time += 1

return len(schedule), schedule


if __name__ == "__main__":
import doctest

doctest.testmod()

print(max_tasks([(4, 20), (1, 10), (1, 40), (1, 30)]))