• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

[Algorithms] Pairs of two

Status
Not open for further replies.
Level 16
Joined
Oct 12, 2008
Messages
1,570
Given 2n points in a metric space (X, d), (just think of d as "the" distance function), I desire a set (or list, array, whatever) of size n of sets (these really need to be sets, as in order does not matter, but content does) of size 2 of given points such that all the size 2 sets are pairwise disjoint (thus make up the entire set of 2n points), and such that the sum of the distance between all pairs is minimal.

On a more mathematical note:
Given: set A, |A|=2n
Result: set B, |B| = n
B = {B1, ... , Bn}
where, for all i in {1, ... , n}: |Bi|=2,
for all i, j in {1, ..., n}: Bi and Bj are disjoint if i /= j,
Sum from k = 1 to n of: d(Bk, 1, Bk, 2) is minimal
(where Bk = {Bk, 1, Bk, 2} )

The attached PDF file contains the above with more strict notation.

I have been thinking, but only a little, and the best I could come up with is an O(n2) algorithm, a brute force one, first calculating all distances between all points, taking O(n2), and then taking the n smallest, which would take at most O(n2), depending on what method one takes, for instance first sorting and then just taking the first n elements.

More info on how I think this problem could be generalised and the origin of the problem will come in an edit.

EDIT:
A generalisation of the problem (though already quite abstract by using d, instead of a simple minded Euclidean distance function), would be to not create sets of 2, but sets of k. How would one go about generalising to the "distance" of those k elements in each set? I was thinking about a path through all k points, and measuring the distance of that. Then, however, to get the minimal distance, one would again have to get the shortest path through all elements. Graph theory would come in hand here. However, when one can come up with a satisfying function f : B -> R+ (real positive numbers, or 0 included when desired) one could just formalise the problem to minimalise the sum of all f(Bk)

EDIT2:
Where did I come up with such a problem? My job. I see maths and algorithms everywhere. We bake baguettes, and then pack them in pairs. When cooled down enough, we throw a pile of baguettes onto a table, and start packing. I was thinking, how can I take pairs of two and pack them, without having to move my arms more than neccessary? Well when there are only a few, it is quite intuitive and easy, but I then started to set loose my mathematical mind. This problem is all in Euclidean 2-dimensional space, but what about other spaces? Or what about when we start packing in 5's when there is some sort of discount when buying a pack of 5? All these thoughts created this problem.

Now I could of course start to devise my own algorithm, and I would sure enjoy it, but I think I would even more enjoy the discussion created by multiple people thinking about this problem and coming up with varying solutions!

So, now, all and every thought, please share! :)
 

Attachments

  • problem.pdf
    61.2 KB · Views: 152
Last edited:

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,259
Someone has been doing too much set theory. Trust me, it is not healthy.

This is one of these traveling salesman type problems where it is computationally impossible to ever prove anything you create to solve it. You can only ever approximate the best solution and prove its effectiveness by a metric of performance.

In this case I am guessing a matching algorithm similar to online dating systems use would work the best. The match criteria in this case would be the distance between two points where smaller is better. As this becomes based on distance you can simplify problem complexity immediately using efficient structures such as trees.

The idea is to find the closet N points to every point which you consider as possible matches (larger may result in best possible matching but at higher resource cost). Then you run the matching algorithm such that it tries to match points with the point that is closest to it without compromising distance on other points. The result is that some points may take their second or third best match to allow for other points to use them as they are closer and will produce higher overall match.

I recall the matching algorithm itself being highly efficient and it already does what you want, aims for overall best total matching (in this case that would be least total distance). Of course it might not give the perfect results, but it is computationally feasible.
 
Level 6
Joined
Aug 26, 2009
Messages
192
This is one of these traveling salesman type problems where it is computationally impossible to ever prove anything you create to solve it. You can only ever approximate the best solution and prove its effectiveness by a metric of performance.

Oum, what? No program code can be proven correct by an algorithm in finite time, that's provable, but has nothing to do with the traveling salesman problem. The traveling salesman problem can be calculated, just not in polynomial time, which is quite bad. It's in the complexity class NP.

@Problem, use the word "Partition". You actually look for a partition B of A such that for b in B, |b| = 2 and the sum of the distance of two elements of all b in B is minimal. (is there actually LaTeX in hive? Would be fucking cool, inwc has it afaik...)
I Couldn't come up with an algorithm yet, but gonna look on it!
 

peq

peq

Level 6
Joined
May 13, 2007
Messages
171
Not sure, but I think this might help: Fast and Simple Algorithms for Weighted Perfect Matching

The paper claims the algorithm takes O(n²) which is already optimal since you have O(n²) distances to consider (assuming distances are arbitrary).

I have been thinking, but only a little, and the best I could come up with is an O(n²) algorithm, a brute force one, first calculating all distances between all points, taking O(n²), and then taking the n smallest, which would take at most O(n²), depending on what method one takes, for instance first sorting and then just taking the first n elements.

I think this does not work. Consider the following example:
n=2
A = {a,b,c,d}
d(a,b)=1
d(a,c)=2
d(a,d)=2
d(b,c)=2
d(b,d)=2
d(c,d)=99999

If you take the minimal distance set {a,b} first, then you have to choose {c,d} as the second set and you will not get an optimal solution.
 
Level 14
Joined
Jun 27, 2008
Messages
1,325
I think this does not work. Consider the following example:
n=2
A = {a,b,c,d}
d(a,b)=1
d(a,c)=2
d(a,d)=2
d(b,c)=2
d(b,d)=2
d(c,d)=99999

If you take the minimal distance set {a,b} first, then you have to choose {c,d} as the second set and you will not get an optimal solution.

True, but do you really need the optimal solution? Often greedy algorithms or randomized (monte-carlo/las-vegas) algorithms exist that are much faster than the optimal solvers still throw sufficient solutions. (often these algorithms are much more fun too)
 
Status
Not open for further replies.
Top