Activity Selection Problem

Distribute Education like Computer Geek

Understanding the Activity Selection Problem

.

When you’re planning your day, have you ever wondered how to make the most of your time and attend as many events as possible without any clashes? That’s where the “Activity Selection Problem” comes into play.

In the world of algorithms, this problem is an interesting puzzle that can be applied to various scheduling scenarios.

Multitasking Man
Multi-tasking Man

If you can multi-tasking, then there is no problem but Activity Selection Algorithm can not multitask.

.

Introduction to Activity Selection

The Activity Selection Problem is a classic scheduling problem.

.
ProblemGiven a list of activities, each with a ‘start time’ and an ‘finish time’, how can we select the maximum number of activities to attend without any overlap?

.

Imagine you’re planning to attend a conference with multiple talks and workshops. Each has a specific start and finish time. You want to figure out the best combination of sessions to attend without missing out on any interesting content.

If an experience person manually arranges the sessions, then he can do the optimal scheduling. But how to represent the logic of scheduling in computer? Following is a better logic which you implement in the algorithm.

.

How It Works

The key idea behind solving this problem is to select activities in such a way that they don’t clash with each other.

We will sort the ‘finish time’ of the activities, and write them into a table, and we will start from the first activity in that table. The goal is to attend as many activities as possible.

.

Algorithm of Activity Selection Algorithm
  1. Sort all the activities based on their ‘finish times’ in non-descending order. Use stable sort.
  2. Choose the first activity because it ends early.
  3. For the remaining activities, pick each one if its start time is greater than or equal to the finish time of the previously selected activity.
  4. Keep selecting activities that meet the condition until there are no more activities left to consider.

After that, when you’ve checked all the activities, you will have a list of selected activities that do not overlap with each other. This list represents the maximum number of activities you can attend without any conflicts.

.

Working of Activity Selection Problem

Given 10 activities along with their start and finish time as

Activity Selection Problem Input

Now, we’ll sort the activities based on their finish times in non-descending order while keeping their relative order based on the original sequence (using a stable sort).

.

Sorted Finish Times

Array Sorting of Activity Selection Problem

.

Now, let’s perform the greedy selection of activities

1. Start with Activity A3 (Start Time: 0, Finish Time: 4) since it has the earliest finish time.

1st activity of Activity Selection Problem

Selected Activity: A3

.

2. Activity A2 (Start Time: 3, Finish Time: 5) cannot be selected because it overlaps with A3.

2nd activity of Activity Selection Problem

Selected Activity: A3

.

3. Activity A1 (Start Time: 0, Finish Time: 6) cannot be selected because it overlaps with A3.

3rd Activity of Activity Selection Problem

Selected Activity: A3

.

4. Activity A4 (Start Time: 4, Finish Time: 6) can be selected since it starts after A3 finishes.

4th Activity of Activity Selection Problem

Selected Activities: A3, A4

.

5. Activity A8 (Start Time: 7, Finish Time: 8) can be selected as it starts after A4 finishes.

5th Activity of Activity Selection Problem

Selected Activities: A3, A4, A8

.

6. Activity A6 (Start Time: 8, Finish Time: 10) can be selected as it starts after A8 finishes.

6th Activity of Activity Selection Problem

Selected Activities: A3, A4, A8, A6

.

7. Activity A5 (Finish Time: 11, Start Time: 7) cannot be selected because it overlaps with A6.

7th Activity

Selected Activity: A3, A4, A8, A6

.

8. Activity A7 (Finish Time: 11, Start Time: 9) cannot be selected because it overlaps with A6 (Start Time: 9).

8th Activity

Selected Activities: A3, A4, A8, A6

.

9. Activity A9 (Finish Time: 16, Start Time: 8) cannot be selected because it overlaps with A6 (Start Time: 8).

9th Activity

Selected Activities: A3, A4, A8, A6

.

10. Activity A10 (Finish Time: 16, Start Time: 13) can be selected as it starts after A6 finishes.

10th Activity

Selected Activities: A3, A4, A8, A6, A10

