# Manhattan Distance

May 20, 2023

In the field of **artificial intelligence (AI)** and **machine learning (ML)**, distance metrics play a crucial role in measuring the similarity or dissimilarity between two or more data points. **Manhattan distance**, also known as **city block distance** or **taxicab distance**, is one such distance metric that is commonly used in various applications such as image recognition, clustering, and recommendation systems. In this article, we will explore what Manhattan distance is, how it works, and its applications in AI and ML.

## Definition and Calculation

Manhattan distance is a distance metric that measures the distance between two points in a grid-like system such as a city block. It is computed as the sum of the absolute differences of their coordinates. The formula for calculating Manhattan distance between two points `(x1, y1)`

and `(x2, y2)`

is:

```
d = |x1 - x2| + |y1 - y2|
```

Here, `|x1 - x2|`

and `|y1 - y2|`

represent the absolute differences between the corresponding coordinates of the two points. The result `d`

represents the Manhattan distance between the two points.

## Example

Let’s consider an example to understand how Manhattan distance works. Suppose we have two points `A`

and `B`

in a two-dimensional grid, as shown below:

```
| | | | B |
___|___|___|___|___|
| | | | |
___|___|___|___|___|
| | A | | |
___|___|___|___|___|
| | | | |
___|___|___|___|___|
| | | | |
| | | | |
```

The coordinates of point `A`

are `(2, 3)`

and the coordinates of point `B`

are `(4, 1)`

. To calculate the Manhattan distance between these two points, we use the formula:

```
d = |2 - 4| + |3 - 1|
= 2 + 2
= 4
```

Hence, the Manhattan distance between points `A`

and `B`

is `4`

.

## Properties

Manhattan distance has some interesting properties that make it useful in various applications. Some of these properties are:

**Non-negative**: Manhattan distance is always non-negative. It is equal to`0`

only when the two points are at the same location.**Symmetric**: The distance between point`A`

and point`B`

is the same as the distance between point`B`

and point`A`

.**Triangle inequality**: The distance between point`A`

and point`C`

is less than or equal to the sum of the distances between point`A`

and point`B`

and between point`B`

and point`C`

. In other words, the shortest distance between two points is a straight line.

## Applications

Manhattan distance has various applications in AI and ML. Some of these applications are:

### Image Recognition

In image recognition, Manhattan distance can be used to compare two images and determine their similarity. Each image is represented as a vector of pixel values. The Manhattan distance between the two vectors can be calculated to determine how similar or dissimilar the images are.

### Clustering

Clustering is a process of grouping similar data points together. Manhattan distance can be used as a distance metric in clustering algorithms such as K-means. In K-means, the algorithm tries to minimize the sum of the distances between each data point and its assigned centroid. Manhattan distance can be used to calculate these distances.

### Recommendation Systems

Recommendation systems use machine learning algorithms to recommend items to users based on their past behavior. Manhattan distance can be used as a distance metric to compare the preferences of different users. The closer the preferences of two users are, the more likely they are to like the same items.