Skip to content

Commit 5f4479f

Browse files
Merge pull request #206 from abhisheks008/main
Activity Selection Problem using Greedy Approach
2 parents b89a4d7 + d92bbad commit 5f4479f

File tree

4 files changed

+155
-0
lines changed

4 files changed

+155
-0
lines changed
Loading
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# Activity Selection Problem using Greedy Approach
2+
🔴 Language used : **Python 3**
3+
4+
## 🎯 Aim
5+
The aim of this script is to find out the maximum number of activities to be performed by a single person in between the start and finish time given in the problem.
6+
7+
## 👉 Purpose
8+
The main purpose of this script is to show the implementation of Greedy Approach to find out the maximum number of activities to be performed by a single person in between the start and finish time given in the problem.
9+
10+
## 📄 Description
11+
You are given n activities with their start and finish times. Select the maximum number of activities that can be performed by a single person, assuming that a person can only work on a single activity at a time.
12+
13+
Suppose `S = {1, 2....n}` is the set of n proposed activities. The activities share resources which can be used by only one activity at a time, e.g., Tennis Court, Lecture Hall, etc. Each Activity `i` has start time si and a finish time `fi`, where `si ≤fi`. If selected activity `i` take place meanwhile the half-open time interval `[si,fi)`. Activities `i` and `j` are compatible if the intervals `(si, fi)` and `[si, fi)` do not overlap (i.e. `i` and `j` are compatible if `si≥fi` or `si≥fi)`. The activity-selection problem chosen the maximum-size set of mutually consistent activities.
14+
15+
🔴 Examples:
16+
17+
```
18+
Constraints:
19+
start[] -> an array of start time for the activities.
20+
finish[] -> an array of finish time for the activities.
21+
output[] -> an array of maximum set of activities to be done by a person.
22+
23+
Input:
24+
Consider the following 6 activities sorted by finish time.
25+
26+
start[] = {1, 3, 0, 5, 8, 5};
27+
finish[] = {2, 4, 6, 7, 9, 9};
28+
29+
Output:
30+
A person can perform at most four activities. The maximum set of activities that can be executed is {0, 1, 3, 4}
31+
[These are indexes in start[] and finish[]]
32+
```
33+
34+
## 📊 Flowchart
35+
```
36+
printMaxActivities(s, f)
37+
1. n ← length [s]
38+
2. A ← {1}
39+
3. j ← 1.
40+
4. for i ← 2 to n
41+
5. do if si ≥ fi
42+
6. then A ← A ∪ {i}
43+
7. j ← i
44+
8. return A
45+
```
46+
47+
## 🧮 Algorithm
48+
- This algorithm is called Greedy-Iterative-Activity-Selector, because it is first of all a greedy algorithm, and then it is iterative. There's also a recursive version of this greedy algorithm.
49+
- Sorts in increasing order of finish times the array of activities `A` by using the finish times stored in the array `f`. This operation can be done in `O(n log n)` time, using for example merge sort, heap sort, or quick sort algorithms.
50+
- Creates a set `S` to store the selected activities, and initialises it with the activity that has the earliest finish time.
51+
- Creates a variable `k` that keeps track of the index of the last selected activity.
52+
- Starts iterating from the second element of that array `A` up to its last element.
53+
- If the start time `s[i]` of the `ith` activity `A[i]` is greater or equal to the finish time `f[k]` of the last selected activity `A[k]`, then `A[i]` is compatible to the selected activities in the set `S`, and thus it can be added to `S`.
54+
- The index of the last selected activity is updated to the just added activity `A[i]`.
55+
56+
57+
## 💻 Input and Output
58+
- **Test Case 1 :**
59+
```
60+
Input Given :
61+
s = [1 , 3 , 0 , 5 , 8 , 5]
62+
f = [2 , 4 , 6 , 7 , 9 , 9]
63+
```
64+
65+
![](https://github.com/abhisheks008/PyAlgo-Tree/blob/main/Greedy/Activity%20Selection%20Problem/Images/asp-1.png)
66+
67+
- **Test Case 2 :**
68+
```
69+
Input Given :
70+
s = [1, 2, 3, 4, 7, 8, 9, 9, 11, 12]
71+
f = [3, 5, 4, 7, 10, 9, 11, 13, 12, 14]
72+
```
73+
![](https://github.com/abhisheks008/PyAlgo-Tree/blob/main/Greedy/Activity%20Selection%20Problem/Images/asp-2.png)
74+
75+
## ⏰ Time and Space complexity
76+
- **Time Complexity :** `O(n)`.
77+
- **Space Complexity :** `O(1)`.
78+
79+
---------------------------------------------------------------
80+
## 🖋️ Author
81+
**Code contributed by, _Abhishek Sharma_, 2022 [@abhisheks008](github.com/abhisheks008)**
82+
83+
[![forthebadge made-with-python](http://ForTheBadge.com/images/badges/made-with-python.svg)](https://www.python.org/)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# Problem Name: Activity Selection Problem
2+
# Method used: Greedy Approach
3+
4+
# -------------------------------------------------------------------------
5+
6+
# Problem Statement: You are given n activities with their start and finish times.
7+
# Select the maximum number of activities that can be performed
8+
# by a single person, assuming that a person can only work on a
9+
# single activity at a time.
10+
11+
# -------------------------------------------------------------------------
12+
13+
# Constraints:
14+
# n --> Total number of activities
15+
# s[]--> An array that contains start time of all activities
16+
# f[] --> An array that contains finish time of all activities
17+
18+
19+
# Let's solve the problem.
20+
21+
# This is the actual function, which is going to find out the result for this problem.
22+
# The function printMaxActivities takes two parameters, s and f
23+
# s[] denotes all the start time activities.
24+
# f[] denotes all the finish time activities.
25+
def printMaxActivities(s, f ):
26+
n = len(f)
27+
print ("The following activities are selected")
28+
29+
# The first activity is always selected
30+
i = 0
31+
print (i,end=' ')
32+
33+
# Consider rest of the activities
34+
for j in range(1, n):
35+
36+
# If this activity has start time greater than
37+
# or equal to the finish time of previously
38+
# selected activity, then select it
39+
if s[j] >= f[i]:
40+
print (j,end=' ')
41+
i = j
42+
43+
# Driver program to test above function
44+
print ("-- Activity Selection Problem using Greedy Approach --\n")
45+
s = [1 , 3 , 0 , 5 , 8 , 5]
46+
f = [2 , 4 , 6 , 7 , 9 , 9]
47+
print ("** Activities during start time **")
48+
print (s)
49+
print ("** Activities during finish time **")
50+
print (f)
51+
print ()
52+
printMaxActivities(s , f)
53+
54+
55+
# -------------------------------------------------------------------------
56+
57+
# Output:
58+
# -- Activity Selection Problem using Greedy Approach --
59+
60+
# ** Activities during start time **
61+
# [1, 3, 0, 5, 8, 5]
62+
# ** Activities during finish time **
63+
# [2, 4, 6, 7, 9, 9]
64+
65+
# The following activities are selected
66+
# 0 1 3 4
67+
68+
# -------------------------------------------------------------------------
69+
70+
# Code contributed by, Abhishek Sharma, 2022
71+
72+
# -------------------------------------------------------------------------

0 commit comments

Comments
 (0)