23

Activity selection problem

Embed Size (px)

Citation preview

ANALYSIS OF ALGORITHMS

PREPARED BY:Qurat Ul Ain

SUBMITTED TO:Ma’am Samreen

TOPIC:

CHAPTER 16; GREEDY ALGORITHMS

16.1; AN ACTIVITY-SELECTION PROBLEM

GREEDY ALGORITHMS: 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).

ACTIVITY SELECTION PROBLEM:

COMPATIBLE ACTIVITIES:

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.

EXAMPLE: 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].

DYNAMIC PROGRAMMING APPROACH FOR ASP:

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:

RECURSIVE ALGORITHM FOR ASP:

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 + 12 while m<= n and s[m] < f[k] // Find the

first activity in Sk to finish3 m = m + 14 if m <=n then5 return {am} RECURSIVE-ACTIVITY-

SELECTOR(s,f,m,n)6 else return Ф;

DESCRIPTION OF ALGORITHM: 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

RUNNING TIME: 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 sortedaccording to their finishing time, from theearliest to the latest. Then the activities aregreedily selected by going down the list

andby picking whatever activity that iscompatible with the current selection.

It collects selected activities into a set A and returns this set when it is done.

GREEDY ALGORITHM FOR ASP:

GREEDY ALGORITHM :GREEDY-ACTIVITY-SELECTOR (s,f) n=s.length2 A={a1}3 K=14 For m=2 to n5 If s[m]>=f[k]6 A= aU{am}7 K=m8 Return A

ALGORITHM DESCRIPTION:

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)

RUNNING TIME: 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).