Activity Selection Problem
Activity Selection Problem
Activity Selection Problem
Each activity is marked by a start and finish time. Greedy technique is used
for finding the solution since this is an optimization problem.
Let's consider that you have n activities with their start and finish times, the
objective is to find solution set having maximum number of non-
conflicting activities that can be executed in a single time frame,
assuming that only one person or machine is available for execution.
sol[] array refering to the solution set containing the maximum number
of non-conflicting activities.
Steps for Activity Selection Problem
Following are the steps we will be following to solve the activity selection
problem,
Step 2: Select the first activity from sorted array act[] and add it
to sol[] array.
Step 4: If the start time of the currently selected activity is greater than or
equal to the finish time of previously selected activity, then add it to
the sol[] array.
In the table below, we have 6 activities with corresponding start and end
time, the objective is to compute an execution schedule having maximum
number of non-conflicting activities:
Finish Time
Start Time (s) Activity Name
(f)
5 9 a1
1 2 a2
3 4 a3
0 6 a4
5 7 a5
8 9 a6
A possible solution would be:
Finish Time
Start Time (s) Activity Name
(f)
1 2 a2
3 4 a3
0 6 a4
5 7 a5
5 9 a1
8 9 a6
Step 2: Select the first activity from sorted array act[] and add it to
the sol[] array, thus sol = {a2}.
Step 3: Repeat the steps 4 and 5 for the remaining activities in act[].
Step 4: If the start time of the currently selected activity is greater than or
equal to the finish time of the previously selected activity, then add it to sol[].
A. Select activity a3. Since the start time of a3 is greater than the finish
time of a2 (i.e. s(a3) > f(a2)), we add a3 to the solution set. Thus sol =
{a2, a3}.
B. Select a4. Since s(a4) < f(a3), it is not added to the solution set.
C. Select a5. Since s(a5) > f(a3), a5 gets added to solution set. Thus sol =
{a2, a3, a5}
D. Select a1. Since s(a1) < f(a5), a1 is not added to the solution set.
E. Select a6. a6 is added to the solution set since s(a6) > f(a5). Thus sol =
{a2, a3, a5, a6}.
Finish Time
Start Time (s) Activity Name
(f)
1 2 a2
3 4 a3
0 6 a4
5 7 a5
5 9 a1
8 9 a6
In the above diagram, the selected activities have been highlighted in grey.
Implementation of Activity Selection Problem
Algorithm
Now that we have an overall understanding of the activity selection problem
as we have already discussed the algorithm and its working details with the
help of an example, following is the C++ implementation for the same.
#include <bits/stdc++.h>
using namespace std;
#define N 6 // defines the number of activities
// Structure represents an activity having start time and finish time.
struct Activity
{
int start, finish;
};
// Driver program
int main()
{
Activity arr[N];
for(int i=0; i<=N-1; i++)
{
cout<<"Enter the start and end time of "<<i+1<<" activity \n";
cin>>arr[i].start>>arr[i].finish;
}
print_Max_Activities(arr, N);
return 0;
}
Following are the scenarios for computing the time complexity of Activity
Selection Algorithm: