C Program for Activity Selection Problem

The activity selection problem is a classic optimization problem where we are given a set of activities with their starting and finishing times. The goal is to select the maximum number of activities that can be performed by a single person, given that only one activity can be performed at a time.

This problem is efficiently solved using a greedy algorithm approach. The greedy algorithm makes locally optimal choices at each step, hoping to find a global optimum. It selects activities based on their finishing times in ascending order.

Syntax

void activitySelection(int start[], int finish[], int n);

Algorithm

The greedy approach for activity selection follows these steps −

  1. Sort activities by their finishing time (assume already sorted)
  2. Select the first activity
  3. For remaining activities, select an activity if its start time is greater than or equal to the finish time of the previously selected activity
Activity Selection Timeline 0 10 15 20 25 A1: (1,10) A2: (5,13) A3: (12,23) Selected Activity Not Selected Maximum activities selected: A1 and A3

Example

Here's a complete C program that implements the activity selection algorithm −

#include <stdio.h>

void activitySelection(int start[], int finish[], int n) {
    int i, j;
    
    printf("Selected activities: ");
    
    // First activity is always selected
    i = 0;
    printf("A%d ", i + 1);
    
    // Consider remaining activities
    for (j = 1; j < n; j++) {
        // If start time of current activity is greater than or equal 
        // to finish time of previously selected activity
        if (start[j] >= finish[i]) {
            printf("A%d ", j + 1);
            i = j;
        }
    }
    printf("<br>");
}

int main() {
    int start[] = {1, 5, 12, 3, 8};
    int finish[] = {10, 13, 23, 7, 12};
    int n = sizeof(start) / sizeof(start[0]);
    
    printf("Activities with start and finish times:<br>");
    for (int i = 0; i < n; i++) {
        printf("A%d: Start=%d, Finish=%d<br>", i + 1, start[i], finish[i]);
    }
    printf("<br>");
    
    // Note: Activities should be sorted by finish time for optimal solution
    // For this example, we assume they are already sorted
    activitySelection(start, finish, n);
    
    return 0;
}
Activities with start and finish times:
A1: Start=1, Finish=10
A2: Start=5, Finish=13
A3: Start=12, Finish=23
A4: Start=3, Finish=7
A5: Start=8, Finish=12

Selected activities: A1 A3

Key Points

  • The greedy approach gives the optimal solution for activity selection
  • Activities must be sorted by their finishing time for the algorithm to work correctly
  • Time complexity: O(n) if activities are pre-sorted, O(n log n) including sorting
  • Space complexity: O(1) as only a few variables are used

Conclusion

The activity selection problem demonstrates the power of greedy algorithms in solving optimization problems. By always choosing the activity that finishes earliest, we maximize the number of activities that can be performed.

Updated on: 2026-03-15T12:33:35+05:30

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements