Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

Problem 2

The algorithm we use to get our optimal solution is essentially greedy’s with a slight variation.
The algorithm is as follows:

Define a considered student as a student who is either paired up or who the algorithm
attempted to pair up but had no eligible match.

While there is still a student who has not been considered


Select the next earliest student who is not matched (considered or not) and currently has
the earliest finish time, and call this student the current student
Consider all the students whose time intervals overlaps with the current student by at
least one hour. Choose the one with the next earliest finish time, and pair him/her up with the
current student.
Mark all the other students with overlaps as considered
Repeat this process

This runs in O(n^2) time. At worst, you would have to consider all other students for each
student who currently needs a partner. This means that in the worst case you have to run (n-1)+
(n-3)+(n-5)....+(0 if even, 1 if odd) considerations., which is bounded above by n^2.

Proof:
We will use the exchange argument as follows:
Assume that we ran our Greedy algorithm and the result is the best possible solution, which we
will call O. Assume that there is an alternative optimal solution, which we will call O’.
Consider all the pairs that we have in O and all the pairs in O’. For each possible pair (j, k) that
is in O that is not in O’, there are 3 different cases for each pair in O’:

1. J is not paired with anyone, but k is with k’


2. K is not paired up, but j is with j’
3. J is with j’ and k is with k’

Observe what happens if we try to transform each pair in O’ so as to gradually match the state
of O:

Case 1: simply switch k’ with j. J and k are now paired, and the number of pairs did not
decrease
Case 2: same as case 1. Simply switch j’ with k, and now j and k are paired with loss in the
number of pairs.
Case 3: This case is slightly more challenging. J is paired up with K in O, but j and with j’ and k
is with k’ in O’.
The fact that j and k are together implies that the finish times of both j’ and k’ come after
the finish time of the later member of the pair (j,k). Also, we know that j’ starts before j ends, and
k’ starts before k ends, in order for them to have been paired up in O’. Any other case would
violate the conditions of either case 3 or the fact that (j,k) is a pair in O (and obviously, k has to
start before j ends, if k is the later of the two in the pair (j,k), and k’ starts before j’ if k’ is the later
of the two).
Given this configuration of the time intervals for j, k, j’, and k’, we can very easily
rearrange the students to have j be with k and j’ be with k’, and thus we still retain the same
number of pairs.

By showing that we can change the state of O’ to match the state of O, we have shown that our
greedy algorithm returns a solution that is at least as good as any other solution.

You might also like