X + Y sorting

In computer science, $$\boldsymbol{X}+\boldsymbol{Y}$$ sorting is the problem of sorting pairs of numbers by their sums. Applications of the problem include transit fare minimisation, VLSI design, and sparse polynomial multiplication. As with comparison sorting and integer sorting more generally, algorithms for this problem can be based only on comparisons of these sums, or on other operations that work only when the inputs are small integers.

It is unknown whether this problem has a comparison-based solution whose running time is asymptotically faster than sorting an unstructured list of equally many items. Therefore, research on the problem has focused on two approaches to settle the question of whether such an improvement is possible: the development of algorithms that improve on unstructured sorting in their number of comparisons rather than in their total running time, and lower bounds for the number of comparisons based on counting cells in subdivisions of high-dimensional spaces. Both approaches are historically tied together, in that the first algorithms that used few comparisons were based on the weakness of the cell-counting lower bounds.

Problem statement and history
The input to the $$X+Y$$ sorting problem consists of two finite collections of numbers $$X$$ and $$Y$$, of the same length. The problem's output is the collection of all pairs of a number from $$X$$ and a number from $$Y$$, arranged into sorted order by the sum of each pair. As a small example, for the inputs $$X=\{1,2,9\}$$ and $$Y=\{0,4,9\}$$, the output should be the list of pairs$$(1,0),\, (2,0),\, (1,4),\, (2,4),\, (9,0),\, (1,9),\, (2,9),\, (9,4),\, (9,9)$$of one element from $$X$$ and one element from $$Y$$, listed in sorted order by their sums of pairs$$1, 2, 5, 6, 9, 10, 11, 13, 18.$$One way to solve the problem would be to construct the pairs to be sorted (the Cartesian product of the two collections) and use these pairs as input to a standard comparison sorting algorithm such as merge sort or heapsort. When the inputs have length $$n$$, they form $$n^2$$ pairs, and the time to sort the pairs in this way is $$O(n^2\log n)$$. In terms of its big O notation, this method is the fastest known algorithm for $$X+Y$$ sorting. Whether a faster algorithm exists is an open problem, posed by Elwyn Berlekamp prior to 1975.

A variant of the problem sorts the sumset, the set of sums of pairs, with duplicate sums condensed to a single value. For this variant, the size of the sumset may be significantly smaller than $$n^2$$, and output-sensitive algorithms for constructing it have been investigated.

Applications
Steven Skiena recounts a practical application in transit fare minimisation, an instance of the shortest path problem: find the cheapest two-hop airplane ticket between two given cities, from an input that describes both the cost of each hop and which pairs of hops may be combined into a single ticket. Skiena's solution consists of sorting pairs of hops by their total cost as an instance of the $$X+Y$$ sorting problem, and then testing the resulting pairs in this sorted order until finding one that is allowed. To generate the sorted pairs in this order, Skiena uses a priority queue of pairs, initially containing only a single pair, the one consisting of the two cheapest hops. Then, when a pair $$(x,y)$$ is removed from the queue and found to be disallowed, two more pairs are added, with one of these two pairs combining $$x$$ with the next hop after $$y$$ in a sorted list of the hops to the destination, and the other pair combining $$y$$ with the next hop after $$x$$ in a sorted list of hops from the start. In this way, each successive pair can be found in logarithmic time, and only the pairs up to the first allowable one need to be sorted.

$$X+Y$$ sorting is the most expensive subroutine in an algorithm for a problem in VLSI design, in which one must place two subunits of a VLSI circuit side-by-side along a communications channel in order to minimize the width of the channel needed to route pairs of wires from one subunit to the other. As one subunit is continuously shifted relative to the other, the channel width only changes at discrete positions where the ends of two wires line up with each other, and finding the sorted ordering of these positions in order to compute the sequence of changes to the width can be performed by $$X+Y$$ sorting. If this sorting problem could be sped up, it would also speed up this VLSI design task.

Another application involves polynomial multiplication for polynomials of a single variable that may have many fewer terms than their degrees. The product of two polynomials can be expressed as a sum of products of pairs of terms, one from each polynomial, and placing these term-by-term products into degree order amounts to sorting them by the sum of degrees. For example, the instance of $$X+Y$$ sorting with $$n=3$$ given as an example above corresponds to the multiplication of two three-term polynomials to produce a nine-term polynomial: $$\begin{align} (&x+x^2+x^9)(1+x^4+x^9)\\ &=x+x^2+x^5+x^6+x^9+x^{10}+x^{11}+x^{13}+x^{18}.\\ \end{align}$$ The degrees are always integers, so integer-based algorithms for $$X+Y$$ sorting may be applied. However, for polynomials whose number of terms is comparable to their degree, FFT-based polynomial multiplication algorithms may be significantly more efficient than term-by-term multiplication.

Number of orderings
A well-known lower bound for unstructured sorting, in the decision tree model, is based on the factorial number of sorted orders that an unstructured list may have. Because each comparison can at best reduce the number of possible orderings by a factor of two, sorting requires a number of comparisons at least equal to the binary logarithm of the factorial, which is $$n\log_2 n-O(n)$$. Early work on $$X+Y$$ sorting followed a similar approach by asking how many different sorted orderings are possible for this problem, and proving that this number is at most $$O(n^{8n})$$. However, because its binary logarithm is at most $$8n\log_2 n+O(1)$$, much smaller than the known time bounds for $$X+Y$$ sorting, this method can only lead to weak lower bounds on the number of comparisons.

The proof of this bound relates $$X+Y$$ sorting to the complexity of an arrangement of hyperplanes in high-dimensional geometry. The two input collections for the $$X+Y$$ sorting problem comprise $$2n$$ numbers, which can alternatively be interpreted as the Cartesian coordinates of a point in the $$2n$$-dimensional space $$\mathbb{R}^{2n}$$. This space can be subdivided into cells, so that within a single cell all points correspond to inputs that produce the same sorted order. For this subdivision, each boundary between two cells lies within a hyperplane defined by an equality of pairs $$x_i+y_j=x_k+y_\ell$$, where $$(x_i,y_j)$$ and $$(x_k,y_\ell)$$ are two pairs whose ordering changes from one adjacent cell to the other. These hyperplanes are either generated by two disjoint pairs, or they have the simplified forms $$x_i=x_k$$ or $$y_j=y_\ell$$, so the number of distinct hyperplanes that can be determined in this way is $$k=2\binom{n}{2}^2+2\binom{n}{2}.$$ The number of cells that this number of hyperplanes can divide a space of dimension $$2n$$ into is $$\binom{k}{2n}+\binom{k}{2n-1}+\cdots+\binom{k}{0}=O(n^{8n}).$$ Therefore, the set $$X+Y$$ has $$O(n^{8n})$$ different possible sorted orderings.

A similar style of analysis has been more successful in ruling out fast solutions to certain generalizations of $$X+Y$$ sorting, by showing that they have too many orderings to sort quickly. In particular, suggest separately sorting $$X$$ and $$Y$$, and then constructing a two-dimensional matrix of the values of $$X+Y$$ that is sorted both by rows and by columns before using this partially-sorted data to complete the sort of $$X+Y$$. This idea of using a row-sorted and column-sorted matrix forms the basis for the method used by Skiena in the transportation application, and it can reduce the number of comparisons by a constant factor relative to naive comparison sorting. However, for matrices whose rows and columns are sorted in this way, the number of possible sorted orderings of the whole matrix is much larger than $$O(n^{8n})$$, so large that any comparison sorting algorithm that can work for arbitrary $$n\times n$$ matrices that are sorted by rows and columns still requires $$\Omega(n^2\log n)$$ comparisons. Therefore, if the $$X+Y$$ sorting problem is to be solved quickly, the solution must use additional information about the set $$X+Y$$ beyond this matrix ordering.

