Activity Selection Program in C++

Activity Selection is a problem in combinatorial optimization. The problem can be stated as follows: Given a set of activities with their start and finish times, select the maximum number of activities that can be performed by a single person, assuming that a person can only work on a single activity at a time. The activity selection problem can be solved using greedy algorithms, which make the best choice at each step and never look back. The key idea behind a greedy algorithm for the activity selection problem is to always select the activity that finishes earliest, as this will allow the maximum number of activities to be performed.

To implement a greedy algorithm for the activity selection problem, we first sort the activities in non-descending order of their finish times. After sorting, we start with the first activity and include it in the maximum-sized subset of activities. Then, we choose the next activity that starts after the finish time of the current activity and include it in the subset. This process is repeated until no more activities can be added to the subset. The algorithm can be implemented in a simple and straightforward manner, as follows:

Sort the activities in non-descending order of their finish times.

  1. Initialize the current activity to be the first activity in the sorted array.
  2. For each subsequent activity, if its start time is greater than or equal to the finish time of the current activity, include it in the subset and update the current activity to be this activity.
  3. Repeat step 3 until all activities have been considered.
  4. The time complexity of the greedy algorithm for the activity selection problem is O(n log n), where n is the number of activities. This is because sorting the activities takes O(n log n) time and the rest of the algorithm takes O(n) time.

The greedy algorithm for the activity selection problem has several important properties:

  1. Optimal Substructure: If an optimal solution to the activity selection problem contains a particular activity, then it must also contain all the activities that precede it in the sorted array.
  2. Greedy Choice Property: A greedy algorithm for the activity selection problem makes the locally optimal choice at each step, that is, it selects the activity that finishes earliest. This choice leads to a globally optimal solution.
  3. The proof of the correctness of the greedy algorithm for the activity selection problem can be established using these two properties. In essence, it can be shown that the greedy choice of selecting the activity that finishes earliest at each step leads to an optimal solution to the problem.

C++ Code

Output

The following activities are selected: 
(1, 2) (3, 4) (5, 7) (8, 9)

Explanation:

In conclusion, the activity selection problem is a classic example of a problem that can be solved using greedy algorithms. The greedy algorithm for this problem is simple to implement, has a reasonable time complexity, and is guaranteed to produce an optimal solution. By understanding the activity selection problem and its solution, we can gain a deeper appreciation for the power and elegance of greedy algorithms, and the insight they provide into solving optimization problems.