Greedy Vs DP (Activity Selection)
Greedy Vs DP (Activity Selection)
1 1 4 a1
2 3 5 a2
3 0 6 a3
4 5 7 a4
5 3 8 a5
6 5 9 a6
7 6 10 a7
8 8 11 a8
9 8 12 a9
10 2 13 a10
11 12 14 a11
time
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
Dynamic Programming
We consider several choices when determining which
subproblems to use in an optimal solution.
Optimal Substructure
Let us denote by Sij the set of activities that start after activity ai
finishes and that finish before activity aj starts.
Suppose that we wish to find a maximum set of mutually
compatible activities in Sij
Suppose further that such a maximum set is Aij, which includes
some activity ak.
By including ak in an optimal solution, we are left with two
subproblems:
finding mutually compatible activities in the set Sik
finding mutually compatible activities in the set Skj
Optimal Substructure
Let Aik = Aij∩Sik and Akj = Aij∩Skj.
Aik contains the activities in Aij that finish before ak starts.
Akj contains the activities in Aij that start after ak finishes.
We have Aij = Aik ∪ {ak} ∪ Akj.
The maximum-size set Aij of mutually compatible activities in Sij consists of
|Aij| = |Aik| + |Akj| + 1 activities.
The optimal solution Aij must also include optimal solutions to the two
subproblems for Sik and Skj.
If we find a set A’kj of mutually compatible activities in Skj where |A’kj| >
|Akj|, then we could use A’kj , rather than Akj , in a solution to the
subproblem for Sij.
We would have constructed a set of |Aik| + |A’kj| + 1 > |Aik| + |Akj| + 1 =
|Aij| mutually compatible activities, which contradicts the assumption that Aij
is an optimal solution.
A symmetric argument applies to the activities in Sik.
Examples of Sij
a1
a2
𝑺𝟐,𝟖 a3
a4
a5
a6
a7
a8
a9
a10
a11
time
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
Examples of Sij
a1
a2
𝑺𝟑,𝟏𝟏 a3
a4
a5
a6
a7
a8
a9
a10
a11
time
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
Dynamic Programming
Sij = { ak ∈ S : fi ≤ sk < fk ≤ sj }
a0: f0 = 0, an+1: sn+1 = ∞, S = S0,n+1
Assume that the activities are sorted in increasing order of finish
time
f0 ≤ f1 ≤ f2 ≤ … ≤ fn < fn+1
Sij = , whenever i ≥ j
Let an optimal solution Aij to Sij and ak∈Aij
Aij = Aik ∪ {ak} ∪ Akj.
A0,n+1: an optimal solution to the entire problem
An Example of Activities
k sk fk
0 - 0 a0
1 1 4 a1
2 3 5 a2
3 0 6 a3
4 5 7
5 3 8 a5
a4
𝑺𝟎,𝟏𝟐
6 5 9 a6
7 6 10 a7
8 8 11 a8
9 8 12 a9
10 2 13 a10
11 12 14 a11
12 ∞ - … a12
time
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ∞
Dynamic Programming
Recursive definition
c[i,j]: the number of activities in a maximum-size subset of
mutually compatible activities in Sij
C[i, j] = c[i, k] + c[k, j] +1
0 if Sij =
C[i, j] =
max{c[i ,k ] c[k , j ] 1} if Sij ≠
ak Sij
Making the Greedy Choice
What if we could choose an activity to add to our optimal
solution without having to first solve all the subproblems?
That could save us from having to consider all the choices
inherent in recurrence.
In fact, for the activity selection problem, we need to consider
only one choice: the greedy choice.
Intuition suggests that we should choose an activity that leaves
the resource available for as many other activities as possible.
Choose the activity in S with the earliest finish time, since that
would leave the resource available for as many of the activities
that follow it as possible.
Making the Greedy Choice
Since the activities are sorted in monotonically increasing order
by finish time, the greedy choice is activity a1.
If we make the greedy choice, we have only one remaining
subproblem to solve: finding activities that start after a1
finishes.
Why don’t we have to consider activities that finish before a1
starts?
We have that s1 < f1, and f1 is the earliest finish time of any
activity, and therefore no activity can have a finish time less
than or equal to s1.
Thus, all activities that are compatible with activity a1 must start
after a1 finishes.
Making the Greedy Choice
Let Sk = {ai ∈ S : si ≥ fk } be the set of activities that start after activity
ak finishes.
If we make the greedy choice of activity a1, then S1 remains as the
only subproblem to solve.
Optimal substructure tells us that
If a1 is in the optimal solution,
An optimal solution to the original problem consists of activity a1 and all the
activities in an optimal solution to the subproblem S1.
Proof
Assume that A1 is an optimal solution of S1 and there is an optimal solution
O such that | {a1} ∪ A1 |< |O| where O has a1.
If we replace (O – {a1}) by A1, we have |O| ≤ |{a1} ∪ A1|.
Thus, | {a1} ∪ A1 |< |{a1} ∪ A1|which contradicts.
Greedy Solution
Theorem 16.1
Consider any nonempty subproblem Sk, and let am be the activity in
Sk with the earliest finish time.
Then, activity am isincluded in some maximum-size subset of
mutually compatible activities of Sk.
Proof
Let Ak be a maximum-size subset of mutually compatible activities
in Sk, and let aj be the activity in Ak with the earliest finish time.
If aj = am, we are done, since we have shown that am is in some
maximum-size subset of mutually compatible activities of Sk.
If aj ≠ am, let the set A’k = Ak – { aj } ∪ { am }.
The activities in A’k are disjoint, because the activities in Ak are
disjoint, aj is the first activity in Ak to finish, and fm ≤ fj.
Since |A’k| = |Ak|, we conclude that A’k is a maximum-size subset
of mutually compatible activities of Sk, and it includes am.
Greedy Solution
Theorem 16.1
Consider any nonempty subproblem Sk, and let am be the activity in Sk with
the earliest finish time.
Then, activity am isincluded in some maximum-size subset of mutually
compatible activities of Sk.
Although we might be able to solve the activity-selection problem with
dynamic programming, we don’t need to.
Instead, we can repeatedly choose the activity that finishes first, keep
only the activities compatible with this activity, and repeat until no
activities remain.
Because we always choose the activity with the earliest finish time, the
finish times of the activities we choose must strictly increase.
We can consider each activity just once overall, in monotonically
increasing order of finish times.
Greedy Solution
An algorithm to solve the activity-selection problem does not
need to work bottom-up, like a table-based dynamic-
programming algorithm.
Instead, it can work top-down, choosing an activity to put into
the optimal solution and then solving the subproblem of
choosing activities from those that are compatible with those
already chosen.
Greedy algorithms typically have this top-down design.
Make a choice and then solve a subproblem, rather than the
bottom-up technique of solving subproblems before making a
choice.
Greedy Solution
Recursive definition revisted
c[i,j]: the number of activities in a maximum-size subset of
mutually compatible activities in Sij
C[i, j] = c[i, k] + c[k, j] +1
0 if Sij =
C[i, j] =
max{c[i ,k ] c[k , j ] 1} if Sij ≠
ak Sij
A Recursive Greedy Algorithm
The procedure RECURSIVE-ACTIVITY-SELECTOR takes
the start and finish times of the activities, represented as arrays s
and f
the index k that defines the subproblem Sk it is to solve
the size n of the original problem
We assume that the n input activities are already ordered by
monotonically
In order to start, we add the fictitious activity a0 with f0 = 0, so
that subproblem S0 is the entire set of activities S. increasing
finish time.
The initial call, which solves the entire problem, is RECURSIVE-
ACTIVITY-SELECTOR(s, f, 0, n).
A Recursive Greedy Algorithm
RECURSIVE-ACTIVITY-SELECTOR(s, f, k, n)
1 m = k+1
2 while m ≤ n and s[m] < f[k]
3 m = m+1
4 if m ≤ n
5 return {am} U RECURSIVE-ACTIVITY-SELECTOR(s,f,m,n)
6 else
7 return
7 return 1 1 4 a 1
2 3 5 a2
3 0 6 a3
4 5 7 a4
5 3 8 a5
6 5 9 a6
7 6 10 a7
8 8 11 a8
9 8 12 a9
10 2 13 a10
11 12 14 a11
12 ∞ -
time
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
A Recursive Greedy Algorithm
RECURSIVE-ACTIVITY-SELECTOR(s, f, k, n)
1 m = k+1 n = 11
2 while m ≤ n and s[m] < f[k] k=0
3 m = m+1 m=1
4 if m ≤ n
5 return {am} U RECURSIVE-ACTIVITY-SELECTOR(s,f,m,n)
k sk fk
6 else 0 - 0 a 0
7 return 1 1 4 a 1
2 3 5 a2
3 0 6 a3
4 5 7 a4
5 3 8 a5
6 5 9 a6
7 6 10 a7
8 8 11 a8
9 8 12 a9
10 2 13 a10
11 12 14 a11
12 ∞ -
time
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
A Recursive Greedy Algorithm
RECURSIVE-ACTIVITY-SELECTOR(s, f, k, n)
1 m = k+1 n = 11
2 while m ≤ n and s[m] < f[k] k=0
3 m = m+1 m=1
4 if m ≤ n
5 return {am} U RECURSIVE-ACTIVITY-SELECTOR(s,f,m,n)
k sk fk
6 else 0 - 0 a 0
7 return 1 1 4 a 1
2 3 5 a2
3 0 6 a3
4 5 7 a4
5 3 8 a5
6 5 9 a6
7 6 10 a7
8 8 11 a8
9 8 12 a9
10 2 13 a10
11 12 14 a11
12 ∞ -
time
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
A Recursive Greedy Algorithm
RECURSIVE-ACTIVITY-SELECTOR(s, f, k, n)
1 m = k+1 n = 11
2 while m ≤ n and s[m] < f[k] k=0
3 m = m+1 m=1
4 if m ≤ n
5 return {am} U RECURSIVE-ACTIVITY-SELECTOR(s,f,m,n)
k sk fk
6 else 0 - 0 a 0
7 return 1 1 4 a 1 m=1
2 3 5 a2
3 0 6 a3
4 5 7 a4
5 3 8 a5
6 5 9 a6
7 6 10 a7
8 8 11 a8
9 8 12 a9
10 2 13 a10
11 12 14 a11
12 ∞ -
a1
time
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
A Recursive Greedy Algorithm
RECURSIVE-ACTIVITY-SELECTOR(s, f, k, n)
1 m = k+1 n = 11
2 while m ≤ n and s[m] < f[k] k=1
3 m = m+1 m=2
4 if m ≤ n
5 return {am} U RECURSIVE-ACTIVITY-SELECTOR(s,f,m,n)
k sk fk
6 else 0 - 0 a 0
7 return 1 1 4 a 1 m=1
2 3 5 a2
3 0 6 a3
4 5 7 a4
5 3 8 a5
6 5 9 a6
7 6 10 a7
8 8 11 a8
9 8 12 a9
10 2 13 a10
11 12 14 a11
12 ∞ -
a1
time
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
A Recursive Greedy Algorithm
RECURSIVE-ACTIVITY-SELECTOR(s, f, k, n)
1 m = k+1 n = 11
2 while m ≤ n and s[m] < f[k] k=1
3 m = m+1 m=2
4 if m ≤ n
5 return {am} U RECURSIVE-ACTIVITY-SELECTOR(s,f,m,n)
k sk fk
6 else 0 - 0 a 0
7 return 1 1 4 a 1 m=1
2 3 5 a2
3 0 6 a3
4 5 7 a4
5 3 8 a5
6 5 9 a6
7 6 10 a7
8 8 11 a8
9 8 12 a9
10 2 13 a10
11 12 14 a11
12 ∞ -
a1
time
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
A Recursive Greedy Algorithm
RECURSIVE-ACTIVITY-SELECTOR(s, f, k, n)
1 m = k+1 n = 11
2 while m ≤ n and s[m] < f[k] k=1
3 m = m+1 m=3
4 if m ≤ n
5 return {am} U RECURSIVE-ACTIVITY-SELECTOR(s,f,m,n)
k sk fk
6 else 0 - 0 a 0
7 return 1 1 4 a 1 m=1
2 3 5 a2
3 0 6 a3
4 5 7 a4
5 3 8 a5
6 5 9 a6
7 6 10 a7
8 8 11 a8
9 8 12 a9
10 2 13 a10
11 12 14 a11
12 ∞ -
a1
time
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
A Recursive Greedy Algorithm
RECURSIVE-ACTIVITY-SELECTOR(s, f, k, n)
1 m = k+1 n = 11
2 while m ≤ n and s[m] < f[k] k=1
3 m = m+1 m=4
4 if m ≤ n
5 return {am} U RECURSIVE-ACTIVITY-SELECTOR(s,f,m,n)
k sk fk
6 else 0 - 0 a 0
7 return 1 1 4 a 1 m=1
2 3 5 a2
3 0 6 a3
4 5 7 a4
5 3 8 a5
6 5 9 a6
7 6 10 a7
8 8 11 a8
9 8 12 a9
10 2 13 a10
11 12 14 a11
12 ∞ -
a1
time
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
A Recursive Greedy Algorithm
RECURSIVE-ACTIVITY-SELECTOR(s, f, k, n)
1 m = k+1 n = 11
2 while m ≤ n and s[m] < f[k] k=1
3 m = m+1 m=4
4 if m ≤ n
5 return {am} U RECURSIVE-ACTIVITY-SELECTOR(s,f,m,n)
k sk fk
6 else 0 - 0 a 0
7 return 1 1 4 a 1 m=1
2 3 5 a2
3 0 6 a3
4 5 7 a4 m=4
5 3 8 a5
6 5 9 a6
7 6 10 a7
8 8 11 a8
9 8 12 a9
10 2 13 a10
11 12 14 a11
12 ∞ -
a1 a4
time
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
A Recursive Greedy Algorithm
RECURSIVE-ACTIVITY-SELECTOR(s, f, k, n)
1 m = k+1 n = 11
2 while m ≤ n and s[m] < f[k] k=4
3 m = m+1 m=5
4 if m ≤ n
5 return {am} U RECURSIVE-ACTIVITY-SELECTOR(s,f,m,n)
k sk fk
6 else 0 - 0 a 0
7 return 1 1 4 a 1 m=1
2 3 5 a2
3 0 6 a3
4 5 7 a4 m=4
5 3 8 a5
6 5 9 a6
7 6 10 a7
8 8 11 a8
9 8 12 a9
10 2 13 a10
11 12 14 a11
12 ∞ -
a1 a4
time
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
A Recursive Greedy Algorithm
RECURSIVE-ACTIVITY-SELECTOR(s, f, k, n)
1 m = k+1 n = 11
2 while m ≤ n and s[m] < f[k] k=4
3 m = m+1 m=5
4 if m ≤ n
5 return {am} U RECURSIVE-ACTIVITY-SELECTOR(s,f,m,n)
k sk fk
6 else 0 - 0 a 0
7 return 1 1 4 a 1 m=1
2 3 5 a2
3 0 6 a3
4 5 7 a4 m=4
5 3 8 a5
6 5 9 a6
7 6 10 a7
8 8 11 a8
9 8 12 a9
10 2 13 a10
11 12 14 a11
12 ∞ -
a1 a4
time
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
A Recursive Greedy Algorithm
RECURSIVE-ACTIVITY-SELECTOR(s, f, k, n)
1 m = k+1 n = 11
2 while m ≤ n and s[m] < f[k] k=4
3 m = m+1 m=6
4 if m ≤ n
5 return {am} U RECURSIVE-ACTIVITY-SELECTOR(s,f,m,n)
k sk fk
6 else 0 - 0 a 0
7 return 1 1 4 a 1 m=1
2 3 5 a2
3 0 6 a3
4 5 7 a4 m=4
5 3 8 a5
6 5 9 a6
7 6 10 a7
8 8 11 a8
9 8 12 a9
10 2 13 a10
11 12 14 a11
12 ∞ -
a1 a4
time
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
A Recursive Greedy Algorithm
RECURSIVE-ACTIVITY-SELECTOR(s, f, k, n)
1 m = k+1 n = 11
2 while m ≤ n and s[m] < f[k] k=4
3 m = m+1 m=7
4 if m ≤ n
5 return {am} U RECURSIVE-ACTIVITY-SELECTOR(s,f,m,n)
k sk fk
6 else 0 - 0 a 0
7 return 1 1 4 a 1 m=1
2 3 5 a2
3 0 6 a3
4 5 7 a4 m=4
5 3 8 a5
6 5 9 a6
7 6 10 a7
8 8 11 a8
9 8 12 a9
10 2 13 a10
11 12 14 a11
12 ∞ -
a1 a4
time
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
A Recursive Greedy Algorithm
RECURSIVE-ACTIVITY-SELECTOR(s, f, k, n)
1 m = k+1 n = 11
2 while m ≤ n and s[m] < f[k] k=4
3 m = m+1 m=8
4 if m ≤ n
5 return {am} U RECURSIVE-ACTIVITY-SELECTOR(s,f,m,n)
k sk fk
6 else 0 - 0 a 0
7 return 1 1 4 a 1 m=1
2 3 5 a2
3 0 6 a3
4 5 7 a4 m=4
5 3 8 a5
6 5 9 a6
7 6 10 a7
8 8 11 a8
9 8 12 a9
10 2 13 a10
11 12 14 a11
12 ∞ -
a1 a4
time
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
A Recursive Greedy Algorithm
RECURSIVE-ACTIVITY-SELECTOR(s, f, k, n)
1 m = k+1 n = 11
2 while m ≤ n and s[m] < f[k] k=4
3 m = m+1 m=8
4 if m ≤ n
5 return {am} U RECURSIVE-ACTIVITY-SELECTOR(s,f,m,n)
k sk fk
6 else 0 - 0 a 0
7 return 1 1 4 a 1 m=1
2 3 5 a2
3 0 6 a3
4 5 7 a4 m=4
5 3 8 a5
6 5 9 a6
7 6 10 a7
8 8 11 a8 m=8
9 8 12 a9
10 2 13 a10
11 12 14 a11
12 ∞ -
a1 a4 a8
time
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
A Recursive Greedy Algorithm
RECURSIVE-ACTIVITY-SELECTOR(s, f, k, n)
1 m = k+1 n = 11
2 while m ≤ n and s[m] < f[k] k=8
3 m = m+1 m=9
4 if m ≤ n
5 return {am} U RECURSIVE-ACTIVITY-SELECTOR(s,f,m,n)
k sk fk
6 else 0 - 0 a 0
7 return 1 1 4 a 1 m=1
2 3 5 a2
3 0 6 a3
4 5 7 a4 m=4
5 3 8 a5
6 5 9 a6
7 6 10 a7
8 8 11 a8 m=8
9 8 12 a9
10 2 13 a10
11 12 14 a11
12 ∞ -
a1 a4 a8
time
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
A Recursive Greedy Algorithm
RECURSIVE-ACTIVITY-SELECTOR(s, f, k, n)
1 m = k+1 n = 11
2 while m ≤ n and s[m] < f[k] k=8
3 m = m+1 m=9
4 if m ≤ n
5 return {am} U RECURSIVE-ACTIVITY-SELECTOR(s,f,m,n)
k sk fk
6 else 0 - 0 a 0
7 return 1 1 4 a 1 m=1
2 3 5 a2
3 0 6 a3
4 5 7 a4 m=4
5 3 8 a5
6 5 9 a6
7 6 10 a7
8 8 11 a8 m=8
9 8 12 a9
10 2 13 a10
11 12 14 a11
12 ∞ -
a1 a4 a8
time
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
A Recursive Greedy Algorithm
RECURSIVE-ACTIVITY-SELECTOR(s, f, k, n)
1 m = k+1 n = 11
2 while m ≤ n and s[m] < f[k] k=8
3 m = m+1 m = 10
4 if m ≤ n
5 return {am} U RECURSIVE-ACTIVITY-SELECTOR(s,f,m,n)
k sk fk
6 else 0 - 0 a 0
7 return 1 1 4 a 1 m=1
2 3 5 a2
3 0 6 a3
4 5 7 a4 m=4
5 3 8 a5
6 5 9 a6
7 6 10 a7
8 8 11 a8 m=8
9 8 12 a9
10 2 13 a10
11 12 14 a11
12 ∞ -
a1 a4 a8
time
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
A Recursive Greedy Algorithm
RECURSIVE-ACTIVITY-SELECTOR(s, f, k, n)
1 m = k+1 n = 11
2 while m ≤ n and s[m] < f[k] k=8
3 m = m+1 m = 11
4 if m ≤ n
5 return {am} U RECURSIVE-ACTIVITY-SELECTOR(s,f,m,n)
k sk fk
6 else 0 - 0 a 0
7 return 1 1 4 a 1 m=1
2 3 5 a2
3 0 6 a3
4 5 7 a4 m=4
5 3 8 a5
6 5 9 a6
7 6 10 a7
8 8 11 a8 m=8
9 8 12 a9
10 2 13 a10
11 12 14 a11
12 ∞ -
a1 a4 a8
time
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
A Recursive Greedy Algorithm
RECURSIVE-ACTIVITY-SELECTOR(s, f, k, n)
1 m = k+1 n = 11
2 while m ≤ n and s[m] < f[k] k=8
3 m = m+1 m = 11
4 if m ≤ n
5 return {am} U RECURSIVE-ACTIVITY-SELECTOR(s,f,m,n)
k sk fk
6 else 0 - 0 a 0
7 return 1 1 4 a 1 m=1
2 3 5 a2
3 0 6 a3
4 5 7 a4 m=4
5 3 8 a5
6 5 9 a6
7 6 10 a7
8 8 11 a8 m=8
9 8 12 a9
10 2 13 a10
11 12 14 a11
12 ∞ -
a1 a4 a8 a11
time
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
A Recursive Greedy Algorithm
RECURSIVE-ACTIVITY-SELECTOR(s, f, k, n)
1 m = k+1 n = 11
2 while m ≤ n and s[m] < f[k] k = 11
3 m = m+1 m = 12
4 if m ≤ n
5 return {am} U RECURSIVE-ACTIVITY-SELECTOR(s,f,m,n)
k sk fk
6 else 0 - 0 a 0
7 return 1 1 4 a 1 m=1
2 3 5 a2
3 0 6 a3
4 5 7 a4 m=4
5 3 8 a5
6 5 9 a6
7 6 10 a7
8 8 11 a8 m=8
9 8 12 a9
10 2 13 a10
11 12 14 a11
12 ∞ -
a1 a4 a8 a11
time
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
A Recursive Greedy Algorithm
RECURSIVE-ACTIVITY-SELECTOR(s, f, k, n)
1 m = k+1 n = 11
2 while m ≤ n and s[m] < f[k] k = 11
3 m = m+1 m = 12
4 if m ≤ n
5 return {am} U RECURSIVE-ACTIVITY-SELECTOR(s,f,m,n)
k sk fk
6 else 0 - 0 a 0
7 return 1 1 4 a 1 m=1
2 3 5 a2
3 0 6 a3
4 5 7 a4 m=4
5 3 8 a5
6 5 9 a6
7 6 10 a7
8 8 11 a8 m=8
9 8 12 a9
10 2 13 a10
11 12 14 a11
12 ∞ -
a1 a4 a8 a11
time
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
A Recursive Greedy Algorithm
RECURSIVE-ACTIVITY-SELECTOR(s, f, k, n)
1 m = k+1 n = 11
2 while m ≤ n and s[m] < f[k] k = 11
3 m = m+1 m = 12
4 if m ≤ n
5 return {am} U RECURSIVE-ACTIVITY-SELECTOR(s,f,m,n)
k sk fk
6 else 0 - 0 a 0
7 return 1 1 4 a 1 m=1
2 3 5 a2
3 0 6 a3
4 5 7 a4 m=4
5 3 8 a5
6 5 9 a6
7 6 10 a7
8 8 11 a8 m=8
9 8 12 a9
10 2 13 a10
11 12 14 a11
12 ∞ -
a1 a4 a8 a11
time
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
A Recursive Greedy Algorithm
RECURSIVE-ACTIVITY-SELECTOR(s, f, k, n)
1 m = k+1 n = 11
2 while m ≤ n and s[m] < f[k] k = 11
3 m = m+1 m = 12
4 if m ≤ n
5 return {am} U RECURSIVE-ACTIVITY-SELECTOR(s,f,m,n)
k sk fk
6 else 0 - 0 a 0
7 return 1 1 4 a 1 m=1
2 3 5 a2
3 0 6 a3
4 5 7 a4 m=4
5 3 8 a5
6 5 9 a6
7 6 10 a7
8 8 11 a8 m=8
9 8 12 a9
10 2 13 a10
11 12 14 a11
12 ∞ -
a1 a4 a8 a11
time
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
Operation of RECURSIVE-ACTIVITY-SELECTOR
k sk fk
0 - 0 a0 m=0
1 1 4 a1 m=1
2 3 5 a2
3 0 6 a3
4 5 7 a4 m=4
5 3 8 a5
6 5 9 a6
7 6 10 a7
8 8 11 a8 m=8
9 8 12 a9
10 2 13 a10
11 12 14 a11 m = 11
12 ∞ - a1 a4 a8 a11
time
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
An Iterative Greedy Algorithm
GREEDY-ACTIVITY-SELECTOR(s, f)
1 n = s.length
2 A = {a1}
3 k=1
4 for m = 2 to n
5 if s[m] ≥ f[k]
6 A = A U {am}
7 k=m
8 return A