0

Suppose I have 5 lists different lenths that contain positions (2D points). I need to calculate distances of all pairs of points possible between these 5 lists in order to filter the pairs that have distance smaller than a threshold, e.g., 10.

A naive approach is making loops, over every points of every list, and verify if there is a pair that have distance smaller 10.

for position in list1
   for position in list2
      for position in list3
         for position in list4
            for position in list5
               list all pairs possible of 5 positions
               calculate distance of each pair
               if (distance < threshold):
                  add pair positions in final result

This approach has exponential complexity when I add more new list of position.

Another approach is instead of making a loop of 5 lists, I create 10 loops of all possible 2 lists (2-combination of 5 lists). So if I add more new list, the complexity will increase following the number of combination (not exponential) but I'm still not happy with that.

Is there anyway to calculate all pairs distance of multi-lists in linear complexity ? Thanks

Amateur
  • 151
  • 1
  • 2
  • 12
  • Does anyone have some idea or keyword about this problem ? – Amateur May 15 '20 at 02:48
  • If you have n points in total across all of the lists, then there are `n * (n - 1) / 2` pairs of points. You cannot compute the distances of all these pairs in O(n). Even if we say that we do not need to check the distances of pairs that come from the same input list, so long as each input list has at least 1 point the number of pairs that must be compared is still O(n^2). – Callum May 20 '20 at 00:57

0 Answers0