|
| 1 | +package com.thealgorithms.randomized; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.Arrays; |
| 5 | +import java.util.Collections; |
| 6 | +import java.util.Comparator; |
| 7 | +import java.util.List; |
| 8 | +import java.util.Random; |
| 9 | + |
| 10 | +/** |
| 11 | + * Randomized Closest Pair of Points Algorithm |
| 12 | + * |
| 13 | + * Use Case: |
| 14 | + * - Efficiently finds the closest pair of points in a 2D plane. |
| 15 | + * - Applicable in computational geometry, clustering, and graphics. |
| 16 | + * |
| 17 | + * Time Complexity: |
| 18 | + * - Expected: O(n log n) using randomized divide and conquer |
| 19 | + * |
| 20 | + * @see <a href="https://en.wikipedia.org/wiki/Closest_pair_of_points_problem">Closest Pair of Points - Wikipedia</a> |
| 21 | + */ |
| 22 | +public final class RandomizedClosestPair { |
| 23 | + |
| 24 | + // Prevent instantiation of utility class |
| 25 | + private RandomizedClosestPair() { |
| 26 | + throw new UnsupportedOperationException("Utility class"); |
| 27 | + } |
| 28 | + |
| 29 | + public static class Point { |
| 30 | + public final double x; |
| 31 | + public final double y; |
| 32 | + |
| 33 | + public Point(double x, double y) { |
| 34 | + this.x = x; |
| 35 | + this.y = y; |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + public static double findClosestPairDistance(Point[] points) { |
| 40 | + List<Point> shuffled = new ArrayList<>(Arrays.asList(points)); |
| 41 | + Collections.shuffle(shuffled, new Random()); |
| 42 | + |
| 43 | + Point[] px = shuffled.toArray(new Point[0]); |
| 44 | + Arrays.sort(px, Comparator.comparingDouble(p -> p.x)); |
| 45 | + |
| 46 | + Point[] py = px.clone(); |
| 47 | + Arrays.sort(py, Comparator.comparingDouble(p -> p.y)); |
| 48 | + |
| 49 | + return closestPair(px, py); |
| 50 | + } |
| 51 | + |
| 52 | + private static double closestPair(Point[] px, Point[] py) { |
| 53 | + int n = px.length; |
| 54 | + if (n <= 3) { |
| 55 | + return bruteForce(px); |
| 56 | + } |
| 57 | + |
| 58 | + int mid = n / 2; |
| 59 | + Point midPoint = px[mid]; |
| 60 | + |
| 61 | + Point[] qx = Arrays.copyOfRange(px, 0, mid); |
| 62 | + Point[] rx = Arrays.copyOfRange(px, mid, n); |
| 63 | + |
| 64 | + List<Point> qy = new ArrayList<>(); |
| 65 | + List<Point> ry = new ArrayList<>(); |
| 66 | + for (Point p : py) { |
| 67 | + if (p.x <= midPoint.x) { |
| 68 | + qy.add(p); |
| 69 | + } else { |
| 70 | + ry.add(p); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + double d1 = closestPair(qx, qy.toArray(new Point[0])); |
| 75 | + double d2 = closestPair(rx, ry.toArray(new Point[0])); |
| 76 | + |
| 77 | + double d = Math.min(d1, d2); |
| 78 | + |
| 79 | + List<Point> strip = new ArrayList<>(); |
| 80 | + for (Point p : py) { |
| 81 | + if (Math.abs(p.x - midPoint.x) < d) { |
| 82 | + strip.add(p); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + return Math.min(d, stripClosest(strip, d)); |
| 87 | + } |
| 88 | + |
| 89 | + private static double bruteForce(Point[] points) { |
| 90 | + double min = Double.POSITIVE_INFINITY; |
| 91 | + for (int i = 0; i < points.length; i++) { |
| 92 | + for (int j = i + 1; j < points.length; j++) { |
| 93 | + min = Math.min(min, distance(points[i], points[j])); |
| 94 | + } |
| 95 | + } |
| 96 | + return min; |
| 97 | + } |
| 98 | + |
| 99 | + private static double stripClosest(List<Point> strip, double d) { |
| 100 | + double min = d; |
| 101 | + int n = strip.size(); |
| 102 | + for (int i = 0; i < n; i++) { |
| 103 | + for (int j = i + 1; j < n && (strip.get(j).y - strip.get(i).y) < min; j++) { |
| 104 | + min = Math.min(min, distance(strip.get(i), strip.get(j))); |
| 105 | + } |
| 106 | + } |
| 107 | + return min; |
| 108 | + } |
| 109 | + |
| 110 | + private static double distance(Point p1, Point p2) { |
| 111 | + return Math.hypot(p1.x - p2.x, p1.y - p2.y); |
| 112 | + } |
| 113 | +} |
0 commit comments