-
Notifications
You must be signed in to change notification settings - Fork 19.9k
Added RandomizedClosestPair code and test #6224
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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<>(); | ||
Comment on lines
+59
to
+63
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please write qx instead of Qx because this is not camelCase. |
||
for (Point p : py) { | ||
if (p.x <= midPoint.x) Qy.add(p); | ||
else Ry.add(p); | ||
Comment on lines
+65
to
+66
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 'if' construct must use '{}'s (see checktyle) |
||
} | ||
|
||
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); | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Line has trailing spaces according to checkstyle, please remove this |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package com.thealgorithms.randomized; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import com.thealgorithms.randomized.RandomizedClosestPair.Point; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class RandomizedClosestPairTest { | ||
|
||
@Test | ||
public void testClosestPairBasic() { | ||
Point[] points = new Point[] { | ||
new Point(2, 3), | ||
new Point(12, 30), | ||
new Point(40, 50), | ||
new Point(5, 1), | ||
new Point(12, 10), | ||
new Point(3, 4) | ||
}; | ||
double result = RandomizedClosestPair.findClosestPairDistance(points); | ||
assertEquals(Math.hypot(1, 1), result, 0.01); // Closest pair: (2,3) and (3,4) | ||
} | ||
|
||
@Test | ||
public void testIdenticalPoints() { | ||
Point[] points = new Point[] { | ||
new Point(0, 0), | ||
new Point(0, 0), | ||
new Point(1, 1), | ||
}; | ||
double result = RandomizedClosestPair.findClosestPairDistance(points); | ||
assertEquals(0.0, result, 0.00001); | ||
} | ||
|
||
@Test | ||
public void testMinimalCase() { | ||
Point[] points = new Point[] { | ||
new Point(0, 0), | ||
new Point(3, 4) | ||
}; | ||
double result = RandomizedClosestPair.findClosestPairDistance(points); | ||
assertEquals(5.0, result, 0.00001); | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Line has trailing spaces according to checkstyle, please remove this |
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Each variable declaration must be in its own statement. Please split these two declarations.