How to Calculate Manhattan Distance
Manhattan distance — also called taxicab distance or city block distance — measures how far apart two points are if you can only move along axis-aligned directions, like a taxi restricted to a city grid of streets and avenues. Instead of cutting across diagonally the way straight-line (Euclidean) distance does, Manhattan distance adds up the horizontal and vertical steps separately. For two points A(x₁, y₁, z₁) and B(x₂, y₂, z₂), the formula is d₁ = |x₂−x₁| + |y₂−y₁| + |z₂−z₁|. For plain 2D points, simply drop the z term or leave it at 0.
Manhattan vs. Euclidean vs. Chebyshev distance
These three "distance" formulas differ only in how they combine the per-axis differences Δx, Δy, Δz. Manhattan distance (the L1 norm) sums the absolute differences: |Δx| + |Δy| + |Δz|. Euclidean distance (the L2 norm) is the straight-line distance from the Pythagorean theorem: √(Δx² + Δy² + Δz²). Chebyshev distance (the L∞ norm) takes only the single largest axis difference: max(|Δx|, |Δy|, |Δz|). For any pair of points these always satisfy d∞ ≤ d₂ ≤ d₁, with all three equal only when the points differ along a single axis.
Why it matters and where it's used
Manhattan distance is the natural metric whenever movement is restricted to a grid: navigating city blocks, counting rook or king moves on part of a chessboard, or routing wires on a circuit board that only run horizontally and vertically. In data science and machine learning it appears as the L1 norm, used in k-nearest-neighbors classification, L1 regularization (Lasso), and clustering algorithms, because it is less sensitive to large outliers in any single dimension than the squared differences used by Euclidean distance.