Metric Spaces and Distances

Part 4, Chapter 4: Point-Set Topology Basics

Learning objectives

  • Define a metric space (X,d)(X,d) via the three metric axioms
  • Compute distances using the Euclidean, taxicab, and discrete metrics
  • Recognize that distinct metrics may or may not induce the same topology
  • Predict that completeness depends on the metric, while topology does not

A metric space is the right setting for almost every question in analysis that needs a notion of "distance." Metric spaces sit between general topological spaces (where only "nearness" makes sense) and mathbbRn\mathbb{R}^n (where you also have coordinates and dot products). They give you enough structure for sequences, Cauchy convergence, and completeness, while still being abstract enough to cover function spaces, sequence spaces, and the data-science notion of "similarity." Whenever you hear "Euclidean distance," "cosine similarity," or "edit distance," you are inside a metric space.

The definition

A metric space is a pair (X,d)(X,d) where XX is a set and d:XtimesXto[0,infty)d:X\times X\to[0,\infty) is a function (the metric or distance) satisfying:

  • (M1) Identity: d(x,y)=0iffx=yd(x,y)=0\iff x=y.
  • (M2) Symmetry: d(x,y)=d(y,x)d(x,y)=d(y,x).
  • (M3) Triangle inequality: d(x,z)leqd(x,y)+d(y,z)d(x,z)\leq d(x,y)+d(y,z) for all x,y,zinXx,y,z\in X.

Every metric induces a topology: declare UsubseteqXU\subseteq X open when for every xinUx\in U there is r>0r>0 with B(x,r)subseteqUB(x,r)\subseteq U. The metric axioms guarantee the three topology axioms automatically. So every metric space is a topological space, but not every topological space comes from a metric (those that do are called metrizable).

Three canonical metrics on mathbbRn\mathbb{R}^n

  • Euclidean: d2(mathbfx,mathbfy)=sqrtsumi(xiyi)2d_2(\mathbf{x},\mathbf{y})=\sqrt{\sum_i(x_i-y_i)^2}i(xiyi)2. Straight-line distance.
  • Taxicab (Manhattan, ell1\ell^1): d1(mathbfx,mathbfy)=sumixiyid_1(\mathbf{x},\mathbf{y})=\sum_i|x_i-y_i|ixiyi. Distance along axis-aligned moves only.
  • Chebyshev (\ell^\infty): dinfty(mathbfx,mathbfy)=maxixiyid_\infty(\mathbf{x},\mathbf{y})=\max_i|x_i-y_i|ixiyi. Maximum-coordinate-difference distance.

These three metrics are topologically equivalent: they induce the same topology on mathbbRn\mathbb{R}^n, the same open sets, the same continuous functions, the same notion of convergence. The unit balls look different (a Euclidean disc, a diamond, a square) but each one fits inside the others when scaled, which is the key to the equivalence proof.

Contrast with the discrete metric d(x,y)=1d(x,y)=1 if xneqyx\neq y else 00: it induces the discrete topology, where every singleton is open. The discrete metric is NOT topologically equivalent to the Euclidean metric on mathbbRn\mathbb{R}^n.

Try plotting y=sqrtx2+11y=\sqrt{x^2+1}-1 in the grapher. This is the Euclidean distance from the point (x,0)(x,0) to (0,1)(0,1) minus 1, a "shifted" distance function. The grapher cannot display the unit balls of different metrics in 2D directly, but it is useful for visualising 1D distance variations as you change a parameter.

Where this shows up
  • Machine learning, cosine similarity and k-NN: Document retrieval and recommendation systems represent items as vectors and compute "similarity" via cosine distance d(mathbfu,mathbfv)=1fracmathbfucdotmathbfvmathbfumathbfvd(\mathbf{u},\mathbf{v})=1-\frac{\mathbf{u}\cdot\mathbf{v}}{\|\mathbf{u}\|\|\mathbf{v}\|}. This is a true metric on the unit sphere (after care for the d(mathbfu,mathbfu)=0d(\mathbf{u},\mathbf{u})=0 check). k-nearest-neighbours algorithms make sense in any metric space; the choice of metric (Euclidean for images, Mahalanobis for whitened features, edit distance for strings) is half the model design.
  • Distance-based clustering: k-means, DBSCAN, hierarchical clustering all consume a metric. Picking the right metric (e.g. taxicab for sparse features, cosine for high-dim normalized embeddings) often matters more than the algorithm choice. The triangle inequality is what allows the spatial-indexing tricks (KD-trees, ball trees, VP-trees) that make distance queries scalable.
  • Bioinformatics, sequence alignment: Edit distance, Hamming distance, and the more general Levenshtein metric measure dissimilarity between DNA, RNA, or protein sequences. These are real metrics (they satisfy M1-M3), which justifies indexing and search algorithms that depend on triangle inequalities for pruning.

