You are on page 1of 9

Apex Institute of Technology

Department of Computer Science & Engineering


Bachelor of Engineering (Computer Science & Engineering)
Design and Analysis of Algorithms– (20CSH-282)
Prepared By: Mr. Vikas Kumar (E13657)

04/24/2023 DISCOVER . LEARN . EMPOWER


1
Content

Activity Selection problem

2
Activity-Selection Problem
• Here are a set of start and finish times

• What is the maximum number of activities that can be completed?


• {a3, a9, a11} can be completed
• But so can {a1, a4, a8’ a11} which is a larger set
• But it is not unique, consider {a2, a4, a9’ a11}
• We will solve this problem in the following manner
• Show the optimal substructure property holds
• Solve the problem using dynamic programming
• Show it is greedy and provide a recursive greedy solution
• Provide an iterative greedy solution 3
A Top Down Recursive Solution

• The step by step solution is on the next slide


• Assuming the activities have been sorted by finish times,
then the complexity of this algorithm is Q(n)
• Developing an iterative algorithm would be even faster

4
Activity-Selection Problem

• Here is a step by step solution


• Notice that the solution is not
unique
• But the solution is still optimal
• No larger set of activities can
be found

5
An Iterative Approach
• The recursive algorithm is almost tail recursive (what is that?) but there is a
final union operation
• We let fi be the maximum finishing time for any activity in A

• The loop in lines 4-7 stops when the earliest finishing time is found

•The overall complexity of this algorithm is Q(n) 6


Developing a Dynamic Solution
• Define the following subset of activities which are activities that
can start after ai finishes and finish before aj starts

• Sort the activities according to finish time

• We now define the the maximal set of activities from i to j as

• Let c[i,j] be the maximal number of activities

• Our recurrence relation for finding c[i, j] becomes

• We can solve this using dynamic programming, but a simpler


approach exists
REFERENCES
Text books:
• Cormen, Leiserson, Rivest, Stein, “Introduction to Algorithms”, Prentice Hall of India, 3rd edition 2012. problem, Graph
coloring.

Websites:
• https://www.tutorialspoint.com/data_structures_algorithms/kruskals_spanning_tree_algorithm.htm
• https://www.geeksforgeeks.org/kruskals-minimum-spanning-tree-algorithm-greedy-algo-2/
THANK YOU

You might also like