Skip to content
Tutorials Center
My learning

Topics

Lesson 6 of 36 ยท Metric Spaces

Examples of Metrics

Explore the Euclidean, taxicab, maximum and p-metrics on the plane, then Hamming distance on strings and the railway metric, all tested with runnable code.

Having a definition is one thing; having a feel for different metrics is another. This lesson tours the most useful examples. You will meet metrics on the plane that disagree about which points are nearest, metrics on strings used in computing, and a strange metric that sends every journey through a hub.

Metrics on the plane

For points x = (xโ‚, xโ‚‚) and y = (yโ‚, yโ‚‚) in โ„ยฒ:

Name Formula
Euclidean (dโ‚‚) โˆš((xโ‚ โˆ’ yโ‚)ยฒ + (xโ‚‚ โˆ’ yโ‚‚)ยฒ)
Taxicab / Manhattan (dโ‚) `
Maximum / Chebyshev (dโˆž) `max(

The taxicab metric is the distance a taxi drives on a city grid, where it cannot cut diagonally. The maximum metric is the number of moves a chess king needs on an empty board. For any two points,

Text
dโˆž โ‰ค dโ‚‚ โ‰ค dโ‚ โ‰ค 2ยทdโˆž

so the three always agree on whether two points are close. That comparison is a first hint that they define the same topology on the plane.

More generally, for p โ‰ฅ 1 there is the p-metric d_p(x, y) = (|xโ‚ โˆ’ yโ‚|^p + |xโ‚‚ โˆ’ yโ‚‚|^p)^(1/p). The case p = 1 is taxicab and p = 2 is Euclidean. As p grows, d_p approaches the maximum metric. The triangle inequality for d_p is called Minkowski's inequality; it needs p โ‰ฅ 1, and for p < 1 the formula is not a metric.

Python Three plane metrics, compared

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 last lines show that the nearest candidate can differ between metrics, even though all three metrics describe the same "closeness" in the topological sense (you will prove this in Open and Closed Balls).

Hamming distance

Let X be the set of all binary strings of length n. The Hamming distance d(u, v) counts the positions where u and v differ. For 0110 and 0011 it is 2. It is used for error-correcting codes: a code that keeps codewords at Hamming distance at least 3 from each other can correct any single flipped bit.

Is it a metric? Positivity and symmetry are clear. For the triangle inequality: every position where u and w differ must be a position where either u and v differ or v and w differ (otherwise u = v = w there). So the count for (u, w) is at most the sum of the other two counts.

We can confirm that exhaustively for strings of length 4, which has only 16 strings and 4,096 triples.

Python Hamming distance is a metric on 4-bit strings

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 railway (or hub) metric

Imagine a country where every train line goes through the capital O. To travel between two towns you must go through O. Model it on the plane with O at the origin and Euclidean length โ€–xโ€–. Define

Text
d(x, y) = โ€–x โˆ’ yโ€–   if x and y lie on the same ray from O
d(x, y) = โ€–xโ€– + โ€–yโ€–  otherwise

(Two points are on the same ray if one is a positive multiple of the other.) The triangle inequality holds because any detour is forced to pass through O, the very place where you would have gone anyway. This metric is sometimes called the French railway metric, or the hedgehog or post office metric. Near the hub, nearby points on different rays are actually far apart in this metric, even though they are close in the plane.

Python The hub (railway) 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.

Metrics on spaces of functions

Metrics also live on sets of functions. On the continuous functions on [0, 1], the supremum metric is d(f, g) = max over x of |f(x) โˆ’ g(x)|: the largest vertical gap between the graphs. Two functions are close when one graph fits inside a thin band around the other. This is the metric behind uniform convergence in analysis.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is the taxicab distance between (1, 2) and (4, -2)?

    |4 - 1| + |-2 - 2| = 3 + 4 = 7.
  2. 2. What is the Hamming distance between 10110 and 11100?

    The strings differ in positions 2 and 4, so the distance is 2.
  3. 3. For two points in the plane, which inequality always holds?

    The taxicab distance is never smaller than the Euclidean distance, which is never smaller than the maximum metric: d_inf <= d2 <= d1.
  4. 4. In the hub (railway) metric, what is the distance between (1, 0) and (0, 1)?

    The points are on different rays, so d = ||x|| + ||y|| = 1 + 1 = 2.

Summary

  • On the plane, the taxicab, Euclidean and maximum metrics satisfy dโˆž โ‰ค dโ‚‚ โ‰ค dโ‚ โ‰ค 2ยทdโˆž.
  • The p-metrics need p โ‰ฅ 1; Minkowski's inequality is the triangle inequality.
  • Hamming distance on strings is a metric, used in error-correcting codes.
  • The hub metric routes everything through a center and shows that metrics can disagree sharply with the straight-line picture.
  • Function spaces get metrics too, such as the supremum metric.

Next: Open and Closed Balls