Daniel Sutantyo, Department of Computing, Macquarie University
5.2 - Activity-Selection Problem
5.2 - Activity-Selection Problem
0
2
4
6
8
10
12
14
16
time
5.2 - Activity-Selection Problem
0
2
4
6
8
10
12
14
16
5.2 - Activity-Selection Problem
0
2
4
6
8
10
12
14
16
activity
start
finish
4 5 6 7 9 9 10 11 12 14 16
1 3 0 5 3 5 6 8 8 2 12
1 2 3 4 5 6 7 8 9 10 11
5.2 - Activity-Selection Problem
0
2
4
6
8
10
12
14
16
activity
start
finish
4 5 6 7 9 9 10 11 12 14 16
1 3 0 5 3 5 6 8 8 2 12
1 2 3 4 5 6 7 8 9 10 11
5.2 - Activity-Selection Problem
0
2
4
6
8
10
12
14
16
activity
start
finish
4 5 6 7 9 9 10 11 12 14 16
1 3 0 5 3 5 6 8 8 2 12
1 2 3 4 5 6 7 8 9 10 11
5.2 - Activity-Selection Problem
5.2 - Activity-Selection Problem
5.2 - Activity-Selection Problem
0
2
4
6
8
10
12
14
16
activity
start
finish
4 5 6 7 9 9 10 11 12 14 16
1 3 0 5 3 5 6 8 8 2 12
1 2 3 4 5 6 7 8 9 10 11
5.2 - Activity-Selection Problem
\( [ ] \)
\( [ a_1 ] \)
\( [ a_1, a_2 ] \)
\( [ a_1, a_2, a_3 ] \)
\( [ ] \)
\( [ a_1 ] \)
\( [ a_2 ] \)
\( [ ] \)
\( [ a_1, a_2 ] \)
\( [ a_1, a_3] \)
\( [ a_1 ] \)
\( [ a_2, a_3] \)
\( [ a_2] \)
\( [ a_3] \)
\( [ ] \)
pick \(a_1\)?
pick \(a_2\)?
pick \(a_3\)?
5.2 - Activity-Selection Problem
\(\text{select}(S) \)
pick \(a_1\)?
pick \(a_2\)?
\(\text{select}(S\setminus\{a_1\}) \)
\(\text{select}(S\setminus\{a_1,a_2\}) \)
\(\text{select}(S\setminus\{a_1\}) \)
\(\text{select}(S\setminus\{a_1\}) \)
\(\text{select}(S\setminus\{a_1\}) \)
\(\text{select}(S) \)
\(\text{select}(S) \)
5.2 - Activity-Selection Problem
0
2
4
6
8
10
12
14
16
\(S_{1,11}\)
\(a_1\)
\(a_{11}\)
5.2 - Activity-Selection Problem
0
2
4
6
8
10
12
14
16
\(S_{4,?}\)
\(a_{4}\)
\(S_{?,4}\)
5.2 - Activity-Selection Problem
5.2 - Activity-Selection Problem
0
2
4
6
8
10
12
14
16
\(S_{4,12}\)
\(a_{4}\)
\(S_{0,4}\)
5.2 - Activity-Selection Problem
\(\text{select}(S_{0,12})\)
\(\text{select}(S_{0,1}) + \text{select}(S_{1,12})\)
\(\text{select}(S_{0,11}) + \text{select}(S_{11,12})\)
\(\text{select}(S_{0,2}) + \text{select}(S_{2,12})\)
\(\text{select}(S_{0,10}) + \text{select}(S_{10,12})\)
pick \(a_1\)
\(\dots\)
pick \(a_2\)
pick \(a_{10}\)
pick \(a_{11}\)
5.2 - Activity-Selection Problem
\(\text{select}(S_{i,j})\)
\(\text{select}(S_{i,i+1}) + \text{select}(S_{i+1,j})\)
\(\text{select}(S_{i,j-1}) + \text{select}(S_{j-1,j})\)
\(\text{select}(S_{i,i+2}) + \text{select}(S_{i+2,j})\)
\(\text{select}(S_{i,k}) + \text{select}(S_{k,j})\)
pick \(a_{i+1}\)
pick \(a_{j-1}\)
pick \(a_{i+2}\)
\(\dots\)
\(\dots\)
pick \(a_{k}\)
5.2 - Activity-Selection Problem
0
2
4
6
8
10
12
14
16
\(S_{2,11}\)
\(a_{4}\)
\(a_{8}\)
\(A_{2,11}\)
5.2 - Activity-Selection Problem
\(\text{select}(S_{i,j})\)
\(\text{select}(S_{i,k})\)
\(\text{select}(S_{k,j})\)
optimal solution: \(A_{i,j}\)
optimal solution: \(A_{i,k}\)
optimal solution: \(A_{k,j}\)
5.2 - Activity-Selection Problem
\(\text{select}(S_{i,j})\)
\(\text{select}(S_{i,k})\)
\(\text{select}(S_{k,j})\)
optimal solution: \(A_{i,j}\)
solution: \(A_{i,k}\)
optimal solution: \(A_{k,j}\)
optimal solution: \(A^\prime_{i,k}\)
5.2 - Activity-Selection Problem
\(\text{select}(S_{i,j})\)
\(\text{select}(S_{i,i+1}) + \text{select}(S_{i+1,j})\)
\(\text{select}(S_{i,j-1}) + \text{select}(S_{j-1,j})\)
\(\text{select}(S_{i,i+2}) + \text{select}(S_{i+2,j})\)
\(\text{select}(S_{i,k}) + \text{select}(S_{k,j})\)
pick \(a_{i+1}\)
pick \(a_{j-1}\)
pick \(a_{i+2}\)
\(\dots\)
\(\dots\)
pick \(a_{k}\)
\[|A_{i,j}| = \begin{cases}\displaystyle{\max_{a_k\in S_{i,j}}} \left(|A_{i,k}| + 1 + |A_{k,j}|\right) & \text{if $S_{i,j}$ is not empty}\\ 0 & \text{if $S_{i,j}$ is empty} \end{cases}\]
5.2 - Activity-Selection Problem
5.2 - Activity-Selection Problem
0
2
4
6
8
10
12
14
16
5.2 - Activity-Selection Problem
0
2
4
6
8
10
12
14
16
5.2 - Activity-Selection Problem
5.2 - Activity-Selection Problem
5.2 - Activity-Selection Problem
5.2 - Activity-Selection Problem
5.2 - Activity-Selection Problem
5.2 - Activity-Selection Problem
5.2 - Activity-Selection Problem
// int s[] is the array containing start times
// int f[] is the array containing finish times
// remember that the arrays are sorted by finishing time
// so f[m] < f[m+1], i.e. activity m finishes before activity m+1
// calling select(k,n), select only activities that starts after k finishes
// activity n is the phantom activity
HashSet<Integer> select(int k, int n){}{
int m = k+1;
// find the next activity that starts after activity k finishes
while (m <= n && s[m] < f[k])
m++:
// add the activity to the set of solution
if (m <= n){
HashSet<Integer> ans = new HashSet<>(select(m,n));
ans.add(m);
return ans;
}
}
5.2 - Activity-Selection Problem
// int s[] is the array containing start times
// int f[] is the array containing finish times
HashSet<Integer> select(){
int n = s.length;
HashSet<Integer> answer = new HashSet<Integer>();
answer.add(1);
k = 1;
for(int m = 2; m < n; m++){
if (s[m] >= f[k]){
answer.add(m);
k = m;
}
}
return answer;
}