Activity Selection Problem (1)
Activity Selection Problem (1)
Problem
Problem
Statement
You are tasked with scheduling several competing activities that require exclusive use of a
common resource, such as a conference room. Given a set S = {a1, a2, ..., an} of n activities,
where each activity ai has a start time si and a finish time fi (0 ≤ si ≤ fi < ∞), the goal is to select
the largest subset of mutually compatible activities. Two activities ai and aj are considered
compatible if their intervals [si, fi) and [sj, fj) do not overlap, i.e., if fi ≤ sj or fj ≤ si.
The objective is to find a maximum-size subset of S such that all activities in the subset are
mutually compatible.
i 1 2 3 4 5 6 7 8 9 10 11
1 3 0 5 3 5 6 7 8 2 12
4 6 6 7 9 9 10 11 12 14 16
EXAMPLE
{a1, a4, a8, a11} is a largest subset of mutually compatible events, and another largest subset is
{a2, a4, a9, a11}.
List of Greedy Strategies:
1. Select the activity with the earliest start time that is compatible with previously selected
activities.
2. Select the activity with the shortest duration (fi-si).
3. Select the activity with the fewest conflicts (i.e., the least overlap with other activities).
4. Select the activity with the earliest finish time that is compatible with previously selected
activities.
The most effective strategy for the activity selection problem is the Earliest Finish
Time First strategy.
Algorithm:
GREEDY-ACTIVITY-SELECTOR (activities, n)
1. Sort the activities by their finish times f[i], in ascending order.
2. A ← {activities[1]}
3. k ← 1
4. for m ← 2 to n do
5. if activities[m].start ≥ activities[k].finish then
6. A ← A ∪ {activities[m]}
7. k ← m
8. return A
Running Time:
1. Sorting activities:
sorting the n activities by their finish time takes:
O(n log n)
2. Iterative Solution:
Iterating through the sorted activities to select non-overlapping ones takes:
O(n)
3. Total time Complexity:
O(n log n) + O(n) = O(n log n)
PROOF OF CORRECTNESS
Greedy choice property
The greedy choice property ensures that the locally optimal choice at each step leads
to a globally optimal solution. For this algorithm, the greedy choice is selecting
the earliest finishing activity that is compatible with the previously selected
activities.
Proof by Induction:
Consider any non-empty subproblem Sk and let am be an activity in Sk with the earliest finish time.
Then am is included in some maximum-size subset of mutually compatible activities of Sk. 1
PROOF OF CORRECTNESS:
Greedy choice property:
The greedy choice property ensures that the locally optimal choice at each step leads to a
globally optimal solution. For this algorithm, the greedy choice is selecting the earliest
finishing activity that is compatible with the previously selected activities.
Proof by Induction:
Consider any non-empty subproblem and let be an activity in with the earliest finish time.
Then is included in some maximum-size subset of mutually compatible activities of Sk.
Claim: Let be a maximum-size subset of mutually compatible activities in , and let be the activity in with the
earliest finish time. If = , we are done, since we have shown that belongs to some maximum-size subset of
mutually compatible activities of S
Inductive Step:
Now consider , a subproblem with k+1 activities.
Let am be the activity in with earliest finish time.
According to the greedy choice, am is selected.
Case 1: is in the maximum subset
If is already included in the maximum subset , the theorem holds for .
Case 2: is not in the maximum subset
Let be a maximum-size subset of mutually compatible activities of that does not include am.
we can construct a new subset as follows:
= ( - {}) ∪ {} where is the activity in Ak+1 with the earliest finish time such that ≥ . Observation-1: is still a
subset of mutually compatible activities.
Replacing with am does not create conflicts because ≤ (greedy choice ensures am has the earliest
finish time).
Observation-2: The new size of the subset is | | = | |.
Since, includes , it is a maximum-size subset of mutually compatible activities.