SlideShare ist ein Scribd-Unternehmen logo
1 von 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

Weitere ähnliche Inhalte

Was ist angesagt?

Elements of dynamic programming
Elements of dynamic programmingElements of dynamic programming
Elements of dynamic programmingTafhim Islam
 
Lecture: Regular Expressions and Regular Languages
Lecture: Regular Expressions and Regular LanguagesLecture: Regular Expressions and Regular Languages
Lecture: Regular Expressions and Regular LanguagesMarina Santini
 
Data Structure: Algorithm and analysis
Data Structure: Algorithm and analysisData Structure: Algorithm and analysis
Data Structure: Algorithm and analysisDr. Rajdeep Chatterjee
 
03 Analysis of Algorithms: Probabilistic Analysis
03 Analysis of Algorithms: Probabilistic Analysis03 Analysis of Algorithms: Probabilistic Analysis
03 Analysis of Algorithms: Probabilistic AnalysisAndres Mendez-Vazquez
 
finding Min and max element from given array using divide & conquer
finding Min and max element from given array using  divide & conquer finding Min and max element from given array using  divide & conquer
finding Min and max element from given array using divide & conquer Swati Kulkarni Jaipurkar
 
Divide and conquer - Quick sort
Divide and conquer - Quick sortDivide and conquer - Quick sort
Divide and conquer - Quick sortMadhu Bala
 
Knapsack problem using dynamic programming
Knapsack problem using dynamic programmingKnapsack problem using dynamic programming
Knapsack problem using dynamic programmingkhush_boo31
 
Algorithms Lecture 3: Analysis of Algorithms II
Algorithms Lecture 3: Analysis of Algorithms IIAlgorithms Lecture 3: Analysis of Algorithms II
Algorithms Lecture 3: Analysis of Algorithms IIMohamed Loey
 
Time and space complexity
Time and space complexityTime and space complexity
Time and space complexityAnkit Katiyar
 
1.10. pumping lemma for regular sets
1.10. pumping lemma for regular sets1.10. pumping lemma for regular sets
1.10. pumping lemma for regular setsSampath Kumar S
 
Chapter 09 design and analysis of algorithms
Chapter 09  design and analysis of algorithmsChapter 09  design and analysis of algorithms
Chapter 09 design and analysis of algorithmsPraveen M Jigajinni
 
0/1 knapsack
0/1 knapsack0/1 knapsack
0/1 knapsackAmin Omi
 
5.2 divide and conquer
5.2 divide and conquer5.2 divide and conquer
5.2 divide and conquerKrish_ver2
 
NFA Non Deterministic Finite Automata by Mudasir khushik
NFA Non Deterministic Finite Automata by Mudasir khushikNFA Non Deterministic Finite Automata by Mudasir khushik
NFA Non Deterministic Finite Automata by Mudasir khushikMudsaraliKhushik
 

Was ist angesagt? (20)

Best,worst,average case .17581556 045
Best,worst,average case .17581556 045Best,worst,average case .17581556 045
Best,worst,average case .17581556 045
 
Elements of dynamic programming
Elements of dynamic programmingElements of dynamic programming
Elements of dynamic programming
 
Minimum spanning tree
Minimum spanning treeMinimum spanning tree
Minimum spanning tree
 
Lecture: Regular Expressions and Regular Languages
Lecture: Regular Expressions and Regular LanguagesLecture: Regular Expressions and Regular Languages
Lecture: Regular Expressions and Regular Languages
 
Data Structure: Algorithm and analysis
Data Structure: Algorithm and analysisData Structure: Algorithm and analysis
Data Structure: Algorithm and analysis
 
NP completeness
NP completenessNP completeness
NP completeness
 
Recursive algorithms
Recursive algorithmsRecursive algorithms
Recursive algorithms
 
03 Analysis of Algorithms: Probabilistic Analysis
03 Analysis of Algorithms: Probabilistic Analysis03 Analysis of Algorithms: Probabilistic Analysis
03 Analysis of Algorithms: Probabilistic Analysis
 