Pause and think: The function d(x,y)=(xy)2d(x,y)=(x-y)^2 on mathbbR\mathbb{R} is non-negative, symmetric, and zero exactly when x=yx=y. Does it satisfy the triangle inequality? Try x=0,y=1,z=2x=0,y=1,z=2: d(0,2)=4d(0,2)=4 but d(0,1)+d(1,2)=1+1=2d(0,1)+d(1,2)=1+1=2. So 4leq24\leq 2? No, the triangle inequality fails. Squaring a metric does NOT produce a metric in general.

Try it

  • Predict first: in mathbbR2\mathbb{R}^2 with the taxicab metric, what does the unit ball B(mathbf0,1)B(\mathbf{0},1) look like? (Answer: a square rotated 45 degrees, a "diamond" with vertices at (pm1,0)(\pm 1, 0) and (0,pm1)(0, \pm 1).)
  • Compute the taxicab and Euclidean distances between (0,0)(0,0) and (3,4)(3,4). (Taxicab =7=7; Euclidean =5=5.) Notice taxicab geq\geq Euclidean in mathbbRn\mathbb{R}^n, with equality only for axis-aligned displacements.
  • Predict: in the discrete metric on any set, every subset is open. Use this to argue that a sequence (xn)(x_n)n) converges to xx in the discrete metric iff xn=xx_n=xn=x eventually.
  • Verify the triangle inequality for cosine distance d(mathbfu,mathbfv)=1mathbfucdotmathbfvd(\mathbf{u},\mathbf{v})=1-\mathbf{u}\cdot\mathbf{v} on the unit sphere. (Hint: it follows from mathbfumathbfv2=22mathbfucdotmathbfv\|\mathbf{u}-\mathbf{v}\|^2=2-2\mathbf{u}\cdot\mathbf{v} and the Euclidean triangle inequality.)
  • Trap: completeness is a metric property, NOT a topological one. The interval (0,1)(0,1) with the metric d(x,y)=arctan(tan(pix/2))arctan(tan(piy/2))d(x,y)=|\arctan(\tan(\pi x/2))-\arctan(\tan(\pi y/2))|... actually simpler: (0,1)(0,1) with usual metric is NOT complete (Cauchy sequence 1/n1/n has no limit in the space), but (0,1)(0,1) is homeomorphic to mathbbR\mathbb{R}, which IS complete in its usual metric. Same topology, different completeness.
  • A trap to watch for

    "Distance" in everyday English does not always satisfy the triangle inequality. The squared-Euclidean "distance" xy2\|x-y\|^2 fails it (as the pause-check showed). Many ad-hoc similarity scores in machine-learning literature (Jaccard, certain probability divergences) do not satisfy all three axioms either, KL divergence is asymmetric, so it is not a metric. Calling these "distances" loosely is harmless conversation, but algorithms that assume the triangle inequality (KD-trees, metric-tree indexing) silently break on non-metrics. Always check the axioms before using a "metric" in code.

    What you now know

    You can verify metric axioms for distance functions, recognize when two metrics induce the same topology, and choose between Euclidean, taxicab, Chebyshev, and discrete metrics for different applications. The next section turns to bases, a way to describe entire topologies via small, manageable collections of "primitive" open sets.

    Mark section complete →

    References

    • Garrity, T. (2002). All the Mathematics You Missed. Cambridge UP, ch. 4.
    • Munkres, J. R. (2000). Topology (2nd ed.). Prentice-Hall, ch. 2 (sections 20-21).
    • Willard, S. (1970). General Topology. Addison-Wesley, ch. 8.
    • Kelley, J. L. (1955). General Topology. Van Nostrand, ch. 4.
    • Rudin, W. (1976). Principles of Mathematical Analysis (3rd ed.). McGraw-Hill, ch. 2.

    This page is prerendered for SEO and accessibility. The interactive widgets above hydrate on JavaScript load.