SlideShare a Scribd company logo
1 of 23
PREPARED BY:
Qurat Ul Ain
SUBMITTED TO:
Ma’am Samreen
 A greedy algorithm is a mathematical process
that looks for simple, easy-to-implement
solutions to complex, multi-step problems by
deciding which next step will provide the
most obvious benefit.
 Such algorithms are called greedy because
while the optimal solution to each smaller
instance will provide an immediate output,
the algorithm doesn’t consider the larger
problem as a whole. Once a decision has
been made, it is never reconsidered.
 Greedy algorithms work by recursively
constructing a set of objects from the
smallest possible constituent parts.
 RECURSION is an approach to problem
solving in which the solution to a particular
problem depends on solutions to smaller
instances of the same problem.
 The advantage to using a greedy algorithm is
that solutions to smaller instances of the
problem can be straightforward and easy to
understand.
 The disadvantage is that it is entirely
possible that the most optimal short-term
solutions may lead to the worst possible
long-term outcome.
 Each step of the Greedy algorithm must take
one of several possible choices. The greedy
strategy advocates making the choice that is
the best at the moment. It is used for solving
optimization problems.
 an optimization problem is the problem of
finding the best solution from all feasible
solutions.
 Greedy algorithms are often used in ad
hoc mobile networking to efficiently
route packets with the fewest number
of hops and the shortest delay possible. They
are also used in machine learning, business
intelligence (BI), artificial intelligence (AI)
and programming.
 We are given a set S of n activities S={a1, . .
. , an}
 Each activity ai has a start time si and finish
time fi, where i-th activity ai = [si, fi) starts
at time si and finishes at time fi.
Where 0<=si<=fi<= ∞
 These activities require the same resource
(for example, one lecture hall).
Two activities ai and aj are compatible if
the intervals [si, fi) and [sj , fj) do not
overlap, i.e they are disjoint.
 The activity-selection problem (ASP) is to
select a maximum size subset of mutually
compatible activities.
 Consider following set of activities,
 The subset {a3, a9, a11},
{a1, a4, a8, a11}
(a2, a4, a9, a11}
ARE COMPATIBLE ACTIVITIES.
i 1 2 3 4 5 6 7 8 9 10 11
Si 1 3 0 5 3 5 6 8 8 2 12
Fi 4 5 6 7 9 9 10 11 12 14 16
 Assume activities sorted in increasing order
of finish time fi
 Let c[i,j] be the number of activities in the
maximal solution for the subset Sij, i.e. the
largest number of compatible activities
starting from time fi, finishing at time sj
 Recursively
C[I,j]= { 0 if Sij=Ф }
C[I,j]= {max i<k<j c[I,k]+c[k,j]+1 otherwise }
 Then the maximum number of compatible
activities is c[0, n + 1].
 Greedy choice. Select an activity with
minimum fi. Remove it and incompatible
activities. Continue until no more activities
left.
This can be implemented with a recursive
algorithm:
 Input: s[1..n] is the array of start times, f[1..n] is
the array of finish times , k is the index, n is the
total no. of activities
 RECURSIVE-ACTIVITY-SELECTOR(s, f, k, n)
// Activities are sorted by finish time.
// i is the activity selected previously. Start from k=0
1 m = k + 1
2 while m<= n and s[m] < f[k] // Find the first
activity in Sk to finish
3 m = m + 1
4 if m <=n then
5 return {am} RECURSIVE-ACTIVITY-
SELECTOR(s,f,m,n)
6 else return Ф;
 Line 1: Initial call: REC-ACTIVITY-
SELECTOR(s, f, 0, n). increment in m by k
(moving next position)
 Line 2-3: the while loop looks for the first
activity in Sk to finish. The loop examines all
activities until it finds first activity am that is
compatible with ak (line 4)
 Line 5: returns am activity, adding it (union)
in the set sk
 Line 6: when we have examined all activities
m>n then the loop terminates
 The running time of the Algorithm is:
