Activity Selection Problem



Activity Selection Problem

The activity selection problem is an example of a greedy algorithm where the maximum number of non-overlapping activities are selected from the given activity set. A person can complete one activity at a time. The activities are given in the form of their starting and completion times.

In this article, we have an array of integers that stores the starting and completion time of each activity. Our task is to select the maximum number of non-overlapping activities from the given activity array.

Scenario

An example of the maximum activity selection problem is given below:

Input:
arr[] = {{5,9},{1,2},{3,4},{0,6},{5,7},{8,9}}

Output:
4

The visual explanation of the above example is given below:

Activity Selection Problem Example

In the figure above, we have selected the maximum number of activities marked in the green color. The selected activities are non-overlapping, i.e., no two activities are occurring simultaneously. To select the maximum activities, we rejected the w1(5,9) and selected two other activities, i.e., w5(5,7) and w6(8,9).

Algorithm to Solve Activity Selection Problem

The steps to solve the activity selection problem are given below:

  • First, we sort the given activity array in ascending order based on the completion time of each activity.
  • After sorting, we select the first activity.
  • Then, for rest of the activities, we compare the starting time of the current activity (j) with the completion time of the previous activity (i).
  • If the starting time of the current activity is greater than or equal to the completion time of the previous activity, we select this activity as it is a non-overlapping activity.
  • Then, we update the value of i with j and increase the count by 1.
  • After completion of the loop, we return the count of total selected activities.

C++ Implementation of Activity Selection Problem

Here is the code implementation of the above steps for solving the activity selection problem using the greedy algorithm:

#include <iostream>
#include <algorithm>
using namespace std;

struct Activity
{
   int start, end;
};

bool comp(Activity act1, Activity act2)
{
   return (act1.end < act2.end);
}

void maxActivity(Activity act[], int n)
{
   sort(act, act + n, comp);
   cout << "
Selected Activities are: " << endl; int i = 0, count = 1; cout << "Activity " << i << "-> Start: " << act[i].start << " End: " << act[i].end << endl; for (int j = 1; j < n; j++) { if (act[j].start >= act[i].end) { cout << "Activity " << j << "-> Start: " << act[j].start << " End: " << act[j].end << endl; i = j; count++; } } cout << "
Total Selected Activities: " << count << endl; } int main() { Activity actArr[] = {{5, 9}, {1, 2}, {3, 4}, {0, 6}, {5, 7}, {8, 9}}; int n = 6; cout << "Activities with their start and completion time: " << endl; for (int i = 0; i < n; i++) { cout << "Activity " << i << "-> Start: " << actArr[i].start << " End: " << actArr[i].end << endl; } maxActivity(actArr, n); return 0; }

The output of the above code is as follows:

Activities with their start and completion time: 
Activity 0-> Start: 5 End: 9
Activity 1-> Start: 1 End: 2
Activity 2-> Start: 3 End: 4
Activity 3-> Start: 0 End: 6
Activity 4-> Start: 5 End: 7
Activity 5-> Start: 8 End: 9

Selected Activities are: 
Activity 0-> Start: 1 End: 2
Activity 1-> Start: 3 End: 4
Activity 3-> Start: 5 End: 7
Activity 5-> Start: 8 End: 9

Total Selected Activities: 4

Complexity of Activity Selection Problem

The time and space complexity to solve the activity selection problem using the greedy approach is given below:

  • The time complexity of the activity selection problem using the greedy approach is O(n log n). If the completion time is given in sorted order then, the time complexity becomes O(n).
  • The space complexity of the activity selection problem using the greedy approach is O(n).

Conclusion

In this article, we discussed the activity selection problem and understood the algorithm to solve the problem using the greedy approach and its code implementation.

Updated on: 2025-07-21T19:07:32+05:30

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements