Distance measured along axes: . Grid movement, city blocks, and taxicab problems.

The rotation trick — the one thing to remember

Rotating by 45° (and scaling by ) turns Manhattan into Chebyshev, which decouples the coordinates — and over independent coordinates is far easier to optimise than a sum of absolute values.

// transform
u = x + y;  v = x - y;
// inverse
x = (u + v) / 2;  y = (u - v) / 2;

Immediate consequences

ProblemWith the rotation
Maximum Manhattan distance among points
Maximum distance in dimensions over sign patterns of
Manhattan balls (diamonds)become axis-aligned squares
Do two diamonds intersect?do two squares intersect — trivial
Manhattan nearest neighbourChebyshev nearest neighbour
Points within Manhattan distance points in a square

Chebyshev distance

— the number of king moves on a chessboard. The inverse rotation converts Chebyshev back to Manhattan.

Sum of pairwise Manhattan distances

Because the coordinates are independent,

and each term is computed in by sorting:

long long sumAbsDiff(vector<long long> a) {
    sort(a.begin(), a.end());
    long long total = 0, pref = 0;
    for (int i = 0; i < (int)a.size(); i++) {
        total += (long long)i * a[i] - pref;
        pref += a[i];
    }
    return total;
}

This separability is the defining advantage of over , and it makes many “sum of distances” problems easy that would be intractable in Euclidean geometry.

Minimising the sum of distances

The point minimising is the coordinate-wise median — independently in and in . That is an (or with nth_element) answer, whereas the Euclidean version (the geometric median) has no closed form.

For a weighted version, take the weighted median.

The Manhattan MST

The minimum spanning tree under has a beautiful algorithm:

For each point, only its nearest neighbour in each of 8 octants can be an MST edge.

So sweep in 4 directions (each covering 2 octants by symmetry), using a BIT to find the nearest candidate, producing candidate edges; then run Kruskal.

instead of on the complete graph. The Euclidean analogue needs Delaunay; the Manhattan one needs only sorting and a BIT, which makes it considerably more approachable.

Where Manhattan geometry appears

ProblemNote
Grid movement (4 directions)distance =
King movesdistance =
VLSI routingwires run along axes
Warehouse / facility placementmedian minimises total travel
Taxicab problemsthe name
”Rotate the grid 45°” puzzlesthe transformation above
Rectilinear Steiner treeNP-hard, unlike the MST
Rectangle union / stabbingaxis-aligned throughout

Diagonal movement

On a grid where diagonal moves also cost 1, the distance is Chebyshev. If diagonals cost (or 1.5 as in some game engines), the distance is the “octile” metric

which is the standard admissible heuristic for grid A*.

See also: Distances · Sweep Line · Minimum Spanning Tree