For Random Numbers: Randomgenerator
For Random Numbers: Randomgenerator
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
Page 3