Skip to content

Commit 6aa1b16

Browse files
committed
As required for PRs, formatted with clang-format
1 parent 384df67 commit 6aa1b16

2 files changed

Lines changed: 59 additions & 15 deletions

File tree

src/main/java/com/thealgorithms/randomized/ClosestPair.java

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,9 @@
11
package com.thealgorithms.randomized;
2-
import java.util.*;
32
import java.math.BigDecimal;
43
import java.math.RoundingMode;
5-
/**
6-
* This class implements the randomized Closest Pair Algorithm; given some number of points
7-
* in a plane find the pair with minimum euclidean distance from each other. This solution
8-
* uses the divide and conquer approach.
9-
* @author Bri Harris
10-
*/
11-
124
import java.util.*;
135

14-
class Point implements Comparable<Point> {
6+
final class Point implements Comparable<Point> {
157
double x;
168
double y;
179

@@ -30,20 +22,29 @@ static double distance(Point p1, Point p2) {
3022
}
3123
}
3224

33-
public class ClosestPair {
25+
public final class ClosestPair {
26+
3427
public static double closest(List<Point> points) {
28+
if (points == null || points.isEmpty()) {
29+
throw new IllegalArgumentException("There are no pairs to compare.");
30+
}
31+
32+
if (points.size() == 1) {
33+
throw new IllegalArgumentException("There is only one pair.");
34+
}
35+
3536
Collections.sort(points);
3637
double result = closestRecursiveHelper(points, 0, points.size() - 1);
3738

38-
//Return distance of closest pair rounded to 2 decimal places
39+
// Return distance of closest pair rounded to 2 decimal places
3940
return new BigDecimal(result).setScale(2, RoundingMode.HALF_UP).doubleValue();
4041
}
4142

4243
private static double closestRecursiveHelper(List<Point> points, int left, int right) {
43-
//Base Case occurs with 3 or fewer points
44+
// Base Case occurs with 3 or fewer points
4445
if (right - left <= 2) return baseCase(points, left, right);
4546

46-
//Divide and conquer
47+
// Divide and conquer
4748
int mid = (left + right) / 2;
4849
double midX = points.get(mid).x;
4950

@@ -67,15 +68,15 @@ private static double baseCase(List<Point> points, int left, int right) {
6768
}
6869

6970
private static double checkBoundary(List<Point> points, int left, int right, double midX, double minDist) {
70-
//Consider a boundary by the dividing line
71+
// Consider a boundary by the dividing line
7172
List<Point> boundary = new ArrayList<>();
7273
for (int i = left; i <= right; i++) {
7374
if (Math.abs(points.get(i).x - midX) < minDist) {
7475
boundary.add(points.get(i));
7576
}
7677
}
7778

78-
//sort by y coordinate within the boundary and check for closer points
79+
// sort by y coordinate within the boundary and check for closer points
7980
boundary.sort(Comparator.comparingDouble(p -> p.y));
8081
for (int i = 0; i < boundary.size(); i++) {
8182
for (int j = i + 1; j < boundary.size() && (boundary.get(j).y - boundary.get(i).y) < minDist; j++) {
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.thealgorithms.randomized;
2+
import static org.junit.jupiter.api.Assertions.assertEquals;
3+
import static org.junit.jupiter.api.Assertions.assertThrows;
4+
5+
import java.util.ArrayList;
6+
import java.util.Arrays;
7+
import java.util.List;
8+
import org.junit.jupiter.api.Test;
9+
10+
public class ClosestPairTest {
11+
12+
// Tests sorting of an array with multiple elements, including duplicates.
13+
@Test
14+
public void testMultiplePairs() {
15+
List<Point> points = Arrays.asList(new Point(1, 2), new Point(3, 4), new Point(5, 1), new Point(7, 8), new Point(2, 3), new Point(6, 2));
16+
double expected = 1.41;
17+
assertEquals(expected, ClosestPair.closest(points));
18+
}
19+
20+
// Test if there are no pairs.
21+
@Test
22+
public void testNoPoints() {
23+
List<Point> points = new ArrayList<>();
24+
Exception exception = assertThrows(IllegalArgumentException.class, () -> { ClosestPair.closest(points); });
25+
assertEquals("There are no pairs to compare.", exception.getMessage());
26+
}
27+
28+
// Test if there is one point, no pairs.
29+
@Test
30+
public void testOnePoint() {
31+
List<Point> points = Arrays.asList(new Point(1, 2));
32+
Exception exception = assertThrows(IllegalArgumentException.class, () -> { ClosestPair.closest(points); });
33+
assertEquals("There is only one pair.", exception.getMessage());
34+
}
35+
36+
// Test if there is a duplicate points as a pair
37+
@Test
38+
public void testPoints() {
39+
List<Point> points = Arrays.asList(new Point(1, 2), new Point(5, 1), new Point(5, 1), new Point(7, 8), new Point(2, 3), new Point(6, 2));
40+
double expected = 0.00;
41+
assertEquals(expected, ClosestPair.closest(points));
42+
}
43+
}

0 commit comments

Comments
 (0)