Distance

Euclidean Distance

Two-Dimensional Space

Definition

Euclidean distance, also commonly called Euclidean distance, is defined as follows. In a Cartesian coordinate system, let points A,BA,B have coordinates A(x1,y1),B(x2,y2)A(x_1,y_1),B(x_2,y_2) respectively. Then the Euclidean distance between them is:

∣AB∣=(x2−x1)2+(y2−y1)2\left | AB \right | = \sqrt{\left ( x_2 - x_1 \right )^2 + \left ( y_2 - y_1 \right )^2}

Explanation

For example, suppose that in a Cartesian coordinate system there are two points A(6,5),B(2,2)A(6,5),B(2,2). Using the formula, we can easily obtain the Euclidean distance between A,BA,B:

∣AB∣=(2−6)2+(2−5)2=42+32=5\left | AB \right | = \sqrt{\left ( 2 - 6 \right )^2 + \left ( 2 - 5 \right )^2} = \sqrt{4^2+3^2} = 5

In addition, the Euclidean distance from P(x,y)P(x,y) to the origin can be written as:

∣P∣=x2+y2|P| = \sqrt{x^2+y^2}

n-Dimensional Space

Introduction

What is the Euclidean-distance formula for two points in three-dimensional space? Let us examine the figure below.

dis-3-dimensional
dis-3-dimensional

It is easy to see that in △ADC\triangle ADC, ∠ADC=90∘\angle ADC = 90^\circ; in △ACB\triangle ACB, ∠ACB=90∘\angle ACB = 90^\circ.

∴ ∣AB∣=∣AC∣2+∣BC∣2=∣AD∣2+∣CD∣2+∣BC∣2\begin{aligned} \therefore ~ |AB| &= \sqrt{|AC|^2+|BC|^2} \\ &= \sqrt{|AD|^2+|CD|^2+|BC|^2} \end{aligned}

Definition

It follows that the Euclidean-distance formula in three-dimensional space is:

∣AB∣=(x2−x1)2+(y2−y1)2+(z2−z1)2∣P∣=x2+y2+z2\begin{gathered} \left | AB \right | = \sqrt{\left ( x_2 - x_1 \right )^2 + \left ( y_2 - y_1 \right )^2 + \left ( z_2 - z_1 \right )^2} \\ |P| = \sqrt{x^2+y^2+z^2} \end{gathered}

Explanation

NOIP2017 Advanced Group Cheese uses this fact and can serve as an example problem for Euclidean distance.

Continuing in this way, we obtain the Euclidean-distance formula in nn-dimensional space: for A⃗(x11,x12,⋯ ,x1n), B⃗(x21,x22,⋯ ,x2n)\vec A(x_{11}, x_{12}, \cdots,x_{1n}) ,~ \vec B(x_{21}, x_{22}, \cdots,x_{2n}), we have

∥AB→∥=(x11−x21)2+(x12−x22)2+⋅⋅⋅+(x1n−x2n)2=∑i=1n(x1i−x2i)2\begin{aligned} \lVert\overrightarrow{AB}\rVert &= \sqrt{\left ( x_{11} - x_{21} \right )^2 + \left ( x_{12} - x_{22} \right )^2 + \cdot \cdot \cdot +\left ( x_{1n} - x_{2n} \right )^2}\\ &= \sqrt{\sum_{i = 1}^{n}(x_{1i} - x_{2i})^2} \end{aligned}

Although Euclidean distance is very useful, it also has an obvious drawback. When computing the distance between two lattice points, the answer is often a floating-point number and therefore has some error.

Manhattan Distance

Definition

In two-dimensional space, Manhattan distance is the sum of the absolute differences of the x- and y-coordinates of two points. Given points A(x1,y1),B(x2,y2)A(x_1,y_1),B(x_2,y_2), the Manhattan distance between A,BA,B is:

d(A,B)=∣x1−x2∣+∣y1−y2∣d(A,B) = |x_1 - x_2| + |y_1 - y_2|

Explanation

Observe the figure below:

manhattan-dis-diff
manhattan-dis-diff

Between A,BA,B, the yellow and orange lines represent Manhattan distance, the red and blue lines represent equivalent Manhattan distances, and the green line represents Euclidean distance.

In the same example, the coordinates of A,BA,B in the figure below are A(25,20),B(10,10)A(25,20),B(10,10).

manhattan-dis
manhattan-dis

Using the formula, we can easily obtain the Manhattan distance between A,BA,B:

d(A,B)=∣20−10∣+∣25−10∣=10+15=25d(A,B) = |20 - 10| + |25 - 10| = 10 + 15 = 25

By derivation, we obtain the formula for Manhattan distance in nn-dimensional space:

