Distance between multisets of integers

235 Views Asked by At

If you have two multisets of integers $X$ and $Y$ both of size $n$, I define a matching to be a multiset of pairs with one integer from $X$ and one from $Y$ in each pair. A matching must have exactly $n$ pairs and each integer from $X$ (resp $Y$) can only be used once. In other words we are making a perfect bipartite matching of the elements of $X$ and $Y$.

The weight of a pair is the absolute difference between the two integers. I define the distance between $X$ and $Y$ as the minimum total weight of any matching.

Is there a simple and fast way to compute the distance between any two such $X$ and $Y$? It is tempting just to pair each integer in $X$ with the nearest value in $Y$ that has not already been taken but I am not sure this is always optimal.

1

There are 1 best solutions below

3
On BEST ANSWER

It is better to sort $X$ and $Y$, then match the largest element of $X$ with the largest of $Y$ and continue down the line. As an example, let $X=\{5,10\}, Y=\{1,6\}$. If you match $5$ and $6$ because they are close the distance is $10$. If you match $10$ and $6$ the distance is $8$.