You signed in with another tab or window.Reload to refresh your session.You signed out in another tab or window.Reload to refresh your session.You switched accounts on another tab or window.Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/geometry/manhattan-distance.md
+7-7Lines changed: 7 additions & 7 deletions
Original file line number
Diff line number
Diff line change
@@ -8,7 +8,7 @@ tags:
8
8
##Definition
9
9
For points $p$ and $q$ on a plane, we can define the distance between them as the sum of the differences between their $x$ and $y$ coordinates:
10
10
11
-
$$d(p,q) = |p.x -q.x| + |p.y -q.y|$$
11
+
$$d(p,q) = |x_p -x_q| + |y_p -y_q|$$
12
12
13
13
Defined this way, the distance corresponds to the so-called[Manhattan (taxicab) geometry](https://en.wikipedia.org/wiki/Taxicab_geometry), in which the points are considered intersections in a well designed city, like Manhattan, where you can only move on the streets horizontally or vertically, as shown in the image below:
14
14
@@ -20,19 +20,19 @@ There are some interesting tricks and algorithms that can be done with this dist
20
20
21
21
##Farthest pair of points in Manhattan distance
22
22
23
-
Given $n$ points $P$, we want to find the pair of points $p,q$ that are farther apart, that is, maximize $|p.x -q.x| + |p.y -q.y|$.
23
+
Given $n$ points $P$, we want to find the pair of points $p,q$ that are farther apart, that is, maximize $|x_p -x_q| + |y_p -y_q|$.
24
24
25
-
Let's think first in one dimension, so $y=0$. The main observation is that we can bruteforce if $|p.x -q.x|$ is equal to $p.x -q.x$ or $-p.x +q.x$, because if we "miss the sign" of the absolute value, we will get only a smaller value, so it can't affect the answer. More formally, it holds that:
25
+
Let's think first in one dimension, so $y=0$. The main observation is that we can bruteforce if $|x_p -x_q|$ is equal to $x_p -x_q$ or $-x_p +x_q$, because if we "miss the sign" of the absolute value, we will get only a smaller value, so it can't affect the answer. More formally, it holds that:
26
26
27
-
$$|p.x -q.x| = \max(p.x -q.x, -p.x +q.x)$$
27
+
$$|x_p -x_q| = \max(x_p -x_q, -x_p +x_q)$$
28
28
29
-
So, for example, we can try to have $p$ such that $p.x$ has the plus sign, and then $q$ must have the negative sign. This way we want to find:
29
+
So, for example, we can try to have $p$ such that $x_p$ has the plus sign, and then $q$ must have the negative sign. This way we want to find:
Notice that we can extend this idea further for 2 (or more!) dimensions. For $d$ dimensions, we must bruteforce $2^d$ possible values of the signs. For example, if we are in $2$ dimensions and bruteforce that $p$ has both the plus signs we want to find: