Post · 9 min read· pp. 551–558

Manhattan Distance

Definition

For points pp and qq on a plane, we can define the distance between them as the sum of the differences between their xx and yy coordinates:

d(p,q)=∣xp−xq∣+∣yp−yq∣d(p,q) = |x_p - x_q| + |y_p - y_q|

Defined this way, the distance corresponds to the so-called Manhattan (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:

Manhattan Distance

This images show some of the smallest paths from one black point to the other, all of them with length 1212.

There are some interesting tricks and algorithms that can be done with this distance, and we will show some of them here.

Farthest pair of points in Manhattan distance

Given nn points PP, we want to find the pair of points p,qp,q that are farther apart, that is, maximize ∣xp−xq∣+∣yp−yq∣|x_p - x_q| + |y_p - y_q|.

Let's think first in one dimension, so y=0y=0. The main observation is that we can bruteforce if ∣xp−xq∣|x_p - x_q| is equal to xp−xqx_p - x_q or −xp+xq-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:

∣xp−xq∣=max⁡(xp−xq,−xp+xq)|x_p - x_q| = \max(x_p - x_q, -x_p + x_q)

So, for example, we can try to have pp such that xpx_p has the plus sign, and then qq must have the negative sign. This way we want to find:

max⁡p,q∈P(xp+(−xq))=max⁡p∈P(xp)+max⁡q∈P(−xq).\max\limits_{p, q \in P}(x_p + (-x_q)) = \max\limits_{p \in P}(x_p) + \max\limits_{q \in P}( - x_q ).

Notice that we can extend this idea further for 2 (or more!) dimensions. For dd dimensions, we must bruteforce 2d2^d possible values of the signs. For example, if we are in 22 dimensions and bruteforce that pp has both the plus signs we want to find:

max⁡p,q∈P[(xp+(−xq))+(yp+(−yq))]=max⁡p∈P(xp+yp)+max⁡q∈P(−xq−yq).\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).

As we made pp and qq independent, it is now easy to find the pp and qq that maximize the expression.

The code below generalizes this to dd dimensions and runs in O(n⋅2d⋅d)O(n \cdot 2^d \cdot d).

text
long long ans = 0;
for (int msk = 0; msk < (1 << d); msk++) {
    long long mx = LLONG_MIN, mn = LLONG_MAX;
    for (int i = 0; i < n; i++) {
        long long cur = 0;
        for (int j = 0; j < d; j++) {
            if (msk & (1 << j)) cur += p[i][j];
            else cur -= p[i][j];
        }
        mx = max(mx, cur);
        mn = min(mn, cur);
    }
    ans = max(ans, mx - mn);
}

Rotating the points and Chebyshev distance

It's well known that, for all m,n∈Rm, n \in \mathbb{R},

∣m∣+∣n∣=max(∣m+n∣,∣m−n∣).|m| + |n| = \text{max}(|m + n|, |m - n|).

To prove this, we just need to analyze the signs of mm and nn. And it's left as an exercise.

We may apply this equation to the Manhattan distance formula to find out that

d((x1,y1),(x2,y2))=∣x1−x2∣+∣y1−y2∣=max(∣(x1+y1)−(x2+y2)∣,∣(y1−x1)−(y2−x2)∣).d((x_1, y_1), (x_2, y_2)) = |x_1 - x_2| + |y_1 - y_2| = \text{max}(|(x_1 + y_1) - (x_2 + y_2)|, |(y_1 - x_1) - (y_2 - x_2)|).

The last expression in the previous equation is the Chebyshev distance of the points (x1+y1,y1−x1)(x_1 + y_1, y_1 - x_1) and (x2+y2,y2−x2)(x_2 + y_2, y_2 - x_2). This means that, after applying the transformation

α:(x,y)→(x+y,y−x),\alpha : (x, y) \to (x + y, y - x),

the Manhattan distance between the points pp and qq turns into the Chebyshev distance between α(p)\alpha(p) and α(q)\alpha(q).

Also, we may realize that α\alpha is a spiral similarity (rotation of the plane followed by a dilation about a center OO) with center (0,0)(0, 0), rotation angle of 45∘45^{\circ} in clockwise direction and dilation by 2\sqrt{2}.

Here's an image to help visualizing the transformation:

Chebyshev transformation

Manhattan Minimum Spanning Tree

The Manhattan MST problem consists of, given some points in the plane, find the edges that connect all the points and have a minimum total sum of weights. The weight of an edge that connects two points is their Manhattan distance. For simplicity, we assume that all points have different locations. Here we show a way of finding the MST in O(nlog⁡n)O(n \log{n}) by finding for each point its nearest neighbor in each octant, as represented by the image below. This will give us O(n)O(n) candidate edges, which, as we show below, will guarantee that they contain the MST. The final step is then using some standard MST, for example, Kruskal algorithm using disjoint set union.

8 octants picture *The 8 octants relative to a point S*

The algorithm shown here was first presented in a paper from H. Zhou, N. Shenoy, and W. Nichollos (2002). There is also another know algorithm that uses a Divide and conquer approach by J. Stolfi, which is also very interesting and only differ in the way they find the nearest neighbor in each octant. They both have the same complexity, but the one presented here is easier to implement and has a lower constant factor.