d(A,B)=∣x1−y1∣+∣x2−y2∣+⋅⋅⋅+∣xn−yn∣=∑i=1n∣xi−yi∣\begin{aligned} d(A,B) &= |x_1 - y_1| + |x_2 - y_2| + \cdot \cdot \cdot + |x_n - y_n|\\ &= \sum_{i = 1}^{n}|x_i - y_i| \end{aligned}

Properties

In addition to its formula, Manhattan distance has the following mathematical properties:

  • Non-negativity: Manhattan distance is non-negative, that is, d(i,j)≥0d(i,j)\geq 0.
  • Identity: the Manhattan distance from a point to itself is 00, that is, d(i,i)=0d(i,i) = 0.
  • Symmetry: the Manhattan distance from AA to BB equals that from BB to AA, that is, d(i,j)=d(j,i)d(i,j) = d(j,i).
  • Triangle inequality: the direct distance from point ii to jj is no greater than the distance through any other point kk, that is, d(i,j)≤d(i,k)+d(k,j)d(i,j)\leq d(i,k)+d(k,j).

Example

P5098「USACO04OPEN」Cave Cows 3

From the statement, for the expression ∣x1−x2∣+∣y1−y2∣|x_1-x_2|+|y_1-y_2|, we may assume x1−x2≥0x_1 - x_2 \geq 0 and split into two cases according to the sign of y1−y2y_1 - y_2:

  • (y1−y2≥0)→∣x1−x2∣+∣y1−y2∣=x1+y1−(x2+y2)(y_1 - y_2 \geq 0)\rightarrow |x_1-x_2|+|y_1-y_2|=x_1 + y_1 - (x_2 + y_2)

  • (y1−y2<0)→∣x1−x2∣+∣y1−y2∣=x1−y1−(x2−y2)(y_1 - y_2 < 0)\rightarrow |x_1-x_2|+|y_1-y_2|=x_1 - y_1 - (x_2 - y_2)

The answer can be obtained by finding the maximum and minimum values of x+y,x−yx+y, x-y.

Reference code

C++

#include <algorithm>
#include <cstdio>
using namespace std;

int main() {
  int n, x, y, minx = 0x7fffffff, maxx = 0, miny = 0x7fffffff, maxy = 0;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d%d", &x, &y);
    minx = min(minx, x + y), maxx = max(maxx, x + y);
    miny = min(miny, x - y), maxy = max(maxy, x - y);
  }
  printf("%d\n", max(maxx - minx, maxy - miny));
  return 0;
}

Python

minx = 0x7FFFFFFF
maxx = 0
miny = 0x7FFFFFFF
maxy = 0
n = int(input())
for i in range(1, n + 1):
    x, y = map(lambda x: int(x), input().split())
    minx = min(minx, x + y)
    maxx = max(maxx, x + y)
    miny = min(miny, x - y)
    maxy = max(maxy, x - y)
print(max(maxx - minx, maxy - miny))

There is also a second method: transform Manhattan distance into Chebyshev distance, as discussed later.

Chebyshev Distance

Definition

Chebyshev distance is a metric on a vector space in which the distance between two points is defined as the maximum absolute difference among their coordinates.1

In two-dimensional space, the Chebyshev distance between two points is the maximum of the absolute differences of their x- and y-coordinates. Given points A(x1,y1),B(x2,y2)A(x_1,y_1),B(x_2,y_2), the Chebyshev distance between A,BA,B is:

d(A,B)=max⁡(∣x1−x2∣,∣y1−y2∣)d(A,B) = \max(|x_1 - x_2|, |y_1 - y_2|)

The Chebyshev-distance formula in nn-dimensional space is:

d(x,y)=max⁡{∣x1−y1∣,∣x2−y2∣,⋅⋅⋅,∣xn−yn∣}=max⁡{∣xi−yi∣}(i∈[1,n])\begin{aligned} d(x,y) &= \max\begin{Bmatrix} |x_1 - y_1|,|x_2 - y_2|,\cdot \cdot \cdot,|x_n - y_n|\end{Bmatrix} \\ &= \max\begin{Bmatrix} |x_i - y_i|\end{Bmatrix}(i \in [1, n])\end{aligned}

Explanation

Again using the same example, the coordinates of A,BA,B in the figure below are A(25,20),B(10,10)A(25,20),B(10,10).

Chebyshev-dis
Chebyshev-dis
d(A,B)=max⁡(∣20−10∣,∣25−10∣)=max⁡(10,15)=15d(A,B) = \max(|20 - 10|, |25 - 10|) = \max(10, 15) = 15

Converting Between Manhattan and Chebyshev Distance

Procedure

First, consider plotting all points in the Cartesian coordinate system whose Manhattan distance from the origin is 11.

