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
[ ]
[a1]
[a1,a2]
[a1,a2,a3]
[ ]
[a1]
[a2]
[ ]
[a1,a2]
[a1,a3]
[a1]
[a2,a3]
[a2]
[ a3]
[ ]
pick a1?
pick a2?
pick a3?
5.2 - Activity-Selection Problem
select(S)
pick a1?
pick a2?
select(S∖{a1})
select(S∖{a1,a2})
select(S∖{a1})
select(S∖{a1})
select(S∖{a1})
select(S)
select(S)
5.2 - Activity-Selection Problem
0
2
4
6
8
10
12
14
16
S1,11
a1
a11
5.2 - Activity-Selection Problem
0
2
4
6
8
10
12
14
16
S4,?
a4
S?,4
5.2 - Activity-Selection Problem
5.2 - Activity-Selection Problem
0
2
4
6
8
10
12
14
16
S4,12
a4
S0,4
5.2 - Activity-Selection Problem
select(S0,12)
select(S0,1)+select(S1,12)
select(S0,11)+select(S11,12)
select(S0,2)+select(S2,12)
select(S0,10)+select(S10,12)
pick a1
…
pick a2
pick a10
pick a11
5.2 - Activity-Selection Problem
select(Si,j)
select(Si,i+1)+select(Si+1,j)
select(Si,j−1)+select(Sj−1,j)
select(Si,i+2)+select(Si+2,j)
select(Si,k)+select(Sk,j)
pick ai+1
pick aj−1
pick ai+2
…
…
pick ak
5.2 - Activity-Selection Problem
0
2
4
6
8
10
12
14
16
S2,11
a4
a8
A2,11
5.2 - Activity-Selection Problem
select(Si,j)
select(Si,k)
select(Sk,j)
optimal solution: Ai,j
optimal solution: Ai,k
optimal solution: Ak,j
5.2 - Activity-Selection Problem
select(Si,j)
select(Si,k)
select(Sk,j)
optimal solution: Ai,j
solution: Ai,k
optimal solution: Ak,j
optimal solution: Ai,k′
5.2 - Activity-Selection Problem
select(Si,j)
select(Si,i+1)+select(Si+1,j)
select(Si,j−1)+select(Sj−1,j)
select(Si,i+2)+select(Si+2,j)
select(Si,k)+select(Sk,j)
pick ai+1
pick aj−1
pick ai+2
…
…
pick ak
∣Ai,j∣=⎩⎨⎧ak∈Si,jmax(∣Ai,k∣+1+∣Ak,j∣) 0 if Si,j is not emptyif Si,j is empty
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; }