nearest neighbor 2 dimensions - nearest-neighbor

Given a set S of points in 2-dimensional space, provide an algorithm that computes nearest neighbor(euclidian) for each point in the set. I think its called nearest neighbor graph, isn't it? Any existing efficient algorithm (N log N), where N = len(S)?

The kd-tree is a pretty standard algorithm for nearest neighbor search (even in 2-space, don't let the first illustration throw you).

Related

Most isolated point on 2d map - algorithm

I have a set of points, and need to know which one has the farthest euclidean distance from any other points.
I want to improve this from O(n^2)
Now guys i've heard about Kd trees for solution, BUT
KD Trees doesn't provide nearest distance if the point 'x' is already present in Kd tree. And there is no implementation for it to remove.
Edit:
You can do this by ignoring self in "nearest search algo" and "where we set root/parent" initially to begin search
given n points Pi, 1 <= i <= n:
build kd-tree (with an O(n) median of median algorithm this is O(n log n))
for all points Pi: find second closest point (closest point will be point itself), compute distance and remember Pi if the distance is a new minimum; this is O(n log n) again.
Alltogether this is an O(n log n) algorithm.
I assume that you want to find the point that maximises the distance to the nearest neighbour. Like a small island in the south pacific being 1100 miles away from the nearest land.
Well, you should be nowhere near O (n^2). Say you have a million points. Divide the points into a 1000 x 1000 grid. To find the nearest point, you would only have to examine the nine neigbouring grids, so you are far below O (n^2). If a grid contains lots of points, they will be close together so you can remove them from the search quickly.

Calculate farthest point of every point

Suppose we have two sets of points, say A and B (both of size O(n)) in the plane. Can we find farthest pair of points each being in A & B in O(n) time?
No, you can not calculate the furthest point for each point in O(n). The best you can obtain is O(n log n) with a 2-d tree. You can do this with a technique, similar to finding a closest point.
Read a more detailed answer here where I show a couple of other approaches to solve a similar problem.

How to find the smallest N dimensional simplex from a set of points that contains a given point?

I've looked all over google and stack but haven't found an answer to this problem yet. I keep finding results relating to the simplex method or results for finding the smallest arbitrary simplex (i.e. the vertices are not constrained). Neither can I think of an analytical solution.
Given a set of N-dimensional points, M, and an arbitrary N-dimensional point, q, how do I find the smallest N-dimensional simplex, S, that contains q as an interior point if the vertices of S must be in M? I'm sure I could solve it with an optimization, but I'd like an analytical solution if possible. A deterministic algorithm would be ok, as well.
I was originally using a K nearest neighbors approach, but then I realized it's possible that the N+1 nearest neighbors to q won't necessarily create a simplex that contains q.
Thanks in advance for any assistance provided.
I think you can do this is O(N^2) using an iterative process very similar to K-NN, but perhaps there is a more efficient way. This should return the minimum simplex in terms of the number of vertices.
For each coordinate i in q, we can iterate through all elements of M, comparing the q_i and m_i. We will select the two points in M which give us the min positive difference and min negative difference. If we repeat this process for every coordinate, then we should have our min set S.
Am I understanding the problem correctly?

Minimum manhattan distance with certain blocked points

The minimum Manhattan distance between any two points in the cartesian plane is the sum of the absolute differences of the respective X and Y axis. Like, if we have two points (X,Y) and (U,V) then the distance would be: ABS(X-U) + ABS(Y-V). Now, how should I determine the minimum distance between several pairs of points moving only parallel to the coordinate axis such that certain given points need not be visited in the selected path. I need a very efficient algorithm, because the number of avoided points can range up to 10000 with same range for the number of queries. The coordinates of the points would be less than ABS(50000). I would be given the set of points to be avoided in the beginning, so I might use some offline algorithm and/or precomputation.
As an example, the Manhattan distance between (0,0) and (1,1) is 2 from either path (0,0)->(1,0)->(1,1) or (0,0)->(0,1)->(1,1). But, if we are given the condition that (1,0) and (0,1) cannot be visited, the minimum distance increases to 6. One such path would then be: (0,0)->(0,-1)->(1,-1)->(2,-1)->(2,0)->(2,1)->(1,1).
This problem can be solved by breadth-first search or depth-first search, with breadth-first search being the standard approach. You can also use the A* algorithm which may give better results in practice, but in theory (worst case) is no better than BFS.
This is provable because your problem reduces to solving a maze. Obviously you can have so many obstacles that the grid essentially becomes a maze. It is well known that BFS or DFS are the only way to solve mazes. See Maze Solving Algorithms (wikipedia) for more information.
My final recommendation: use the A* algorithm and hope for the best.
You are not understanding the solutions here or we are not understanding the problem:
1) You have a cartesian plane. Therefore, every node has exactly 4 adjacent nodes, given by x+/-1, y+/-1 (ignoring the edges)
2) Do a BFS (or DFS,A*). All you can traverse is x/y +/- 1. Prestore your 10000 obstacles and just check if the node x/y +/-1 is visitable on demand. you don't need a real graph object
If it's too slow, you said you can do an offline calculation - 10^10 only requires 1.25GB to store an indexed obstacle lookup table. leave the algorithm running?
Where am I going wrong?

Calculate the maximum distance between vectors in an array

Assume we have an array that holds n vectors. We want to calculate the maximum euclidean distance between those vectors.
The easiest (naive?) approach would be to iterate the array and for each vector calculate its distance with the all subsequent vectors and then find the maximum.
This algorithm, however, would grow (n-1)! with respect to the size of the array.
Is there any other more efficient approach to this problem?
Thanks.
Your computation of the naive algorithm's complexity is wonky, it should be O(n(n-1)/2), which reduces to O(n^2). Computing the distance between two vectors is O(k) where k is the number of elements in the vector; this still gives a complexity well below O(n!).
Complexity is O(N^2 * K) for brute force algorithm (K is number of elem in vector). But we can do better by knowing that in euclidean space for points A,B and C:
|AB| + |AC| >= |BC|
Algorithm should be something like this:
If max distance found so far is MAX and for a |AB| there is a point C, such that distance |AC| and |CB| already computed and MAX > |AC|+|CB|, then we can skip calculation for |AB|.
It is difficult to tell complexity of this algorithm, but my gut feeling tells me it is not far from O(N*log(N)*K)
This question has been here before, see How to find two most distant points?
And the answer is: is can be done in less than O(n^2) in Euclidean space. See also http://mukeshiiitm.wordpress.com/2008/05/27/find-the-farthest-pair-of-points/
So suppose you have a pair of points A and B. Consider the hypersphere that have A and B at the north and south pole respectively. Could any point C contained in the hypersphere be farther from A than B?
Further suppose we partition the pointset into sqrt(N) hyperboxes with sqrt(N) points each. For any pair of hyperboxes, we can calculate in k time the maximum distance possible between any two points of the infinite set of points contained within them - by simply calculating the distance between their furthest corners. If we already have a candidate better than this we can discard all pairs of points from those hyperboxes.

Resources