Number of comparisons
For the classical comparison sorting problem, the time to sort and the number of comparisons needed to sort are within constant factors of each other. But for $$X + Y$$ sorting, the number of comparisons is smaller than the best time bound known: Michael Fredman showed in 1976 that $$X + Y$$ sorting can be done using only $$O(n^2)$$ comparisons. More generally, he showed that any set of $$N$$ elements, whose sorted ordering has already been restricted to a family $$\Gamma$$ of orderings, can be sorted using $$\log_2|\Gamma|+O(N)$$ comparisons, by a form of binary insertion sort. For the $$X + Y$$ sorting problem, $$N=n^2$$, and $$|\Gamma|=O(n^{8n})$$, so $$\log_2|\Gamma|=O(n\log n)$$ and Fredman's bound implies that only $$O(n^2)$$ comparisons are needed. However, in Fredman's method, the time needed to decide which comparisons to perform may be significantly higher than the bound on the number of comparisons.

The first explicit algorithm that achieves both $$O(n^2)$$ comparisons and $$O(n^2\log n)$$ total complexity was published sixteen years after Fredman by. The algorithm performs the following steps: The part of the algorithm that recursively sorts $$X+X$$ (or equivalently $$Y+Y$$) does so by the following steps: The number of comparisons $$C(n)$$ needed to perform this recursive algorithm on an input of $$n$$ items can be analyzed using the recurrence relation $$C(n)\le 2C(n/2)+O(n^2),$$ where the $$2C(n/2)$$ term of the recurrence counts the number of comparisons in the recursive calls to the algorithm to sort $$A+A$$ and $$B+B$$, and the $$O(n^2)$$ term counts the number of comparisons used to merge the results. The master theorem for recurrence relations of this form shows that $$C(n)=O(n^2).$$ The total time complexity is slower, $$O(n^2\log n)$$, because of the steps of the algorithm that use already-made comparisons to infer orderings of other sets. These steps can be performed in time $$O(n^2\log n)$$ by using a standard comparison-sorting algorithm with its comparison steps replaced by the stated inferences.
 * 1) Recursively sort the two sets $$X+X$$ and $$Y+Y$$.
 * 2) Use the equivalence $$x_i-x_j\le x_k-x_\ell \Leftrightarrow x_i+x_\ell\le x_j+x_k$$ to infer the sorted orderings of $$X-X$$ and $$Y-Y$$ without additional comparisons.
 * 3) Merge the two sets $$X-X$$ and $$Y-Y$$ into a single sorted order, using a number of comparisons linear in their total size.
 * 4) Use the merged order and the equivalence $$x_i+y_j\le x_k+y_\ell\Leftrightarrow x_i-x_k\le y_\ell-y_j$$ to infer the sorted order of $$X+Y$$ without additional comparisons.
 * 1) Split $$X$$ into two equal sublists $$A$$ and $$B$$.
 * 2) Recursively sort $$A+A$$ and $$B+B$$
 * 3) Infer the ordering on $$A+B$$ using only the comparisons from a single merge step as above.
 * 4) Merge the sorted results $$A+A$$, $$B+B$$, and $$A+B$$ together.

If only comparisons between elements of $$X+Y$$ are allowed, then there is also a matching lower bound of $$\Omega(n^2)$$ on the number of comparisons, but with more general comparisons involving linear combinations of constant numbers of elements, only $$O(n\log^2 n)$$ comparisons are needed.

Non-comparison-based algorithms
Just as integer sorting can be faster than comparison sorting for small-enough integers, the same is true for $$X+Y$$ sorting. In particular, with integer inputs in the range from $$0$$ to some upper limit $$M$$, the problem can be solved in $$O(n+M\log M)$$ operations by means of the fast Fourier transform.

Related problems
Several other problems in computational geometry have equivalent or harder complexity to $$X+Y$$ sorting, including constructing Minkowski sums of staircase polygons, finding the crossing points of an arrangement of lines in sorted order by their $$x$$-coordinates, listing pairs of points in sorted order by their distances, and testing whether one rectilinear polygon can be translated to fit within another.

The problem of testing whether two of the pairs in the $$X+Y$$ sorting problem have equal sums can be solved by sorting the pairs and then testing consecutive pairs for equality. In turn, it could be used to solve the 3SUM problem, implying that it is unlikely to have a strongly subquadratic algorithm.