6

Consider a unit square containing n 2D points. We say that two points p and q are independent in a square, if the Euclidean distance between them is greater than 1. A unit square can contain at most 3 mutually independent points. I would like to find those 3 mutually independent points in the given unit square in O(n log n). Is it possible? Please help me.

Can this problem be solved in O(n^2) without using any spatial data structures such as Quadtree, kd-tree, etc?

user2311963
  • 143
  • 6
  • Dividing the set into fours subsets where each subset contains all points in a given quadrant then independent points must be in different subsets. I've no idea if this tends towards O(n log n) but the "log n" does suggest some kind of spatial divison is required. – Skizz Apr 23 '15 at 10:27

3 Answers3

2

Use a spatial data structure such as a Quadtree to store your points. Each node in the quadtree has a bounding box and a set of 4 child nodes, and a list of points (empty except for the leaf nodes). The points are stored in the leaf nodes.

The point quadtree is an adaptation of a binary tree used to represent two-dimensional point data. It shares the features of all quadtrees but is a true tree as the center of a subdivision is always on a point. The tree shape depends on the order in which data is processed. It is often very efficient in comparing two-dimensional, ordered data points, usually operating in O(log n) time.

For each point, maintain a set of all points that are independent of that point.

Insert all your points into the quadtree, then iterate through the points and use the quadtree to find the points that are independent of each:

main()
{
     for each point p
         insert p into quadtree
         set p's set to empty

     for each point p
         findIndependentPoints(p, root node of quadtree)
}

findIndependentPoints(Point p, QuadTreeNode n)
{
     Point f = farthest corner of bounding box of n
     if distance between f and p < 1
         return  // none of the points in this node or
                 // its children are independent of p

     for each point q in n
         if distance between p and q > 1
             find intersection r of q's set and p's set
             if r is non-empty then
                 p, q, r are the 3 points -> ***SOLVED***
             add p to q's set of independent points
             add q to p's set of independent points

     for each subnode m of n (up 4 of them)
         findIndependentPoints(p, m)
}

You could speed up this:

find intersection r of q's set and p's set

by storing each set as a quadtree. Then you could find the intersection by searching in q's quadtree for a point independent of p using the same early-out technique:

// find intersection r of q's set and p's set:
//     r = findMututallyIndependentPoint(p, q's quadtree root)

Point findMututallyIndependentPoint(Point p, QuadTreeNode n)
{
     Point f = farthest corner of bounding box of n
     if distance between f and p < 1
         return  // none of the points in this node or
                 // its children are independent of p

     for each point r in n
         if distance between p and r > 1
             return r

     for each subnode m of n (up 4 of them)
         findMututallyIndependentPoint(p, m)
}

An alternative to using Quadtrees is using K-d trees, which produces more balanced trees where each leaf node is a similar depth from the root. The algorithm for finding independent points in that case would be the same, except that there would only be up to 2 and not 4 child nodes for each node in the data structure, and the bounding boxes at each level would be of variable size.

samgak
  • 23,944
  • 4
  • 60
  • 82
  • Thank you so much for your help. I am reading about Quad Trees as it is very new concept to me. I will post if I have any doubts. Thanks for your help, once again. – user2311963 Apr 23 '15 at 07:52
  • This approach will be efficient on average, but the worst case can be far more than O(n log n). For example, if one point is in the lower left corner and all other points are clustered in the upper right quadrant. The running times of quadtree insertions and queries are highly output-sensitive. – radiaph Apr 23 '15 at 13:52
  • @phari. What would be its worst case complexity? Would it be in terms of O(n poly-logarithmic )? – user2311963 Apr 24 '15 at 04:55
  • @samgak. What is the worst case time complexity? I am not able to figure out it. – user2311963 Apr 24 '15 at 10:57
  • @user2311963, the worst case complexity is a function of the height of the quadtree, and this depends on the actual coordinates stored in it. More specifically, it depends on how far apart they are. You can make the quadtree height arbitrarily large by making the points arbitrarily close together. See https://www.cs.umd.edu/class/spring2008/cmsc420/L17-18.QuadTrees.pdf – radiaph Apr 27 '15 at 17:31
  • @phari. Can the proposed problem be solved in O(n^2) without using quadtree structure? If yes, please post your idea. Thank you. – user2311963 Apr 28 '15 at 06:23
  • Using a balanced K-d tree instead of a quadtree will address the problem of the tree height growing too large, however it's still O(n) in the worst case. – samgak Apr 28 '15 at 06:45
  • It depends on whether you are talking about the average case or the worst case, which your question did not specify. The average case will be better than O(n^2), and I answered your original question based on the average case. As for the worst case I'm not sure, because there are so many ways to contrive special cases to increase the execution time. Also, the geometric constraints specified in the problem could have a limiting effect on what are valid inputs which might limit the execution time. I'm not 100% sure what the absolute worst case is, so I don't want to answer. – samgak Apr 29 '15 at 12:36
