Finding the distance between two sets in Manhattan

2019-04-02 16:51发布

问题:

I'm learning for the test from algorithms and I spotted problem that I cannot deal with for a few days. So I'm writing here for help.

For a given two disjoint sets on plane:

G={(x_1^G, y_1^G), (x_2^G, y_2^G), ..., (x_n^G, y_n^G)}
D={(x_1^D, y_1^D), (x_2^D, y_2^D), ..., (x_n^D, y_n^D)}

Where for every 1 <= i, j <= n we have y_i^D < y_j^G, so G is above D.

Find an effective algorithm that counts the distance between them defined as:

d(G,D) = min{ d(a,b): a \in G and b\in D }, 
where d(a,b) = |x_a - x_b| + |y_a - y_b|

O(n^2) is trivial, so it is not the answer.

I hope the solution isn't too hard since it is from materials to review before the test. Can anybody help?

I think it will appear that this is a special case of some common problem. But if it is a special case, maybe the solution can be easier?

回答1:

There are a few different ways to do this in O(n log n) time.

One: Compute the manhattan distance Voronoi diagram of the G points and build a point location data structure based on that. This takes O(n log n) time. For each D point, find the closest G point using the point location data structure. This takes O(log n) time per D point. Take the min of the distances between the pairs you just found and that's your answer.

Two: You can adapt Fortune's algorithm to this problem; just keep separate binary trees for D and G points. Kind of annoying to describe.

The next idea computes the distance of the closest pair for the infinity-norm, which is max(|x1-x2|, |y1-y2|). You can tilt your problem 45 degrees (substituting u = x-y, v = x+y) to get it into the appropriate form.

Three (variant of two): Sort all of the points by y coordinate. Maintain d, the distance between the closest pair seen so far. We'll sweep a line from top to bottom, maintaining two binary search trees, one of G points and one of D points. When a point is d or farther above the sweep line, we remove it from its binary search tree. When a point is first encountered by the sweep line, say a D point, we (1) check the G binary search tree to see if it has any elements whose x-coordinate is within d of the new point's, updating d as necessary, and (2) insert the new point into D's binary search tree. Each point only causes a constant number of binary search tree operations plus a constant amount of additional work, so the sweep is O(n log n). The sort is too, unsurprisingly, so our overall time complexity is as desired.

You can probably make a divide-and-conquer strategy work too based on similar ideas to three.



标签: algorithm set