0% found this document useful (0 votes)
24 views25 pages

L14 - Activity Selection

Uploaded by

swatikar.2708
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)
24 views25 pages

L14 - Activity Selection

Uploaded by

swatikar.2708
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/ 25

Greedy Programming

What is Greedy Algorithm?


• Greedy is an algorithmic paradigm that builds up a solution piece by
piece, always choosing the next piece that offers the most obvious and
immediate benefit.

• So the problems where choosing locally optimal also leads to global


solution are the best fit for Greedy.
Advantages of Greedy Approach
• The algorithm is easier to describe.
• This algorithm can perform better than other algorithms (but, not in
all cases).
Drawback of Greedy Approach
• As mentioned earlier, the greedy algorithm doesn't always produce the
optimal solution. This is the major disadvantage of the algorithm
• For example, suppose we want to find the longest path in the graph
below from root to leaf. Let's use the greedy algorithm here.
Greedy Approach
• 1. Let's start with the root node 20. The weight of the right child is 3
and the weight of the left child is 2.
• 2. Our problem is to find the largest path. And, the optimal solution at
the moment is 3. So, the greedy algorithm will choose 3.
• 3. Finally the weight of an only child of 3 is 1. This gives us our final
result 20 + 3 + 1 = 24.
• However, it is not the optimal solution. There is another path that
carries more weight (20 + 2 + 10 = 32) as shown in the image below.
• Therefore, greedy algorithms do not always give an optimal/feasible
solution.
Activity Selection Problem
Greedy Programming

6
Activity Selection Problem
• Input: A set of activities S = {a1,…, an}

• Each activity has start time (si) and a finish time (fi) ai=(si, fi)
• Two activities are compatible if and only if their interval does not
overlap.

• Output: A maximum-size subset of mutually


compatible activities.

7
Example: Activity Selection
• 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} .
8
Activity Selection: A
Greedy Algorithm
The greedy solution is simple:

1. Sort the activities by finish time.


2. Schedule the first activity.
3. Then schedule the next activity in sorted list
which starts after previous activity finishes.
4. Repeat until no more activities.

9
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Solution

Activities selected: {a1, a4, a8’ a11}

17
Algorithm: Activity Selection
Sort the input activities by increasing finishing time (f1 ≤ f2 ≤ . . . ≤ fn)
n = length [s]
A={i}
j=1
For i = 2 to n
do if si ≥ fj
then A= AU{i}
j=i
Return A
18
Complexity Analysis
• Sorting the activities on the basis of finishing line = O(nlgn)
• For loop in the algorithm = Ɵ(n)

• Overall Complexity:
When activities are sorted by their finish time: O(N)
When activities are not sorted by their finish time, the time
complexity is O(N log N) due to complexity of sorting.
19
In Class Practice
Ques: Look at the following table containing activities, and their start
and end time.

Here, si and fi are the starting and the finishing time of the activity ai.

20
Solution

21
Problem:
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
Solution:
Example:
• Given 10 activities along with their start and end time as
S = (A1 A2 A3 A4 A5 A6 A7 A8 A9 A10)
Si = (1,2,3,4,7,8,9,9,11,12)
fi = (3,5,4,7,10,9,11,13,12,14)
Thus the final Activity
schedule is:

You might also like