0% found this document useful (0 votes)
34 views3 pages

For Random Numbers: Randomgenerator

This document contains code for finding the closest pair of points among a set of points in a plane. It defines a Point class with x and y coordinates and methods to compare points and calculate distances between points. It contains two algorithms - a simple O(n^2) algorithm that calculates all pairwise distances, and a more efficient O(n log n) divide and conquer algorithm. The code takes user input for the number of points, generates random points, runs both algorithms, and outputs the closest distances found and running times.

Uploaded by

sara amer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views3 pages

For Random Numbers: Randomgenerator

This document contains code for finding the closest pair of points among a set of points in a plane. It defines a Point class with x and y coordinates and methods to compare points and calculate distances between points. It contains two algorithms - a simple O(n^2) algorithm that calculates all pairwise distances, and a more efficient O(n log n) divide and conquer algorithm. The code takes user input for the number of points, generates random points, runs both algorithms, and outputs the closest distances found and running times.

Uploaded by

sara amer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

closestPair.

java

1 import java.util.*;
2 import java.lang.*;
3 import static java.lang.Math.min;
4 import static java.lang.StrictMath.abs;
5
6 public class closestPair {
7
8 private static Random randomGenerator; // for random numbers
9
10 public static class Point implements Comparable<Point> {
11
12 public long x, y;
13
14 // Constructor
15 public Point(long x, long y) {
16 this.x = x;
17 this.y = y;
18 }
19
20 public int compareTo(Point p) {
21 // compare this and p and there are three results: >0, ==0, or <0
22 if (this.x == p.x) {
23 if (this.y == p.y)
24 return 0;
25 else
26 return (this.y > p.y)? 1 : -1;
27 }
28 else
29 return (this.x > p.x)? 1 : -1;
30 }
31
32 public String toString() {
33 return " ("+Long.toString(this.x)+","+Long.toString(this.y)+")";
34 }
35
36 public double distance(Point p) {
37 long dx = (this.x - p.x);
38 long dy = (this.y - p.y);
39 return Math.sqrt(dx*dx + dy*dy);
40 }
41 }
42
43 public static Point[] plane;
44
45 public static Point[] T;
46
47 public static Point[] Y;
48
49 public static int N; // number of points in the plane
50
51 public static void main(String[] args) {
52
53 // Read in the Size of a maze
54 Scanner scan = new Scanner(System.in);
55 try {
56 System.out.println("Hello I'm ClosestPair my operation basic is find Euclidean
distance between the closest pair of points");
57 System.out.println("Time efficiency : O(n log n) & space efficiency :
O(1)");
58 System.out.println("How many points in your plane? ");
59 N = scan.nextInt();
60 }

Page 1
closestPair.java

61 catch(Exception ex){
62 ex.printStackTrace();
63 }
64 scan.close();
65
66 // Create plane of N points.
67 plane = new Point[N];
68 Y = new Point[N];
69 T = new Point[N];
70 randomGenerator = new Random();
71
72 for (int i = 0; i < N; ++i) {
73 long x = randomGenerator.nextInt(N<<6);
74 long y = randomGenerator.nextInt(N<<6);
75 plane[i] = new Point(x, y);
76 }
77 Arrays.sort(plane); // sort points according to compareTo.
78 for (int i = 1; i < N; ++i) // make all x's distinct.
79 if (plane[i-1].x >= plane[i].x) plane[i].x = plane[i-1].x + 1;
80
81 //for (int i = 1; i < N; i++)
82 // if (plane[i-1].y >= plane[i].y) plane[i].y = plane[i-1].y +
1;
83 //
84 //
85 System.out.println(N + " points are randomly created.");
86 System.out.println("The first two points are"+plane[0]+" and"+plane[1]);
87 System.out.println("The distance of the first two points is
"+plane[0].distance(plane[1]));
88
89
90 long start = System.currentTimeMillis();
91 // Compute the minimal distance of any pair of points by exhaustive search.
92 double min1 = minDisSimple();
93 long end = System.currentTimeMillis();
94 System.out.println("The distance of the two closest points by minDisSimple is
"+min1);
95 System.out.println("The running time for minDisSimple is "+(end-start)+"
mms");
96 // Compute the minimal distance of any pair of points by divide-and-conquer
97 long start1 = System.currentTimeMillis();
98 double min2 = minDisDivideConquer(0, N-1);
99 long end1 = System.currentTimeMillis();
100
101 System.out.println("The distance of the two closest points by misDisDivideConquer
is "+min2);
102 System.out.println("The running time for misDisDivideConquer is
"+(end1-start1)+" mms");
103
104 }
105
106 static double minDisSimple() {
107 // A straightforward method for computing the distance
108 // of the two closest points in plane[0..N-1].
109
110 // to be completed
111 double midDis = Double.POSITIVE_INFINITY;
112 double temp;
113 for (int i = 0; i < N - 1; i++) {
114 for (int j = i + 1; j < N; j++) {
115 temp = plane[i].distance(plane[j]);
116 if (temp < midDis) {

Page 2
closestPair.java

117 midDis = temp;


118 }
119 }
120 }
121 return midDis;
122
123 }
124
125 static void exchange(int i, int j) {
126 Point x = plane[i];
127 plane[i] = plane[j];
128 plane[j] = x;
129 }
130
131 static double minDisDivideConquer(int low, int high) {
132 // Initialize necessary values
133 double minIntermediate;
134 double minmin;
135 double minDis;
136
137 if (high == low+1) { // two points
138 if (plane[low].y > plane[high].y) exchange(low, high);
139 return plane[low].distance(plane[high]);
140 }
141 else if (high == low+2) { // three points
142 // sort these points by y-coordinate
143 if (plane[low].y > plane[high].y) exchange(low, high);
144 if (plane[low].y > plane[low+1].y) exchange(low, low+1);
145 else if (plane[low+1].y > plane[high].y) exchange(low+1, high);
146 // compute pairwise distances
147 double d1 = plane[low].distance(plane[high]);
148 double d2 = plane[low].distance(plane[low+1]);
149 double d3 = plane[low+1].distance(plane[high]);
150 return ((d1 < d2)? ((d1 < d3)? d1 : d3) : (d2 < d3)? d2 : d3); // return
min(d1, d2, d3)
151 } else { // 4 or more points: Divide and conquer
152 int mid = (high + low)/2;
153 double lowerPartMin = minDisDivideConquer(low,mid);
154 double upperPartMin = minDisDivideConquer(mid+1,high);
155 minIntermediate = min(lowerPartMin, upperPartMin);
156 int k = 0;
157 double x0 = plane[mid].x;
158 for(int i = 1; i < N; i++){
159 if(abs(plane[i].x-x0) <= minIntermediate){
160 k++;
161 T[k] = plane[i];
162 }
163 }
164 minmin = 2 * minIntermediate;
165 for (int i = 1; i < k-1; i++){
166 for(int j = i + 1; j < min(i+7,k);j++){
167 double distance0 = abs(T[i].distance(T[j]));
168 if(distance0 < minmin){
169 minmin = distance0;
170 }
171 }
172 }
173 minDis = min(minmin, minIntermediate);
174 }
175 return minDis;
176 }
177 }

Page 3

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy