-
-
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 4 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,37 @@ | ||
""" | ||
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. | ||
|
||
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. | ||
|
||
""" | ||
|
||
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
|
||
|
||
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] | ||
|
||
return maximal_set | ||
|
||
|
||
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() | ||
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() | ||
ftimes = [int(ft) for ft in ftimes] | ||
|
||
ans = interval_scheduling(stimes, ftimes) | ||
|
||
|
||
if __name__ == "__main__": | ||
from doctest import testmod | ||
|
||
testmod() |
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