Manhattan Distance Memo

TL;DR

Notes. For details, please refer to Recent 45-Degree Rotation Developments.

Manhattan Distance

The Manhattan distance between two points ii and jj is expressed as: xixj+yiyj|x_i - x_j| + |y_i - y_j|

Maximum Manhattan Distance

Given m points, we want to find the maximum Manhattan distance between any pair of points. A brute-force approach would take O(m2)O(m^2).

Instead, we use a technique called 45-degree rotation. A 45-degree rotation is the transformation (x,y)>(xy,x+y)(x', y') -> (x - y, x + y). Originally, a factor of 2\sqrt{2} is applied to the magnitude, but when magnitude is not essential, it can be ignored. In other words, when you see x+yx+y or xyx-y, you should suspect a 45-degree rotation might be applicable.

The Manhattan distance doesn't reveal this directly, but by applying the following transformation, x+yx+y and xyx-y emerge.

First, consider the absolute value as follows:

x=max(x,x)|x| = max(x, -x)

Then the Manhattan distance becomes:

xixj+yiyj=max(xixj,xi+xj)+max(yiyj,yi+yj)=max((xixj)+(yiyj),(xixj)+(yiyj),(xixj)(yiyj),(xixj)+(yiyj))=max((xi+yi)(xj+yj),(xiyi)+(xjyj),(xiyi)(xjyj),(xi+yi)+(xj+yj))\begin{aligned} |x_i - x_j| + |y_i - y_j| &= max(x_i - x_j, -x_i + x_j) + max(y_i - y_j, -y_i + y_j) \\ &= max((x_i - x_j) + (y_i - y_j), -(x_i - x_j) + (y_i - y_j), (x_i - x_j) - (y_i - y_j), -(x_i - x_j) + (y_i - y_j)) \\ &= max((x_i + y_i) - (x_j + y_j), -(x_i - y_i) + (x_j - y_j), (x_i - y_i) - (x_j - y_j), -(x_i + y_i) + (x_j + y_j)) \end{aligned}

Looking at the 1st and 4th terms, and the 2nd and 3rd terms, they can be converted back to absolute values:

max((xi+yi)(xj+yj),(xiyi)(xjyj))max((|x_i + y_i) - (x_j + y_j)|, |(x_i - y_i) - (x_j - y_j)|)

This reveals x+yx + y and xyx - y, so we can substitute x=x+yx' = x + y and y=xyy' = x - y.

Therefore:

dij=max(xixj,yiyj)d_{ij} = max(|x_i' - x_j'|, |y_i' - y_j'|)

Our original goal was to find:

max1in,1jn(dij)max_{1\leq i\leq n,1\leq j \leq n}(d_{ij})

Transforming this gives:

max1in,1jn(max(xixj,yiyj))max_{1\leq i\leq n,1\leq j \leq n}(max(|x_i' - x_j'|, |y_i' - y_j'|))

The outer max1in,1jnmax_{1\leq i\leq n,1\leq j \leq n} and the inner maxmax can be swapped without changing the result:

max(max1in,1jn(xixj,yiyj))max(max_{1\leq i\leq n,1\leq j \leq n}(|x_i' - x_j'|, |y_i' - y_j'|))

Now, xx' and yy' can be computed independently, so we just need to subtract the smallest from the largest. Therefore:

max1in,1jn(xixj)=max1in(xi)min1jn(xj)max_{1\leq i\leq n,1\leq j \leq n}(|x_i' - x_j'|) = max_{1\leq i \leq n}(x_i') - min_{1\leq j \leq n}(x'_j)

Applying the same to the yy' side:

max(max1in(xi)min1jn(xj),max1in(yi)min1jn(yj))max(max_{1\leq i \leq n}(x_i') - min_{1\leq j \leq n}(x'_j), max_{1\leq i \leq n}(y_i') - min_{1\leq j \leq n}(y'_j))

Since finding the maximum and minimum of x+yx+y and xyx-y respectively is O(m)O(m), the problem can be solved.

Incidentally, when extended to higher-dimensional spaces, for RkR^k the complexity is (O(k2km))(O(k2^km)). Quite challenging, indeed.

Create an issue on GitHub about this article

Read Next