SlideShare a Scribd company logo
AN ACTIVITY-SELECTION PROBLEM
BY SUMITA DAS
Created by Sumita Das
An Activity-selection problem
 Suppose we have a set of activities S={a1,a2,….,an} (Total n
activities) that wish to use a common resource which can serve
only one activity at a time.
 Mutually exclusive access to single resource.
 Each activity ai is having start time si and finish time fi, where
0<=si<=fi<∞
 Goal: To find a maximum-size subset of mutually compatible
activities
 Compatible Activities: Activities ai and aj are compatible if si>=fj
or sj>=fi
i.e. Second Activity starts before first activity finishes.
Created by Sumita Das
Let set S consists of following activities which is sorted in
monotonically increasing order of finish time.
k 0 1 2 3 4 5 6 7 8 9 10 11
sk - 1 3 0 5 3 5 6 8 8 2 12
fk 0 4 5 6 7 8 9 10 11 12 14 16
Created by Sumita Das
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
A1
A2
A3
A4
A5
A6
A7
A8
A9
A10
a11
Created by Sumita Das
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. then return {am}U RECURSIVE-ACTIVITY- SELECTOR
(s, f, m, n)
6. else return Ø
A Recursive Greedy Algorithm
Created by Sumita Das
s- array of start time of activities
f- array of finish time of activities
k- index of subproblem Sk
Where Sk is the set of activities that start after activity ak
finishes.
n- size of original problem
 Add a fictitious activity a0 with f0=0
 So Subproblem S0 is entire set of activities S.
 Initial call RECURSIVE-ACTIVITY-SELECTOR( s, f, 0,
n) solves the entire problem
Created by Sumita Das
RECURSIVE-ACTIVITY-SELECTOR( s, f, 0, 11)
1. m= k +1
2. while m <= n and s[m] < f[k]
3. m=m +1
4. if m <= n
5. then return {am}U RECURSIVE-ACTIVITY- SELECTOR
(s, f, m, n)
6. else return Ø
k=0 m=0+1=1
1<=11 and s[1]<f[0]
1<=11
1<0 false. While loop exits
Return a1 U RECURSIVE-
ACTIVITY- SELECTOR (s, f,
1, 11)
k 0 1 2 3 4 5 6 7 8 9 10 11
sk - 1 3 0 5 3 5 6 8 8 2 12
fk 0 4 5 6 7 8 9 10 11 12 14 16
Created by Sumita Das
RECURSIVE-ACTIVITY-SELECTOR( s, f, 1, 11)
1. m= k +1
2. while m <= n and s[m] < f[k]
3. m=m +1
4. if m <= n
5. then return {am}U RECURSIVE-ACTIVITY- SELECTOR
(s, f, m, n)
6. else return Ø
k=1 m=1+1=2
2<=11 and s[2]<f[1] 3<4 true
m=2+1=3
3<=11 and s[3]<f[1] 0<4 true
m=3+1=4
4<=11 and s[4]<f[1] 5<4 false. While loop exits4<=11
Return a4 U RECURSIVE-
ACTIVITY- SELECTOR (s, f,
4, 11)
k 0 1 2 3 4 5 6 7 8 9 10 11
sk - 1 3 0 5 3 5 6 8 8 2 12
fk 0 4 5 6 7 8 9 10 11 12 14 16
Created by Sumita Das
RECURSIVE-ACTIVITY-SELECTOR( s, f, 4, 11)
1. m= k +1
2. while m <= n and s[m] < f[k]
3. m=m +1
4. if m <= n
5. then return {am}U RECURSIVE-ACTIVITY- SELECTOR
(s, f, m, n)
6. else return Ø
k=4 m=4+1=5
5<=11 and s[5]<f[4] 3<7 true
m=5+1=6
6<=11 and s[6]<f[4] 5<7 true
m=6+1=7
7<=11 and s[7]<f[4] 6<7 true
m=7+1=8
8<=11 and
s[8]<f[4] 8<7 false.While loop exits8<=11
Return a8 U RECURSIVE-
ACTIVITY- SELECTOR (s, f,
8, 11)
k 0 1 2 3 4 5 6 7 8 9 10 11
sk - 1 3 0 5 3 5 6 8 8 2 12
fk 0 4 5 6 7 8 9 10 11 12 14 16
Created by Sumita Das
RECURSIVE-ACTIVITY-SELECTOR( s, f, 8, 11)
1. m= k +1
2. while m <= n and s[m] < f[k]
3. m=m +1
4. if m <= n
5. then return {am}U RECURSIVE-ACTIVITY- SELECTOR
(s, f, m, n)
6. else return Ø
k=8 m=8+1=9
9<=11 and s[9]<f[8] 8<11 true
m=9+1=10
10<=11 and s[10]<f[8] 2<11 true
m=10+1=11
11<=11 and s[11]<f[8] 12<11 falseWhile
loop exits
11<=11
Return a11 U RECURSIVE-
ACTIVITY- SELECTOR (s, f,
11, 11)
k 0 1 2 3 4 5 6 7 8 9 10 11
sk - 1 3 0 5 3 5 6 8 8 2 12
fk 0 4 5 6 7 8 9 10 11 12 14 16
Created by Sumita Das
 RECURSIVE-ACTIVITY- SELECTOR (s, f, 11, 11)
