0

So the problem is following: given N points in a database (edit: just assume for the moment it's 2 arrays x[] and y[]), each with coordinate (x[i], y[i]), and queries with format (X,Y,R), list all points in the circle with center (X,Y), radius R.

Edit - Input constraint: x=[-180,180], y=[-180,180]

Which Python library can solve this the fastest way possible? I'm looking for O(log(N) + K) time complexity per query, and <= O(n * log(N)^2) space requirement, where K is the output length.

Solution 0: Loop over N points, use Pythagoras to check.

-> Memory: O(N). Time per query: O(N)

Solution 0.1: Same as above, but add small things like sorting by x, and use binary search to start searching only points with coordinate x[i] >= X - R. Use squared distance instead of distance, etc

-> Time per query: still O(N) but faster

Solution 1 Quadtree. Assume points are centered around (0,0). Root node of tree is at (0,0), containing 2D spaces (x=[-inf,inf],y=[-inf,inf]). Each node has 4 child: where each child contains 1/4 quadrants of the father. So whenever we go down a level, search space is reduced by 75%. Keep going down deeper until the current node is outside the query circle, or contains no data point. -> Memory: maybe O(n * log(N)^2)? , time: ~~O(log(N)^2)

Is their any python library that already solve this problem?

Duke Le
  • 332
  • 3
  • 14
  • Pythagorean theorem would help I hope! Show us some code, we'll help you debug it. – zabop Jan 08 '21 at 12:42
  • Start coding it, it is an easy problem, then waiting for your code to discuss. – Nour-Allah Hussein Jan 08 '21 at 12:43
  • 1
    @Nour-AllahHussein the simple solution is obvious, but I'm asking about a fast solution (quadtree, other range-query tree, etc). But I apologized for not writing in detail enough, will add right now. – Duke Le Jan 08 '21 at 12:44
  • What will be the range of values for X[i], Y[i] and R? – Abhinav Mathur Jan 08 '21 at 12:54
  • @AbhinavMathur I added the input constraint – Duke Le Jan 08 '21 at 12:55
  • BallTree is a option, https://scikit-learn.org/stable/modules/generated/sklearn.neighbors.BallTree.html which support the radius query. Not sure about the complexity/storage in terms of N and K – Willem Hendriks Jan 08 '21 at 15:05
  • If the points are in an actual database, you might want a spatial index like [PostGIS](https://postgis.net/). – David Eisenstat Jan 08 '21 at 15:05
  • Are you taking into account the time required to put the data into the desired data structure? `O(log(N) + K)` is sub-linear i.e. you are not even inspecting every point once. This would only make sense if at the time of querying the data structure you wanted is already there to be used. – wookie919 Feb 11 '21 at 04:39
  • The data structure is initialized once. So we don't take that into account – Duke Le Feb 11 '21 at 04:40

0 Answers0