|
| 1 | +package com.packt.datastructuresandalg.lesson4.points; |
| 2 | + |
| 3 | +import java.util.*; |
| 4 | + |
| 5 | +public class ClosestPairOfPoints { |
| 6 | + static class Point { |
| 7 | + int x; |
| 8 | + int y; |
| 9 | + |
| 10 | + public Point(int _x, int _y) { |
| 11 | + x = _x; |
| 12 | + y = _y; |
| 13 | + } |
| 14 | + } |
| 15 | + |
| 16 | + static class PointPair { |
| 17 | + Point p; |
| 18 | + Point q; |
| 19 | + |
| 20 | + public PointPair(Point _p, Point _q) { |
| 21 | + p = _p; |
| 22 | + q = _q; |
| 23 | + } |
| 24 | + |
| 25 | + public double distance() { |
| 26 | + return Math.sqrt(Math.pow(p.x - q.x, 2) + Math.pow(p.y - q.y, 2)); |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + PointPair bruteForce(List<Point> points) { |
| 31 | + PointPair best = new PointPair(points.get(0), points.get(1)); |
| 32 | + for (int i = 2; i < points.size(); i++) { |
| 33 | + for (int j = i - 1; j >= 0; j--) { |
| 34 | + PointPair candidate = new PointPair(points.get(i), points.get(j)); |
| 35 | + if (candidate.distance() < best.distance()) |
| 36 | + best = candidate; |
| 37 | + } |
| 38 | + } |
| 39 | + return best; |
| 40 | + } |
| 41 | + |
| 42 | + PointPair bestWithStrip(List<Point> points, PointPair bestSoFar) { |
| 43 | + PointPair best = bestSoFar; |
| 44 | + List<Point> sortedPoints = new ArrayList<>(points); |
| 45 | + Collections.sort(sortedPoints, (o1, o2) -> Integer.signum(o1.y - o2.y)); |
| 46 | + |
| 47 | + for (int i = 0; i < points.size(); i++) { |
| 48 | + for (int j = i + 1; j < points.size() && (points.get(j).y - points.get(i).y) < best.distance(); j++) { |
| 49 | + PointPair candidate = new PointPair(points.get(i), points.get(j)); |
| 50 | + if (candidate.distance() < best.distance()) |
| 51 | + best = candidate; |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + return best; |
| 56 | + } |
| 57 | + |
| 58 | + PointPair divideAndConquerAux(List<Point> points) { |
| 59 | + int N = points.size(); |
| 60 | + if (N <= 3) |
| 61 | + return bruteForce(points); |
| 62 | + |
| 63 | + int mid = N / 2; |
| 64 | + Point midPoint = points.get(mid); |
| 65 | + |
| 66 | + PointPair bl = divideAndConquer(points.subList(0, mid)); |
| 67 | + PointPair br = divideAndConquer(points.subList(mid, N)); |
| 68 | + |
| 69 | + PointPair bestSoFar = bl; |
| 70 | + if (br.distance() < bl.distance()) |
| 71 | + bestSoFar = br; |
| 72 | + |
| 73 | + List<Point> strip = new ArrayList<>(); |
| 74 | + for (int i = 0; i < N; i++) { |
| 75 | + if (Math.abs(points.get(i).x - midPoint.x) < bestSoFar.distance()) |
| 76 | + strip.add(points.get(i)); |
| 77 | + } |
| 78 | + |
| 79 | + return bestWithStrip(strip, bestSoFar); |
| 80 | + } |
| 81 | + |
| 82 | + PointPair divideAndConquer(List<Point> points) { |
| 83 | + List<Point> sortedPoints = new ArrayList<>(points); |
| 84 | + Collections.sort(sortedPoints, (o1, o2) -> Integer.signum(o1.x - o2.x)); |
| 85 | + return divideAndConquerAux(sortedPoints); |
| 86 | + } |
| 87 | + |
| 88 | + public static void main(String[] args) { |
| 89 | + List<Point> points = new ArrayList<>(); |
| 90 | + points.add(new Point(2, 3)); |
| 91 | + points.add(new Point(12, 30)); |
| 92 | + points.add(new Point(40, 50)); |
| 93 | + points.add(new Point(5, 1)); |
| 94 | + points.add(new Point(12, 10)); |
| 95 | + points.add(new Point(3, 4)); |
| 96 | + |
| 97 | + ClosestPairOfPoints closest = new ClosestPairOfPoints(); |
| 98 | + PointPair best1 = closest.bruteForce(points); |
| 99 | + PointPair best2 = closest.divideAndConquer(points); |
| 100 | + |
| 101 | + System.out.println("Distance = " + best1.distance()); |
| 102 | + System.out.println("Distance = " + best2.distance()); |
| 103 | + } |
| 104 | +} |
0 commit comments