在飞机上的红色蓝点,如何找到不同颜色的两个最近点

时间:2011-09-18 15:53:24

标签: algorithm

当点只有相同的颜色时,我已经解决了问题,并且将解决方案与使用分而治之的cormen相匹配。这是算法

1. divide the plane into two halves.
2. solve the problem recursivley for both halves.
3. for points which could have lesser distance and belong to different halves do the following:
    a. get the minimum distance in points pair call it delta1 and delta 2.
    b. find the minimum of these two call it delta
    c. from the central line build a strip of delta size in both direction and compare,
       now only the points in this strip actually needs to be considered. Since these are the only points which could have less distance than delta.
    d.sort the points across y axis in this strip.
    e. start scanning from the top creating a box of size delta by 2*delta, around every point that occurs.
    f. Now in this size box you can only fit 7 or 8 points, otherwise it would points would become closer than delta, and it would break the constraint.
    g. So there are only a constant number of points to be searched in this box.
    h. number of boxes we have to create depends on number of points. So this whole scanning in the strip would take n*O(1) = O(n) time.
4. So the recursion now is = T(n) = 2*T(n/2) + O(n), solving it gives us O(n lg n) complexity.

现在我的问题是如何将其转换为两个色点。任何建议??

注意:我的不好,我实际上需要找到两种不同颜色的点。

2 个答案:

答案 0 :(得分:0)

运行您当前的算法两次,一次用于蓝点,一次用于红点。这将产生两个点对,红色和蓝色。选择距离较小的点对。

答案 1 :(得分:0)

如果将同一对之间的距离设置为无穷大并运行相同的算法,则可能会有效。如果我错了,复杂性不应该改变,纠正我