0% found this document useful (0 votes)
3 views11 pages

0e_disj-intv

The document discusses the greedy algorithm for the Activity Selection problem, which aims to select the largest subset of disjoint intervals from a given set. It outlines the algorithm's steps, provides examples, and proves the optimality of the solution through two claims. The document concludes by suggesting a method to implement the algorithm in O(n log n) time.

Uploaded by

owlflowertank
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views11 pages

0e_disj-intv

The document discusses the greedy algorithm for the Activity Selection problem, which aims to select the largest subset of disjoint intervals from a given set. It outlines the algorithm's steps, provides examples, and proves the optimality of the solution through two claims. The document concludes by suggesting a method to implement the algorithm in O(n log n) time.

Uploaded by

owlflowertank
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Greedy 1: Activity Selection

Yufei Tao

Department of Computer Science and Engineering


Chinese University of Hong Kong

1/11
Yufei Tao Activity Selection
In this lecture, we will commence our discussion of greedy algorithms,
which enforce a simple strategy: make the locally optimal decision at
each step. Although this strategy does not always guarantee finding a
globally optimal solution, sometimes it does. The nontrivial part is to
prove (or disprove) the global optimality.

2/11
Yufei Tao Activity Selection
Activity Selection

Input: A set S of n intervals of the form [s, f ] where s and f are integers.
Output: A subset T of disjoint intervals in S with the largest size |T |.

Remark: You can think of [s, f ] as the duration of an activity, and


consider the problem as picking the largest number of activities
that do not have time conflicts.

3/11
Yufei Tao Activity Selection
Activity Selection

Example: Suppose

S = {[1, 9], [3, 7], [6, 20], [12, 19], [15, 17], [18, 22], [21, 24]}.

T = {[3, 7], [15, 17], [18, 22]} is an optimal solution, and so is T =


{[1, 9], [12, 19], [21, 24]}.

4/11
Yufei Tao Activity Selection
Activity Selection

Algorithm
Repeat until S becomes empty:
Add to T the interval I ∈ S with the smallest finish time.
Remove from S all the intervals intersecting I (including I itself)

5/11
Yufei Tao Activity Selection
Activity Selection

Example: Suppose S = {[1, 9], [3, 7], [6, 20], [12, 19], [15, 17], [18, 22],
[21, 24]}.

For convenience, let us rearrange the intervals in S in ascending


order of finish time:
S = {[3, 7], [1, 9], [15, 17], [12, 19], [6, 20], [18, 22], [21, 24]}.

We first add [3, 7] to T , after which intervals [3, 7], [1, 9] and [6, 20]
are removed. Now S becomes {[15, 17], [12, 19], [18, 22], [21, 24]}.
The next interval added to T is [15, 17], which shrinks S further to
{[18, 22], [21, 24]}. After [18, 22] is added to T , S becomes empty
and the algorithm terminates.

6/11
Yufei Tao Activity Selection
Next, we will prove that the algorithm returns an optimal solution. Let us
start with a crucial claim.

Claim 1: Let I1 be the first interval picked by our algorithm. There


must be an optimal solution containing I1 .

Proof: Let T ∗ be an arbitrary optimal solution. If I1 ∈ T ∗ , Claim 1 is


true and we are done. Next, we assume I1 ∈ / T ∗.

We will turn T ∗ into another optimal solution T containing I. For this


purpose, first identify the interval I1′ in T ∗ with the smallest finish time.
Construct T as follows: add all the intervals in T ∗ to T except I ′ , and
finally add I to T .

We will prove that all the intervals in T are disjoint. This indicates that
T is also an optimal solution, and hence, will complete the proof.

7/11
Yufei Tao Activity Selection
It suffices to prove that I1 cannot intersect with any other interval in
J ∈ T . This is true because
the start time of J is after the finish time of I1′ ;
the finish time of Ik is less than or equal to the finish time of I1′ .

8/11
Yufei Tao Activity Selection
Claim 2: Let I1 , I2 , ..., Ik be the first k ≥ 2 intervals picked by our
algorithm (in the order shown). Assume that there is an optimal
solution containing I1 , ..., Ik−1 . Then, there must exist an optimal
solution containing I1 , ..., Ik−1 , Ik .

Proof: Let T ∗ be an optimal solution containing I1 , ..., Ik−1 . Observe:

All the intervals in T ∗ \ {I1 , ..., Ik−1 } must start strictly after the
finish time of Ik−1 .

Think: Why is the observation true?

9/11
Yufei Tao Activity Selection
If Ik ∈ T ∗ , Claim 2 is true and we are done. Next, we consider the case
where Ik ∈/ T ∗.

Let Ik′ be the interval in T ∗ \ {I1 , ..., Ik−1 } that has the smallest finish
time. Construct a set T of intervals as follows: add all the intervals of
T ∗ to T except Ik′ , and finally add Ik to T .

To prove that T is an optimal solution, it suffices to prove that Ik is


disjoint with every interval J ∈ T ∗ \ {I1 , ..., Ik−1 , Ik′ }. This is true
because
the start time of J is after the finish time of Ik′ ;
the finish time of Ik is less than or equal to the finish time of Ik′ .

10/11
Yufei Tao Activity Selection
Activity Selection

Think: How to implement the algorithm in O(n log n) time?

11/11
Yufei Tao Activity Selection

You might also like