Selected Activities is A3, A4, A8, A6, A10 which is the output of Activity Selection Problem. Also, they do not overlap with each other. This is the optimal solution to the Activity Selection Problem for the given input.

.

Source Code for Activity Selection Problem

Time and Space Complexity of Activity Selection Problem
  1. The sorting step takes O(n log n), and the selection step takes O(n). So, the total time complexity is O(n log n).
  2. The space complexity is O(1), which means it uses a constant amount of memory.

.

Advantages of Activity Selection Problem
  1. Simple and efficient algorithm.
  2. Finds a solution that maximizes the number of activities.
  3. Greedy algorithms provide a globally optimal solution when selecting a maximum number of non-overlapping activities.
  4. Easy to Extend.
  5. Fast Execution.
  6. Minimal Resource Utilization.

.

Disadvantages of Activity Selection Problem
  1. The greedy algorithm does not account for dependencies among activities. In some scheduling problems, activities must be performed in a specific order or in parallel with others.

.

Applications of Activity Selection Problem
  1. Task scheduling in operating systems.
  2. Scheduling classes in schools and universities.
  3. Job scheduling in factories.
  4. Optimizing resource usage in various industries.

So, the next time you’re faced with a scheduling challenge, remember the Activity Selection Problem and how it can help you make the most of your time!

Test Yourself

Q1- Describe the key idea behind solving the Activity Selection Problem.

The key idea is to select maximum activities based on their finish times in non-decreasing order. Starting with the activity that finishes earliest, choose activities that don’t overlap with the previously selected ones.

Q2- Why is the sorting of activities based on finish times necessary in the Activity Selection algorithm?

Sorting by finish times ensures that the selected activities end as early as possible, maximizing the number of activities that can be scheduled without overlap.

Q3- Can the Activity Selection Problem handle scenarios where activities have dependencies or specific order requirements?

No, the greedy approach doesn’t account for dependencies or order requirements; it’s suitable for non-overlapping activities only.

Q4- What is the significance of a stable sort when sorting activities by finish times?

A stable sort preserves the relative order of activities with the same finish times, ensuring the original sequence is maintained.

Q5- What is the goal of the Activity Selection Problem?  
  1. Minimize the number of activities
  2. Maximize the number of non-overlapping activities
  3. Equalize the start and finish times
  4. Randomly select activities

Ans – (2)

Explanation – The goal is to maximize the number of non-overlapping activities

Q6- Why is it important to sort activities by finish times?
  1. It’s not necessary
  2. To minimize the number of activities
  3. To maximize the number of activities
  4. To minimize time complexity

Ans – (3)

Explanation – Sorting by finish times maximizes the number of activities

Q7- Which data structure is typically used to implement the Activity Selection Problem?
  1. Stack
  2. Queue
  3. Linked list
  4. Array

Ans – (4)

Explanation – Arrays are commonly used to represent activities in this problem.

Q8- In the Activity Selection algorithm, which activity is selected first?
  1. The activity with the earliest finish time
  2. The activity with the latest finish time
  3. The activity with the lowest priority
  4. The activity with the highest weight

Ans – (1)

Explanation

The activity with the earliest finish time is selected first, so the correct answer is (1).

Q9- You have a set of 6 activities with the following start and finish times:
Activity Selection Problem Question 1
 
   Using the greedy approach, calculate the maximum number of activities that can be selected.

The maximum number of activities that can be selected using the greedy approach is 3 (A1, A3, A6).

Q10- You have a set of 5 activities with the following start and finish times

Question 2

Calculate the maximum number of activities that can be selected using the greedy approach.

The maximum number of activities that can be selected using the greedy approach is 2 (A1, A4).

Q11- You have a set of 7 activities with the following start and finish times

Question 3

   Calculate the maximum number of activities that can be selected using the greedy approach.

Sort the finish time 

Answer 3

The maximum number of activities that can be selected using the greedy approach is 4 (A2, A3, A7, A4).

BOOKS

Algorithm T H CoremanAlgorithm by Udit AgarwalAlgorithm Ellis HorowitzData Structure & Algorithm Made Easy Narasimha

Â