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
Show file tree
Hide file tree
Changes from 5 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
59 changes: 11 additions & 48 deletions matrix/count_paths.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,54 +20,17 @@
"""


def depth_first_search(grid: list[list[int]], row: int, col: int, visit: set) -> int:
"""
Recursive Backtracking Depth First Search Algorithm

Starting from top left of a matrix, count the number of
paths that can reach the bottom right of a matrix.
1 represents a block (inaccessible)
0 represents a valid space (accessible)

0 0 0 0
1 1 0 0
0 0 0 1
0 1 0 0
>>> grid = [[0, 0, 0, 0], [1, 1, 0, 0], [0, 0, 0, 1], [0, 1, 0, 0]]
>>> depth_first_search(grid, 0, 0, set())
2

0 0 0 0 0
0 1 1 1 0
0 1 1 1 0
0 0 0 0 0
>>> grid = [[0, 0, 0, 0, 0], [0, 1, 1, 1, 0], [0, 1, 1, 1, 0], [0, 0, 0, 0, 0]]
>>> depth_first_search(grid, 0, 0, set())
2
"""
row_length, col_length = len(grid), len(grid[0])
if (
min(row, col) < 0
or row == row_length
or col == col_length
or (row, col) in visit
or grid[row][col] == 1
):
return 0
if row == row_length - 1 and col == col_length - 1:
return 1

visit.add((row, col))

count = 0
count += depth_first_search(grid, row + 1, col, visit)
count += depth_first_search(grid, row - 1, col, visit)
count += depth_first_search(grid, row, col + 1, visit)
count += depth_first_search(grid, row, col - 1, visit)

visit.remove((row, col))
return count

intervals = [(4, 5), (0, 2), (2, 7), (1, 3), (0, 4)]

Check failure on line 23 in matrix/count_paths.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W291)

matrix/count_paths.py:23:53: W291 Trailing whitespace
intervals.sort(key=lambda x: x[1])

Check failure on line 24 in matrix/count_paths.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W291)

matrix/count_paths.py:24:35: W291 Trailing whitespace
count = 0

Check failure on line 25 in matrix/count_paths.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W291)

matrix/count_paths.py:25:10: W291 Trailing whitespace
end = 0

Check failure on line 26 in matrix/count_paths.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W291)

matrix/count_paths.py:26:8: W291 Trailing whitespace
answer = []

Check failure on line 27 in matrix/count_paths.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W291)

matrix/count_paths.py:27:12: W291 Trailing whitespace

Check failure on line 28 in matrix/count_paths.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

matrix/count_paths.py:28:1: W293 Blank line contains whitespace
for interval in intervals:

Check failure on line 29 in matrix/count_paths.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W291)

matrix/count_paths.py:29:27: W291 Trailing whitespace
if(end <= interval[0]):

Check failure on line 30 in matrix/count_paths.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W291)

matrix/count_paths.py:30:28: W291 Trailing whitespace
end = interval[1]

Check failure on line 31 in matrix/count_paths.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W291)

matrix/count_paths.py:31:26: W291 Trailing whitespace
count += 1
answer.append(interval)

Check failure on line 33 in matrix/count_paths.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W291)

matrix/count_paths.py:33:32: W291 Trailing whitespace

if __name__ == "__main__":
import doctest
Expand Down
36 changes: 36 additions & 0 deletions scheduling/Iinterval_scheduling_algorithm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
"""
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):

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/Iinterval_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 item: ftimes[item])

maximal_set = set()
prev_finish_time = 0
for item in index:
if stimes[item] >= prev_finish_time:
maximal_set.add(item)
prev_finish_time = ftimes[item]

return maximal_set


n = int(input('Enter number of activities: '))
stimes = input('Enter the start time of the {} activities in order: .{n}').split()
stimes = [int(st) for st in stimes]
ftimes = input('Enter the finish times of the {} activities in order:.{n}').split()
ftimes = [int(ft) for ft in ftimes]

ans = interval_scheduling(stimes, ftimes)


if __name__ == "__main__":
import doctest

doctest.testmod()

Loading