K在2d平面中最近的邻居

时间:2016-12-29 20:06:28

标签: java algorithm heap priority-queue

问题陈述如下:

  

给定数组,在2D平面中找到距离原点最近的K点   包含N个点。输出必须是非递减顺序。

解决方案:我已使用比较器和优先级队列解决了这个问题,我的代码如下所示:

class Point {
    double x;
    double y;

    public Point(double x, double y) {
        this.x = x;
        this.y = y;
    }
}

public class KClosestPoints {
    public Point[] getKNearestPoints(Point[] points, int k) {
        if (k == 0 || points.length == 0) {
            return new Point[0];
        }
        Point[] rValue = new Point[k];
        int index = k - 1;
        if (points.length < k) {
            index = points.length - 1;
        }
        final Point org = new Point(0, 0);
        PriorityQueue<Point> pq = new PriorityQueue<Point>(k,
                new Comparator<Point>() {
                    @Override
                    public int compare(Point o1, Point o2) {
                    Double d2 = getDistance(o2, org);
                    Double d1 = getDistance(o1, org);
                    if (d2 > d1) {
                        return 1;
                    } else if (d2 < d1) {
                        return -1;
                    } else
                        return 0;
                }
                });
        for (int i = 0; i < points.length; i++) {
            pq.offer(points[i]);
            if (pq.size() > k) {
                pq.poll();
            }
        }
        while (!pq.isEmpty()) {
            rValue[index] = pq.poll();
            index--;
        }
        return rValue;
    }

    private static double getDistance(Point a, Point b) {
        return Math.sqrt(((a.x - b.x) * (a.x - b.x))
                + ((a.y - b.y) * (a.y - b.y)));
    }

我的代码适用于我使用的所有测试用例,除此之外:

test6[0] = new Point(Double.MIN_VALUE, Double.MAX_VALUE);
test6[1] = new Point(Double.MIN_VALUE, Double.MIN_VALUE);
test6[2] = new Point(Double.MAX_VALUE, Double.MAX_VALUE);
getKNearestPoints(test6, 2);

答案应该是test6 [0]和test6 [1],而这段代码给出了test6 [0]和test6 [2]的答案。帮助我找到问题。

编辑:后来我注意到它在所有测试用例中给出了错误的答案,当点为正数和负数时,k = 2,上面提到了一个这样的测试用例

1 个答案:

答案 0 :(得分:1)

对于您要问的问题,计算距离时出现问题:

Point # 0 = (0, 0)
Point # 1 = (Double.MIN_VALUE, Double.MAX_VALUE) -> (4.9E-324, 1.7E308)
Point # 2 = (Double.MIN_VALUE, Double.MIN_VALUE) -> (4.9E-324, 4.9E-324)
Point # 3 = (Double.MAX_VALUE, Double.MAX_VALUE) -> (1.7E308, 1.7E308)

注意: Double.MIN_VALUE是一个正数。

现在,在计算d = Math.sqrt(((a.x - b.x) * (a.x - b.x)) + ((a.y - b.y) * (a.y - b.y)));Infinity之间以及Point # 0Point # 1之间的距离时,欧氏距离Point # 0会返回Point # 3,因为:

第1点

(a.x - b.x) * (a.x - b.x) = (1.7E308 - 0) * (1.7E308 - 0) = 1.7E308 * 1.7E308 = Infinity
Math.sqrt(Infinity + Infinity) = Infinity;

获得Point # 1Point # 0Infinity)之后的距离,然后将其与Point # 3Point # 0的距离进行比较(同时{{ 1}})然后InfinityInfinity = Infinity,因此true表示“两个点都相等”,Comparator不按您的意愿排序。

对于使用PriorityQueue的操作,您不得使用Double.MAX_VALUE,而是使用Double

BigDecimal

为什么我们不计算平方根的实际距离?这是因为:

  • BigDecimal类没有这样的方法(如果你可以使用here)。
  • 因为如果private BigDecimal getDistance(Point a, Point b) { BigDecimal dx = BigDecimal.valueOf(a.x - b.x); BigDecimal dy = BigDecimal.valueOf(a.y - b.y); BigDecimal distance = dx.pow(2).add(dy.pow(2)); return distance; } 然后a > b,它的比例,那么它就足以获得值而不应用平方根函数。

利用sqrt(a) > sqrt(b),它实现了自己的BigDecimal,因此我们在Comparator定义的compareTo方法中使用它:

Comparator