Analysis of Algorithm
Lec 16
Activity Selection
• The Activity Selection Problem is a classic problem
in combinatorial optimization that deals with selecting
the maximum number of mutually compatible activities
from a given set of activities. Each activity has a defined
start time and end time, and two activities are
compatible if they do not overlap in time.
Problem Description
• Input:
A list of activities, where each activity is represented as
a pair (si,fi) where:
• si: The start time of the activity.
• fi: The finish time of the activity.
• Output:
The maximum number of activities that can be selected,
such that no two selected activities overlap.
Approach (Greedy Algorithm)
• The problem can be efficiently solved using a greedy
algorithm. The idea is to always choose the activity
that finishes the earliest and is compatible with the
previously selected activity.
Steps for the Greedy Algorithm
• Sort the activities based on their finish times (fif_ifi)
in ascending order.
• Select the first activity (the one that finishes earliest).
• For each subsequent activity
• Check if the start time (si) of the current activity is
greater than or equal to the finish time (flast) of the last
selected activity.
• If yes, select the activity and update flast.
• Continue until all activities have been considered.
Example
• Activities: (s,f):
• (1,4),(3,5),(0,6),(5,7),(3,8),(5,9),(6,10),(8,11),(8,12),
(2,13),(12,14)
• Output:
• Selected Activities
• (1,4),(5,7),(8,11),(12,14)
Time Complexity
• Sorting the activities: O(nlogn)
• Selecting activities: O(n)
• Total Time Complexity: O(nlogn)
Applications
•Scheduling problems (e.g., job scheduling, conference room allocation).
•Resource allocation in distributed systems.
•Event planning to maximize participation.