The question is:
Given N points(in 2D) with x and y coordinates, find a point P (in N
given points) such that the sum of distances from other(N-1) points to
P is minimum.
This point is commonly known as Geometric Median. Is there any efficient algorithm to solve this problem, other than the naive O(N^2) one?
I solved something similar for a local online judge once using simulated annealing. That was the official solution as well and the program got AC.
The only difference was that the point I had to find did not have to be part of the
Ngiven points.This was my C++ code, and
Ncould be as large as50000. The program executes in0.1son a 2ghz pentium 4.Then I think It’s correct to pick the one from your list that is closest to the
(x, y)returned by this algorithm.This algorithm takes advantage of what this wikipedia paragraph on the geometric median says:
The first paragraph above explains why this works: because the function we are trying to optimize does not have any local minimums, so you can greedily find the minimum by iteratively improving it.
Think of this as a sort of binary search. First, you approximate the result. A good approximation will be the center of gravity, which my code computes when reading the input. Then, you see if adjacent points to this give you a better solution. In this case, a point is considered adjacent if it as a distance of
stepaway from your current point. If it is better, then it is fine to discard your current point, because, as I said, this will not trap you into a local minimum because of the nature of the function you are trying to minimize.After this, you half the step size, just like in binary search, and continue until you have what you consider to be a good enough approximation (controlled by the
epsconstant).The complexity of the algorithm therefore depends on how accurate you want the result to be.