Tutorials › Topology (Undergraduate) › What Makes a Distance a Metric

Metric Spaces and Open Balls · Tutorial 35 of 799

What Makes a Distance a Metric

Learn to test whether a distance function is a metric by checking nonnegativity, identity of indiscernibles, symmetry, and the triangle inequality.

Beginner 9 min read

What You'll Learn

  • State the four metric axioms with their quantifiers.
  • Interpret what each axiom requires of distances between points.
  • Verify the usual absolute-value distance on the real line.
  • Identify failures of the triangle inequality and symmetry in distance-like functions.
  • Prove basic consequences of the metric axioms.
  • Apply positive rescaling without losing the metric properties.

When Does a Distance Count as a Metric?

A formula can assign a nonnegative number to every pair of points and still fail to be a metric. The defining conditions do more than require nonnegative outputs: they ensure that distance zero identifies the same point, that reversing the order of two points changes nothing, and that travelling through an intermediate point cannot be shorter than the direct distance. These conditions make a metric a reliable way to measure separation on a set.

Throughout, \(X\) is a set. A distance function on \(X\) is a function \(d:X\times X\to[0,\infty)\), so each ordered pair \((x,y)\) is assigned a finite nonnegative real number \(d(x,y)\). The set \([0,\infty)\) includes zero.

Definition: A metric on \(X\) is a function \(d:X\times X\to[0,\infty)\) satisfying, for all \(x,y,z\in X\):

Nonnegativity and identity of indiscernibles: \(d(x,y)=0\) if and only if \(x=y\).
Symmetry: \(d(x,y)=d(y,x)\).
Triangle inequality: \(d(x,z)\le d(x,y)+d(y,z)\).

The first condition combines two requirements. Nonnegativity says no assigned distance is negative; the “if and only if” says that equal points have distance zero and that distinct points have strictly positive distance. Symmetry says the distance from \(x\) to \(y\) equals the distance from \(y\) to \(x\). The triangle inequality says that the direct distance from \(x\) to \(z\) is no greater than the sum of the distances from \(x\) to \(y\) and from \(y\) to \(z\). Each condition must hold for every indicated choice of points.

If \(X\) is empty, the unique function with domain \(X\times X=\varnothing\) satisfies all four conditions vacuously, so it is a metric. If \(X\) has one point, the only possible value is the distance from that point to itself, which must be zero; this function is a metric. On larger sets, a proposed formula must be checked against all four axioms, not just a few sample pairs.

The Absolute-Value Distance

Worked Example: The Usual Distance on the Real Line

Let \(X=\mathbb{R}\), with its usual real numbers, and define \(d(x,y)=|x-y|\). We verify all four metric conditions.

Nonnegativity and identity. Absolute values are nonnegative, so \(d(x,y)=|x-y|\ge0\). Also, \(|x-y|=0\) exactly when \(x-y=0\), which is exactly when \(x=y\).

Symmetry. Since \(y-x=-(x-y)\) and \(|-t|=|t|\) for every real \(t\), \(d(y,x)=|y-x|=|x-y|=d(x,y)\).

Triangle inequality. For real numbers \(u,v\), the inequalities \(-|u|\le u\le|u|\) and \(-|v|\le v\le|v|\) give \(-(|u|+|v|)\le u+v\le|u|+|v|\). Therefore \(|u+v|\le|u|+|v|\). Apply this with \(u=x-y\) and \(v=y-z\). Since \(u+v=x-z\), it follows that \(d(x,z)=|x-z|\le|x-y|+|y-z|=d(x,y)+d(y,z)\).

Numerical check. For \(x=2\), \(y=-1\), and \(z=4\), the three distances in the triangle inequality are \(d(2,4)=|2-4|=2\), \(d(2,-1)=|2-(-1)|=3\), and \(d(-1,4)=|-1-4|=5\). Thus \(2\le3+5=8\), as required. This example checks one triple; the preceding argument proves the inequality for every triple.

Two Ways a Distance Formula Can Fail

Checking the axioms separately helps locate exactly what goes wrong. A failure of one condition is enough to disqualify a function, even if it satisfies the other three.

Worked Example: Squared Distance Fails the Triangle Inequality

On \(X=\mathbb{R}\), consider \(q(x,y)=|x-y|^2\). For every \(x,y\), \(q(x,y)\ge0\). Moreover, \(q(x,y)=0\) exactly when \(|x-y|=0\), or equivalently \(x=y\). Symmetry holds because \(|y-x|=|x-y|\), and squaring preserves that equality.

To test the triangle inequality, take \(x=0\), \(y=1\), and \(z=2\). The direct distance is \(q(0,2)=|0-2|^2=2^2=4\). The two distances through \(1\) are \(q(0,1)=|0-1|^2=1^2=1\) and \(q(1,2)=|1-2|^2=1^2=1\). Their sum is \(1+1=2\), so the required inequality would be \(4\le2\), which is false. Therefore \(q\) is not a metric.

Check: This calculation identifies a specific failed axiom, not a failure of nonnegativity, identity, or symmetry. In fact, those three conditions were verified for arbitrary pairs above.

Worked Example: A Nonnegative Asymmetric Distance

On \(X=\mathbb{R}\), define \(r(x,y)=|x-y|+\frac{y-x}{2}\). Although this formula is not symmetric in its inputs, it is nonnegative. To see this, write \(t=y-x\). If \(t\ge0\), then \(r(x,y)=t+\frac{t}{2}=\frac{3t}{2}\ge0\). If \(t<0\), then \(|t|=-t\), so \(r(x,y)=-t+\frac{t}{2}=-\frac{t}{2}>0\).