returns Ø.
 Resulting set of selected activities is {a1, a4 a8, a11}
Created by Sumita Das
References
 Introduction to Algorithms 2nd ,Cormen, Leiserson,
Rivest and Stein, The MIT Press, 2001.
 Introduction to Design & Analysis Computer Algorithm
3rd, Sara Baase, Allen Van Gelder, Adison-Wesley, 2000.
Created by Sumita Das

More Related Content

PPTX
Activity selection problem
PPTX
Activity selection problem class 12
PPTX
Data structures Lecture no.6
PPTX
Python Programming | JNTUA | UNIT 3 | Updating Variables & Iteration |
PPTX
Arithmetic and Arithmetic assignment operators
PPTX
Gradient Boosting
PPT
Array 2
PPTX
Hidden Markov Model
Activity selection problem
Activity selection problem class 12
Data structures Lecture no.6
Python Programming | JNTUA | UNIT 3 | Updating Variables & Iteration |
Arithmetic and Arithmetic assignment operators
Gradient Boosting
Array 2
Hidden Markov Model

Similar to Activity selection problem (20)

PPSX
Design and Analysis of Algorithms (Greedy Algorithm)
PPTX
Computational Thinking 11- ActivitySelection.pptx
PPTX
Design and analysis of algorithms
PDF
Greedy is Good
PDF
Activity Selection Problem
PDF
FADML 09 PPC NP Completeness (1).pdf
PDF
Recursive algorithms
DOCX
homework_chap_3_a_solutions.docx
PPTX
Applied Algorithms Introduction to Algorithms.pptx
PPTX
Greedy aproach towards problem solution
PPT
GreedyAlgorithms.ppt
PPT
GreedyAlgorithms.ppt
PPT
3-Chapter Three - Divide and Conquer.ppt
PPT
test pre
PPTX
Algorithm Design and Complexity - Course 5
DOC
ALGORITHMS - SHORT NOTES
PPTX
Computer Science Exam Help
PPT
5.3 dynamic programming
PDF
Sec16 greedy algorithm no1
PDF
Greedy algorithm activity selection fractional
Design and Analysis of Algorithms (Greedy Algorithm)
Computational Thinking 11- ActivitySelection.pptx
Design and analysis of algorithms
Greedy is Good
Activity Selection Problem
FADML 09 PPC NP Completeness (1).pdf
Recursive algorithms
homework_chap_3_a_solutions.docx
Applied Algorithms Introduction to Algorithms.pptx
Greedy aproach towards problem solution
GreedyAlgorithms.ppt
GreedyAlgorithms.ppt
3-Chapter Three - Divide and Conquer.ppt
test pre
Algorithm Design and Complexity - Course 5
ALGORITHMS - SHORT NOTES
Computer Science Exam Help
5.3 dynamic programming
Sec16 greedy algorithm no1
Greedy algorithm activity selection fractional
Ad

More from Sumita Das (10)

