|
| 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 | + |
| 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 | + |
| 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 | +[](https://www.python.org/) |
0 commit comments