-
-
Notifications
You must be signed in to change notification settings - Fork 46.9k
Add activity selection new algorithm #11667
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
Open
Hardvan
wants to merge
4
commits into
TheAlgorithms:master
Choose a base branch
from
Hardvan:activity_selection_new_algo2
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
""" | ||
The Activity Selection Problem is a classic problem in which a set of activities, | ||
each with a start and end time, needs to be scheduled in such a way that | ||
the maximum number of non-overlapping activities is selected. | ||
This is a greedy algorithm where at each step, | ||
we choose the activity that finishes the earliest | ||
and does not conflict with previously selected activities. | ||
|
||
Wikipedia: https://en.wikipedia.org/wiki/Activity_selection_problem | ||
""" | ||
|
||
|
||
def activity_selection(activities: list[tuple[int, int]]) -> list[tuple[int, int]]: | ||
""" | ||
Solve the Activity Selection Problem using a greedy algorithm by selecting | ||
the maximum number of non-overlapping activities from a list of activities. | ||
|
||
Parameters: | ||
activities: A list of tuples where each tuple contains | ||
the start and end times of an activity. | ||
|
||
Returns: | ||
A list of selected activities that are non-overlapping. | ||
|
||
Example: | ||
>>> activity_selection([(1, 3), (2, 5), (3, 9), (6, 8)]) | ||
[(1, 3), (6, 8)] | ||
|
||
>>> activity_selection([(0, 6), (1, 4), (3, 5), (5, 7), (5, 9), (8, 9)]) | ||
[(1, 4), (5, 7), (8, 9)] | ||
|
||
>>> activity_selection([(1, 2), (2, 4), (3, 5), (0, 6)]) | ||
[(1, 2), (2, 4)] | ||
|
||
>>> activity_selection([(5, 9), (1, 2), (3, 4), (0, 6)]) | ||
[(1, 2), (3, 4), (5, 9)] | ||
""" | ||
|
||
# Step 1: Sort the activities by their end time | ||
sorted_activities = sorted(activities, key=lambda x: x[1]) | ||
|
||
# Step 2: Select the first activity (the one that finishes the earliest) | ||
# as the initial activity | ||
selected_activities = [sorted_activities[0]] | ||
|
||
# Step 3: Iterate through the sorted activities and select the ones | ||
# that do not overlap with the last selected activity | ||
for i in range(1, len(sorted_activities)): | ||
if sorted_activities[i][0] >= selected_activities[-1][1]: | ||
selected_activities.append(sorted_activities[i]) | ||
|
||
return selected_activities |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.