PPTX
Numerical on bisection method
PPTX
Numerical on dichotomous search
PPT
Presentation on binary search, quick sort, merge sort and problems
PPTX
Loop alignment
PPTX
Maps in android
PPTX
Hardware and software parallelism
PPTX
Trusted systems1
PPTX
Multiprotocol label switching
PPTX
Asymptotic analysis of parallel programs
PPTX
Distributed systems1
Numerical on bisection method
Numerical on dichotomous search
Presentation on binary search, quick sort, merge sort and problems
Loop alignment
Maps in android
Hardware and software parallelism
Trusted systems1
Multiprotocol label switching
Asymptotic analysis of parallel programs
Distributed systems1
Ad

Recently uploaded (20)

PDF
A novel scalable deep ensemble learning framework for big data classification...
PDF
Accuracy of neural networks in brain wave diagnosis of schizophrenia
PDF
August Patch Tuesday
PDF
NewMind AI Weekly Chronicles - August'25-Week II
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PPTX
Group 1 Presentation -Planning and Decision Making .pptx
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Hindi spoken digit analysis for native and non-native speakers
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PPTX
A Presentation on Touch Screen Technology
PDF
Web App vs Mobile App What Should You Build First.pdf
PDF
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
PDF
Assigned Numbers - 2025 - Bluetooth® Document
PDF
1 - Historical Antecedents, Social Consideration.pdf
PDF
Approach and Philosophy of On baking technology
PDF
Hybrid model detection and classification of lung cancer
PDF
Encapsulation_ Review paper, used for researhc scholars
PPTX
1. Introduction to Computer Programming.pptx
PPTX
Tartificialntelligence_presentation.pptx
PPTX
Programs and apps: productivity, graphics, security and other tools
A novel scalable deep ensemble learning framework for big data classification...
Accuracy of neural networks in brain wave diagnosis of schizophrenia
August Patch Tuesday
NewMind AI Weekly Chronicles - August'25-Week II
Digital-Transformation-Roadmap-for-Companies.pptx
Group 1 Presentation -Planning and Decision Making .pptx
Building Integrated photovoltaic BIPV_UPV.pdf
Hindi spoken digit analysis for native and non-native speakers
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
A Presentation on Touch Screen Technology
Web App vs Mobile App What Should You Build First.pdf
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
Assigned Numbers - 2025 - Bluetooth® Document
1 - Historical Antecedents, Social Consideration.pdf
Approach and Philosophy of On baking technology
Hybrid model detection and classification of lung cancer
Encapsulation_ Review paper, used for researhc scholars
1. Introduction to Computer Programming.pptx
Tartificialntelligence_presentation.pptx
Programs and apps: productivity, graphics, security and other tools

Activity selection problem

  • 1. AN ACTIVITY-SELECTION PROBLEM BY SUMITA DAS Created by Sumita Das
  • 2. An Activity-selection problem  Suppose we have a set of activities S={a1,a2,….,an} (Total n activities) that wish to use a common resource which can serve only one activity at a time.  Mutually exclusive access to single resource.  Each activity ai is having start time si and finish time fi, where 0<=si<=fi<∞  Goal: To find a maximum-size subset of mutually compatible activities  Compatible Activities: Activities ai and aj are compatible if si>=fj or sj>=fi i.e. Second Activity starts before first activity finishes. Created by Sumita Das
  • 3. Let set S consists of following activities which is sorted in monotonically increasing order of finish time. k 0 1 2 3 4 5 6 7 8 9 10 11 sk - 1 3 0 5 3 5 6 8 8 2 12 fk 0 4 5 6 7 8 9 10 11 12 14 16 Created by Sumita Das
  • 4. 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 A1 A2 A3 A4 A5 A6 A7 A8 A9 A10 a11 Created by Sumita Das
  • 5. 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. then return {am}U RECURSIVE-ACTIVITY- SELECTOR (s, f, m, n) 6. else return Ø A Recursive Greedy Algorithm Created by Sumita Das
  • 6. s- array of start time of activities f- array of finish time of activities k- index of subproblem Sk Where Sk is the set of activities that start after activity ak finishes. n- size of original problem  Add a fictitious activity a0 with f0=0  So Subproblem S0 is entire set of activities S.  Initial call RECURSIVE-ACTIVITY-SELECTOR( s, f, 0, n) solves the entire problem Created by Sumita Das
  • 7. RECURSIVE-ACTIVITY-SELECTOR( s, f, 0, 11) 1. m= k +1 2. while m <= n and s[m] < f[k] 3. m=m +1 4. if m <= n 5. then return {am}U RECURSIVE-ACTIVITY- SELECTOR (s, f, m, n) 6. else return Ø k=0 m=0+1=1 1<=11 and s[1]<f[0] 1<=11 1<0 false. While loop exits Return a1 U RECURSIVE- ACTIVITY- SELECTOR (s, f, 1, 11) k 0 1 2 3 4 5 6 7 8 9 10 11 sk - 1 3 0 5 3 5 6 8 8 2 12 fk 0 4 5 6 7 8 9 10 11 12 14 16 Created by Sumita Das
  • 8. RECURSIVE-ACTIVITY-SELECTOR( s, f, 1, 11) 1. m= k +1 2. while m <= n and s[m] < f[k] 3. m=m +1 4. if m <= n 5. then return {am}U RECURSIVE-ACTIVITY- SELECTOR (s, f, m, n) 6. else return Ø k=1 m=1+1=2 2<=11 and s[2]<f[1] 3<4 true m=2+1=3 3<=11 and s[3]<f[1] 0<4 true m=3+1=4 4<=11 and s[4]<f[1] 5<4 false. While loop exits4<=11 Return a4 U RECURSIVE- ACTIVITY- SELECTOR (s, f, 4, 11) k 0 1 2 3 4 5 6 7 8 9 10 11 sk - 1 3 0 5 3 5 6 8 8 2 12 fk 0 4 5 6 7 8 9 10 11 12 14 16 Created by Sumita Das
  • 9. RECURSIVE-ACTIVITY-SELECTOR( s, f, 4, 11) 1. m= k +1 2. while m <= n and s[m] < f[k] 3. m=m +1 4. if m <= n 5. then return {am}U RECURSIVE-ACTIVITY- SELECTOR (s, f, m, n) 6. else return Ø k=4 m=4+1=5 5<=11 and s[5]<f[4] 3<7 true m=5+1=6 6<=11 and s[6]<f[4] 5<7 true m=6+1=7 7<=11 and s[7]<f[4] 6<7 true m=7+1=8 8<=11 and s[8]<f[4] 8<7 false.While loop exits8<=11 Return a8 U RECURSIVE- ACTIVITY- SELECTOR (s, f, 8, 11) k 0 1 2 3 4 5 6 7 8 9 10 11 sk - 1 3 0 5 3 5 6 8 8 2 12 fk 0 4 5 6 7 8 9 10 11 12 14 16 Created by Sumita Das
  • 10. RECURSIVE-ACTIVITY-SELECTOR( s, f, 8, 11) 1. m= k +1 2. while m <= n and s[m] < f[k] 3. m=m +1 4. if m <= n 5. then return {am}U RECURSIVE-ACTIVITY- SELECTOR (s, f, m, n) 6. else return Ø k=8 m=8+1=9 9<=11 and s[9]<f[8] 8<11 true m=9+1=10 10<=11 and s[10]<f[8] 2<11 true m=10+1=11 11<=11 and s[11]<f[8] 12<11 falseWhile loop exits 11<=11 Return a11 U RECURSIVE- ACTIVITY- SELECTOR (s, f, 11, 11) k 0 1 2 3 4 5 6 7 8 9 10 11 sk - 1 3 0 5 3 5 6 8 8 2 12 fk 0 4 5 6 7 8 9 10 11 12 14 16 Created by Sumita Das
  • 11.  RECURSIVE-ACTIVITY- SELECTOR (s, f, 11, 11) returns Ø.  Resulting set of selected activities is {a1, a4 a8, a11} Created by Sumita Das
  • 12. References  Introduction to Algorithms 2nd ,Cormen, Leiserson, Rivest and Stein, The MIT Press, 2001.  Introduction to Design & Analysis Computer Algorithm 3rd, Sara Baase, Allen Van Gelder, Adison-Wesley, 2000. Created by Sumita Das