Finding the closest, "farthest" point from a set of points in 3-dimensions - algorithm

Suppose I have some n points (in my case, 4 points) in 3 dimensions. I want to determine both the point a which minimizes the squared distance to each of these n points, as well as the largest difference that can exist between the distance from an arbitrary point b and any two of these n points (i.e. the two "farthest points").
How can this be most efficiently accomplished? I know that, in 2 dimensions and with 3 points, the solution to the point that minimized distance is the centroid of the triangle formed by the 3 points, and the solution to the largest difference can be found by taking a point located precisely at one (any?) of the 3 points. It seems that the same should be true in 3 dimensions, although I am unsure.

I want to determine both the point that minimizes distance from each of these n points
The centroid minimizes the sum of the squared distances to every point in the set. But will not minimize the max distance (the farther distance) to the points.
I suspect that you are interested in computing the center and radius of the minimal sphere containing every point in the set. This is a classic problem in CG that can be solved in linear time quite easily in an approximate way, or exactly if you program the algorithm propossed by Emmerich Welzl.
If the number of points is as small as 4, an approximate solution is search the pair of point with maximum distance (there is 12 possible pairs) and compute the midpoint as center and half-distance as radius . Then, ensure that the other two points are also inside the sphere, or make it grow if necessary.
See more information at
https://en.wikipedia.org/wiki/Bounding_sphere
https://en.wikipedia.org/wiki/Smallest-circle_problem

The largest difference between the distances of a point to two given points is achieved when the three points are aligned and the unknown point is "outside" (there are infinitely many solutions). In this configuration, the difference is just the distance between the two given points.
If you mean to maximize all differences simultaneously (or rather the sum of differences), you must go to infinity in some direction. That direction maximizes the sum of the lengths of the projections of all edges.

Related

How to find largest circle between disjoint polygons all enclosed within a square

Let P1, . . . , Pk be a collection of pairwise disjoint simple polygons with a total of n edges, all enclosed within a given square. Find the largest
disk that can be inscribed in this square so that it is disjoint from all the interiors of the polygons Pi.
was thinking of using Voronoi diagram of line segments...
Consider the distance field d(x, y) on the square that defines for every point (x, y) the distance to the nearest polygon. Your aim is to find the maximum of this distance field.
A very easy approach is to discretize the distance field on a grid and find the vertex with the maximum value. You can also subdivide the grid further around candidate points.
But you can also incorporate a continuous optimization step. For this, calculate the discretized distance field and use the top vertices as seeds for continuous optimization. In this optimization, move the point, such that the distance increases until you cannot move anymore. This will find local maxima. With proper initialization, one of these maxima is most likely the global maximum.
To find the direction in which to move, find the intersections of the current disk with all polygons. If there is one intersection, moving in the opposite direction will definitely increase the distance. If there are more intersections, you could average the directions. You can either use a pre-defined step length or an estimate. To estimate the step length, find the distance to the next intersections (excluding the already found intersection primitives - not the whole polygons), let's name this distance d_next. You can safely move (d_next - d_current) / 2 without causing a new intersection. There may be smarter way to calculate the step length.

Two salesmen - one always visits the nearest neighbour, the other the farthest

Consider this question relative to graph theory:
Let G a complete (every vertex is connected to all the other vertices) non-directed graph of size N x N. Two "salesmen" travel this way: the first always visits the nearest non visited vertex, the second the farthest, until they have both visited all the vertices. We must generate a matrix of distances and the starting points for the two salesmen (they can be different) such that:
All the distances are unique Edit: positive integers
The distance from a vertex to itself is always 0.
The difference between the total distance covered by the two salesmen must be a specific number, D.
The distance from A to B is equal to the distance from B to A
What efficient algorithms cn be useful to help me? I can only think of backtracking, but I don't see any way to reduce the work to be done by the program.
Geometry is helpful.
Using the distances of points on a circle seems like it would work. Seems like you could determine adjust D by making the circle radius larger or smaller.
Alternatively really any 2D shape, where the distances are all different could probably used as well. In this case you should scale up or down the shape to obtain the correct D.
Edit: Now that I think about it, the simplest solution may be to simply pick N random 2D points, say 32 bit integer coordinates to lower the chances of any distances being too close to equal. If two distances are too close, just pick a different point for one of them until it's valid.
Ideally, you'd then just need to work out a formula to determine the relationship between D and the scaling factor, which I'm not sure of offhand. If nothing else, you could also just use binary search or interpolation search or something to search for scaling factor to obtain the required D, but that's a slower method.

Minimum number of moves required for converging between 3 points in m*n matrix

I have a (m*n) matrix, in which lies 3 different points. I have to find the minimum number of moves such that all 3 points converge at a point inside the matrix.
Solution(s) I have tried till now:
Brute force solution: Try all the points in the matrix, find the
point which needed the minimum moves from the 3 given points.
Bound the three points as a triangle and try the points with in this region. This will more likely to be the centroid of this triangle.
I would like to know about other optimized solutions for this problem. Thanks.
Each move you change either x- or y- coordinates by +/- 1. Vertical and horizontal distances are that way independent. So, firstly lead points to one x-coordinate, then y-coordinate. The most optimal way of doing it is moving points with minimal and maximal x to the x-position of the third point. Repeat same for y and you are done.
This way the coordinate of that final point is x coordinate of middle point on x-axis and y coordinate of middle point on y-axis (there can be many such points though, but this must be one of the minimizing set). (if they overlap, obviously either of the overlapping coordinates will be middle one).
Programmatically take an array of x-values, remove max and min values, same with y and you'll be left with your closest-to-all point.
For Manhattan distance:
Simply take the middle x-coordinate and the middle y-coordinate of the 3 points as the target. By middle I mean the median of the sorted coordinates.
Example:
Input: (1,5), (2,4), (3,6)
The x-points are 1, 2 and 3 (sort to 1, 2, 3). The middle one is 2.
The y-points are 5, 4 and 6 (sort to 4, 5, 6). The middle one is 5.
Thus the point that minimizes the distance is (2,5).
Proof:
If we were to start at the above mentioned point and move in any direction, the distance to the point we're moving towards would decrease and the distance to the other 2 points would increase, thus every move by 1 would cause a total decrease of 1 but a total increase of 2. Once we've moved past the last point, all 3 distances would increase, thus the total increase will be 3 with no decrease. Thus any move causes an increase in distance, thus the above point is optimal.
For Euclidean distance:
(I realize now that you said minimum moves, thus this is probably not what you want)
This is somewhat more complicated. It involves minimizing this equation:
sqrt((x-x1)^2 + (y-y1)^2) + sqrt((x-x2)^2 + (y-y2)^2) + sqrt((x-x3)^2 + (y-y3)^2)
Because of the sqrt's, one cannot simplify the equation by saying that x and y are distinct.
However, the greedy approach is likely to work:
Pick any point (maybe the one that minimizes the Manhattan distance is a good start).
While one of the neighbouring points of the picked point results in lower euclidean distance, pick that point.
I think this is one of the variants of the http://en.wikipedia.org/wiki/Steiner_tree_problem - probably the http://en.wikipedia.org/wiki/Rectilinear_Steiner_tree problem. This looks sufficiently intimidating that I would stick to trying all points within the triangle.
Actually with just points converging on a single point I think sashkello is right

Two closest points in Manhattan distance

I'm wondering about Manhattan distance. It is very specific, and (I don't know if it's a good word) simple. For example when we are given a set of n points in this metric, then it is very easy to find the distance between two farthest points, in linear time. But is it also easy to find two closest points?
I heard, that there exists universal algorithm for finding two closest points in any metric, but it's complicated. I'm wondering if in this situation (Manhattan metric) it is possible to use special properties of this distance and come up with an easier algorithm, that will be more friendly in implementation?
EDIT: n points on a plane, and lets say -10^9 <= x,y <= 10^9 for all points.
Assuming you're talking about n points on a plane, find among the coordinates the minimal and maximal values of x and y coordinates. Create a matrix sized maxX-minX x maxY-minY, such that all points are representable by a cell in the matrix. Fill the matrix with the n given points (not all cells will be filled, set NaN there, for example). Scan the matrix - shortest distance is between adjacent filled cells in the matrix (there are might be several such pairs).

find a point closest to other points

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?
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.
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.
I'm not sure if I understand your question but when you calculate the minimum spanning tree the sum from any point to any other point from the tree is minimum.

Resources