Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

find a point closest to other points

Tags:

algorithm

Given N points(in 2D) with x and y coordinates. You have to find a point P (in N given points) such that the sum of distances from other(N-1) points to P is minimum.

for ex. N points given p1(x1,y1),p2(x2,y2) ...... pN(xN,yN). we have find a point P among p1 , p2 .... PN whose sum of distances from all other points is minimum.

I used brute force approach , but I need a better approach. I also tried by finding median, mean etc. but it is not working for all cases.

then I came up with an idea that I would treat X as a vertices of a polygon and find centroid of this polygon, and then I will choose a point from Y nearest to the centroid. But I'm not sure whether centroid minimizes sum of its distances to the vertices of polygon, so I'm not sure whether this is a good way? Is there any algorithm for solving this problem?

like image 245
kamal Avatar asked Apr 25 '12 05:04

kamal


People also ask

How do you find the point on a function closest to another point?

The easiest way to solve it is to use the distance formula. Simply put, it is the pythagorean theorem but with any two points. Given that y (or in this case, f(x) is equal to x2 , we can substitute it in for y2 . x2 is just x , x1 is the x-value of the point and y1 is the y-value of the point.

How do you find a point near an origin?

Closest point from origin will be the perpendicular distance from origin to the line. We need to find an equation of the perpendicular from (0,0) on y = 2x + 3. Therefore, the point on the line is (-6/5, 3/5).

How do you find the nearest point in Matlab?

k = dsearchn( P , PQ ) returns the indices of the closest points in P to the query points in PQ measured in Euclidean distance.


2 Answers

If your points are nicely distributed and if there are so many of them that brute force (calculating the total distance from each point to every other point) is unappealing the following might give you a good enough answer. By 'nicely distributed' I mean (approximately) uniformly or (approximately) randomly and without marked clustering in multiple locations.

Create a uniform k*k grid, where k is an odd integer, across your space. If your points are nicely distributed the one which you are looking for is (probably) in the central cell of this grid. For all the other cells in the grid count the number of points in each cell and approximate the average position of the points in each cell (either use the cell centre or calculate the average (x,y) for points in the cell).

For each point in the central cell, compute the distance to every other point in the central cell, and the weighted average distance to the points in the other cells. This will, of course, be the distance from the point to the 'average' position of points in the other cells, weighted by the number of points in the other cells.

You'll have to juggle the increased accuracy of higher values for k against the increased computational load and figure out what works best for your points. If the distribution of points across cells is far from uniform then this approach may not be suitable.

This sort of approach is quite widely used in large-scale simulations where points have properties, such as gravity and charge, which operate over distances. Whether it suits your needs, I don't know.

like image 191
High Performance Mark Avatar answered Oct 09 '22 21:10

High Performance Mark


The point in consideration is known as the Geometric Median

The centroid or center of mass, defined similarly to the geometric median as minimizing the sum of the squares of the distances to each sample, can be found by a simple formula — its coordinates are the averages of the coordinates of the samples but no such formula is known for the geometric median, and it has been shown that no explicit formula, nor an exact algorithm involving only arithmetic operations and kth roots can exist in general.

like image 22
Sajal Jain Avatar answered Oct 09 '22 21:10

Sajal Jain