Basis Lab

Answers · The mathematics underneath

Why does Manhattan distance give a different answer from straight-line distance?

In shortThey measure different things: one is the straight gap, the other is the total along a grid, so they disagree whenever the points differ in more than one coordinate.

They measure different things. Straight-line distance is the length of the direct gap between two points, while Manhattan distance is the total length of an axis-aligned route between them. They agree only when every coordinate but one is the same, because then the direct gap and the grid route lie along the same line. Once two or more coordinates differ, cutting the corner and walking the streets give different numbers, and neither is a mistake.

∥u−v∥=(u1−v1)2+⋯+(un−vn)2,∥u−v∥1=∣u1−v1∣+⋯+∣un−vn∣.\lVert u-v\rVert = \sqrt{(u_1-v_1)^2+\cdots+(u_n-v_n)^2},\qquad \lVert u-v\rVert_1 = |u_1-v_1|+\cdots+|u_n-v_n|.

A worked pair

Take u=(1,2,3) and v=(4,6,3). Subtracting coordinate by coordinate gives (3,4,0). Straight-line: 3^2+4^2+0^2=9+16=25, and sqrt(25)=5. Manhattan: |3|+|4|+|0|=7. The third coordinate agrees, so it contributes nothing under either measure. The 3 and 4 are not added as 7 in the straight-line answer: they are the two legs of a right-angled triangle, and the direct path is the hypotenuse of length 5. The grid route is 3 blocks east and 4 blocks north, total 7.

FIG 1
paircoordinate differencesstraight-lineManhattan
A(6, 0)66
B(4, 4)sqrt(32) = 5.6578

The natural guess is that both measures add up the same coordinate differences, so they ought to agree. They do not, because straight-line distance is not a sum of differences: it is the square root of a sum of squares. For differences 3 and 4, Manhattan gives 3+4=7. Straight-line gives sqrt(3^2+4^2)=5. Squaring before adding means the differences do not add linearly: 3^2+4^2=25, while (3+4)^2=49. The diagonal is shorter because it replaces two legs by one hypotenuse, not because it loses a block.

For two non-negative coordinate differences a and b, (a+b)^2=a^2+b^2+2ab. The cross term 2ab is non-negative, so (a+b)^2 is at least a^2+b^2, and therefore a+b is at least sqrt(a^2+b^2). Applying this coordinate by coordinate shows that Manhattan distance can never be smaller than straight-line distance. Equality needs every cross term to vanish, which means at most one coordinate differs. This ordering of the two measures is not an accident of the example: it follows from the algebra.

(a+b)2=a2+b2+2ab≥a2+b2⇒a+b≥a2+b2(a+b)^2=a^2+b^2+2ab\ge a^2+b^2 \quad\Rightarrow\quad a+b\ge\sqrt{a^2+b^2}

Why the largest difference wins

Squaring changes which disagreements matter. A difference of 10 contributes 100 to the squared sum but only 10 to the Manhattan sum. A difference of 1 contributes 1 to both. So the ten-unit disagreement is 100 times as influential as the one-unit disagreement under straight-line distance, but only 10 times as influential under Manhattan distance. Straight-line distance is therefore controlled by the largest disagreements, while Manhattan spreads influence more evenly. That is why the two can rank pairs differently, not merely report different totals.

Consider two pairs. Pair A has differences (6,0): straight-line sqrt(36)=6 and Manhattan 6. Pair B has differences (4,4): straight-line sqrt(32)=5.657 and Manhattan 8. Straight-line says A is farther apart than B, since 6 is greater than 5.657. Manhattan says B is farther apart than A, since 8 is greater than 6. Neither pair changed; only the rule for turning differences into a single number changed. A measure is not a neutral summary when it can reverse an ordering. That is a stronger failure than a different number: it changes which item is nearest.

Where the agreement returns

The agreement returns along the axes. If the difference is (d,0,0,...,0), both measures give |d|, and in one dimension there is only one possible route. The assumptions underneath both measures are that coordinates are comparable and that adding their contributions makes sense. A coordinate running into the thousands next to one between 0 and 1 will decide every distance. Subtracting a typical value and dividing by a typical spread puts the coordinates on a comparable scale before measuring; without that, a distance can be faithfully reporting the units rather than the similarity.

The choice is about the route that is allowed and the cost of a step. If movement is free through space, straight-line distance is the actual length. If movement is confined to a grid, Manhattan is the actual length. For comparing vectors, the choice is a modelling decision about which disagreements should dominate. Calling one of them the distance hides that decision, because the word distance suggests a single quantity while the formulas give two.

Worth keeping

  • Straight-line distance squares each coordinate difference before adding; Manhattan distance adds the absolute differences directly, so their totals and sometimes their rankings differ.
  • A pair that differs in only one coordinate gets the same answer from both measures.
  • Both measures assume the coordinates are on comparable scales, or one coordinate will decide every distance.

Where this goes next

This question is one step of Vectors, Distance and Similarity, a course that works through its subject from the beginning. The page that teaches this part of it is How Far Apart, and it reads the whole way through whether or not you have an account.

What an account changes is the practice: problems standing under every idea, marked with a reason rather than a tick, in a copy of the course that is yours.

Read that pageThe whole course

Other questions people ask about this

  1. What does a negative dot product tell you about two vectors?A negative dot product means the angle between the vectors is greater than a right angle, so they point broadly apart rather than together.
  2. Why does cosine similarity ignore how long the vectors are?Cosine similarity compares direction only because dividing by both vector lengths cancels their size exactly, leaving the angle between them.
  3. Why do distances between random points become almost equal in high dimensions?Each coordinate adds a small independent amount to the squared distance, and many such amounts average out, so typical distances grow while their spread does not keep up.
  4. What's the difference between a vector as a point and a vector as a direction?A vector as a point is a location, while a vector as a direction is an arrow from the origin whose length and orientation matter; the same numbers can be read either way.
  5. What is the difference between variance and standard deviation?They measure the same spread: variance is the average squared distance from the mean, standard deviation is its square root, so you report the standard deviation and calculate with the variance.
  6. How do you make a vague claim precise enough to tell whether it's true?Fix what is being talked about, what is asserted, which cases are covered and the cut-off, then name an observation that would prove it false.

Read alongside