Given 2N-points in a 2D-plane, you have to group them into N pairs such that the overall sum of distances between the points of all of the pairs is the minimum possible value.The desired output is only the sum.
In other words, if a1,a2,..an are the distances between points of first, second…and nth pair respectively, then (a1+a2+…an) should be minimum.
Let us consider this test-case, if the 2*5 points are :
{20,20},
{40, 20},
{10, 10},
{2, 2},
{240, 6},
{12, 12},
{100, 120},
{6, 48},
{12, 18},
{0, 0}
The desired output is 237.
This is not my homework,I am inquisitive about different approaches rather than brute-force.
You seem to be looking for Minimum weight perfect matching.
There are algorithms to exploit the fact that these are points in a plane. This paper: Mincost Perfect Matching in the Plane has an algorithm and also mentions some previous work on it.
As requested, here is a brief description of a “simple” algorithm for minimum weighted perfect matching in a graph. This is a short summary of parts of the chapter on weighted matching in the book Combinatorial Optimization, Algorithms and Complexity by Papadimitriou & Steiglitz.
Say you are given a weighted undirected graph G(with an even number of nodes). The graph can be considered a complete weighted graph, by adding the missing edges and assigning them very large weights.
Suppose the vertices are labelled 1 to n and the weight of edge between vertices i and j is c(i,j).
We have n(n-1)/2 variables x(i,j) which denote a matching of G. x(i,j) = 1 if the edge between i and j is in the matching and x(i,j) = 0 if it isn’t.
Now the matching problem can be written as the Linear Programming Problem:
minimize Sum c(i,j) * x(i,j)
subject to the condition that
Sum x(1,j) = 1, where j ranges from 1 to n.
Sum x(2,j) = 1, where j ranges from 1 to n.
.
.
.
Sum x(n,j) = 1, where j ranges from 1 to n.
(Sum x(1,j) = 1 basically means that we are selecting exactly one edge incident the vertex labelled 1.)
And the final condition that
x(i,j) >= 0
(we could have said x(i,j) = 0 or 1, but that would not make this a Linear Programming Problem as the constraints are either linear equations or inequalities)
There is a method called the Simplex method which can solve this Linear Programming problem to give an optimal solution in polynomial time in the number of variables.
Now, if G were bipartite, it can be shown that we can obtain an optimal solution such that x(i,j) = 0 or 1. Thus by solving this linear programming problem for a bipartite graph, we get a set of assignments to each x(i,j), each being 0 or 1. We can now get a matching by picking those edges (i,j) for which x(i,j) = 1. The constraints guarantee that it will be a matching with smallest weight.
Unfortunately, this is not true for general graphs (i.e x(i,j) being 0 or 1). Edmonds figured out that this was because of the presence of odd cycles in the graph.
So in addition to the above contraints, Edmonds added the additional constraint that in any subset of vertices of size 2k+1 (i.e of odd size), the number of matched edges is no more than k
Enumerate each odd subset of vertices to get a list of sets S(1), S(2), …, S(2^n – n). Let size of S(r) be 2*s(r) + 1.
Then the above constraints are, for each set S(r)
Sum x(i,j) + y(r) = s(r), for i, j in S(r).
Edmonds then proved that this was enough to guarantee that each x(i,j) is 0 or 1, thus giving us a minimum weight perfect matching.
Unfortunately, now the number of variables has become exponential in size. So the simplex algorithm, if just run on this as it is, will lead to an exponential time algorithm.
To get past this, Edmonds considers the dual of this linear programming problem (I won’t go into details here), and shows that primal-dual algorithm when run on the dual takes only O(n^4) steps to reach a solution, thus giving us a polynomial time algorithm! He shows this by showing that at most O(n) of the y(r) are non-zero at any step of the algorithm (which he calls blossoms).
Here is a link which should explain it in a little more detail: http://www.cs.ucl.ac.uk/staff/V.Kolmogorov/papers/blossom5.pdf , Section 2.
The book I mentioned before is worth reading (though it can be a bit dry) to get a deeper understanding.
Phew. Hope that helps!