Because over all recursive calls, each
activity is examined exactly once. So it
takes constant time Θ (n) for n activities.
In this algorithm the activities are sorted
according to their finishing time, from the
earliest to the latest. Then the activities are
greedily selected by going down the list and
by picking whatever activity that is
compatible with the current selection.
It collects selected activities into a set A and
returns this set when it is done.
GREEDY-ACTIVITY-SELECTOR (s,f)
n=s.length
2 A={a1}
3 K=1
4 For m=2 to n
5 If s[m]>=f[k]
6 A= aU{am}
7 K=m
8 Return A
 The procedure works as follows.
 The variable k indexes the most recent
addition to A, corresponding to the activity
ak in the recursive version. Since we
consider the activities in order of
monotonically increasing finish time, fk is
always the maximum finish time of any
activity in A.
 Lines 2–3 select activity a1, initialize A to contain just
this activity, and initialize k to index this activity.
 The for loop of lines 4–7 finds the earliest activity
in Sk to finish. The loop considers each activity am in
turn and adds am to A if it is compatible with all
previously selected activities; such an activity is the
earliest in Sk to finish.
 line 5 check that its start time sm is not earlier
than the finish time fk of the activity most recently
added to A.
 If activity am is compatible, then lines 6–7 add
activity am to A and set k to m.
 The set A returned by the call GREEDY-ACTIVITY-
SELECTOR ( s,f) is precisely the set returned by the
call RECURSIVE-ACTIVITY-SELECTOR(s; f; 0; n).
 Lines 2–3 select activity a1, initialize A to contain
just this activity, and initialize k to index this
activity.
 The for loop of lines 4–7 finds the earliest activity in
Sk to finish. The loop considers each activity am in
turn and adds am to A if it is compatible with all
previously selected activities; such an activity is the
earliest in Sk to finish.
 check (in line 5) that its start time sm is not earlier
than the finish time fk of the activity most recently
added to A.
 If activity am is compatible, then lines 6–7 add
activity am to A and set k to m. The set A returned
by the call GREEDY-ACTIVITY-SELECTOR(s,f)is
precisely the set returned by the call RECURSIVE-
ACTIVITY-SELECTOR(s,f, 0, n)
 The sorting part can be as
 small as O(n log n) and the
 other part is O(n), so the
 total is O(n log n).
Activity selection problem

More Related Content

What's hot

daa-unit-3-greedy method
daa-unit-3-greedy methoddaa-unit-3-greedy method
daa-unit-3-greedy method
hodcsencet
 
Stressen's matrix multiplication
Stressen's matrix multiplicationStressen's matrix multiplication
Stressen's matrix multiplication
Kumar
 

What's hot (20)

daa-unit-3-greedy method
daa-unit-3-greedy methoddaa-unit-3-greedy method
daa-unit-3-greedy method
 
Greedy Algorithm - Knapsack Problem
Greedy Algorithm - Knapsack ProblemGreedy Algorithm - Knapsack Problem
Greedy Algorithm - Knapsack Problem
 
Divide and Conquer - Part 1
Divide and Conquer - Part 1Divide and Conquer - Part 1
Divide and Conquer - Part 1
 
All pair shortest path
All pair shortest pathAll pair shortest path
All pair shortest path
 
Elements of Dynamic Programming
Elements of Dynamic ProgrammingElements of Dynamic Programming
Elements of Dynamic Programming
 
Greedy Algorithms
Greedy AlgorithmsGreedy Algorithms
Greedy Algorithms
 
Longest Common Subsequence
Longest Common SubsequenceLongest Common Subsequence
Longest Common Subsequence
 
Amortized Analysis of Algorithms
Amortized Analysis of Algorithms Amortized Analysis of Algorithms
Amortized Analysis of Algorithms
 
Unit 3 daa
Unit 3 daaUnit 3 daa
Unit 3 daa
 
Kruskal’s Algorithm
Kruskal’s AlgorithmKruskal’s Algorithm
Kruskal’s Algorithm
 
Bruteforce algorithm
Bruteforce algorithmBruteforce algorithm
Bruteforce algorithm
 