It also has zero exactly for equal points: when \(t=0\), \(r(x,y)=0\); when \(t>0\), \(r(x,y)=\frac{3t}{2}>0\); and when \(t<0\), \(r(x,y)=-\frac{t}{2}>0\).

For arbitrary \(x,y,z\), the absolute-value inequality gives \(|z-x|\le|y-x|+|z-y|\), while the linear terms satisfy \(\frac{z-x}{2}=\frac{y-x}{2}+\frac{z-y}{2}\). Adding these statements gives \(r(x,z)\le r(x,y)+r(y,z)\). Thus nonnegativity, identity, and the triangle inequality hold.

Symmetry fails. Substituting \(x=0\), \(y=2\) gives \(r(0,2)=|0-2|+\frac{2-0}{2}=2+1=3\), whereas \(r(2,0)=|2-0|+\frac{0-2}{2}=2-1=1\). Since \(3\ne1\), \(r\) is not a metric.

Check: The failure is specifically symmetry. The general arguments above verify the other conditions, including the triangle inequality for every triple.

Consequences of the Axioms

Proposition: If \(d\) is a metric on \(X\), then for any \(x,y,z\in X\), \(\lvert d(x,z)-d(y,z)\rvert\le d(x,y)\).

Proof. The triangle inequality applied to \(x,y,z\) gives \(d(x,z)\le d(x,y)+d(y,z)\). Subtracting \(d(y,z)\) gives \(d(x,z)-d(y,z)\le d(x,y)\). Applying the triangle inequality to \(y,x,z\), and using symmetry \(d(y,x)=d(x,y)\), gives \(d(y,z)\le d(x,y)+d(x,z)\). Subtracting \(d(x,z)\) gives \(d(y,z)-d(x,z)\le d(x,y)\). These two inequalities together say that the absolute value of the difference is at most \(d(x,y)\), as claimed. \(\square\)

This bound says that changing the first point from \(x\) to \(y\) cannot change its distance to a fixed point \(z\) by more than the distance between \(x\) and \(y\). The conclusion depends on both the triangle inequality and symmetry; they were used explicitly in the proof.

Theorem: If \(d\) is a metric on \(X\) and \(c>0\), then \(d_c(x,y)=c\,d(x,y)\) is also a metric on \(X\).

Proof. If \(X\) is empty, the assertion holds vacuously. Otherwise, take arbitrary \(x,y,z\in X\). Since \(c>0\) and \(d(x,y)\ge0\), \(d_c(x,y)=c\,d(x,y)\ge0\); also, \(d_c(x,y)=0\) exactly when \(d(x,y)=0\), which holds exactly when \(x=y\). Symmetry follows from \(d_c(x,y)=c\,d(x,y)=c\,d(y,x)=d_c(y,x)\). Finally, multiplying the triangle inequality for \(d\) by the positive number \(c\) preserves its direction: \(d_c(x,z)=c\,d(x,z)\le c\,d(x,y)+c\,d(y,z)=d_c(x,y)+d_c(y,z)\). All metric conditions hold, so \(d_c\) is a metric. \(\square\)

Worked Example: Rescaling a Metric

Start with \(d(x,y)=|x-y|\) on \(\mathbb{R}\), and choose \(c=3\). The theorem shows that \(d_3(x,y)=3|x-y|\) is a metric: the multiplier \(3\) is positive.

For example, with \(x=-2\) and \(y=1\), \(d_3(-2,1)=3|-2-1|=3\cdot3=9\), and \(d_3(1,-2)=3|1-(-2)|=3\cdot3=9\). With \(z=4\), the triangle inequality reads \(d_3(-2,4)\le d_3(-2,1)+d_3(1,4)\). The left side is \(3|-2-4|=3\cdot6=18\); the right side is \(3|-2-1|+3|1-4|=9+9=18\). Thus equality holds for these three points, in agreement with the general theorem.

Check: This example verifies the values for the displayed points; the theorem proves that every positive rescaling works for every metric and all its points.

A Common Mistake

Common pitfall. A formula may be nonnegative, symmetric, and zero exactly on equal pairs, yet still fail to be a metric. For the squared-distance formula \(q(x,y)=|x-y|^2\), one might incorrectly reason that it is a metric because it inherits these three properties from absolute value. The triangle inequality calculation at \(0,1,2\) corrects that claim: \(q(0,2)=4\), but \(q(0,1)+q(1,2)=1+1=2\), and \(4\le2\) is false. The triangle inequality is an independent condition, so it must be checked rather than inferred from the other axioms.

In practice, begin by checking the formula’s range and its value when the two inputs agree. Then check symmetry and the triangle inequality. A single counterexample disproves a required condition; a proof for arbitrary points is needed to establish that the condition always holds. Later metric examples use these same four tests, even when their formulas look different from absolute value.

Check Your Understanding

For each question, identify which metric axiom is relevant and state the quantifiers in your reasoning.

  1. Why does \(d(x,y)=0\) for a metric imply \(x=y\), rather than merely allowing \(x=y\)?
  2. On \(\mathbb{R}\), does \(s(x,y)=|x-y|+2\) define a metric? Which condition decides the answer?
  3. For \(p(x,y)=|x-y|^2\), give a triple that disproves the triangle inequality and write out all three distances.
  4. Why must the constant in the rescaling theorem be strictly positive? What happens to identity of indiscernibles if the constant is zero?
  5. Use the triangle inequality and symmetry to derive a bound on \(|d(x,z)-d(y,z)|\) for a metric \(d\).