-
-
Notifications
You must be signed in to change notification settings - Fork 46.7k
Interval scheduling #11853
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
Interval scheduling #11853
Changes from 2 commits
8901d6f
34baa94
e43a7ca
772d5da
e810066
9aa9505
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
""" | ||
interval scheduling is a class of problems. The programs take a number of tasks into account. Every task is represented by an interval that indicates the amount of time it should take a machine to complete it. If there is no overlap between any two intervals on the system or resource, a subset of intervals is compatible. | ||
Check failure on line 2 in scheduling/interval_scheduling_algorithm.py
|
||
|
||
The goal of the interval scheduling maximization problem is to identify the largest compatible set or a collection of intervals with the least possible overlap. The idea is to optimize throughput by completing as many tasks as you can. | ||
Check failure on line 4 in scheduling/interval_scheduling_algorithm.py
|
||
|
||
""" | ||
|
||
def interval_scheduling(stimes, ftimes): | ||
vinaysanwal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
index = list(range(len(stimes))) | ||
# sort according to finish times | ||
index.sort(key=lambda i: ftimes[i]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide descriptive name for the parameter:
vinaysanwal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Check failure on line 12 in scheduling/interval_scheduling_algorithm.py
|
||
maximal_set = set() | ||
prev_finish_time = 0 | ||
for i in index: | ||
if stimes[i] >= prev_finish_time: | ||
maximal_set.add(i) | ||
prev_finish_time = ftimes[i] | ||
|
||
Check failure on line 19 in scheduling/interval_scheduling_algorithm.py
|
||
return maximal_set | ||
|
||
Check failure on line 21 in scheduling/interval_scheduling_algorithm.py
|
||
|
||
Check failure on line 22 in scheduling/interval_scheduling_algorithm.py
|
||
n = int(input('Enter number of activities: ')) | ||
stimes = input('Enter the start time of the {} activities in order: ' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As mentioned in the Contributing Guidelines, please do not use printf style formatting or There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As mentioned in the Contributing Guidelines, please do not use printf style formatting or |
||
.format(n)).split() | ||
Check failure on line 25 in scheduling/interval_scheduling_algorithm.py
|
||
stimes = [int(st) for st in stimes] | ||
ftimes = input('Enter the finish times of the {} activities in order: ' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As mentioned in the Contributing Guidelines, please do not use printf style formatting or
vinaysanwal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
.format(n)).split() | ||
Check failure on line 28 in scheduling/interval_scheduling_algorithm.py
|
||
ftimes = [int(ft) for ft in ftimes] | ||
|
||
Check failure on line 30 in scheduling/interval_scheduling_algorithm.py
|
||
ans = interval_scheduling(stimes, ftimes) | ||
print('A maximum-size subset of activities that are mutually compatible is', ans) | ||
Check failure on line 32 in scheduling/interval_scheduling_algorithm.py
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please provide return type hint for the function:
interval_scheduling
. If the function does not return a value, please provide the type hint as:def function() -> None:
As there is no test file in this pull request nor any test function or class in the file
scheduling/interval_scheduling_algorithm.py
, please provide doctest for the functioninterval_scheduling
Please provide type hint for the parameter:
stimes
Please provide type hint for the parameter:
ftimes