What is Activity selection?
The Activity Selection Problem is an optimization problem which deals with the selection of non-conflicting activities that needs to be executed by a single person or machine in a given time frame.
Code:
#include <stdio.h>
void printMaxActivities(int s[], int f[], int n)
{
int i, j;
printf("Following activities are selected \n");
i = 0;
printf("%d ", 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])
{
printf("%d ", j);
i = j;
}
}
}
// driver program to test above function
int main()
{
int i, size;
printf("\nAim: Activity Selection in C");
printf("\nUser Input: By user");
printf("\n==============================================\n");
printf("Enter the no of activity: ");
scanf("%d", &size);
int s[size];
int f[size];
printf("Enter the %d activity :\n", size);
printf("Activity No \t Start Time \t Finish Time\n");
for (i = 0; i < size; i++)
{
printf("Activity[%d]: \t", i + 1);
scanf("%d\t%d", &s[i], &f[i]);
}
int n = sizeof(s) / sizeof(s[0]);
printMaxActivities(s, f, n);
return 0;
}
Output: