AI Glossary / Mathematics and optimization / Beginner
Manhattan distance
Definition
Manhattan distance is the sum of the absolute coordinate differences between two numerical vectors. Also called city-block distance or L1 distance, it is written d(x,y)=Σ|xᵢ−yᵢ|. It measures separation by adding differences along the chosen coordinate axes rather than taking a straight-line Euclidean distance.
What it means
In two dimensions, the name evokes travel along a rectangular street grid: moving three blocks east and four blocks north requires seven blocks of axis-aligned travel. The straight-line distance between the endpoints would be five blocks.
For data, the coordinates might represent measurements rather than physical streets. Adding their absolute differences gives every coordinate a separate contribution. Units and scaling therefore matter. A feature measured in thousands can dominate one measured between zero and one even when it is not more relevant.
Manhattan distance is a metric on numerical vectors with the same coordinate system. It is nonnegative, symmetric, zero only for identical vectors, and satisfies the triangle inequality. It does not automatically express semantic similarity, travel time, or the likelihood that two records describe the same entity.
How it works
Align the coordinates, subtract corresponding values, take absolute values, and add the results. Unlike Euclidean distance, the calculation does not square the differences or take a square root. A weighted version uses Σwᵢ|xᵢ−yᵢ| with explicitly chosen nonnegative weights.
Strictly positive weights preserve the metric's ability to distinguish every pair of different vectors. A zero weight deliberately ignores a coordinate and can make different full vectors have zero distance. Negative weights are not an ordinary distance convention.
Because absolute differences are tied to coordinate axes, rotating the representation can change Manhattan distance. This is different from Euclidean distance, which is unchanged by an orthogonal rotation. Choose the representation before interpreting a numerical distance or comparing it with another metric.
Three practical examples
- A nearest-neighbor method can compare scaled numerical feature vectors using total absolute coordinate difference.
- A planning problem on an unobstructed rectangular grid can use Manhattan distance to count the minimum number of unit horizontal and vertical moves.
- A clustering or location problem can use an L1 objective when additive absolute deviations suit the desired notion of discrepancy.
Think of it this way
Think of counting how many horizontal and vertical steps separate two points on a grid. The analogy is exact only under the stated movement rules. Real roads can include barriers, diagonals, one-way restrictions, or unequal travel times, so Manhattan distance should not be presented as an actual route estimate without checking the setting.
Worked example
Consider an original pair of vectors x=(2,−1,5) and y=(−1,3,4). The coordinate differences have magnitudes 3, 4, and 1, so their Manhattan distance is 3+4+1=8. Their Euclidean distance is √(9+16+1)=√26, approximately 5.099. The two values describe different geometries, not conflicting calculations.
Now use positive weights (1,0.5,2). The weighted Manhattan distance is 1×3+0.5×4+2×1=7. Changing weights changes which discrepancies matter and should reflect a stated modeling choice.
For a separate two-dimensional example, take displacement (1,1). Its Manhattan length is 2. Rotate it by 45 degrees to obtain (0,√2), whose Manhattan length is √2. Its Euclidean length remains √2 before and after rotation. This demonstrates why the coordinate representation is part of an L1 comparison.
Finally, compare candidate points A=(3,0) and B=(2,2) with the origin. Manhattan distance ranks A closer because 3<4. Euclidean distance ranks B closer because √8<3. A nearest-neighbor answer can therefore change when the metric changes, even with the same unaltered records. Choosing a distance is a substantive modeling decision, not merely an interchangeable implementation setting.
Illustrative case study
In a fictional inventory-analysis project, a team compares products using package weight and weekly demand. Raw Manhattan distance is dominated by demand because its numerical range is much larger. Reviewers find that some supposedly close products have very different packaging characteristics.
The team documents a scaling policy, examines representative neighbor pairs, and evaluates whether the resulting comparisons support its task. It keeps the original measurements available for interpretation. This hypothetical workflow illustrates the importance of units and relevance rather than claiming a universal scaling method or measured business benefit.
Common misconceptions and mistakes
Do not call Manhattan distance the straight-line distance. Do not assume it is invariant to arbitrary rotations. Do not sum features with incompatible scales without considering what the result means.
Absolute differences are less rapidly increasing than squared differences, but that does not make every method using Manhattan distance robust to every kind of outlier. High-dimensional data can still make distance-based comparisons difficult. Also, missing coordinates need an explicit policy: treating missing values as zero can create artificial proximity or separation.
Business use
Manhattan distance can be useful when total coordinate-wise change is an understandable comparison. Teams should record feature order, units, scaling, weights, and missing-value treatment. Review whether each contribution reflects a meaningful discrepancy for the task.
If distance is used to trigger an action or merge records, evaluate the full decision rule rather than assuming the metric itself validates the action. Similarity under chosen coordinates may ignore a crucial attribute. Thresholds should be tested on realistic cases, and changes in preprocessing should prompt reconsideration of earlier distance thresholds.
People also ask
Is Manhattan distance the same as L1 distance?
Yes, for the usual sum of absolute coordinate differences.
Does it work only in two dimensions?
No. The formula applies to any finite number of aligned numerical coordinates.
Can it give a different nearest neighbor from Euclidean distance?
Yes. The two metrics weight the shape of a displacement differently, so rankings can change.
Check your understanding
What is the Manhattan distance between (1,4) and (6,2)?
Answer: |1−6|+|4−2|=5+2=7. A path restricted to unit horizontal and vertical moves on an unobstructed grid needs at least seven steps.
Related terms
- Euclidean distance
- Minkowski distance
- Nearest neighbor
- L1 norm
- distance metric
- feature scaling
- absolute deviation
- weighted distance
- grid path
Learn next
Next, compare Manhattan and Euclidean neighbor rankings on three two-dimensional points. Then rescale one coordinate and observe how the Manhattan ranking changes, making the consequences of units explicit.
More in Mathematics and optimization
- Absolute error loss - Absolute error loss measures a numerical prediction's error as the absolute difference between its predicted and reference values: L(y,y-hat)=|y-y-hat|.
- Active-set method - An active-set method is an optimization approach that works with a changing subset of constraints believed to be tight at the solution.
- Adjoint state method - The adjoint state method computes how a chosen objective changes when the parameters of a mathematical model change.
- Σ-algebra
- Algebraic connectivity - Algebraic connectivity is the second-smallest eigenvalue of a graph's Laplacian matrix.
- Algorithm IMED - Algorithm IMED, short for Indexed Minimum Empirical Divergence, is a method for choosing actions in a stochastic multi-armed bandit problem.
Sources
Primary implementation reference: SciPy, scipy.spatial.distance.cityblock, official Manhattan-distance formula and weighting interface: https://docs.scipy.org/doc/scipy/reference/generated/scipy.spatial.distance.cityblock.html . The vector, rotation, and nearest-neighbor examples are original calculations.
Previous: Mandatory access control Next: Manifestation shift