Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitb319183

Browse files
authored
Merge pull request#1351 from izanbf1803/master
Manhattan Distance - Notation consistency: p.x -> x_p
2 parentsdef19fc +c87425c commitb319183

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

‎src/geometry/manhattan-distance.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ tags:
88
##Definition
99
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:
1010

11-
$$d(p,q) = |p.x -q.x| + |p.y -q.y|$$
11+
$$d(p,q) = |x_p -x_q| + |y_p -y_q|$$
1212

1313
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:
1414

@@ -20,19 +20,19 @@ There are some interesting tricks and algorithms that can be done with this dist
2020

2121
##Farthest pair of points in Manhattan distance
2222

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|$.
2424

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:
2626

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)$$
2828

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:
3030

31-
$$\max\limits_{p, q \in P}(p.x + (-q.x)) = \max\limits_{p \in P}(p.x) + \max\limits_{q \in P}( -q.x ).$$
31+
$$\max\limits_{p, q \in P}(x_p + (-x_q)) = \max\limits_{p \in P}(x_p) + \max\limits_{q \in P}( -x_q ).$$
3232

3333
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:
3434

35-
$$\max\limits_{p, q \in P} [(p.x + (-q.x)) + (p.y + (-q.y))] = \max\limits_{p \in P}(p.x +p.y) + \max\limits_{q \in P}(-q.x -q.y).$$
35+
$$\max\limits_{p, q \in P} [(x_p + (-x_q)) + (y_p + (-y_q))] = \max\limits_{p \in P}(x_p +y_p) + \max\limits_{q \in P}(-x_q -y_q).$$
3636

3737
As we made $p$ and $q$ independent, it is now easy to find the $p$ and $q$ that maximize the expression.
3838

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp