forked from jainaman224/Algo_Ds_Notes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathActivity_Selection.py
35 lines (31 loc) · 1.12 KB
/
Activity_Selection.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
'''
The problem statement for Activity Selection is that "Given a set of n activities with their start and finish times, we need to select maximum number of non-conflicting activities that can be performed by a single person, given that the person can handle only one activity
at a time." The Activity Selection problem follows a Greedy approach.
'''
# Function to compute the activities to be chosen
def activitySelection(s, f ):
n = len(f)
print("Following activities are selected:")
i = 0
#First Activity is always selected
print(i)
for j in range(0,n):
if s[j] >= f[i]: #We select only those activities whose starting time is greater than equal to
print(j) #finishing time of previously selected activity.
i = j
#The array of n elements where s[i] denotes starting time of ith activity
s = [1, 3, 1, 5, 6, 8]
#The array of n elements where f[i] denotes finishing time of ith activity
f = [2, 6, 6, 7, 8, 10]
# Call to activitySelection function
activitySelection(s, f)
'''
Input:
Start
1 3 1 5 6 8
Finish
2 6 6 7 8 10
Output:
Following activities are selected
0 1 4 5
'''