Daa unit 4
Daa unit 4Daa unit 4
Daa unit 4
 
finding Min and max element from given array using divide & conquer
finding Min and max element from given array using  divide & conquer finding Min and max element from given array using  divide & conquer
finding Min and max element from given array using divide & conquer
 
Divide and conquer - Quick sort
Divide and conquer - Quick sortDivide and conquer - Quick sort
Divide and conquer - Quick sort
 
Knapsack problem using dynamic programming
Knapsack problem using dynamic programmingKnapsack problem using dynamic programming
Knapsack problem using dynamic programming
 
Algorithms Lecture 3: Analysis of Algorithms II
Algorithms Lecture 3: Analysis of Algorithms IIAlgorithms Lecture 3: Analysis of Algorithms II
Algorithms Lecture 3: Analysis of Algorithms II
 
Time and space complexity
Time and space complexityTime and space complexity
Time and space complexity
 
1.10. pumping lemma for regular sets
1.10. pumping lemma for regular sets1.10. pumping lemma for regular sets
1.10. pumping lemma for regular sets
 
Chapter 09 design and analysis of algorithms
Chapter 09  design and analysis of algorithmsChapter 09  design and analysis of algorithms
Chapter 09 design and analysis of algorithms
 
0/1 knapsack
0/1 knapsack0/1 knapsack
0/1 knapsack
 
Asymptotic Notation
Asymptotic NotationAsymptotic Notation
Asymptotic Notation
 
5.2 divide and conquer
5.2 divide and conquer5.2 divide and conquer
5.2 divide and conquer
 
NFA Non Deterministic Finite Automata by Mudasir khushik
NFA Non Deterministic Finite Automata by Mudasir khushikNFA Non Deterministic Finite Automata by Mudasir khushik
NFA Non Deterministic Finite Automata by Mudasir khushik
 

Ähnlich wie Activity selection problem

Activity selection problem
Activity selection problemActivity selection problem
Activity selection problemSumita Das
 
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 ProblemsTye Rausch
 
GreedyAlgorithms.ppt
GreedyAlgorithms.pptGreedyAlgorithms.ppt
GreedyAlgorithms.pptssuser422644
 
Algorithm Design and Complexity - Course 5
Algorithm Design and Complexity - Course 5Algorithm Design and Complexity - Course 5
Algorithm Design and Complexity - Course 5Traian Rebedea
 
Algorithm, Pseudocode and Flowcharting in C++
Algorithm, Pseudocode and Flowcharting in C++Algorithm, Pseudocode and Flowcharting in C++
Algorithm, Pseudocode and Flowcharting in C++Johnny Jean Tigas
 
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.docxdickonsondorris
 
Bt0080 fundamentals of algorithms1
Bt0080 fundamentals of algorithms1Bt0080 fundamentals of algorithms1
Bt0080 fundamentals of algorithms1Techglyphs
 
Computational Thinking 11- ActivitySelection.pptx
Computational Thinking 11- ActivitySelection.pptxComputational Thinking 11- ActivitySelection.pptx
Computational Thinking 11- ActivitySelection.pptxssuser1a5f25
 
TIME EXECUTION OF DIFFERENT SORTED ALGORITHMS
TIME EXECUTION   OF  DIFFERENT SORTED ALGORITHMSTIME EXECUTION   OF  DIFFERENT SORTED ALGORITHMS
TIME EXECUTION OF DIFFERENT SORTED ALGORITHMSTanya Makkar
 
test pre
test pretest pre
test prefarazch
 
Lecture 8 dynamic programming
Lecture 8 dynamic programmingLecture 8 dynamic programming
Lecture 8 dynamic programmingOye Tu
 
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...2022cspaawan12556
 
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.pptx15AnasKhan
 
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.pdfishan743441
 

Ähnlich wie 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
 

Kürzlich hochgeladen

Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 
Google AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGGoogle AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGSujit Pal
 
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 WorkerThousandEyes
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 

Kürzlich hochgeladen (20)

Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 
Google AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGGoogle AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAG
 
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
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 

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).