How to calculate manhattan distance
Introduction:
Manhattan distance, also known as city block distance or L1 distance, is a simple yet effective metric used for calculating the distance between two points in a real-world grid-like path. It’s named after the Manhattan borough of New York City, which has a grid layout. In this article, we will walk you through the process of calculating Manhattan distance step by step.
What is Manhattan Distance?
Manhattan distance is a way of measuring the distance between two points on a grid by moving only along the horizontal and vertical grid lines—think of moving like a car in a city, restricted by streets aligned north-south and east-west. The concept applies well to various computational problems, especially in fields such as robotics, computer vision, and pathfinding algorithms.
Calculating Manhattan Distance:
Before we move onto the actual calculation process, let’s refresh our memories on some crucial terminology.
1. Point: A point on a grid is represented by its Cartesian coordinates (x, y).
2. Axes: The grid system has two axes: x-axis (horizontal) and y-axis (vertical).
3. Distance: The total number of units moved along both axes to reach from one point to another.
Given two points A(x1, y1) and B(x2, y2), the Manhattan distance formula is as follows:
Manhattan Distance = |x2 – x1| + |y2 – y1|
Here’s a step-by-step guide for calculating the Manhattan distance:
Step 1: Determine the coordinates of both points in the format (x, y). For example, Point A (4, 3) and Point B (8, 9).
Step 2: Calculate the difference in their x-coordinates (x2 – x1):
8 – 4 = 4.
Step 3: Calculate the difference in their y-coordinates (y2 – y1):
9 – 3 = 6.
Step 4: Add the absolute values of these differences:
|4| + |6| = 10.
Hence, the Manhattan distance between Point A and Point B is 10 units.
Example Problem:
Let’s work through an example problem to help solidify your understanding of calculating Manhattan distance.
Assume you are given two grid coordinates for points A and B as follows:
Point A: (3, 7)
Point B: (14, 2)
Using the formula mentioned above (|x2 – x1| + |y2 – y1|):
Manhattan Distance = |14 – 3| + |2 – 7|
= |11| + |-5|
= 11 + 5
= 16
So, the Manhattan distance between point A and point B is equal to 16 units.
Conclusion:
Calculating Manhattan distance is straightforward once you know the formula. It’s a powerful technique with countless applications in fields like robotics and artificial intelligence. With this newfound skill under your belt, you can now confidently tackle related tasks!