Skip to content

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

Closed
wants to merge 6 commits into from
Closed
Changes from 2 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
32 changes: 32 additions & 0 deletions scheduling/interval_scheduling_algorithm.py
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

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

scheduling/interval_scheduling_algorithm.py:2:89: E501 Line too long (322 > 88)

Check failure on line 2 in scheduling/interval_scheduling_algorithm.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

scheduling/interval_scheduling_algorithm.py:2:89: E501 Line too long (322 > 88)

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

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

scheduling/interval_scheduling_algorithm.py:4:89: E501 Line too long (235 > 88)

Check failure on line 4 in scheduling/interval_scheduling_algorithm.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

scheduling/interval_scheduling_algorithm.py:4:89: E501 Line too long (235 > 88)

"""

def interval_scheduling(stimes, ftimes):

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 function interval_scheduling

Please provide type hint for the parameter: stimes

Please provide type hint for the parameter: ftimes

index = list(range(len(stimes)))
# sort according to finish times
index.sort(key=lambda i: ftimes[i])

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide descriptive name for the parameter: i


Check failure on line 12 in scheduling/interval_scheduling_algorithm.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

scheduling/interval_scheduling_algorithm.py:12:1: W293 Blank line contains whitespace

Check failure on line 12 in scheduling/interval_scheduling_algorithm.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

scheduling/interval_scheduling_algorithm.py:12:1: W293 Blank line contains whitespace
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

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

scheduling/interval_scheduling_algorithm.py:19:1: W293 Blank line contains whitespace

Check failure on line 19 in scheduling/interval_scheduling_algorithm.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

scheduling/interval_scheduling_algorithm.py:19:1: W293 Blank line contains whitespace
return maximal_set

Check failure on line 21 in scheduling/interval_scheduling_algorithm.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

scheduling/interval_scheduling_algorithm.py:21:1: W293 Blank line contains whitespace

Check failure on line 21 in scheduling/interval_scheduling_algorithm.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

scheduling/interval_scheduling_algorithm.py:21:1: W293 Blank line contains whitespace

Check failure on line 22 in scheduling/interval_scheduling_algorithm.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

scheduling/interval_scheduling_algorithm.py:22:1: W293 Blank line contains whitespace

Check failure on line 22 in scheduling/interval_scheduling_algorithm.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

scheduling/interval_scheduling_algorithm.py:22:1: W293 Blank line contains whitespace
n = int(input('Enter number of activities: '))
stimes = input('Enter the start time of the {} activities in order: '

Choose a reason for hiding this comment

The 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 str.format(). Use f-string instead to be more readable and efficient.

Choose a reason for hiding this comment

The 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 str.format(). Use f-string instead to be more readable and efficient.

.format(n)).split()

Check failure on line 25 in scheduling/interval_scheduling_algorithm.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP032)

scheduling/interval_scheduling_algorithm.py:24:16: UP032 Use f-string instead of `format` call

Check failure on line 25 in scheduling/interval_scheduling_algorithm.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP032)

scheduling/interval_scheduling_algorithm.py:24:16: UP032 Use f-string instead of `format` call
stimes = [int(st) for st in stimes]
ftimes = input('Enter the finish times of the {} activities in order: '

Choose a reason for hiding this comment

The 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 str.format(). Use f-string instead to be more readable and efficient.

.format(n)).split()

Check failure on line 28 in scheduling/interval_scheduling_algorithm.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP032)

scheduling/interval_scheduling_algorithm.py:27:16: UP032 Use f-string instead of `format` call

Check failure on line 28 in scheduling/interval_scheduling_algorithm.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP032)

scheduling/interval_scheduling_algorithm.py:27:16: UP032 Use f-string instead of `format` call
ftimes = [int(ft) for ft in ftimes]

Check failure on line 30 in scheduling/interval_scheduling_algorithm.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

scheduling/interval_scheduling_algorithm.py:30:1: W293 Blank line contains whitespace

Check failure on line 30 in scheduling/interval_scheduling_algorithm.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

scheduling/interval_scheduling_algorithm.py:30:1: W293 Blank line contains whitespace
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

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W292)

scheduling/interval_scheduling_algorithm.py:32:82: W292 No newline at end of file

Check failure on line 32 in scheduling/interval_scheduling_algorithm.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W292)

scheduling/interval_scheduling_algorithm.py:32:82: W292 No newline at end of file
Loading