Skip to content

Randomized Closest Pair Algorithm #6231

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
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions Randomized/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>br.com.exemplo</groupId>
<artifactId>meu-projeto</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.10.2</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.2.5</version>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package learningspace.swtest.examples;

import java.util.*;

public class ClosestPairRandomized {

public static class Point {
double x, y;
public Point(double x, double y) {
this.x = x;
this.y = y;
}
}

public static double distance(Point a, Point b) {
return Math.hypot(a.x - b.x, a.y - b.y);
}

public static double closestPair(Point[] points, int samples) {
Random rand = new Random();
List<Point> shuffled = new ArrayList<>(Arrays.asList(points));
Collections.shuffle(shuffled, rand);

double minDist = Double.MAX_VALUE;
for (int i = 0; i < samples; i++) {
Point p1 = shuffled.get(rand.nextInt(shuffled.size()));
Point p2 = shuffled.get(rand.nextInt(shuffled.size()));
if (p1 != p2) {
double d = distance(p1, p2);
if (d < minDist) {
minDist = d;
}
}
}
return minDist;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package learningspace.swtest.examples;


import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

public class ClosestPairRandomizedTest {

@Test
public void testRandomizedClosestPair() {
ClosestPairRandomized.Point[] points = {
new ClosestPairRandomized.Point(0, 0),
new ClosestPairRandomized.Point(3, 4),
new ClosestPairRandomized.Point(1, 1),
new ClosestPairRandomized.Point(2, 2)
};

double result = ClosestPairRandomized.closestPair(points, 10000);
double expectedMinDist = Math.sqrt(2); // Entre (1,1) e (2,2)
assertTrue(result <= expectedMinDist + 0.5, "Distância deve ser próxima ou menor que esperado");
}

@Test
public void testWithIdenticalPoints() {
ClosestPairRandomized.Point[] points = {
new ClosestPairRandomized.Point(1, 1),
new ClosestPairRandomized.Point(1, 1),
new ClosestPairRandomized.Point(2, 2)
};

double result = ClosestPairRandomized.closestPair(points, 1000);
assertEquals(0.0, result, 0.001);
}
}
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
learningspace\swtest\examples\ClosestPairRandomized.class
learningspace\swtest\examples\ClosestPairRandomized$Point.class
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
C:\Users\anell\Downloads\trabalho\Guess the number\src\main\java\learningspace\swtest\examples\ClosestPairRandomized.java
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
learningspace\swtest\examples\ClosestPairRandomizedTest.class
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
C:\Users\anell\Downloads\trabalho\Guess the number\src\test\java\learningspace\swtest\examples\ClosestPairRandomizedTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?xml version="1.0" encoding="UTF-8"?>
<testsuite xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://maven.apache.org/surefire/maven-surefire-plugin/xsd/surefire-test-report-3.0.xsd" version="3.0" name="learningspace.swtest.examples.ClosestPairRandomizedTest" time="0.074" tests="2" errors="0" skipped="0" failures="0">
<properties>
<property name="sun.desktop" value="windows"/>
<property name="awt.toolkit" value="sun.awt.windows.WToolkit"/>
<property name="file.encoding.pkg" value="sun.io"/>
<property name="java.specification.version" value="1.8"/>
<property name="sun.cpu.isalist" value="amd64"/>
<property name="sun.jnu.encoding" value="Cp1252"/>
<property name="java.class.path" value="C:\Users\anell\Downloads\trabalho\Guess the number\target\test-classes;C:\Users\anell\Downloads\trabalho\Guess the number\target\classes;C:\Users\anell\.m2\repository\org\junit\jupiter\junit-jupiter\5.10.2\junit-jupiter-5.10.2.jar;C:\Users\anell\.m2\repository\org\junit\jupiter\junit-jupiter-api\5.10.2\junit-jupiter-api-5.10.2.jar;C:\Users\anell\.m2\repository\org\opentest4j\opentest4j\1.3.0\opentest4j-1.3.0.jar;C:\Users\anell\.m2\repository\org\junit\platform\junit-platform-commons\1.10.2\junit-platform-commons-1.10.2.jar;C:\Users\anell\.m2\repository\org\apiguardian\apiguardian-api\1.1.2\apiguardian-api-1.1.2.jar;C:\Users\anell\.m2\repository\org\junit\jupiter\junit-jupiter-params\5.10.2\junit-jupiter-params-5.10.2.jar;C:\Users\anell\.m2\repository\org\junit\jupiter\junit-jupiter-engine\5.10.2\junit-jupiter-engine-5.10.2.jar;C:\Users\anell\.m2\repository\org\junit\platform\junit-platform-engine\1.10.2\junit-platform-engine-1.10.2.jar;"/>
<property name="java.vm.vendor" value="Oracle Corporation"/>
<property name="sun.arch.data.model" value="64"/>
<property name="user.variant" value=""/>
<property name="java.vendor.url" value="http://java.oracle.com/"/>
<property name="user.timezone" value="America/Sao_Paulo"/>
<property name="java.vm.specification.version" value="1.8"/>
<property name="os.name" value="Windows 10"/>
<property name="user.country" value="BR"/>
<property name="sun.java.launcher" value="SUN_STANDARD"/>
<property name="sun.boot.library.path" value="C:\Program Files\Java\jdk1.8.0_202\jre\bin"/>
<property name="sun.java.command" value="C:\Users\anell\AppData\Local\Temp\surefire2136635121484158896\surefirebooter-20250501205601815_3.jar C:\Users\anell\AppData\Local\Temp\surefire2136635121484158896 2025-05-01T20-56-01_380-jvmRun1 surefire-20250501205601815_1tmp surefire_0-20250501205601815_2tmp"/>
<property name="surefire.test.class.path" value="C:\Users\anell\Downloads\trabalho\Guess the number\target\test-classes;C:\Users\anell\Downloads\trabalho\Guess the number\target\classes;C:\Users\anell\.m2\repository\org\junit\jupiter\junit-jupiter\5.10.2\junit-jupiter-5.10.2.jar;C:\Users\anell\.m2\repository\org\junit\jupiter\junit-jupiter-api\5.10.2\junit-jupiter-api-5.10.2.jar;C:\Users\anell\.m2\repository\org\opentest4j\opentest4j\1.3.0\opentest4j-1.3.0.jar;C:\Users\anell\.m2\repository\org\junit\platform\junit-platform-commons\1.10.2\junit-platform-commons-1.10.2.jar;C:\Users\anell\.m2\repository\org\apiguardian\apiguardian-api\1.1.2\apiguardian-api-1.1.2.jar;C:\Users\anell\.m2\repository\org\junit\jupiter\junit-jupiter-params\5.10.2\junit-jupiter-params-5.10.2.jar;C:\Users\anell\.m2\repository\org\junit\jupiter\junit-jupiter-engine\5.10.2\junit-jupiter-engine-5.10.2.jar;C:\Users\anell\.m2\repository\org\junit\platform\junit-platform-engine\1.10.2\junit-platform-engine-1.10.2.jar;"/>
<property name="sun.cpu.endian" value="little"/>
<property name="user.home" value="C:\Users\anell"/>
<property name="user.language" value="pt"/>
<property name="java.specification.vendor" value="Oracle Corporation"/>
<property name="java.home" value="C:\Program Files\Java\jdk1.8.0_202\jre"/>
<property name="basedir" value="C:\Users\anell\Downloads\trabalho\Guess the number"/>
<property name="file.separator" value="\"/>
<property name="line.separator" value="&#10;"/>
<property name="java.vm.specification.vendor" value="Oracle Corporation"/>
<property name="java.specification.name" value="Java Platform API Specification"/>
<property name="java.awt.graphicsenv" value="sun.awt.Win32GraphicsEnvironment"/>
<property name="surefire.real.class.path" value="C:\Users\anell\AppData\Local\Temp\surefire2136635121484158896\surefirebooter-20250501205601815_3.jar"/>
<property name="sun.boot.class.path" value="C:\Program Files\Java\jdk1.8.0_202\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_202\jre\lib\rt.jar;C:\Program Files\Java\jdk1.8.0_202\jre\lib\sunrsasign.jar;C:\Program Files\Java\jdk1.8.0_202\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_202\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_202\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_202\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_202\jre\classes"/>
<property name="user.script" value=""/>
<property name="sun.management.compiler" value="HotSpot 64-Bit Tiered Compilers"/>
<property name="java.runtime.version" value="1.8.0_202-b08"/>
<property name="user.name" value="anell"/>
<property name="path.separator" value=";"/>
<property name="os.version" value="10.0"/>
<property name="java.endorsed.dirs" value="C:\Program Files\Java\jdk1.8.0_202\jre\lib\endorsed"/>
<property name="java.runtime.name" value="Java(TM) SE Runtime Environment"/>
<property name="file.encoding" value="Cp1252"/>
<property name="java.vm.name" value="Java HotSpot(TM) 64-Bit Server VM"/>
<property name="localRepository" value="C:\Users\anell\.m2\repository"/>
<property name="java.vendor.url.bug" value="http://bugreport.sun.com/bugreport/"/>
<property name="java.io.tmpdir" value="C:\Users\anell\AppData\Local\Temp\"/>
<property name="java.version" value="1.8.0_202"/>
<property name="user.dir" value="C:\Users\anell\Downloads\trabalho\Guess the number"/>
<property name="os.arch" value="amd64"/>
<property name="java.vm.specification.name" value="Java Virtual Machine Specification"/>
<property name="java.awt.printerjob" value="sun.awt.windows.WPrinterJob"/>
<property name="sun.os.patch.level" value=""/>
<property name="java.library.path" value="C:\Program Files\Java\jdk1.8.0_202\jre\bin;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\Program Files (x86)\Common Files\Oracle\Java\java8path;C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;C:\Program Files\dotnet\;C:\Program Files\Git\cmd;C:\Program Files\nodejs\;C:\MinGW\bin;C:\Program Files\Apache\Maven\apache-maven-3.9.9\bin;C:\Program Files\Java\jdk1.8.0_202\bin;C:\Users\anell\AppData\Local\Programs\Python\Python313\Scripts\;C:\Users\anell\AppData\Local\Programs\Python\Python313\;C:\Users\anell\AppData\Local\Programs\Python\Launcher\;C:\Users\anell\AppData\Local\Microsoft\WindowsApps;C:\MinGW\bin\g++;C:\Users\anell\AppData\Local\Programs\Microsoft VS Code\bin\gcc;C:\Users\anell\AppData\Local\Programs\Microsoft VS Code\bin;C:\src\flutter\bin;C:\Users\anell\AppData\Roaming\npm;C:\Program Files\Apache\Maven\apache-maven-3.9.9\bin;;."/>
<property name="java.vm.info" value="mixed mode"/>
<property name="java.vendor" value="Oracle Corporation"/>
<property name="java.vm.version" value="25.202-b08"/>
<property name="java.ext.dirs" value="C:\Program Files\Java\jdk1.8.0_202\jre\lib\ext;C:\WINDOWS\Sun\Java\lib\ext"/>
<property name="sun.io.unicode.encoding" value="UnicodeLittle"/>
<property name="java.class.version" value="52.0"/>
</properties>
<testcase name="testWithIdenticalPoints" classname="learningspace.swtest.examples.ClosestPairRandomizedTest" time="0.041"/>
<testcase name="testRandomizedClosestPair" classname="learningspace.swtest.examples.ClosestPairRandomizedTest" time="0.008"/>
</testsuite>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-------------------------------------------------------------------------------
Test set: learningspace.swtest.examples.ClosestPairRandomizedTest
-------------------------------------------------------------------------------
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.074 s -- in learningspace.swtest.examples.ClosestPairRandomizedTest
Binary file not shown.