Closest pairs problem

Hi forum! A fun challenge for you guys. How do you find a pair of closest points, without producing sub optimal results?
A point can be used only once to create a pair.


Suboptimal case 1:

Sub optimal case 2:
It finds closest points, but when a point can not find a pair, it is forced to be paired with a very distant point.

Correct solution, it computes distances to all points within max distance, and uses 2opt to solve sub optimal cases. But i am not 100% sure its fully correct, and it is exponentially slower with amount of points.


I tried solving it using Delaunay connectivity tree, but it gives many unmatched points:

Take with anemone:

I know the best approach would probably be to use delaunay connectivity for candidate pairs, and blossom method on top of it, but i do not know how to implement blossom algorithm.

THE FILE:

Closest Pairs.gh (30.6 KB)

1 Like

Thank you for the answer! I know this thread, but this solution is incorrect

Ok, please explain as I used it in a project which seemed valid


it finds pairs in two lists, not in one

And some other methods posted in this thread:

Find only one pair in a list, not all the pairs. Or produce suboptimal results:
closest pairs 1


closest pairs 2

Why did you replace standard Pop2D and Pop3D with Python?

This does that: (can’t help with “hungarian matrix”)


Closest Pairs_2026Mar1a.gh (15.2 KB)

One could cull (remove) all lines longer than a certain length…

1 Like

Hi Jospeh, thank you for your attempt!
I made those components instead of original ones, because they work a lot faster when i needed to make few thousand points. Standard components do not populate completely randomly, they populate in a way that avoids cluttering points too close together, i do not need that for this example.

The problem on your screenshot is the exact suboptimal case, because the algorithm does not see “the whole picture”, it is left with those lonely points in the end, that connect together because they have no choice, even though they are clearly not a match for each other.

C# (not Python) components restored.


Closest Pairs_2026Mar1b.gh (18.3 KB)

Care to explain how “hungarian matrix” solves the Sub optimal case?

It was a mistake, the algorithm in the post uses 2 opt refinement, not hungarian matrix (it was my previous attempt). It finds a list of candidates for each point within a max distance, and swaps used points until it gets the shortest solution.

https://en.wikipedia.org/wiki/2-opt|

Hungarian matrix builds a matrix of values to find the best candidate, but i was left with a lot of unmatched points using it

also hungarian is a lot worse when there is more points


Closest Pairs_2026Mar1c.gh (21.2 KB)


Closest Pairs_2026Mar1cc.gh (21.2 KB)

1 Like

nice attempt - you find candidates in a range of distances to the point, take the first one and delete it from initial set. the limitation is that you need a loop within a loop to store and later compare those candidates for optimization. And that is what implemented in the c# in the original post.. Or you can record all candidates instead of one, and compare the branches somehow..