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