The formula gives the equation ∣x∣+∣y∣=1|x| + |y| = 1.

Expanding the absolute values gives 44 linear functions:

y=−x+1(x≥0,y≥0)y=x+1(x≤0,y≥0)y=x−1(x≥0,y≤0)y=−x−1(x≤0,y≤0)\begin{aligned} &y = -x + 1 &(x \geq 0, y \geq 0) \\ &y = x + 1 &(x \leq 0, y \geq 0) \\ &y = x - 1 &(x \geq 0, y \leq 0) \\ &y = -x - 1 &(x \leq 0, y \leq 0) \\ \end{aligned}

Plotting these 44 functions in the Cartesian coordinate system gives a square with side length 2\sqrt{2}, as shown below:

dis-diff-square-1
dis-diff-square-1

Every point on the boundary of the square has Manhattan distance 11 from the origin.

Similarly, consider plotting all points in the Cartesian coordinate system whose Chebyshev distance from the origin is 11.

The formula gives max⁡(∣x∣,∣y∣)=1\max(|x|,|y|)=1.

Expanding the expression likewise gives 44 line segments:

y=1(−1≤x≤1)y=−1(−1≤x≤1)x=1,(−1≤y≤1)x=−1,(−1≤y≤1)\begin{aligned} &y = 1&(-1\leq x \leq 1) \\ &y = -1&(-1\leq x \leq 1) \\ &x = 1,&(-1\leq y \leq 1) \\ &x = -1,&(-1\leq y \leq 1) \\ \end{aligned}

Plotting them in the Cartesian coordinate system gives a square with side length 22, as shown below:

dis-diff-square-2
dis-diff-square-2

Every point on the boundary of the square has Chebyshev distance 11 from the origin.

Comparing the two figures, we make the surprising observation:

The 22 squares are similar figures.

Proof

So, could there be a relationship between Manhattan distance and Chebyshev distance?

Let us give a brief proof:

Assume A(x1,y1),B(x2,y2)A(x_1,y_1),B(x_2,y_2).

Expanding the absolute values in the Manhattan distance gives four values. The largest of these four values is the sum of two nonnegative numbers, namely the Manhattan distance. Thus, the Manhattan distance between A,BA,B is:

d(A,B)=∣x1−x2∣+∣y1−y2∣=max⁡{x1−x2+y1−y2,x1−x2+y2−y1,x2−x1+y1−y2,x2−x1+y2−y1}=max⁡(∣(x1+y1)−(x2+y2)∣,∣(x1−y1)−(x2−y2)∣)\begin{aligned} d(A,B)&=|x_1 - x_2| + |y_1 - y_2|\\ &=\max\begin{Bmatrix} x_1 - x_2 + y_1 - y_2, x_1 - x_2 + y_2 - y_1,x_2 - x_1 + y_1 - y_2, x_2 - x_1 + y_2 - y_1\end{Bmatrix}\\ &= \max(|(x_1 + y_1) - (x_2 + y_2)|, |(x_1 - y_1) - (x_2 - y_2)|) \end{aligned}

It is easy to see that this is the Chebyshev distance between the points (x1+y1,x1−y1),(x2+y2,x2−y2)(x_1 + y_1,x_1 - y_1), (x_2 + y_2,x_2 - y_2).

Therefore, transforming every point (x,y)(x,y) into (x+y,x−y)(x + y, x - y) makes the Chebyshev distance in the new coordinate system equal to the Manhattan distance in the original coordinate system.

Similarly, the Chebyshev distance between A,BA,B is:

d(A,B)=max⁡{∣x1−x2∣,∣y1−y2∣}=max⁡{∣x1+y12−x2+y22∣+∣x1−y12−x2−y22∣}\begin{aligned} d(A,B)&=\max\begin{Bmatrix} |x_1 - x_2|,|y_1 - y_2|\end{Bmatrix}\\ &=\max\begin{Bmatrix} \left|\dfrac{x_1 + y_1}{2}-\dfrac{x_2 + y_2}{2}\right|+\left|\dfrac{x_1 - y_1}{2}-\dfrac{x_2 - y_2}{2}\right|\end{Bmatrix} \end{aligned}

This is the Manhattan distance between the points (x1+y12,x1−y12),(x2+y22,x2−y22)(\dfrac{x_1 + y_1}{2},\dfrac{x_1 - y_1}{2}), (\dfrac{x_2 + y_2}{2},\dfrac{x_2 - y_2}{2}).

Therefore, transforming every point (x,y)(x,y) into (x+y2,x−y2)(\dfrac{x + y}{2},\dfrac{x - y}{2}) makes the Manhattan distance in the new coordinate system equal to the Chebyshev distance in the original coordinate system.