unit-4-dynamic programming
unit-4-dynamic programmingunit-4-dynamic programming
unit-4-dynamic programming
 
Deadlock
DeadlockDeadlock
Deadlock
 
5.2 divide and conquer
5.2 divide and conquer5.2 divide and conquer
5.2 divide and conquer
 
Shortest Path in Graph
Shortest Path in GraphShortest Path in Graph
Shortest Path in Graph
 
A* Search Algorithm
A* Search AlgorithmA* Search Algorithm
A* Search Algorithm
 
Approximation Algorithms
Approximation AlgorithmsApproximation Algorithms
Approximation Algorithms
 
Stressen's matrix multiplication
Stressen's matrix multiplicationStressen's matrix multiplication
Stressen's matrix multiplication
 
Divide and conquer - Quick sort
Divide and conquer - Quick sortDivide and conquer - Quick sort
Divide and conquer - Quick sort
 
15 puzzle problem using branch and bound
15 puzzle problem using branch and bound15 puzzle problem using branch and bound
15 puzzle problem using branch and bound
 

Similar to Activity selection problem

Algorithm Design and Complexity - Course 5
Algorithm Design and Complexity - Course 5Algorithm Design and Complexity - Course 5
Algorithm Design and Complexity - Course 5
Traian Rebedea
 
Unit 2 Modeling of Programs A function maps inputs to out.docx
Unit 2 Modeling of Programs A function maps inputs to out.docxUnit 2 Modeling of Programs A function maps inputs to out.docx
Unit 2 Modeling of Programs A function maps inputs to out.docx
dickonsondorris
 
Lecture 8 dynamic programming
Lecture 8 dynamic programmingLecture 8 dynamic programming
Lecture 8 dynamic programming
Oye Tu
 
35000120060_Nitesh Modi_CSE Presentation on recursion.pptx
35000120060_Nitesh Modi_CSE Presentation on recursion.pptx35000120060_Nitesh Modi_CSE Presentation on recursion.pptx
35000120060_Nitesh Modi_CSE Presentation on recursion.pptx
15AnasKhan
 
2-Algorithms and Complexit data structurey.pdf
2-Algorithms and Complexit data structurey.pdf2-Algorithms and Complexit data structurey.pdf
2-Algorithms and Complexit data structurey.pdf
ishan743441
 

Similar to Activity selection problem (20)

Activity selection problem
Activity selection problemActivity selection problem
Activity selection problem
 
Accelerated Bat Algorithm For Solving Integer Programming Problems
Accelerated Bat Algorithm For Solving Integer Programming ProblemsAccelerated Bat Algorithm For Solving Integer Programming Problems
Accelerated Bat Algorithm For Solving Integer Programming Problems
 
GreedyAlgorithms.ppt
GreedyAlgorithms.pptGreedyAlgorithms.ppt
GreedyAlgorithms.ppt
 
GreedyAlgorithms.ppt
GreedyAlgorithms.pptGreedyAlgorithms.ppt
GreedyAlgorithms.ppt
 
Algorithm Design and Complexity - Course 5
Algorithm Design and Complexity - Course 5Algorithm Design and Complexity - Course 5
Algorithm Design and Complexity - Course 5
 
Lect-2.pptx
Lect-2.pptxLect-2.pptx
Lect-2.pptx
 
21-algorithms.ppt
21-algorithms.ppt21-algorithms.ppt
21-algorithms.ppt
 
21-algorithms (1).ppt
21-algorithms (1).ppt21-algorithms (1).ppt
21-algorithms (1).ppt
 
21-algorithms.ppt
21-algorithms.ppt21-algorithms.ppt
21-algorithms.ppt
 
Algorithm, Pseudocode and Flowcharting in C++
Algorithm, Pseudocode and Flowcharting in C++Algorithm, Pseudocode and Flowcharting in C++
Algorithm, Pseudocode and Flowcharting in C++
 
Unit 2 Modeling of Programs A function maps inputs to out.docx
Unit 2 Modeling of Programs A function maps inputs to out.docxUnit 2 Modeling of Programs A function maps inputs to out.docx
Unit 2 Modeling of Programs A function maps inputs to out.docx
 
