There's already something called K-nearest neighbour algorithm with gives nearest neighbour to points. But what I want is unique neighbour which is not shared by any other neighbour. I've implemented this logic but it's very slow. You can review my code here.
EDIT: Let's assume there're 2 galaxies with 5 stars in each. GalaxyA neighbour list(let's call it closestNeighbourList) in GalaxyB starting from closest to fartherest...
star 0: [4, 1, 0, 3, 2]
star 1: [2, 0, 4, 3, 1]
star 2: [2, 1, 3, 0, 4]
star 3: [0, 3, 2, 4, 1]
star 4: [0, 3, 1, 4, 2]
from these lists default first neighbour list:
star 0: [4]
star 1: [2]
star 2: [2]
star 3: [0]
star 4: [0]
Since I want unique neighbour in GalaxyB, but here you see a problem, star 2 in GalaxyB is closest to two GalaxyA stars 1 & 2 and same for star 0. So these are not unique neighbour as shared by two or more stars. So to overcome this problem what I'm doing first storing all neighbours in a list(closestNeighbourList) then looping over those to check if it's closer to(shared by) some other star then go to next neighbour and when it's find unique neighbour break out of the loop.
after applying unique neighbour algorithm first neighbour list will be:
star 0: [4]
star 1: [2]
star 2: [1]
star 3: [3]
star 4: [0]