Skip to content

Commit 2c3bcdd

Browse files
Hardvancclauss
andauthored
Add activity selection new algorithm (#11667)
* Add activity selection new algorithm * updating DIRECTORY.md * Rename x to activity * updating DIRECTORY.md * updating DIRECTORY.md * Refactor activity_selection function documentation Updated docstring and added a check for empty input. * Fix activity_selection function test cases --------- Co-authored-by: Hardvan <Hardvan@users.noreply.github.com> Co-authored-by: Christian Clauss <cclauss@me.com> Co-authored-by: cclauss <cclauss@users.noreply.github.com>
1 parent 6a995cb commit 2c3bcdd

2 files changed

Lines changed: 58 additions & 0 deletions

File tree

‎DIRECTORY.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -671,6 +671,7 @@
671671
* [Travelling Salesman Problem](graphs/travelling_salesman_problem.py)
672672

673673
## [Greedy Methods](greedy_methods)
674+
* [Activity Selection](greedy_methods/activity_selection.py)
674675
* [Best Time To Buy And Sell Stock](greedy_methods/best_time_to_buy_and_sell_stock.py)
675676
* [Fractional Cover Problem](greedy_methods/fractional_cover_problem.py)
676677
* [Fractional Knapsack](greedy_methods/fractional_knapsack.py)
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
"""
2+
The Activity Selection Problem is a classic problem in which a set of activities,
3+
each with a start and end time, needs to be scheduled in such a way that the
4+
maximum number of non-overlapping activities is selected.
5+
This is a greedy algorithm where at each step,
6+
we choose the activity that finishes the earliest
7+
and does not conflict with previously selected activities.
8+
9+
Wikipedia: https://en.wikipedia.org/wiki/Activity_selection_problem
10+
"""
11+
12+
13+
def activity_selection(activities: list[tuple[int, int]]) -> list[tuple[int, int]]:
14+
"""
15+
Solve the Activity Selection Problem using a greedy algorithm by selecting
16+
the maximum number of non-overlapping activities from a list of activities.
17+
18+
Parameters:
19+
activities: A list of tuples where each tuple contains
20+
the start and end times of an activity.
21+
22+
Returns:
23+
A list of selected activities that are non-overlapping.
24+
25+
Example:
26+
>>> activity_selection([(1, 3), (2, 5), (3, 9), (6, 8)])
27+
[(1, 3), (6, 8)]
28+
29+
>>> activity_selection([(0, 6), (1, 4), (3, 5), (5, 7), (5, 9), (8, 9)])
30+
[(1, 4), (5, 7), (8, 9)]
31+
32+
>>> activity_selection([(1, 2), (2, 4), (3, 5), (0, 6)])
33+
[(1, 2), (2, 4)]
34+
35+
>>> activity_selection([(5, 9), (1, 2), (3, 4), (0, 6)])
36+
[(1, 2), (3, 4), (5, 9)]
37+
38+
>>> all(activity_selection(x) == [] for x in ([], {}, None, False, 0, 0.0))
39+
True
40+
"""
41+
if not activities:
42+
return []
43+
44+
# Step 1: Sort the activities by their end time
45+
sorted_activities = sorted(activities, key=lambda activity: activity[1])
46+
47+
# Step 2: Select the first activity (the one that finishes the earliest)
48+
# as the initial activity
49+
selected_activities = [sorted_activities[0]]
50+
51+
# Step 3: Iterate through the sorted activities and select the ones
52+
# that do not overlap with the last selected activity
53+
for i in range(1, len(sorted_activities)):
54+
if sorted_activities[i][0] >= selected_activities[-1][1]:
55+
selected_activities.append(sorted_activities[i])
56+
57+
return selected_activities

0 commit comments

Comments
 (0)