Skip to content
Tutorials Center
My learning

Topics

Lesson 5 of 36 ยท Metric Spaces

Metric Spaces: Distance Made Abstract

Define a metric space from the three axioms of distance, test candidate distance functions with code, and see why the triangle inequality is the key rule.

Before topology talks about open sets in abstract, it is worth studying a middle level of generality where distance still exists but need not be the familiar straight line. These are the metric spaces. They give you a rich supply of examples, and every metric space produces a topology, so they are the training ground for the rest of the track.

The definition

A metric space is a pair (X, d) where X is a set and d is a function d: X ร— X โ†’ โ„ that gives a distance between any two points and satisfies, for all x, y, z in X:

  1. Positivity: d(x, y) โ‰ฅ 0, and d(x, y) = 0 exactly when x = y.
  2. Symmetry: d(x, y) = d(y, x).
  3. Triangle inequality: d(x, z) โ‰ค d(x, y) + d(y, z).

The function d is called a metric. Axiom 1 says distinct points are at positive distance and each point is at distance zero from itself. Axiom 2 says the distance from x to y is the same as from y to x. Axiom 3 says a detour through y is never shorter than going directly: in a triangle, no side is longer than the sum of the other two.

The standard example is the real line with d(x, y) = |x โˆ’ y|. The triangle inequality for it is the familiar |a + b| โ‰ค |a| + |b| applied to a = x โˆ’ y and b = y โˆ’ z.

Any set can be given a metric. The points do not need to be numbers: they could be words, functions, images or cities. You only need a rule d that obeys the three axioms.

Why the axioms matter

Each axiom prevents something silly.

  • Without positivity, two different points could be "zero apart", and we could not tell them apart by distance.
  • Without symmetry, "close to" would depend on direction (like driving distance on one-way streets).
  • Without the triangle inequality, there would be no sensible idea of "nearby": you could have x near y and y near z but x far from z, and open balls would stop behaving as neighborhoods.

Testing a candidate by brute force

You cannot prove a function is a metric by testing finitely many points, but you can often disprove one by finding a counterexample, and a thorough test on a grid builds confidence. The checker below tries every pair and triple of points from a list.

Python A metric axiom checker

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The squared distance fails the triangle inequality: from 0 to 2 directly gives 4, but going through 1 gives 1 + 1 = 2. The function |xยฒ โˆ’ yยฒ| fails positivity, because โˆ’1 and 1 are different points at distance zero. The plain difference x โˆ’ y is not even nonnegative. The square root of |x โˆ’ y| passes. That one is a metric, and the reason is that the square root is concave, so it never lets a detour be shorter.

Building new metrics from old

If d is a metric on X, you can make others. These are all metrics:

  • min(d, 1), a bounded metric: no two points are more than 1 apart.
  • d / (1 + d), another bounded metric, always less than 1.
  • โˆšd, as seen above.
  • dโ‚ + dโ‚‚, the sum of two metrics on the same set.

These change the sizes of distances but not which points are "near" each other. That observation (very different metrics can describe the same notion of closeness) is the reason topology uses open sets rather than numbers.

Python Bounded versions of a metric

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Notice that the bounded metrics never exceed 1 even though the points are 8 units apart, yet they still pass every axiom.

The discrete metric

Any set X has the discrete metric: d(x, y) = 0 if x = y and d(x, y) = 1 otherwise. The triangle inequality holds because the left side is at most 1, and the right side is at least 1 unless x = y = z, in which case both sides are 0. In the discrete metric, every point is isolated from all others. We use it often as a test case.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which axiom says d(x, z) is at most d(x, y) + d(y, z)?

    That is the triangle inequality: a detour through y is never shorter than the direct path.
  2. 2. Why is d(x, y) = (x - y)^2 not a metric on the real numbers?

    With x = 0, y = 1, z = 2 we get d(0,2) = 4 but d(0,1) + d(1,2) = 2.
  3. 3. In the discrete metric on a set X, what is d(a, b) for two different points a and b?

    By definition the discrete metric gives distance 1 between every pair of distinct points.
  4. 4. If d is a metric, which of these is also a metric?

    min(d, 1) keeps positivity, symmetry and the triangle inequality. Squaring can break the triangle inequality, and -d or d - 1 are not nonnegative.

Summary

  • A metric space is a set with a distance function satisfying positivity, symmetry and the triangle inequality.
  • Finite tests can disprove a candidate metric by finding a counterexample, but they cannot prove it for all points.
  • min(d, 1), d / (1 + d) and โˆšd are metrics whenever d is; dยฒ generally is not.
  • The discrete metric makes every point isolated.

Next: Examples of Metrics