0% found this document useful (0 votes)
69 views12 pages

Activity Selection Problem

Uploaded by

20981a4208
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
69 views12 pages

Activity Selection Problem

Uploaded by

20981a4208
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

What is Activity Selection Problem?

• Let's consider that you have n activities with


their start and finish times, the objective is to
find solution set having maximum number of
non-conflicting activities that can be executed
in a single time frame, assuming that only one
person or machine is available for execution.
Activity Selection Problem

The Activity Selection Problem is an


optimization problem which deals with the
selection of non-conflicting activities that needs
to be executed by a single person or machine in
a given time frame.
Each activity is marked by a start and finish
time. Greedy technique is used for finding the
solution since this is an optimization problem.
Steps for Activity Selection Problem
• Step 1: Sort the given activities in ascending order
according to their finishing time.
• Step 2: Select the first activity from sorted
array act[] and add it to sol[] array.
• Step 3: Repeat steps 4 and 5 for the remaining activities
in act[].
• Step 4: If the start time of the currently selected activity
is greater than or equal to the finish time of previously
selected activity, then add it to the sol[] array.
• Step 5: Select the next activity in act[] array.
• Step 6: Print the sol[] array.
Activity Selection Problem Example
• In the table below, we have 6 activities with corresponding
start and end time, the objective is to compute an execution
schedule having maximum number of non-conflicting activities:

Start time(s) Finish time(f) Activity name

5 9 A1

1 2 A2

3 4 A3

0 6 A4

5 7 A5

8 9 A6
• Step 1: Sort the given activities in ascending order
according to their finishing time.
The table after we have sorted it:

Start time(s) Finish time(f) Activity name

1 2 A2

3 4 A3

0 6 A4

5 7 A5

5 9 A1

8 9 A6
• Step 2: Select the first activity from sorted
array act[] and add it to the sol[] array, thus sol = {A2}.

• Step 3: If the start time of the currently selected


activity is greater than or equal to the finish time of the
previously selected activity, then add it to sol[].

• Step 4: Select the next activity in act[].


• For the data given in the above table,
• Select activity A3. Since the start time of a3 is greater
than the finish time of A2 (i.e. s(A3) > f(A2)), we
add A3 to the solution set. Thus sol = {A2, A3}.
• Select A4. Since s(A4) < f(A3), it is not added to the
solution set.
• Select A5. Since s(A5) > f(A3), A5 gets added to solution
set. Thus sol = {A2, A3, A5}
• Select A1. Since s(A1) < f(A5), A1 is not added to the
solution set.
• Select A6. A6 is added to the solution set since s(A6) >
f(A5). Thus sol = {A2, A3, A5, A6}.
• At last, print the array sol[]
the selected activities have been highlighted in grey.

Hence, the execution schedule of maximum number of non-


conflicting activities will be:
(1,2) (3,4) (5,7) (8,9)
Pseudo code
Example
Time Complexity Analysis
• Case 1: When a given set of activities are already sorted
according to their finishing time, then there is no sorting
mechanism involved, in such a case the complexity of the
algorithm will be O(n).

• Case 2: When a given set of activities is unsorted, then we will


have to use the sort() .
The time complexity of this method will be O(nlogn), which
also defines complexity of the algorithm.

You might also like