Bt0080 fundamentals of algorithms1
Bt0080 fundamentals of algorithms1Bt0080 fundamentals of algorithms1
Bt0080 fundamentals of algorithms1
 
Computational Thinking 11- ActivitySelection.pptx
Computational Thinking 11- ActivitySelection.pptxComputational Thinking 11- ActivitySelection.pptx
Computational Thinking 11- ActivitySelection.pptx
 
TIME EXECUTION OF DIFFERENT SORTED ALGORITHMS
TIME EXECUTION   OF  DIFFERENT SORTED ALGORITHMSTIME EXECUTION   OF  DIFFERENT SORTED ALGORITHMS
TIME EXECUTION OF DIFFERENT SORTED ALGORITHMS
 
test pre
test pretest pre
test pre
 
Lecture 8 dynamic programming
Lecture 8 dynamic programmingLecture 8 dynamic programming
Lecture 8 dynamic programming
 
DSA Complexity.pptx What is Complexity Analysis? What is the need for Compl...
DSA Complexity.pptx   What is Complexity Analysis? What is the need for Compl...DSA Complexity.pptx   What is Complexity Analysis? What is the need for Compl...
DSA Complexity.pptx What is Complexity Analysis? What is the need for Compl...
 
Algorithms
AlgorithmsAlgorithms
Algorithms
 
35000120060_Nitesh Modi_CSE Presentation on recursion.pptx
35000120060_Nitesh Modi_CSE Presentation on recursion.pptx35000120060_Nitesh Modi_CSE Presentation on recursion.pptx
35000120060_Nitesh Modi_CSE Presentation on recursion.pptx
 
2-Algorithms and Complexit data structurey.pdf
2-Algorithms and Complexit data structurey.pdf2-Algorithms and Complexit data structurey.pdf
2-Algorithms and Complexit data structurey.pdf
 

Recently uploaded

Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Recently uploaded (20)

presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 

