Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I've been looking around for an algorithm that would optimize the distance between 2 list of coordinates and choose which coordinate should go together.

Say I have List 1:


List 2:


Calculated Distance between Coords:

205|200 to 210|200 == 5.00
205|200 to 207|190 == 10.20
205|200 to 230|200 == 25.00
205|200 to 234|190 == 30.68

220|210 to 210|200 == 14.14
220|210 to 207|190 == 23.85
220|210 to 230|200 == 14.14
220|210 to 234|190 == 24.41

200|220 to 210|200 == 22.36
200|220 to 207|190 == 30.81
200|220 to 230|200 == 36.06
200|220 to 234|190 == 45.34

200|180 to 210|200 == 22.36
200|180 to 207|190 == 12.21
200|180 to 230|200 == 36.06
200|180 to 234|190 == 35.44

This Algorithm would pick:

205|200 to 230|200 == 25.00
220|210 to 207|190 == 23.85
200|220 to 210|200 == 22.36
200|180 to 234|190 == 35.44

The Algorithm would pick these numbers as they would be the group that would have the littlest variance between the distance. Conditions:

  1. A Coordinate may only be used ones from each list
  2. If List 1 or List2 is larger than it still only uses each coordinate once, but it tries to get the smallest distance variance and does nothing with the unused coordinates.

If you need more clarification please ask.

P.S. I've looked at the Hungarian algorithm and it seems like it will sort of do the job, but not exactly how I was expecting. The Hungarian algorithm will only try and make the least distance from all the coordinates, which can mean the smallest variance, but not every time as variance is more important here then least distance optimization.

Additional Information

I will have an array of List1, List2, and then the distances:

Distance[List1_item_0][List2_item_0] = 5;
Distance[List1_item_0][List2_item_1] = 10.20;
Distance[List1_item_0][List2_item_2] = 25.00;
Distance[List1_item_0][List2_item_3] = 30.68;

Distance[List1_item_1][List2_item_0] = 14.14;
Distance[List1_item_1][List2_item_1] = 23.85;
Distance[List1_item_1][List2_item_2] = 14.14;
Distance[List1_item_1][List2_item_3] = 24.41;

Distance[List1_item_2][List2_item_0] = 22.36;
Distance[List1_item_2][List2_item_1] = 30.81;
Distance[List1_item_2][List2_item_2] = 36.06;
Distance[List1_item_2][List2_item_3] = 45.34;

Distance[List1_item_3][List2_item_0] = 22.36;
Distance[List1_item_3][List2_item_1] = 12.21;
Distance[List1_item_3][List2_item_2] = 36.06;
Distance[List1_item_3][List2_item_3] = 35.44;

From the Distance['List1_item_#] I would need to pick a distance. Once that distance is picked the [List2_item_#] CANNOT be picked by a different [List1_item_#]. The distances picked for each [List1_item_#] element would need to be picked in a way that the variance between them all is minimal. So distance for each [List1_item_#] should be as close as possible to each other without reusing a [List2_item_#] more than once.

share|improve this question
This question looks more for or – Vitalij Zadneprovskij May 25 '12 at 20:18
this looks a lot like the traveling salesman problem... – Ryathal May 25 '12 at 20:49
It's actually for a time based game,, I will be using it to optimize an attack sequence for different people who play the game. – Steven10172 May 25 '12 at 20:51
Do you want to minimize the sum of all distance variances? Or the sum of squares? Or what else? – Doc Brown May 26 '12 at 5:52
I need to minimize the variance between the choose sets of data points from the arrays. I'll add more to the question. – Steven10172 May 26 '12 at 9:30

Looks like a Dynamic Programming problem. It looks like there are N*M pairs possible. The problem is, given points A1 and A2, and B1 and B2, the question whether pairs (A1,B1)+(A2,B2) are better than (A1,B2)+(A2,B1) depends on the addition of points A3 and B3.

I suspect therefore that any analytical solution will be O(exp(N*M)), i.e. really bad.

share|improve this answer
Hence why I was looking for a better solution then going through recursively and figuring it out. Any example of how I could break it down? The only thing I could think of would be the Hungarian Algorithm, which doesn't do exactly what I want to do, but by optimizing them all to be as close to 0 as possible it would sort of be doing what I want most of the time. But a 500x500 grid of coordinates takes ~1min to complete in Javascript and a 1000x1000 grid of coordinates takes ~30mins to complete. – Steven10172 May 29 '12 at 18:33

For small lists, you can try to run a full-search, applying some branch-and-bound algorithm.

If you have list sizes where this cannot be applied any more since the running time will exceed your available time, you need some discrete optimization algorithm like simulated annealing or tabu search to find a good (perhaps not the globally best) solution. Here is a free online-book dealing with a lot of global optimization algorithms, hope this helps:

share|improve this answer
Most likely the input will be 100-5000 coordinates against 100-5000 coordinates. – Steven10172 May 29 '12 at 18:34
Technically it could even go up to 50,000 x 50,000 as I've seen people do that before by hand, without optimizing ,they just randomly picked and used those combinations. – Steven10172 May 29 '12 at 18:36

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.