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: ∣xi−xj∣+∣yi−yj∣|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′)−>(x−y,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 x−yx-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 x−yx-y emerge.

First, consider the absolute value as follows:

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

Then the Manhattan distance becomes:

∣xi−xj∣+∣yi−yj∣=max(xi−xj,−xi+xj)+max(yi−yj,−yi+yj)=max((xi−xj)+(yi−yj),−(xi−xj)+(yi−yj),(xi−xj)−(yi−yj),−(xi−xj)+(yi−yj))=max((xi+yi)−(xj+yj),−(xi−yi)+(xj−yj),(xi−yi)−(xj−yj),−(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)∣,∣(xi−yi)−(xj−yj)∣)max((|x_i + y_i) - (x_j + y_j)|, |(x_i - y_i) - (x_j - y_j)|)

This reveals x+yx + y and x−yx - y, so we can substitute x′=x+yx' = x + y and y′=x−yy' = x - y.

Therefore:

dij=max(∣xi′−xj′∣,∣yi′−yj′∣)d_{ij} = max(|x_i' - x_j'|, |y_i' - y_j'|)

Our original goal was to find:

max1≤i≤n,1≤j≤n(dij)max_{1\leq i\leq n,1\leq j \leq n}(d_{ij})

Transforming this gives:

max1≤i≤n,1≤j≤n(max(∣xi′−xj′∣,∣yi′−yj′∣))max_{1\leq i\leq n,1\leq j \leq n}(max(|x_i' - x_j'|, |y_i' - y_j'|))

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

max(max1≤i≤n,1≤j≤n(∣xi′−xj′∣,∣yi′−yj′∣))max(max_{1\leq i\leq n,1\leq j \leq n}(|x_i' - x_j'|, |y_i' - y_j'|))

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

max1≤i≤n,1≤j≤n(∣xi′−xj′∣)=max1≤i≤n(xi′)−min1≤j≤n(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 y′y' side:

max(max1≤i≤n(xi′)−min1≤j≤n(xj′),max1≤i≤n(yi′)−min1≤j≤n(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 x−yx-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