Euclidean Distance
Two-Dimensional Space
Definition
Euclidean distance, also commonly called Euclidean distance, is defined as follows. In a Cartesian coordinate system, let points have coordinates respectively. Then the Euclidean distance between them is:
Explanation
For example, suppose that in a Cartesian coordinate system there are two points . Using the formula, we can easily obtain the Euclidean distance between :
In addition, the Euclidean distance from to the origin can be written as:
n-Dimensional Space
Introduction
What is the Euclidean-distance formula for two points in three-dimensional space? Let us examine the figure below.

It is easy to see that in , ; in , .
Definition
It follows that the Euclidean-distance formula in three-dimensional space is:
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 -dimensional space: for , we have
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 , the Manhattan distance between is:
Explanation
Observe the figure below:

Between , 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 in the figure below are .
Using the formula, we can easily obtain the Manhattan distance between :
By derivation, we obtain the formula for Manhattan distance in -dimensional space:
Properties
In addition to its formula, Manhattan distance has the following mathematical properties:
- Non-negativity: Manhattan distance is non-negative, that is, .
- Identity: the Manhattan distance from a point to itself is , that is, .
- Symmetry: the Manhattan distance from to equals that from to , that is, .
- Triangle inequality: the direct distance from point to is no greater than the distance through any other point , that is, .
Example
From the statement, for the expression , we may assume and split into two cases according to the sign of :
The answer can be obtained by finding the maximum and minimum values of .
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 , the Chebyshev distance between is:
The Chebyshev-distance formula in -dimensional space is:
Explanation
Again using the same example, the coordinates of in the figure below are .
Converting Between Manhattan and Chebyshev Distance
Procedure
First, consider plotting all points in the Cartesian coordinate system whose Manhattan distance from the origin is .
The formula gives the equation .
Expanding the absolute values gives linear functions:
Plotting these functions in the Cartesian coordinate system gives a square with side length , as shown below:
Every point on the boundary of the square has Manhattan distance from the origin.
Similarly, consider plotting all points in the Cartesian coordinate system whose Chebyshev distance from the origin is .
The formula gives .
Expanding the expression likewise gives line segments:
Plotting them in the Cartesian coordinate system gives a square with side length , as shown below:
Every point on the boundary of the square has Chebyshev distance from the origin.
Comparing the two figures, we make the surprising observation:
The squares are similar figures.
Proof
So, could there be a relationship between Manhattan distance and Chebyshev distance?
Let us give a brief proof:
Assume .
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 is:
It is easy to see that this is the Chebyshev distance between the points .
Therefore, transforming every point into makes the Chebyshev distance in the new coordinate system equal to the Manhattan distance in the original coordinate system.
Similarly, the Chebyshev distance between is:
This is the Manhattan distance between the points .
Therefore, transforming every point into 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 and then scaling it to half its original size.
- After transforming a point to , the Manhattan distance in the original coordinate system equals the Chebyshev distance in the new coordinate system.
- After transforming a point to , 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 to .
The answer becomes .
To maximize the differences between the x-coordinates and between the y-coordinates, it is enough to precompute the maximum and minimum values of .
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 -dimensional space, two points and , and their Minkowski distance as:
In particular:
- When , is the Manhattan distance;
- When , is the Euclidean distance;
- When , is the Chebyshev distance.
Note that Minkowski distance is a metric only when ; see Minkowski distance - Wikipedia for a proof.
References and Links
- A Brief Discussion of Three Common Distance Algorithms, with thanks to the author xuxing for permission.