First, let's understand why it is enough to consider only the nearest neighbor in each octant. The idea is to show that for a point ss and any two other points pp and qq in the same octant, d(p,q)<max⁡(d(s,p),d(s,q))d(p, q) < \max(d(s, p), d(s, q)). This is important, because it shows that if there was a MST where ss is connected to both pp and qq, we could erase one of these edges and add the edge (p,q)(p,q), which would decrease the total cost. To prove this, we assume without loss of generality that pp and qq are in the octanct R1R_1, which is defined by: xs≤xx_s \leq x and xs−ys>x−yx_s - y_s > x - y, and then do some casework. The image below give some intuition on why this is true.

unique nearest neighbor *Intuitively, the limitation of the octant makes it impossible that pp and qq are both closer to ss than to each other*

Therefore, the main question is how to find the nearest neighbor in each octant for every single of the nn points.

Nearest Neighbor in each Octant in O(n log n)

For simplicity we focus on the NNE octant (R1R_1 in the image above). All other directions can be found with the same algorithm by rotating the input.

We will use a sweep-line approach. We process the points from south-west to north-east, that is, by non-decreasing x+yx + y. We also keep a set of points which don't have their nearest neighbor yet, which we call "active set". We add the images below to help visualize the algorithm.

manhattan-mst-sweep *In black with an arrow you can see the direction of the line-sweep. All the points below this lines are in the active set, and the points above are still not processed. In green we see the points which are in the octant of the processed point. In red the points that are not in the searched octant.*

manhattan-mst-sweep *In this image we see the active set after processing the point pp. Note that the $2$ green points of the previous image had pp in its north-north-east octant and are not in the active set anymore, because they already found their nearest neighbor.*

When we add a new point point pp, for every point ss that has it in its octant we can safely assign pp as the nearest neighbor. This is true because their distance is d(p,s)=∣xp−xs∣+∣yp−ys∣=(xp+yp)−(xs+ys)d(p,s) = |x_p - x_s| + |y_p - y_s| = (x_p + y_p) - (x_s + y_s), because pp is in the north-north-east octant. As all the next points will not have a smaller value of x+yx + y because of the sorting step, pp is guaranteed to have the smaller distance. We can then remove all such points from the active set, and finally add pp to the active set.

The next question is how to efficiently find which points ss have pp in the north-north-east octant. That is, which points ss satisfy:

  • xs≤xpx_s \leq x_p
  • xp−yp<xs−ysx_p - y_p < x_s - y_s

Because no points in the active set are in the R1R_1 region of another, we also have that for two points q1q_1 and q2q_2 in the active set, xq1≠xq2x_{q_1} \neq x_{q_2} and their ordering implies xq1<xq2  ⟹  xq1−yq1≤xq2−yq2x_{q_1} < x_{q_2} \implies x_{q_1} - y_{q_1} \leq x_{q_2} - y_{q_2}.

You can try to visualize this on the images above by thinking of the ordering of x−yx - y as a "sweep-line" that goes from the north-west to the south-east, so perpendicular to the one that is drawn.

This means that if we keep the active set ordered by xx the candidates ss are consecutively placed. We can then find the largest xs≤xpx_s \leq x_p and process the points in decreasing order of xx until the second condition xp−yp<xs−ysx_p - y_p < x_s - y_s breaks (we can actually allow that xp−yp=xs−ysx_p - y_p = x_s - y_s and that deals with the case of points with equal coordinates). Notice that because we remove from the set right after processing, this will have an amortized complexity of O(nlog⁡(n))O(n \log(n)). Now that we have the nearest point in the north-east direction, we rotate the points and repeat. It is possible to show that actually we also find this way the nearest point in the south-west direction, so we can repeat only 4 times, instead of 8.

In summary we:

  • Sort the points by x+yx + y in non-decreasing order;
  • For every point, we iterate over the active set starting with the point with the largest xx such that x≤xpx \leq x_p, and we break the loop if xp−yp≥xs−ysx_p - y_p \geq x_s - y_s. For every valid point ss we add the edge (s,p,d(s,p))(s,p, d(s,p)) in our list;
  • We add the point pp to the active set;
  • Rotate the points and repeat until we iterate over all the octants.
  • Apply Kruskal algorithm in the list of edges to get the MST.

Below you can find a implementation, based on the one from KACTL.

text
struct point {
    long long x, y;
};

// Returns a list of edges in the format (weight, u, v). 
// Passing this list to Kruskal algorithm will give the Manhattan MST.
vector<tuple<long long, int, int>> manhattan_mst_edges(vector<point> ps) {
    vector<int> ids(ps.size());
    iota(ids.begin(), ids.end(), 0);
    vector<tuple<long long, int, int>> edges;
    for (int rot = 0; rot < 4; rot++) { // for every rotation
        sort(ids.begin(), ids.end(), [&](int i, int j){
            return (ps[i].x + ps[i].y) < (ps[j].x + ps[j].y);
        });
        map<int, int, greater<int>> active; // (xs, id)
        for (auto i : ids) {
            for (auto it = active.lower_bound(ps[i].x); it != active.end();
            active.erase(it++)) {
                int j = it->second;
                if (ps[i].x - ps[i].y > ps[j].x - ps[j].y) break;
                assert(ps[i].x >= ps[j].x && ps[i].y >= ps[j].y);
                edges.push_back({(ps[i].x - ps[j].x) + (ps[i].y - ps[j].y), i, j});
            }
            active[ps[i].x] = i;
        }
        for (auto &p : ps) { // rotate
            if (rot & 1) p.x *= -1;
            else swap(p.x, p.y);
        }
    }
    return edges;
}

Problems