# A Heuristic Approach to Calculating Geometric Centers

August 22, 2017

I was recently tasked with the following problem:

Given a set of points in space, find a point $Q$ that minimizes the net cost of traveling from $Q$ to each other point.

Ignoring the problem of calculating costTraveling by car, bike, or foot introduces obvious (and important) time differences. Here, we’ll employ Euclidean distance as an uncomplicated cost function. , this problem may seem fairly straightforward. Let’s look at some approaches.

## 0:​$Q$ as the Median

As it happens, the median of a set of points is guaranteed to be the cost-minimum center of those points only in $\Bbb{R}^1$ space. We can prove this via counter-example: create a triangle with vertices $[(0, 0); (0, 1); (1, 0)]$, the median of which is at $(\frac{1}{3}, \frac{1}{3})$:

The net cost of traveling from the vertices to the center point can be improved by $1.54\%$ if we make that center point $(0.211, 0.211)$.

For most, $1.54\%$ isn’t all that much, and the median is a pretty good approximation for the latter point - the geometric center Also known as the geometric median. , or center as I will refer to it from here. But what if the margin grows higher, to $5\%$ or $10\%$ or $20\%$? How do we get to that obscure $0.211$ value?

## 1: Function Minimization

What if we attempt to minimize the net cost function? Of course, first we have to define that function. We can say that for some point $Q = (Q_x, Q_y)$, the net cost of traveling from a set of points $n$ with coordinate form $(x_n, y_n)$ to $(Q_x, Q_y)$ is

Note that this function does _not* have the same minimum as $f(Q) = \sum*{i=1}^n (Q_x - x_i)^2 + (Q_y - y_i)^2$ - that minimum is the _median*.

Hey, pretty good! Let’s do $\frac{\partial}{\partial Q_x} = 0$, $\frac{\partial}{\partial Q_y} = 0$, throw in a second derivative test, and call it a day.

Oh man. Maybe not. That looks like a pain in the ass to solve for $Q*x$, let alone simultaneously with $\frac{\partial}{\partial Q_y}$I have no idea how to do this efficiently, but if you do, please let me know. Looks like it could be worth a lot of money . . Don’t forget this process would have to be automated, too - _wow* that looks fun!

## 2: Heuristic Approximation

You know the old saying:

I may be missing a couple of words, but you get the gist.

If you can’t derive ‘em, approximate ‘em!

…and so heuristics come to the rescue! How? Well, a good place to always start is a contoured version of the function whose extrema are in question.

As expected, the contour slopes in around the previously-shown minimum $(0.211, 0.211)$. Interestingly, there seems to only be one minimum for this function - and in general, there is only one centerSee this paper, which shows that the geometric center is unique and convergent for any set of non-co-linear points. for any set of points! This means we can employ some descent-type optimization method.

Okay, but where do we descend from?

A convenient place would be from the median. It’s cheap to compute and fairly close to the center, so let’s go with that.

How do we descend?

This is a bit trickier. Of course, we can only travel in $\Bbb{R}^2$, so let’s just say we either go up, down, left, or rightMathematically, +ky, -ky, -kx, or +ky. A more accurate algorithm could also employ diagonal directions. . Figuring out which way to go is pretty easy; we can just compute the cost of traveling in each direction and go the most beneficial route.

What if there’s no direction of improvement?

Then we’ve found our center! Except we could probably do better, by decreasing our search radius and repeating the descent mechanism again. And again. Until the search radius reaches some acceptable $\epsilon$.

Alright, let’s code it!These examples are in Rust, but it should be easy to implement the logic in any language of your choice. The full code can be found here, with a C++ version here. First, the boilerplate - a cost function that takes an array of points, a center point, and calculates the net distance from the center to each other point.

And now, we can use the specification previously defined to write a complete algorithm.

Let’s plug in our three points from before, and…

[0.21126302083333331, 0.21126302083333331]

Awesome!

## n: Some Details

This function runs in $O(\log{}n)$ timeA method for near-perfect computation of the geometric center in near-linear is proposed here. , and its precision can be improved by reducing $\epsilon$ (epsilon) to an arbitrary minimum.

<< All Posts    $\int_0^1 \arccos(x^\frac{1}2)\ dx$