The Activity Selection Problem is a classic example of optimization using greedy algorithms. The problem is simple: given a list of activities with a start and finish time, the goal is to select the maximum number of non-overlapping activities. This is a typical problem that can be efficiently solved using a greedy strategy.

 

Problem Statement

You are given a list of activities, each with a start time l and finish time r. Your task is to select the maximum number of non-overlapping activities. The solution must be efficient, meaning we need to find the optimal solution without testing every possible combination.

 

Greedy Approach

The goal is to select the maximum number of non-overlapping activities from a list, each defined by a start time and finish time. The greedy approach works by making the optimal choice at each step, ensuring a globally optimal solution.

The process begins by sorting the activities based on their finish times. The first activity, which finishes the earliest, is always selected. Then, for each subsequent activity, we select it if its start time is greater than or equal to the finish time of the last selected activity. This ensures no overlap.

The key to the greedy approach is the greedy choice property: always choose the activity that finishes first to leave room for future activities. This guarantees the maximum number of activities can be selected.

 

Implementation

 

Time Complexity

Sorting the activities takes $O(n \log n)$, where n is the number of activities. The selection step involves a single pass through the sorted activities, which takes $O(n)$. Therefore, the overall time complexity of the algorithm is $O(n \log n)$, dominated by the sorting step.

 

Code on GitHub

See the implementation code for Activity Selection Problem.

Last updated 09 May 2025, 23:43 +0500 . history