The Activity Selection Problem

Peter is an energetic guy, and usually has many things to do in a given day. However, with the amount of things he wants to do, he is usually unable to do them all in a single day. What he usually does after waking up is write up a list of activities that he has to do, along with their time span. Then, looking at that list, he devises a plan for the day, trying to accommodate as many activities as possible.

Being an energetic guy, he usually rushes through this process and finds himself doing fewer activities than possible throughout the day. Can you help him maximize the amount of activities he can do in a day, given his schedule? An example of a schedule for Peter is given in the following table:

ID Activity Time Span
1 Tidy up his room 10:00 - 12:00
2 Going to the rock concert 20:00 - 23:00
3 Play chess at the local club 17:00 - 19:00
4 Take a shower 10:00 - 10:30
5 Dinner with friends 19:00 - 20:30
6 Play Civilization VI 21:30 - 23:00
7 Have lunch with friends 12:30 - 13:30
8 Go to the cinema 20:00 - 22:00
9 Go biking in the park 17:00 - 19:30
10 Go to the beach 16:00 - 19:00
11 Go to the library 15:00 - 17:00
Table 4.1: Peter's schedule

This is known as the activity selection problem. The problem is to schedule several competing activities that require exclusive use of a common resource (which is Peter, in this case), with the goal of selecting a maximum size set of activities that are mutually compatible.

In other words, we are trying to find the biggest set of activities that Peter can perform in a day. Each activity (ai) has a start time (si) and a finish time (fi). Two activities, ai and aj, are considered compatible if the intervals (si, fi) and (sj, fj) do not overlap, for example, si ≥ fj or sj ≥ fi.

Looking at Peter's schedule, and converting the start and finish times to minutes since the start of a day, we can arrive at the following table. To convert the times to minutes since the start of the day, we multiply the hours by 60 and add the minutes. For example, activity 1 runs from 10:00 to 12:00. 10:00 is 600 (10*60 + 0) minutes since the start of the day, and 12:00 is 720 (12*60 + 0) minutes since the start of the day:

ai 1 2 3 4 5 6 7 8 9 10 11
si 600 1200 1020 600 1140 1290 750 1200 1020 960 900
fi 720 1380 1140 630 1230 1380 810 1320 1170 1140 1020

Table 4.2: Start and finish times of peter's activities

For this example, the subset {a1, a3, a5, a6} consists of mutually compatible activities, as their times don't overlap. It is not a maximum subset (that is, we can find a set with a larger number of activities), since the subset {a3, a4, a5, a6, a7, a11} is larger (note that the order of the activities is a4, a7, a11, a3, a5 and a6). In fact, it is a larger subset of mutually compatible activities. It is not the only one: another possible larger subset is {a1, a7, a11, a3, a5, a6}.

How should we approach this problem to find the maximum size set of activities that are mutually compatible? It turns out we should do the greedy choice. What this means is that, at each step of the algorithm, from the set of activities that we can still perform, we should choose one greedily.

The greedy choice may not be immediate, but you can intuitively think that we should select activities that leave Peter available for as many other activities as possible.

We can have two approaches to resolve this problem, and they are as follows:

  • Always choose the activity with the earliest starting time; however, we can have the activity with the earliest starting time finishing after all the other activities.
  • Choose the activity that consumes the least amount of time; however, we can have a small activity overlapping two or more non-overlapping activities (for example, activities [1, 4), [3, 5) and [4, 8)).

Hence, both of these approaches don't work.

From the set of activities that we are able to choose, we must choose the first one to finish, as that is the activity that would leave Peter available for as many of the activities that follow it as possible. If we sort activities by finish time, we can then always select the first activity we find that is compatible with the last activity selected for Peter.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset