forked from jainaman224/Algo_Ds_Notes
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Activity_Selection.c
51 lines (46 loc) · 1.32 KB
/
Activity_Selection.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*
The problem statement for Activity Selection is that "Given a set of n activities with their start and finish times, we need to select maximum number of non-conflicting activities that can be performed by a single person, given that the person can handle only one activity
at a time." The Activity Selection problem follows a Greedy approach.
*/
#include <stdio.h>
// Function to compute the activities to be chosen
void ActivitySelection(int start[], int finish[], int n)
{
printf("Following activities are selected: ");
// Select the first activity
int i = 0;
printf("\n%d ", i);
int j;
// if start time of current activity j is greater than or equal to previous activity chosen, select activity j
for (j = 1; j < n; j++)
{
if (start[j] >= finish[i])
{
printf("%d ", j);
i = j;
}
}
}
// Driver function
int main()
{
// starting time of activities
int start[] = {1, 3, 1, 5, 6, 8};
// sorted finish time of activities
int finish[] = {2, 6, 6, 7, 8, 10};
// computing size of array
int n = sizeof(start) / sizeof(start[0]);
// call to Activity Selection function
ActivitySelection(start, finish, n);
return 0;
}
/*
Input:
Start
1 3 1 5 6 8
Finish
2 6 6 7 8 10
Output:
Following activities are selected
0 1 4 5
*/