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 all 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
60 changes: 11 additions & 49 deletions matrix/count_paths.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,55 +19,17 @@
0 1 * * 0 1 * *
"""


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)]
intervals.sort(key=lambda x: x[1])
count = 0
end = 0
answer = []

for interval in intervals:
if end <= interval[0]:
end = interval[1]
count += 1
answer.append(interval)

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 @@
"""

Check failure on line 1 in scheduling/Iinterval_scheduling_algorithm.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (N999)

scheduling/Iinterval_scheduling_algorithm.py:1:1: N999 Invalid module name: 'Iinterval_scheduling_algorithm'
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/Iinterval_scheduling_algorithm.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

scheduling/Iinterval_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/Iinterval_scheduling_algorithm.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

scheduling/Iinterval_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/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