Professional Documents
Culture Documents
Activity Selection Problem
Activity Selection Problem
Activity
Selection • Prepared by:-
Problem Sagar Virani
Assistant Professor
Computer Engineering
Department
• A set S = {a1, a2 ,…, an} of n proposed activities that wish to use a resource, such as
lecture hall, which can serve only one activity at a time.
• Each activity ai has a start time si and a finish time fi, where 0 ≤ si < fi < ∞.
• If selected, activity ai takes place during the half-open time interval [si, fi).
• Activities ai and aj are compatible if the intervals [si, fi) and [sj, fj) do not overlap i.e. ai
and aj are compatible if si ≥ fj or sj ≥ fi.
• We assume that the activities are sorted in monotonically increasing order of finish time:
f1 ≤ f2 ≤ f3 ≤ …. ≤ fn – 1 ≤ fn.
Activity Selection Problem
Example:
i 1 2 3 4 5 6 7 8 9 10 11
si 1 3 0 5 3 5 6 8 8 2 12
fj 4 5 6 7 9 9 10 11 12 14 16
• Selected Activity: a3
• Selected Activity: a9
i 1 2 3 4 5 6 7 8 9 10 11
si 1 3 0 5 3 5 6 8 8 2 12
fj 4 5 6 7 9 9 10 11 12 14 16
• Selected Activity: a1
• Selected Activity: a4
• Selected Activity: a8
i 1 2 3 4 5 6 7 8 9 10 11
si 1 3 0 5 3 5 6 8 8 2 12
fj 4 5 6 7 9 9 10 11 12 14 16
• Selected Activity: a2
• Selected Activity: a4
• Selected Activity: a9
• {a2, a4, a9, a11} is also a largest subset of mutually compatible activities.
Activity Selection Problem
Steps for Activity Selection Problem:
i 1 2 3 4 5 6 7 8 9 10 11 A = {a1}
si 1 3 0 5 3 5 6 8 8 2 12
fj 4 5 6 7 9 9 10 11 12 14 16
Algorithm:
• Step1: Select the first activity from input (sorted) ACTIVITY-SELECTOR(s, f):
array and add it to solution array. n = s.length
A = {a1}
Activity Selection Problem
Steps for Activity Selection Problem:
i 1 2 3 4 5 6 7 8 9 10 11 A = {a1}
si 1 3 0 5 3 5 6 8 8 2 12
fj 4 5 6 7 9 9 10 11 12 14 16
Algorithm:
• Step1: Select the first activity from input (sorted) ACTIVITY-SELECTOR(s, f):
array and add it to solution array. n = s.length
for i = 2 to n
Activity Selection Problem
Steps for Activity Selection Problem:
i 1 2 3 4 5 6 7 8 9 10 11 A = {a1}
si 1 3 0 5 3 5 6 8 8 2 12
fj 4 5 6 7 9 9 10 11 12 14 16
Algorithm:
• Step1: Select the first activity from input (sorted) ACTIVITY-SELECTOR(s, f):
array and add it to solution array. n = s.length
i 1 2 3 4 5 6 7 8 9 10 11 A = {a1}
si 1 3 0 5 3 5 6 8 8 2 12
fj 4 5 6 7 9 9 10 11 12 14 16
Algorithm:
• Step1: Select the first activity from input (sorted) ACTIVITY-SELECTOR(s, f):
array and add it to solution array. n = s.length
j=i
• Step4: Select the next activity for the input array.
Activity Selection Problem
Steps for Activity Selection Problem:
i 1 2 3 4 5 6 7 8 9 10 11 A = {a1}
si 1 3 0 5 3 5 6 8 8 2 12
fj 4 5 6 7 9 9 10 11 12 14 16
Algorithm:
• Step1: Select the first activity from input (sorted) ACTIVITY-SELECTOR(s, f):
array and add it to solution array. n = s.length
j=i
• Step4: Select the next activity for the input array.
Activity Selection Problem
Steps for Activity Selection Problem:
i 1 2 3 4 5 6 7 8 9 10 11 A = {a1, a4}
si 1 3 0 5 3 5 6 8 8 2 12
fj 4 5 6 7 9 9 10 11 12 14 16
Algorithm:
• Step1: Select the first activity from input (sorted) ACTIVITY-SELECTOR(s, f):
array and add it to solution array. n = s.length
j=i
• Step4: Select the next activity for the input array.
Activity Selection Problem
Steps for Activity Selection Problem:
i 1 2 3 4 5 6 7 8 9 10 11 A = {a1, a4}
si 1 3 0 5 3 5 6 8 8 2 12
fj 4 5 6 7 9 9 10 11 12 14 16
Algorithm:
• Step1: Select the first activity from input (sorted) ACTIVITY-SELECTOR(s, f):
array and add it to solution array. n = s.length
j=i
• Step4: Select the next activity for the input array.
Activity Selection Problem
Steps for Activity Selection Problem:
i 1 2 3 4 5 6 7 8 9 10 11 A = {a1, a4}
si 1 3 0 5 3 5 6 8 8 2 12
fj 4 5 6 7 9 9 10 11 12 14 16
Algorithm:
• Step1: Select the first activity from input (sorted) ACTIVITY-SELECTOR(s, f):
array and add it to solution array. n = s.length
j=i
• Step4: Select the next activity for the input array.
Activity Selection Problem
Steps for Activity Selection Problem:
i 1 2 3 4 5 6 7 8 9 10 11 A = {a1, a4}
si 1 3 0 5 3 5 6 8 8 2 12
fj 4 5 6 7 9 9 10 11 12 14 16
Algorithm:
• Step1: Select the first activity from input (sorted) ACTIVITY-SELECTOR(s, f):
array and add it to solution array. n = s.length
j=i
• Step4: Select the next activity for the input array.
Activity Selection Problem
Steps for Activity Selection Problem:
Algorithm:
• Step1: Select the first activity from input (sorted) ACTIVITY-SELECTOR(s, f):
array and add it to solution array. n = s.length
j=i
• Step4: Select the next activity for the input array.
Activity Selection Problem
Steps for Activity Selection Problem:
Algorithm:
• Step1: Select the first activity from input (sorted) ACTIVITY-SELECTOR(s, f):
array and add it to solution array. n = s.length
j=i
• Step4: Select the next activity for the input array.
Activity Selection Problem
Steps for Activity Selection Problem:
Algorithm:
• Step1: Select the first activity from input (sorted) ACTIVITY-SELECTOR(s, f):
array and add it to solution array. n = s.length
j=i
• Step4: Select the next activity for the input array.
Activity Selection Problem
Steps for Activity Selection Problem:
Algorithm:
• Step1: Select the first activity from input (sorted) ACTIVITY-SELECTOR(s, f):
array and add it to solution array. n = s.length
j=i
• Step4: Select the next activity for the input array.
Activity Selection Problem
Steps for Activity Selection Problem:
fj 4 5 6 7 9 9 10 11 12 14 16
Algorithm:
• Step1: Select the first activity from input (sorted) ACTIVITY-SELECTOR(s, f):
array and add it to solution array. n = s.length
i 1 2 3 4 5 6 7 8 9 10 11
si 1 3 0 5 3 5 6 8 8 2 12
fj 4 5 6 7 9 9 10 11 12 14 16
Algorithm:
Analysis: ACTIVITY-SELECTOR(s, f):
A = A ∪ {ai}
• The running time of ACTIVITY-SELECTOR = Ө(n lgn) if
j=i
activity are not sorted by their finish time.
return A
Activity Selection Problem
Do it yourself:
i 1 2 3 4 5 6 7
si 1 3 2 0 5 8 11
fj 3 4 5 7 9 10 12
- - - - - {a1}
1 3 2 3 3≥3 {a1, a2 }
2 4 3 2 2≥4 {a1, a2 }
2 4 4 0 0≥4 {a1, a2 }
i 1 2 3 4 5 6 Sort by
finish time
i 1 2 3 4 5 6
si 0 3 1 5 5 8 si 1 3 0 5 5 8
fj 6 4 2 9 7 9 fj 2 4 6 7 9 9
- - - - - {a1}
1 2 2 3 3≥2 {a1, a2 }
2 4 3 0 0≥4 {a1, a2 }