-
Notifications
You must be signed in to change notification settings - Fork 20k
Randomized closest 2 points algorithm #6251
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
Open
bri-harris
wants to merge
23
commits into
TheAlgorithms:master
Choose a base branch
from
bri-harris:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
82fb1f5
closest pair program
bri-harris f1391bd
closest pair program
bri-harris 63ddb36
As required for PRs, formatted with clang-format
bri-harris 21fed7d
As required for PRs, URL of algorithm solution included in a comment
bri-harris b4c88a7
Fixed build errors
bri-harris d7949d6
Private constructor to prevent instanciation
bri-harris a7c2163
Fix bigDecimal erroref
bri-harris 852c2e7
fix infer issue?
bri-harris f854cf7
Update .inferconfig
bri-harris b18bce9
Update infer.yml
bri-harris d1f7ec5
Trying to syncronise dependancies
bri-harris f49710a
fix: Remove pyml pin to resolve dependency conflict with Infer
DenizAltunkapan 94c5ad4
Use a true randomized algorithm solution, Rabin's randomized
bri-harris b5c62ab
fix test case for identical points
bri-harris e89e6c4
used clang formatter on necessary file
bri-harris c93383b
fix build issues
bri-harris 0e32962
rebase and fix build issues
bri-harris cac9706
remove * from import statments
bri-harris e05cfdd
no public or default constructors allowed
bri-harris c386ec9
closest pair class must be final
bri-harris bb66f17
not null error from workflow fixed
bri-harris 92ff833
add null checker in algorithm
bri-harris 20d0d31
add null checker in test and more distinct in rabin function
bri-harris File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
closest pair program
- Loading branch information
commit 82fb1f55e11416deb0e6b8d26d8dd7915c5e1710
There are no files selected for viewing
87 changes: 87 additions & 0 deletions
87
src/main/java/com/thealgorithms/randomized/ClosestPair.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package com.thealgorithms.randomized; | ||
import java.util.*; | ||
import java.math.BigDecimal; | ||
import java.math.RoundingMode; | ||
/** | ||
* This class implements the randomized Closest Pair Algorithm; given some number of points | ||
* in a plane find the pair with minimum euclidean distance from each other. This solution | ||
* uses the divide and conquer approach. | ||
* @author Bri Harris | ||
*/ | ||
|
||
import java.util.*; | ||
|
||
class Point implements Comparable<Point> { | ||
double x, y; | ||
|
||
// Constructor to initialize a point with x and y coordinates | ||
Point(double x, double y) { | ||
this.x = x; | ||
this.y = y; | ||
} | ||
|
||
// Compare points based on x-coordinates (for sorting) | ||
public int compareTo(Point other) { | ||
return Double.compare(this.x, other.x); | ||
} | ||
|
||
// Compute Euclidean distance between two points | ||
static double distance(Point p1, Point p2) { | ||
return Math.sqrt(Math.pow(p1.x - p2.x, 2) + Math.pow(p1.y - p2.y, 2)); | ||
} | ||
} | ||
|
||
public class ClosestPair { | ||
public static double closest(List<Point> points) { | ||
Collections.sort(points); | ||
double result = closestRecursiveHelper(points, 0, points.size() - 1); | ||
|
||
//Return distance of closest pair rounded to 2 decimal places | ||
return new BigDecimal(result).setScale(2, RoundingMode.HALF_UP).doubleValue(); | ||
} | ||
|
||
private static double closestRecursiveHelper(List<Point> points, int left, int right) { | ||
//Base Case occurs with 3 or fewer points | ||
if (right - left <= 2) return baseCase(points, left, right); | ||
|
||
|
||
//Divide and conquer | ||
int mid = (left + right) / 2; | ||
double midX = points.get(mid).x; | ||
|
||
double leftDist = closestRecursiveHelper(points, left, mid); | ||
double rightDist = closestRecursiveHelper(points, mid + 1, right); | ||
|
||
double minDist = Math.min(leftDist, rightDist); | ||
|
||
return checkBoundary(points, left, right, midX, minDist); | ||
} | ||
|
||
private static double baseCase(List<Point> points, int left, int right) { | ||
// Sub-problems fitting the base case can use brute force | ||
double minDist = Double.MAX_VALUE; | ||
for (int i = left; i <= right; i++) { | ||
for (int j = i + 1; j <= right; j++) { | ||
minDist = Math.min(minDist, Point.distance(points.get(i), points.get(j))); | ||
} | ||
} | ||
return minDist; | ||
} | ||
|
||
private static double checkBoundary(List<Point> points, int left, int right, double midX, double minDist) { | ||
//Consider a boundary by the dividing line | ||
List<Point> boundary = new ArrayList<>(); | ||
for (int i = left; i <= right; i++) { | ||
if (Math.abs(points.get(i).x - midX) < minDist) boundary.add(points.get(i)); | ||
} | ||
|
||
//sort by y coordinate within the boundary and check for closer points | ||
boundary.sort(Comparator.comparingDouble(p -> p.y)); | ||
for (int i = 0; i < boundary.size(); i++) { | ||
for (int j = i + 1; j < boundary.size() && (boundary.get(j).y - boundary.get(i).y) < minDist; j++) { | ||
minDist = Math.min(minDist, Point.distance(boundary.get(i), boundary.get(j))); | ||
} | ||
} | ||
return minDist; | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.