tags:

views:

284

answers:

3

Hey there,

I'm currently reading The Algorithm Design Manual and I'm stuck on this exercise. Could you help me out please?


Take a sequence of 2n real numbers as input. Design an O(n log n) algorithm that partitions the numbers into n pairs, with the property that the partition minimizes the maximum sum of a pair. For example, say we are given the numbers (1,3,5,9). The possible partitions are ((1,3),(5,9)), ((1,5),(3,9)), and ((1,9),(3,5)). The pair sums for these partitions are (4,14), (6,12), and (10,8). Thus the third partition has 10 as its maximum sum, which is the minimum over the three partitions.


My guess from some examples is that the solution looks like this:

# in pseudo ruby code
a = [1,3,5,9]
pairs = []
while !a.empty?
    pairs << [a.shift, a.pop]  # [a.first, a.last]
end
pairs

But I can't prove it. Thanks for your help,

John

+1  A: 

I think I can prove this for a sequence with no duplicated numbers, and it should be a reasonably simple exercise for the reader to extend the proof to non-unique sequences.

Pair x0, x2n together, then pair all other numbers according to an optimal solution.

Now consider the pairing of (x0, x2n) against any other pair xy, xz from the optimal subset. x2n + either xy or xz will be greater than xy+xz and also x2n+x0, therefore the pairing of x2n, x0 was optimal.

The proof now extends by induction to the pairing of X1, X2n-1, and further partitions of the subset, eventually producing the OP's pairing.

DigitalRoss
Hi Ross, thanks for your answer. What I'm missing is how to prove this algo will give the "optimal" partition, the one with the property that the partition minimizes the maximum sum of a pair.
John
I'm not sure if my proof is sufficiently rigorous, but I believe it does prove that the pairing is optimal. Note that I compared the pairing of the largest and smallest number to an optimal pairing of the remainder of the set, and showed that any repairing would make things worse. Now you can remove that x0,xn pair, and repeat the proof recursively all the subset, eventually proving by induction that extracting the largest and smallest at each step results in an optimal pairing.
DigitalRoss
+2  A: 

Given the input array

x0 x1 x2 ... x2n

use merge sort to sort it in O( n log n) time to produce a sorted list

xa0 xa1 ... xa2n

where the indices ai indicate the permutation that you have performed on the initial list to obtain the second list.

I claim then that the pairing that produces the minimum maximum sum over all pairings is the following pairing:

(xai, xa2n-i) for i = 0, 1, ..., n. That is, you group the highest valued number with the lowest valued number available.

Proof

Proceed by induction, for the case 2n=2 this is obvious.

Without loss of generality consider that the input is a list of sorted numbers (since if it is not then sort them)

x0 x1 x2 ... x2n.

Consider the pairing of x2n with any number, then clearly the minmum sum of this pairing is achieved with (x2n, x0).

Now consider the pairing of x2n-1, either (x2n,x0),(x2n-1,x1) is the pairings that produce the minumim max sum, or (x2n,x1),(x2n-1,x0) is, or both are. In the latter case our choice doesn't matter. In the seconds to last case, this is impossible (think about it.) In the general case if we proceed inductively by this process, when we are looking for a pair for x2n-k, xk is the lowest unused value that we can pair up with, however; suppose instead we pair up xk with some other x2n-j for j < k, to try to get a lower minum sum. This is impossible as, x2n-j + xk >= x2n-k + xk, so at most we can only achieve the same minimum sum.

This means that choosing (x2n-k,xk) gives us the minimum pairings.

ldog
But doesn't this assume that either (x2n,x0),(x2n-1,x1) or (x2n,x0),(x2n-1,x1) must be pairings? And if so, why is that?
PeterAllenWebb
It assumes that there *exists* an optimal solution with that as a pairing. The problem here is because of the non uniqueness of the list, there can be several optimal solutions. The inductive step tries to show that as you proceed down the list you can't get a *better* solution by swapping in a different pairing, though you can get an identical solution.
ldog
+4  A: 

The algorithm works because when x0, x1, ... x2n is the sorted list, there is always an optimal solution that contains (x0, x2n).

Proof:

Consider any optimal solution which does not contain (x0, x2n). It must contain pairs (x0, xa) and (xb, x2n) with x0 ≤ xa ≤ x2n and x0 ≤ xb ≤ x2n. Remove those pairs from the solution, and in their place put (x0, x2n) and (xa, xb). Could the presence of either new pair have "damaged" the solution? The pair (x0, x2n) could not have, since its sum is less than or equeal to the sum of (xb, x2n) which was a member of the original, optimal solution. Neither again could (xa, xb) have caused damage, since its sum is less than or equal to the sum of (xb, x2n), which was a member of the same solution. We have constructed an optimal solution which does contain (x0, x2n).

Thus the algorithm you give never forecloses the possibility of finding an optimal solution at any step, and when there are only two values left to pair they must be paired together.

PeterAllenWebb
yes thats a nicer proof.
ldog
makes sense now. Thanks you all for your help.
John