Ora

What is the Manhattan Distance on a Square Grid?

Published in Grid Distance 4 mins read

The Manhattan distance, often referred to as taxicab geometry or L1 distance, on a square grid is the sum of the vertical and horizontal distances between two points. It represents the distance a "taxicab" would travel on a city grid, moving only along horizontal and vertical streets.

Understanding Manhattan Distance

Imagine navigating a city laid out in a perfect grid, like Manhattan Island in New York City. To get from one intersection to another, you can only travel along the streets (horizontally or vertically), not diagonally through buildings. The Manhattan distance calculates the shortest path you would take under these constraints.

Unlike the Euclidean distance (which is the straight-line distance, like a bird flying directly between two points), Manhattan distance always follows the grid lines. This makes it particularly relevant in environments where diagonal movement is restricted or costs more, such as in many computer graphics, game development, and urban planning scenarios.

How to Calculate Manhattan Distance

Calculating the Manhattan distance is straightforward. For any two points on a grid, say Point A with coordinates $(x_1, y_1)$ and Point B with coordinates $(x_2, y_2)$, the distance is found by summing the absolute differences of their respective x-coordinates and y-coordinates.

The Formula

The formula for Manhattan distance $D$ between two points $(x_1, y_1)$ and $(x_2, y_2)$ is:

$D = |x_1 - x_2| + |y_1 - y_2|$

Here, the vertical lines | | denote the absolute value, meaning the result is always a non-negative number regardless of the order of subtraction.

Practical Example

Let's calculate the Manhattan distance between two points:

  • Point P1: $(1, 2)$
  • Point P2: $(4, 6)$

Steps:

  1. Calculate the horizontal distance:
    $|x_1 - x_2| = |1 - 4| = |-3| = 3$
  2. Calculate the vertical distance:
    $|y_1 - y_2| = |2 - 6| = |-4| = 4$
  3. Sum the distances:
    $D = 3 + 4 = 7$

So, the Manhattan distance between Point P1 and Point P2 is 7 units. This means you would travel 3 units horizontally and 4 units vertically to get from P1 to P2, summing to 7 units of total travel.

Key Characteristics and Properties

The Manhattan distance possesses several important properties:

  • Always Non-Negative: The distance is always zero or a positive value. It is zero only if the two points are identical.
  • Symmetry: The distance from Point A to Point B is the same as the distance from Point B to Point A ($D(A,B) = D(B,A)$).
  • Triangle Inequality: The distance between two points is always less than or equal to the sum of the distances via a third intermediate point ($D(A,C) \le D(A,B) + D(B,C)$).
  • Grid Dependency: It inherently reflects movement constraints on a grid, making it ideal for pathfinding in environments where diagonal moves are not allowed or have different costs.

Applications of Manhattan Distance

This distance metric is widely used in various fields due to its relevance in grid-based systems:

  • Pathfinding Algorithms: It serves as an effective heuristic in algorithms like the A* search algorithm for finding the shortest path on a grid, especially in games or robotics.
  • Computer Vision and Image Processing: Used for feature matching, comparing image pixels, or calculating distances in pixel grids.
  • Data Science and Machine Learning: Employed in clustering and classification algorithms, particularly when dealing with data that can be interpreted in a grid-like structure or where features are discrete.
  • Logistics and Urban Planning: Useful for calculating travel times or distances for vehicles in city layouts, optimizing delivery routes.
  • Game Development: Determines character movement, attack ranges, or object placement on a tile-based map.

Manhattan Distance vs. Euclidean Distance

While both are distance metrics, their applications and conceptual bases differ significantly:

Feature Manhattan Distance Euclidean Distance
Movement Restricted to horizontal and vertical paths Straight-line path (as the crow flies)
Formula $ x_1 - x_2
Environment Grids, city blocks, discrete spaces Continuous spaces, open fields
Analogy Driving a car on city streets Flying a bird between points
Result Generally larger than Euclidean distance for non-collinear points Shortest possible distance

The choice between Manhattan and Euclidean distance depends entirely on the nature of the problem and the environment being modeled.