1

You might want to try this out.

Pick the top left point (Y) with coordinate (0,1). Calculate distance from each point from the List to point Y.
Sort the result in increasing order into SortedPointList (L)
If the first point (A) and the last point (B) in list L are independent:
    Foreach point P in list L:
        if P is independent to both A and B:
             Return A, B, P
Pick the top right point (X) with coordinate (1,1). Calculate distance from each point from the List to point X.
Sort the result in increasing order into SortedPointList (S)
If the first point (C) and the last point (D) in list L are independent:
    Foreach point O in list S:
        if P is independent to both C and D:
             Return C, D, O
Return null
Toby D
  • 1,465
  • 1
  • 19
  • 31
  • @user2311963 would you mind sharing your implementation? – Toby D Apr 23 '15 at 06:03
  • @user2311963 sorry, updated the pseudocode. It wasn't very clear. – Toby D Apr 23 '15 at 06:08
  • why return null? what is your proof? what if there is another independent triangle that cant be found with your algorithm? – Lrrr Apr 23 '15 at 06:13
  • @Toby D. After finding the two farthest points, we look for the third one, if the distance between two farthest points is greater than one. However, the remaining n-2 points may have distance less than 1 to the two farthest points. In this case your algorithm returns null. Now, consider a point at any corner. It is possible to have two mutually independent points on the interior of non adjacent edges to the chosen corner point with distance less than that of the distance of the farthest two points. – user2311963 Apr 23 '15 at 06:19
1

This is a wrong solution. Kept it just for comments. If one finds another solution based on smallest enclosing circle, please put a link as a comment.


  1. Solve the Smallest-circle problem.

  2. If diameter of a circle <= 1, return null.

  3. If the circle is determined by 3 points, check which are "mutually independent". If there are only two of them, try to find the third by iteration.

  4. If the circle is determined by 2 points, they are "mutually independent". Try to find the third one by iteration.

Smallest-sircle problem can be solved in O(N), thus the whole problem complexity is also O(N).

Nimantha
  • 6,405
  • 6
  • 28
  • 69
Alex Salauyou
  • 14,185
  • 5
  • 45
  • 67
  • Step 4 is problematic. There might be mutually independent sets of size 3, whereas the 2 points of the smallest enclosing circle are not part of any. – Thomas B. Apr 23 '15 at 09:49
  • @ThomasB. okay, on free time I'll try to find an example of such situation (If you already have, please post points coordinates). So far I failed in attempts to find one. – Alex Salauyou Apr 23 '15 at 10:15
  • 1
    Check http://imgur.com/ZVuIo9a with 5 points. D and B are in the opposite corners of the unit square and define the minimum enclosing disk. E,F,G make an equilateral triangle of length 1 and thus are mutually independent. But none of E,F,G is mutually independent with D and B. – Thomas B. Apr 23 '15 at 10:40
  • @ThomasB. In your picture, B, E, F obviously form another mutually independent triple. This contradicts problem preconditions. – Alex Salauyou Apr 23 '15 at 10:44
  • yea, but no mutually independent triple contains *both* {D,B} – Thomas B. Apr 23 '15 at 10:45
  • 1
    You mean "A unit square can contain at most 3 mutually independent points."? I understand it in such a way, that only the size (3) is limited. So there is no set with 4 independent points. But there can be as many 3-sets as you want. – Thomas B. Apr 23 '15 at 10:51
  • @Thomas B. I need just one set of mutually independent points – user2311963 Apr 23 '15 at 13:09