Conclusion

  • The Manhattan coordinate system is obtained by rotating the Chebyshev coordinate system by 45∘45^\circ and then scaling it to half its original size.
  • After transforming a point (x,y)(x,y) to (x+y,x−y)(x + y, x - y), the Manhattan distance in the original coordinate system equals the Chebyshev distance in the new coordinate system.
  • After transforming a point (x,y)(x,y) to (x+y2,x−y2)(\dfrac{x + y}{2},\dfrac{x - y}{2}), the Chebyshev distance in the original coordinate system equals the Manhattan distance in the new coordinate system.

When a problem asks for Chebyshev or Manhattan distance, we can often transform one into the other. The two distances have different advantages and disadvantages in different problems, so they should be used flexibly.

Examples

P4648 “IOI2007” Pairs of Animals (Manhattan distance to Chebyshev distance)

P3964 “TJOI2013” Squirrel Gathering (Chebyshev distance to Manhattan distance)

Here is a second solution to P5098 “USACO04OPEN” Cave Cows 3:

We transform the Manhattan distance requested by the problem into Chebyshev distance, changing each point's coordinates (x,y)(x,y) to (x+y,x−y)(x + y, x - y).

The answer becomes max⁡i,j∈n{max⁡{∣xi−xj∣,∣yi−yj∣}}\max\limits_{i,j\in n}\begin{Bmatrix} \max\begin{Bmatrix} |x_i - x_j|,|y_i - y_j|\end{Bmatrix}\end{Bmatrix}.

To maximize the differences between the x-coordinates and between the y-coordinates, it is enough to precompute the maximum and minimum values of x,yx,y.

Reference code

C++

#include <algorithm>
#include <cstdio>
using namespace std;

int main() {
  int n, x, y, a, b, minx = 0x7fffffff, maxx = 0, miny = 0x7fffffff, maxy = 0;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d%d", &a, &b);
    x = a + b, y = a - b;
    minx = min(minx, x), maxx = max(maxx, x);
    miny = min(miny, y), maxy = max(maxy, y);
  }
  printf("%d\n", max(maxx - minx, maxy - miny));
  return 0;
}

Python

minx = 0x7FFFFFFF
maxx = 0
miny = 0x7FFFFFFF
maxy = 0
n = int(input())
for i in range(1, n + 1):
    a, b = map(lambda x: int(x), input().split())
    x = a + b
    y = a - b
    minx = min(minx, x)
    maxx = max(maxx, x)
    miny = min(miny, y)
    maxy = max(maxy, y)
print(max(maxx - minx, maxy - miny))

Comparing the two pieces of code, we can see that code produced by these two different approaches is completely equivalent. Amazing, isn't it? More advanced topics require further study.

Minkowski Distance

We define, in nn-dimensional space, two points X(x1,x2,…,xn)X(x_1, x_2, \dots, x_n) and Y(y1,y2,…,yn)Y(y_1, y_2, \dots, y_n), and their Minkowski distance as:

D(X,Y)=(∑i=1n∣xi−yi∣p)1p.D(X, Y) = \left(\sum_{i=1}^n \left\vert x_i - y_i \right\vert ^p\right)^{\frac{1}{p}}.

In particular:

  1. When p=1p=1, D(X,Y)=∑i=1n∣xi−yi∣D(X, Y) = \sum_{i=1}^n \left\vert x_i - y_i \right\vert is the Manhattan distance;
  2. When p=2p=2, D(X,Y)=(∑i=1n(xi−yi)2)1/2D(X, Y) = \left(\sum_{i=1}^n (x_i - y_i)^2\right)^{1/2} is the Euclidean distance;
  3. When p→∞p \to \infty, D(X,Y)=lim⁡p→∞(∑i=1n∣xi−yi∣p)1/p=max⁡i=1n∣xi−yi∣D(X, Y) = \lim_{p \to \infty}\left(\sum_{i=1}^n \left\vert x_i - y_i \right\vert ^p\right) ^{1/p} = \max\limits_{i=1}^n \left\vert x_i - y_i \right\vert is the Chebyshev distance.

Note that Minkowski distance is a metric only when p≥1p \ge 1; see Minkowski distance - Wikipedia for a proof.

  1. A Brief Discussion of Three Common Distance Algorithms, with thanks to the author xuxing for permission.

Footnotes

  1. Chebyshev distance - Wikipedia, the free encyclopedia ↩

This content comes from OI Wiki: Distance. See the original for authors and revision history. AIOJ has adapted the formatting, in-site links, and resource paths.

Content is shared under CC BY-SA 4.0 and SATA license; additional terms are governed by the original notice. Thanks to the OI Wiki authors and contributors; per the SATA terms, please visit the OI Wiki repository and star it to show your support.