From 20551f1c0bd082e25327e6bdba11b9b0107428ba Mon Sep 17 00:00:00 2001 From: bansal Date: Sat, 5 Oct 2024 15:14:30 +0530 Subject: [PATCH 1/8] [Activity selection] --- .idea/.gitignore | 3 ++ greedy_algorithms/activity_selection.cpp | 42 ++++++++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 .idea/.gitignore create mode 100644 greedy_algorithms/activity_selection.cpp diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 00000000000..26d33521af1 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/greedy_algorithms/activity_selection.cpp b/greedy_algorithms/activity_selection.cpp new file mode 100644 index 00000000000..70b99b22d16 --- /dev/null +++ b/greedy_algorithms/activity_selection.cpp @@ -0,0 +1,42 @@ +// C++ program for activity selection problem. + +// The following implementation assumes that the activities +// are already sorted according to their finish time +#include +using namespace std; + +// Prints a maximum set of activities that can be done by a +// single person, one at a time. +void printMaxActivities(int s[], int f[], int n) +{ + int i, j; + + cout << "Following activities are selected" << endl; + + // The first activity always gets selected + i = 0; + cout << i << " "; + + // Consider rest of the activities + for (j = 1; j < n; j++) { + // If this activity has start time greater than or + // equal to the finish time of previously selected + // activity, then select it + if (s[j] >= f[i]) { + cout << j << " "; + i = j; + } + } +} + +// Driver code +int main() +{ + int s[] = { 1, 3, 0, 5, 8, 5 }; + int f[] = { 2, 4, 6, 7, 9, 9 }; + int n = sizeof(s) / sizeof(s[0]); + + // Function call + printMaxActivities(s, f, n); + return 0; +} \ No newline at end of file From f744ce770193bcce07703519ff36e0dcae1fc8d3 Mon Sep 17 00:00:00 2001 From: bansal Date: Sat, 5 Oct 2024 15:18:12 +0530 Subject: [PATCH 2/8] [Activity selection] --- greedy_algorithms/activity_selection.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/greedy_algorithms/activity_selection.cpp b/greedy_algorithms/activity_selection.cpp index 70b99b22d16..ea38a87ba37 100644 --- a/greedy_algorithms/activity_selection.cpp +++ b/greedy_algorithms/activity_selection.cpp @@ -2,7 +2,7 @@ // The following implementation assumes that the activities // are already sorted according to their finish time -#include +#include using namespace std; // Prints a maximum set of activities that can be done by a From 013a3d666cc9933a4a63737fcc93a73653fddcb2 Mon Sep 17 00:00:00 2001 From: bansal Date: Sat, 5 Oct 2024 21:56:12 +0530 Subject: [PATCH 3/8] Upating activity selection --- greedy_algorithms/activity_selection.cpp | 58 +++++++++++++++++------- 1 file changed, 41 insertions(+), 17 deletions(-) diff --git a/greedy_algorithms/activity_selection.cpp b/greedy_algorithms/activity_selection.cpp index ea38a87ba37..47989d209b9 100644 --- a/greedy_algorithms/activity_selection.cpp +++ b/greedy_algorithms/activity_selection.cpp @@ -1,42 +1,66 @@ -// C++ program for activity selection problem. - -// The following implementation assumes that the activities -// are already sorted according to their finish time +/** +Problem Statement: +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. +Solution: +Since the activities are sorted in increasing order of finish time, we chose the activity whose finish time is less than +all other activities and start time is more than the previous activities finish time. We should sort the activities in +increasing order of finish time of activities. +*/ +// The assumption here is input activities will be provided in sorted order of finish time of activities. +#include /// for assert #include -using namespace std; +#include // Prints a maximum set of activities that can be done by a // single person, one at a time. -void printMaxActivities(int s[], int f[], int n) +std::vector getMaxActivities(std::vector start, std::vector finish, int n) { int i, j; + std::vector ans; - cout << "Following activities are selected" << endl; - // The first activity always gets selected + // Since we greedily start selecting the activities the first activity is always selected i = 0; - cout << i << " "; + ans.push_back(i); - // Consider rest of the activities + // Considering the rest of the activities for (j = 1; j < n; j++) { // If this activity has start time greater than or // equal to the finish time of previously selected // activity, then select it - if (s[j] >= f[i]) { - cout << j << " "; + if (start[j] >= finish[i]) { + ans.push_back(j); i = j; } } + return ans; +} + +static void tests() { + std::vector start = {1, 3, 0, 5, 8, 5}; + std::vector finish = {2, 4, 6, 7, 9, 9}; + + std::vector ans = getMaxActivities(start, finish, start.size()); + std::vector activities = {0, 1, 6, 4}; + + for(int i = 0 ; i < activities.size(); i++){ + uint64_t activityNumber = ans[i]; + assert(activityNumber == activities[i]); + } } // Driver code int main() { - int s[] = { 1, 3, 0, 5, 8, 5 }; - int f[] = { 2, 4, 6, 7, 9, 9 }; - int n = sizeof(s) / sizeof(s[0]); - + //tests(); + std::vector start = {1, 3, 0, 5, 8, 5}; + std::vector finish = {2, 4, 6, 7, 9, 9}; // Function call - printMaxActivities(s, f, n); + std::vector ans = getMaxActivities(start, finish, start.size()); + std::cout << "Following activities are selected" << "\n"; + for(int x: ans) { + std::cout << x <<", "; + } return 0; } \ No newline at end of file From 19814d9bb3b5c0858cbfb4641d8395536d8aae32 Mon Sep 17 00:00:00 2001 From: Chhavi Bansal Date: Sat, 5 Oct 2024 21:56:43 +0530 Subject: [PATCH 4/8] Delete .idea/.gitignore --- .idea/.gitignore | 3 --- 1 file changed, 3 deletions(-) delete mode 100644 .idea/.gitignore diff --git a/.idea/.gitignore b/.idea/.gitignore deleted file mode 100644 index 26d33521af1..00000000000 --- a/.idea/.gitignore +++ /dev/null @@ -1,3 +0,0 @@ -# Default ignored files -/shelf/ -/workspace.xml From 2582066475fd6c9945c36d84be78ae5a88a94d5d Mon Sep 17 00:00:00 2001 From: bansal Date: Sat, 5 Oct 2024 21:57:57 +0530 Subject: [PATCH 5/8] adding blanck line --- .idea/workspace.xml | 8 ++++++++ greedy_algorithms/activity_selection.cpp | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) create mode 100644 .idea/workspace.xml diff --git a/.idea/workspace.xml b/.idea/workspace.xml new file mode 100644 index 00000000000..198e0d3035c --- /dev/null +++ b/.idea/workspace.xml @@ -0,0 +1,8 @@ + + + + + \ No newline at end of file diff --git a/greedy_algorithms/activity_selection.cpp b/greedy_algorithms/activity_selection.cpp index 47989d209b9..b133a2bdfe4 100644 --- a/greedy_algorithms/activity_selection.cpp +++ b/greedy_algorithms/activity_selection.cpp @@ -63,4 +63,4 @@ int main() std::cout << x <<", "; } return 0; -} \ No newline at end of file +} From 5c3cca6ef40b1d334504ad5597935458d569ca04 Mon Sep 17 00:00:00 2001 From: Chhavi Bansal Date: Sat, 5 Oct 2024 21:59:56 +0530 Subject: [PATCH 6/8] Delete .idea/workspace.xml --- .idea/workspace.xml | 8 -------- 1 file changed, 8 deletions(-) delete mode 100644 .idea/workspace.xml diff --git a/.idea/workspace.xml b/.idea/workspace.xml deleted file mode 100644 index 198e0d3035c..00000000000 --- a/.idea/workspace.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - \ No newline at end of file From eb3e13011253d8fec8d7d7d0fd1f37a6e7139771 Mon Sep 17 00:00:00 2001 From: chh Date: Tue, 15 Oct 2024 23:18:43 +0530 Subject: [PATCH 7/8] activity selection --- greedy_algorithms/activity_selection.cpp | 25 ++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/greedy_algorithms/activity_selection.cpp b/greedy_algorithms/activity_selection.cpp index b133a2bdfe4..447132ed11c 100644 --- a/greedy_algorithms/activity_selection.cpp +++ b/greedy_algorithms/activity_selection.cpp @@ -42,25 +42,30 @@ static void tests() { std::vector finish = {2, 4, 6, 7, 9, 9}; std::vector ans = getMaxActivities(start, finish, start.size()); - std::vector activities = {0, 1, 6, 4}; + // activity 0, 1, 3, 4 are selected since their start time is greater than the end time of the previous activity + std::vector activities = {0, 1, 3, 4}; for(int i = 0 ; i < activities.size(); i++){ uint64_t activityNumber = ans[i]; assert(activityNumber == activities[i]); } + std::vector start2 = {10, 12, 20}; + std::vector finish2 = {20, 25, 30}; + + std::vector ans2 = getMaxActivities(start2, finish2, start2.size()); + // only the first activity is chosen, others dont fulfill the criteria + std::vector activities2 = {0}; + + for(int i = 0 ; i < activities2.size(); i++){ + uint64_t activityNumber = ans2[i]; + assert(activityNumber == activities2[i]); + } + } // Driver code int main() { - //tests(); - std::vector start = {1, 3, 0, 5, 8, 5}; - std::vector finish = {2, 4, 6, 7, 9, 9}; - // Function call - std::vector ans = getMaxActivities(start, finish, start.size()); - std::cout << "Following activities are selected" << "\n"; - for(int x: ans) { - std::cout << x <<", "; - } + tests(); return 0; } From e39a07a9c24737e3358e851f6b177ee9fe75b988 Mon Sep 17 00:00:00 2001 From: chh Date: Sat, 19 Oct 2024 15:09:53 +0530 Subject: [PATCH 8/8] activity selection --- greedy_algorithms/activity_selection.cpp | 34 +++++++++++++++++++----- 1 file changed, 27 insertions(+), 7 deletions(-) diff --git a/greedy_algorithms/activity_selection.cpp b/greedy_algorithms/activity_selection.cpp index 447132ed11c..a7027fafed9 100644 --- a/greedy_algorithms/activity_selection.cpp +++ b/greedy_algorithms/activity_selection.cpp @@ -1,12 +1,32 @@ /** -Problem Statement: -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. -Solution: -Since the activities are sorted in increasing order of finish time, we chose the activity whose finish time is less than -all other activities and start time is more than the previous activities finish time. We should sort the activities in -increasing order of finish time of activities. + * @file + * @brief + * [https://www.geeksforgeeks.org/greedy-algorithms/] - Greedy algorithm + * Problem info: https://www.geeksforgeeks.org/activity-selection-problem-greedy-algo-1/ + * + * @details + * 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. + * + * Input: start[] = {10, 12, 20}, finish[] = {20, 25, 30} + * Output: 0 + * Explanation: A person can perform at most one activities. + * + * + * Input: start[] = {1, 3, 0, 5, 8, 5}, finish[] = {2, 4, 6, 7, 9, 9}; + * Output: 0 1 3 4 + * Explanation: A person can perform at most four activities. The + * maximum set of activities that can be executed + * is {0, 1, 3, 4} [ These are indexes in start[] and finish[] + * + * @Approach + * Since the activities are sorted in increasing order of finish time, we chose the activity whose finish time is less than + * all other activities and start time is more than the previous activities finish time. We should sort the activities in + * increasing order of finish time of activities. + * + * @author [Chhavi Bansal](https://github.com/chhavibansal) */ + // The assumption here is input activities will be provided in sorted order of finish time of activities. #include /// for assert #include