# Activity Selection Problem:

Following is the 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.
```Example 1 : Consider the following 3 activities sorted by finish time.
StartTime[]  =  {10, 12, 20};
FinishTime[] =  {20, 25, 30};
Maximum number of tasks performed by person is 2.Maximum set of activities that can be executed
is {0, 2} [ These are indexes in StartTmie[] and FinishTime[] ]

Example 2 : Consider the following 6 activities
sorted by by finish time.
startTime[]  =  {1, 3, 0, 5, 8, 5};
finishTime[] =  {2, 4, 6, 7, 9, 9};
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 startTime[] and finishTime[] ]```
The greedy choice is to always pick the next activity whose finish time is least among the remaining activities and the start time is more than or equal to the finish time of previously selected activity. We can sort the activities according to their finishing time so that we always consider the next activity as minimum finishing time activity.
1) Sort the activities according to their finishing time
2) Select the first activity from the sorted array and print it.
3) Do following for remaining activities in the sorted array.
-------.a) If the start time of this activity is greater than or equal to the finish time of previously selected activity then select this activity and print it.
How does Greedy Choice work for Activities sorted according to finish time?
Let the given set of activities be S = {1, 2, 3, ..n} and activities be sorted by finish time. The greedy choice is to always pick activity 1. How come the activity 1 always provides one of the optimal solutions. We can prove it by showing that if there is another solution B with the first activity other than 1, then there is also a solution A of the same size with activity 1 as the first activity. Let the first activity selected by B be k, then there always exist A = {B %u2013 {k}} U {1}.(Note that the activities in B are independent and k has smallest finishing time among all. Since k is not 1, finish(k) >= finish(1))

#include <bits/stdc++.h>
using namespace std;

struct activity
{
int start, finish;
};
bool Compare(activity s1, activity s2)
{
return (s1.finish < s2.finish);
}

void print(activity vec[], int n)
{
sort(vec, vec+n, Compare);
cout << "Following activities are selected - ";
int i = 0;
cout << "(" << vec[i].start << ", " << vec[i].finish << "), ";
for (int j = 1; j < n; j++)
{
if (vec[j].start >=vec[i].finish)
{
cout << "(" << vec[j].start << ", "
<< vec[j].finish << "), ";
i = j;
}
}
}
int main()
{
activity vec[] = {{4, 8}, {2, 5}, {4, 6}, {1, 4},
{3, 7}, {8, 9}};
int n = sizeof(vec)/sizeof(vec[0]);
print(vec, n);
return 0;
}

Output:
Following activities are selected - (1,4), (4,6), (8,9)

#### More Articles of M Mounika:

Name Views Likes
C++ Segmented Sieve (Print Primes In a Range) 162 0
C++ Sieve Of Erastosthenes 135 0
C++ Gold Mine Problem 295 0
C++ Merge K Sorted Arrays 116 0
C++ K Centers Problem 239 0
C++ Find Nth Catalan Number 311 0
C++ Inplace Rotate square matrix by 90 degrees 285 0
C++ Find Non Repeating Elements in Array 86 0
C++ Merge Two Binary Trees 120 0
C++ Sum of Numbers From Root To Leaf Paths 89 0
C++ Meta Strings 91 0
C++ Flood Fill Algorithm 402 0
C++ smallest substring with maximum distinct characters 199 0
C++ Smallest window with all characters in string 93 0
C++ Minimum Removal of Characters from string to make its permutation as palindrome 86 0
C++ Minimum characters added at front of string in palindrome conversion 69 0
C++ Number of Bracket Reversals needed to make expression Balanced 72 0
C++ String to Palindrome with Append Function 83 0
C++ WildCard pattern matching 75 0
C++ Anagram substring Search 72 0
C++ Manachars Algorithm 74 0
C++ Search String in Grid 83 0
C++ String Matching(Z Algorithm) 67 0
C++ String Matching(Naive Algorithm) 113 0
C++ String Matching(KMP Algorithm) 140 0
C++ Remove Duplicates From String 110 0
C++ Basics of String Manipulation 85 1
C++ Disjoint Data Structure Cycle Detection 86 0
C++ Problem On Disjoint Data Structures 94 0
C++ Disjoint Data Structures Part3 78 0
Disjoint Data Structures Part2 90 0
Disjoint Data Structures 93 1
C++ Segment Trees 321 2
C++ Trie Cost of Data 290 1
C++ Trie Datastructure 278 1
C++ Greedy Approach Minimum number of coins 525 0
C++ Greedy Approach Maximum height Pyramid 328 1
C++ Greedy Approach String lexicographically largest subsequence 246 0
C++ Greedy Approach Lexicographically largest subsequence 364 0
C++ Greedy Approach Prims MST 398 1
C++ Greedy Approach Krushkals MST 457 1
C++ Greedy Approach N-array maximum sum 333 1
C++ Greedy Approach Policemen Catch Thieves 563 1
C++ Greedy Approach Maximum product Subset 546 1
C++ Greedy Approach Minimum Product Subset 348 1
C++ Greedy Approach Fractional Knapsack 737 1
C++ Greedy Approach-Activity Selection Problem 745 1
C++ Greedy Approach-Egyptian Fractions 639 0
C++ Greedy Approach-Huffman Codes 1031 1
C++ Introduction to Greedy Approach 955 2