Anda di halaman 1dari 2

Homework 8: Activity Selection using

Greedy Algorithms
By Jace McPherson, October 29, 2017

Introduction
The purpose of this assignment was to code an algorithm for solving problem 16.1-5 in the textbook.
Given a set of activities, each with an ID, start time, finish time, and value, the idea is to find all the
possible activity schedules that result in the maximum possible “value” for the entire schedule. Activities
are not allowed to overlap their times, but there may be multiple schedules that result in the same total
schedule value.

Algorithms
To begin, activities are sorted monotonically in order of finish time. I propose a structure called a
“Solution”. Each activity is given one Solution object such that there is an array “solutions” of size “n”.
The first activity, 𝑎𝑎1 , has a solution that looks much like the following:

𝑎𝑎1 .Solution {
value = 𝑎𝑎1 .value,
schedules = [ [𝑎𝑎1 ] ]
}

We consider this our “base” subproblem. That is, this would be the solution if we only had one activity
to schedule. The “schedules” property of every Solution object is a list of every schedule in the
subproblem 𝑆𝑆0𝑖𝑖 , while every schedule is a list of activities. If a Solution object for an activity 𝑎𝑎𝑖𝑖 is empty
then that means there are no schedules in the subproblem 𝑆𝑆0𝑖𝑖 that both include 𝑎𝑎𝑖𝑖 and maximize
schedule value. This is important to note.

For each subsequent activity in our ordered activity list, we look at every solution before our current
activity 𝑎𝑎𝑖𝑖 . We need to know if 𝑎𝑎𝑖𝑖 is compatible with any previous schedule contained in the already-
calculated solutions. This means that for every previous Solution, we must iterate through each of its
possible schedules with some iterator 𝑗𝑗 ∈ [0. . 𝑖𝑖 − 1] to index the Solutions, and some iterator 𝑘𝑘 ∈
[0. . 𝑠𝑠𝑠𝑠𝑠𝑠𝑠𝑠𝑠𝑠𝑠𝑠𝑠𝑠𝑠𝑠𝑠𝑠[𝑗𝑗]. 𝑠𝑠𝑠𝑠ℎ𝑒𝑒𝑒𝑒𝑒𝑒𝑒𝑒𝑒𝑒𝑒𝑒. 𝑙𝑙𝑙𝑙𝑙𝑙𝑙𝑙𝑙𝑙ℎ] to index the schedules for that Solution. There are two cases
when looking at any given schedule:

𝑠𝑠𝑠𝑠𝑠𝑠𝑠𝑠𝑠𝑠𝑠𝑠𝑠𝑠𝑠𝑠𝑠𝑠[𝑗𝑗]. 𝑠𝑠𝑠𝑠ℎ𝑒𝑒𝑒𝑒𝑒𝑒𝑒𝑒𝑒𝑒𝑒𝑒[𝑘𝑘]. 𝑙𝑙𝑙𝑙𝑙𝑙𝑙𝑙. 𝑒𝑒𝑒𝑒𝑒𝑒𝑒𝑒𝑒𝑒𝑒𝑒𝑒𝑒 ≤ 𝑎𝑎𝑖𝑖 . 𝑠𝑠𝑠𝑠𝑠𝑠𝑠𝑠𝑠𝑠𝑠𝑠𝑠𝑠𝑠𝑠𝑠𝑠

• In this case, the current schedule is set up such that 𝑎𝑎𝑖𝑖 can simply be appended to a copy of the
schedule with no time overlap. This new schedule is added to the solution for the subproblem
for 𝑎𝑎𝑖𝑖 .

𝑎𝑎𝑎𝑎𝑎𝑎𝑎𝑎𝑎𝑎𝑎𝑎𝑎𝑎𝑎𝑎. 𝑣𝑣𝑣𝑣𝑣𝑣𝑣𝑣𝑣𝑣 ≥ 𝑠𝑠𝑠𝑠𝑠𝑠𝑠𝑠𝑠𝑠𝑠𝑠𝑠𝑠𝑠𝑠𝑠𝑠[𝑗𝑗]. 𝑣𝑣𝑣𝑣𝑣𝑣𝑣𝑣𝑣𝑣

• Here we create a new schedule that includes only the activity and add it to the solution for the
subproblem for 𝑎𝑎𝑖𝑖 .
Now, as we iterate over the 𝑖𝑖 − 1 previous solution, we have created new “Solution” objects for each of
those. At the end of this process, we have to combine the solutions in a way that is meaningful. That is
to say, we want the newly generated solution with the highest value, and if multiple solution have the
same maximum value, they should combine their collections of schedules.

The lastly generated “Solution” for the subproblem 𝑖𝑖 = 𝑛𝑛 is the solution containing the maximum
possible value. Other subproblems may have Solutions that have the same value, so their schedules
need to be printed in the results as well. Take the example for input2.txt. Our internal solutions array
after execution looks like the following:

[
0: Solution { value: 674, schedules: [[Activity 1]] },
1: Solution { value: 674, schedules: [] },
2: Solution { value: 741, schedules: [[Activity 4]] },
3: Solution { value: 1095, schedules: [[Activity 1, Activity 2]] },
4: Solution { value: 1095, schedules: [] },
5: Solution { value: 1095, schedules: [] },
]

The maximum value in this instance is 1095 (retrieved from 𝑠𝑠𝑠𝑠𝑠𝑠𝑠𝑠𝑠𝑠𝑠𝑠𝑠𝑠𝑠𝑠𝑠𝑠[5]. 𝑣𝑣𝑣𝑣𝑣𝑣𝑣𝑣𝑣𝑣). The optimal solution,
however is not contained in 𝑠𝑠𝑠𝑠𝑠𝑠𝑠𝑠𝑠𝑠𝑠𝑠𝑠𝑠𝑠𝑠𝑠𝑠[5]. 𝑠𝑠𝑠𝑠𝑠𝑠𝑠𝑠𝑠𝑠𝑠𝑠𝑠𝑠𝑠𝑠𝑠𝑠[3] contains the optimal solution, where activities
1 and 2 create the maximum value schedule.

So, for this test case, the program output file looks like:

1095
1 2

Results
Running the two tests cases provided on Moodle, my code works as expected. This is the output for
each of those tests. The first line is the maximum schedule value, and subsequent lines are all of the
activity IDs that result in that maximum value:

input1.txt input2.txt
3 1095
5 1 2
1 4
2 4

As far as I can tell, with the various inputs I have tried and verified. I handled edge cases as well with no
input and cases where the input was not already sorted by finish time.

How to Run
Simply execute the following commands in the same directory as Homework8.java

javac Homework8.java
java Homework8 <input_file> <output_file>

Anda mungkin juga menyukai