I remember reading once about the following algorithm:

Consider a lattice grid and $N$ houses situated at grid points, in which live the town elders. They want to choose a lattice point location where they will build the city hall, such that the total distance walked by all $N$ elders is minimized. The algorithm consists of choosing an initial site at random, and then considering an adjacent site. The town elders then vote on whether to switch to the new site, only considering their own walking distances. The process repeats until no better site can be found, then that site clearly minimizes the sum of the walking distances.

Is there a general use for this type of technique (by that I mean the iterated small-shifts and then voting on them)?

  • 1
    $\begingroup$ Sounds like a form of hill-climbing. I'm not sure whether there's a specific name for this form. $\endgroup$ – Peter Taylor Jan 13 '12 at 10:14
  • $\begingroup$ To me, it is not clear that this procedure minimizes the sum of the walking distances; a priori, it is possible that $n/2+1$ of people go very short distance, but $n/2-1$ go very long, and switching to another site could very slightly increase the cost for $n/2+1$ of people, but decrease significantly the cost for $n/2-1$ of people. $\endgroup$ – sdcvvc Jan 13 '12 at 11:40
  • $\begingroup$ We only shift the site to adjacent grid points, and we use taxicab distance, so it's all fine. $\endgroup$ – Scaramouche Jan 13 '12 at 15:19


Since the town elders can only travel in along the gridlines, the total distance traveled is simply the sum of all displacements along the $x-$axis ($\Delta x$) plus the sum of all displacements along the $y-$axis ($\Delta y$). So simply take the median* of all $x-$coordinates and the median of all $y-$coordinates, and place the city hall there.
* if there an even number of houses, there may be a range of possible values for position (see below)

Why the Median is Best

If $n$ is odd, moving the city hall from the median position by $\delta x$ in either direction increases the $x$-distance by $\delta x$ for at least $\lfloor\frac{n}{2}\rfloor+1$ elders (those on the median plus those on the far side), so

$\Delta x_p \ge (\lfloor\frac{n}{2}\rfloor+1)\delta x$

The move decreases it by at most $\delta x$ for at most $\lfloor\frac{n}{2}\rfloor$ elders (those on the same side of the median as the movement), so

$\Delta x_n \ge -\lfloor\frac{n}{2}\rfloor\delta x$

Thus the net effect is that $\Delta x = \Delta x_p + \Delta x_n \ge \delta x$.

Similar arguments apply for deviations from the median $y$-position.

A Range may be Possible When $n$ is Even

If $n$ is even, and the two middle values in the distribution of $x-$coordinates are $x_k,x_{k+1}$, then any $x: x_k \le x \le x_{k+1}$ will suffice. Likewise we can take any $y : y_m \le y \le y_{m+1}$ where $y_m,y_{m+1}$ are the two middle $y$ values.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.