skip to content

Why does squared Euclidean distance violate the triangle inequality?

level: middleimportance: nice to knowfreq 26%

answer

  1. one of the four metric axioms fails
  2. three evenly spaced points on a line
  3. detours become cheaper than going direct
  4. squaring is convex, so it grows too fast
  5. compare 4 against 1 plus 1

basics

~20 s

Squaring grows faster than adding. For points 0, 1 and 2 on a line, squared distance gives 4 end to end but 1 plus 1 for the legs, so a detour beats the direct route. It is a divergence, not a metric.

solid answer

~50 s

The triangle inequality says `d(a, c) <= d(a, b) + d(b, c)` for any three points, and it follows for any distance built from a norm because `||u + v|| <= ||u|| + ||v||`. Squaring the Euclidean distance breaks it. On a line put `a = 0`, `b = 1`, `c = 2`: the squared distances are `d(a, c) = 4` while `d(a, b) + d(b, c) = 1 + 1 = 2`, and `4 > 2`. The squared version is still non-negative, still symmetric, still zero only when the points coincide, so it fails exactly one of the four metric axioms. That matters wherever distances get added or compared across paths, or where an algorithm prunes a search using the triangle inequality. It does not matter when you only rank distances from a single reference point, since squaring is monotone and preserves the ordering.

go deeper

for a junior

Recall that the triangle inequality says the direct route is never longer than a detour, and that squaring a distance breaks that promise. Being able to state the four metric axioms is enough here.

for a middle

Produce the counterexample without hesitation, three points on a line giving 4 against 1 plus 1, and explain that convexity of squaring is the underlying cause.

for a senior

Separate the safe uses from the unsafe ones: ranking against a common reference is fine, while summing along paths or pruning a search needs a true metric. Say which of your uses is which.

for a principal

Own the vocabulary discipline across a team: a quantity called a distance carries guarantees that a divergence does not, and letting the two words blur is how incorrect pruning and meaningless path costs enter a system.

## The four metric axioms A function `d(a, b)` on pairs of points is a metric when it satisfies all four of these: 1. **Non-negativity:** `d(a, b) >= 0`. 2. **Identity of indiscernibles:** `d(a, b) = 0` exactly when `a = b`. 3. **Symmetry:** `d(a, b) = d(b, a)`. 4. **Triangle inequality:** `d(a, c) <= d(a, b) + d(b, c)` for every third point `b`. The fourth is the one with real content. Informally it says a detour can never be shorter than going direct. ## Norms give metrics for free Every norm induces a metric by `d(a, b) = ||a - b||`. The triangle inequality for the distance follows immediately from the triangle inequality for the norm, `||u + v|| <= ||u|| + ||v||`, by setting `u = a - b` and `v = b - c` so that `u + v = a - c`. This is why Manhattan, Euclidean and Chebyshev distances are all genuine metrics: each comes from the corresponding norm. Mahalanobis distance with a fixed positive definite covariance matrix is a metric for the same reason, since it is a norm applied after an invertible linear transformation. For the Euclidean norm the inequality is a consequence of the Cauchy-Schwarz inequality, and equality holds exactly when one vector is a non-negative multiple of the other, that is, when `u` and `v` point the same way. Check it on `u = (1, 2)` and `v = (2, 4)`: `||u|| = sqrt(5)`, `||v|| = 2*sqrt(5)`, and `u + v = (3, 6)` has norm `3*sqrt(5)`, which is exactly the sum. Now check a bent case, `u = (3, 4)` and `v = (-4, 3)`: both have norm 5, their sum `(-1, 7)` has norm about 7.07, comfortably under 10. ## Where squaring breaks it Squaring is a monotone transformation on non-negative numbers, so it preserves the first three axioms. It destroys the fourth, because squaring is convex and grows faster than linearly: `(x + y)^2 = x^2 + 2xy + y^2`, which exceeds `x^2 + y^2` whenever both terms are positive. The smallest counterexample lives on a line. Let `a = 0`, `b = 1`, `c = 2`, and define `D(u, v) = (u - v)^2`. - `D(a, c) = (0 - 2)^2 = 4` - `D(a, b) + D(b, c) = 1 + 1 = 2` Since `4 > 2`, the triangle inequality fails, and it fails by a factor of two on a configuration as ordinary as three evenly spaced points. Adding intermediate points makes it worse: splitting a distance of length `L` into `k` equal legs gives total squared length `L^2 / k`, which can be made as small as you like. Under squared distance a wandering path is always cheaper than the direct one, which is the opposite of what a distance is supposed to mean. ## Why squared distance is used anyway Squared Euclidean distance is smooth (no square root and therefore no kink at zero), differentiable everywhere with the tidy gradient `2 * (u - v)`, and cheaper to compute since the square root is skipped. It is the canonical example of a divergence: a non-negative measure of dissimilarity, zero only for identical points, that is not required to be a metric. ## When the violation matters and when it does not **Does not matter:** ranking. Because squaring preserves order on non-negative numbers, sorting points by squared distance from one fixed reference gives exactly the same order as sorting by distance. Any nearest-point search that only compares distances to a common query is safe, and skipping the square root is a free speedup. **Matters:** any time distances are *added*, *chained* or *compared across different pairs of endpoints*. Concretely, path costs along a route are meaningless under squared distance, bounds of the form `d(a, c) >= d(a, b) - d(b, c)` do not hold, and data structures that prune a search by reasoning from a stored distance through an intermediate point can prune away the true answer. Any claim of the form we skipped this branch because it cannot be closer needs a real metric behind it. ## The practical rule Use squared distance freely as an objective to minimise or a ranking key. Call it a distance only when nothing downstream relies on the triangle inequality, and take the square root the moment something does.

  • When does the triangle inequality hold with equality for the Euclidean norm?
    Exactly when the two vectors point in the same direction, that is, when one is a non-negative multiple of the other. Then `||u + v|| = ||u|| + ||v||`. Geometrically the triangle has collapsed onto a straight line, so the detour and the direct route coincide. Any angle between them makes the sum strictly larger.
  • Is it safe to rank nearest points by squared Euclidean distance instead of distance?
    Yes, as long as every comparison is against the same reference point. Squaring is monotone on non-negative numbers, so it preserves the ordering exactly, and skipping the square root saves work. It stops being safe as soon as distances are summed along a path or used to prune via the triangle inequality.
  • Do Manhattan and Chebyshev distances satisfy the triangle inequality?
    Yes. Both are induced by genuine norms, the L1 and L-infinity norms, and every norm satisfies `||u + v|| <= ||u|| + ||v||` by definition. So both are proper metrics, as is any distance built as the norm of a difference.

If road tolls were charged per squared mile, drivers would save money by stopping repeatedly along the way, which is a clear sign the charge is not measuring distance.

saying these in an interview costs you the question

  • Thinks squaring preserves all four metric axioms
  • Says squared distance fails symmetry rather than the triangle inequality
  • Cannot produce a counterexample when asked
  • Claims ranking by squared distance changes the order
  • Believes any non-negative dissimilarity is a metric

context