Activity selection problem

  • 1.
  • 2. PREPARED BY: Qurat Ul Ain SUBMITTED TO: Ma’am Samreen
  • 3.
  • 4.  A greedy algorithm is a mathematical process that looks for simple, easy-to-implement solutions to complex, multi-step problems by deciding which next step will provide the most obvious benefit.  Such algorithms are called greedy because while the optimal solution to each smaller instance will provide an immediate output, the algorithm doesn’t consider the larger problem as a whole. Once a decision has been made, it is never reconsidered.
  • 5.  Greedy algorithms work by recursively constructing a set of objects from the smallest possible constituent parts.  RECURSION is an approach to problem solving in which the solution to a particular problem depends on solutions to smaller instances of the same problem.  The advantage to using a greedy algorithm is that solutions to smaller instances of the problem can be straightforward and easy to understand.  The disadvantage is that it is entirely possible that the most optimal short-term solutions may lead to the worst possible long-term outcome.
  • 6.  Each step of the Greedy algorithm must take one of several possible choices. The greedy strategy advocates making the choice that is the best at the moment. It is used for solving optimization problems.  an optimization problem is the problem of finding the best solution from all feasible solutions.  Greedy algorithms are often used in ad hoc mobile networking to efficiently route packets with the fewest number of hops and the shortest delay possible. They are also used in machine learning, business intelligence (BI), artificial intelligence (AI) and programming.
  • 7.  We are given a set S of n activities S={a1, . . . , an}  Each activity ai has a start time si and finish time fi, where i-th activity ai = [si, fi) starts at time si and finishes at time fi. Where 0<=si<=fi<= ∞  These activities require the same resource (for example, one lecture hall).
  • 8. Two activities ai and aj are compatible if the intervals [si, fi) and [sj , fj) do not overlap, i.e they are disjoint.  The activity-selection problem (ASP) is to select a maximum size subset of mutually compatible activities.
  • 9.
  • 10.  Consider following set of activities,  The subset {a3, a9, a11}, {a1, a4, a8, a11} (a2, a4, a9, a11} ARE COMPATIBLE ACTIVITIES. i 1 2 3 4 5 6 7 8 9 10 11 Si 1 3 0 5 3 5 6 8 8 2 12 Fi 4 5 6 7 9 9 10 11 12 14 16
  • 11.  Assume activities sorted in increasing order of finish time fi  Let c[i,j] be the number of activities in the maximal solution for the subset Sij, i.e. the largest number of compatible activities starting from time fi, finishing at time sj  Recursively C[I,j]= { 0 if Sij=Ф } C[I,j]= {max i<k<j c[I,k]+c[k,j]+1 otherwise }  Then the maximum number of compatible activities is c[0, n + 1].
  • 12.  Greedy choice. Select an activity with minimum fi. Remove it and incompatible activities. Continue until no more activities left. This can be implemented with a recursive algorithm:
  • 13.  Input: s[1..n] is the array of start times, f[1..n] is the array of finish times , k is the index, n is the total no. of activities  RECURSIVE-ACTIVITY-SELECTOR(s, f, k, n) // Activities are sorted by finish time. // i is the activity selected previously. Start from k=0 1 m = k + 1 2 while m<= n and s[m] < f[k] // Find the first activity in Sk to finish 3 m = m + 1 4 if m <=n then 5 return {am} RECURSIVE-ACTIVITY- SELECTOR(s,f,m,n) 6 else return Ф;
  • 14.  Line 1: Initial call: REC-ACTIVITY- SELECTOR(s, f, 0, n). increment in m by k (moving next position)  Line 2-3: the while loop looks for the first activity in Sk to finish. The loop examines all activities until it finds first activity am that is compatible with ak (line 4)  Line 5: returns am activity, adding it (union) in the set sk  Line 6: when we have examined all activities m>n then the loop terminates
  • 15.
  • 16.  The running time of the Algorithm is: Because over all recursive calls, each activity is examined exactly once. So it takes constant time Θ (n) for n activities.
  • 17. In this algorithm the activities are sorted according to their finishing time, from the earliest to the latest. Then the activities are greedily selected by going down the list and by picking whatever activity that is compatible with the current selection. It collects selected activities into a set A and returns this set when it is done.
  • 18. GREEDY-ACTIVITY-SELECTOR (s,f) n=s.length 2 A={a1} 3 K=1 4 For m=2 to n 5 If s[m]>=f[k] 6 A= aU{am} 7 K=m 8 Return A
  • 19.  The procedure works as follows.  The variable k indexes the most recent addition to A, corresponding to the activity ak in the recursive version. Since we consider the activities in order of monotonically increasing finish time, fk is always the maximum finish time of any activity in A.
  • 20.  Lines 2–3 select activity a1, initialize A to contain just this activity, and initialize k to index this activity.  The for loop of lines 4–7 finds the earliest activity in Sk to finish. The loop considers each activity am in turn and adds am to A if it is compatible with all previously selected activities; such an activity is the earliest in Sk to finish.  line 5 check that its start time sm is not earlier than the finish time fk of the activity most recently added to A.  If activity am is compatible, then lines 6–7 add activity am to A and set k to m.  The set A returned by the call GREEDY-ACTIVITY- SELECTOR ( s,f) is precisely the set returned by the call RECURSIVE-ACTIVITY-SELECTOR(s; f; 0; n).
  • 21.  Lines 2–3 select activity a1, initialize A to contain just this activity, and initialize k to index this activity.  The for loop of lines 4–7 finds the earliest activity in Sk to finish. The loop considers each activity am in turn and adds am to A if it is compatible with all previously selected activities; such an activity is the earliest in Sk to finish.  check (in line 5) that its start time sm is not earlier than the finish time fk of the activity most recently added to A.  If activity am is compatible, then lines 6–7 add activity am to A and set k to m. The set A returned by the call GREEDY-ACTIVITY-SELECTOR(s,f)is precisely the set returned by the call RECURSIVE- ACTIVITY-SELECTOR(s,f, 0, n)
  • 22.  The sorting part can be as  small as O(n log n) and the  other part is O(n), so the  total is O(n log n).