Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Divide and Conquer Problem

Posted on 2008-11-17
4
Medium Priority
?
909 Views
Last Modified: 2013-11-23
Can you guys look at my code and let me know how to improve it?  I'm finding the closed set of points (X,Y) in 2D plane. I need to do it in O(n^2) and O(nlogn).  O(n^2) was easy.  My problems are on the Divide and Conquer approach.  I've been up two days trying to do it, but I can't seem to get it right.  I couldn't check my method completely because I got a null pointer exception when I sorted my array by the X-coordinate.  I'm sure I'll have more errors. Also I'm not sure if this was the correct approach, I followed this:
http://www.cs.mcgill.ca/~cs251/ClosestPair/ClosestPairDQ.html

Also how do I keep track of which points are the closest ones..... my method only return the distance between the closest points


double dL;
	double dR;
	double d;
	double stVal = Double.POSITIVE_INFINITY;
 
	 public double closestPoints(int l, int r) 
	{
		 if((r - l) ==2){
			 stVal=array[0].eucDist(array[1]); //eucDist computes distance between two points
			 return stVal;
		 }
		 if((r-l)<2){
			 stVal=stVal-1;
			 return stVal;
		 }
		 int median = (r-l)/2;
		 dL = closestPoints(l, median);
		 dR = closestPoints(median+1, r);
		 double d;
		 if(dL<=dR)
			 d=dL;
		 else
			 d=dR;
		 Point[] Y;
		 int n = 0;
//next two for loops compute the number of Points with X < = than d
		 for(int i = median; Math.abs(array[i].getX())<=d; i--){
			 n++;
		 }
		 for(int i=median+1; Math.abs(array[i].getX())<=d; i++){
			 n++;
		 }
		 Y = new Point[n];
		 int index = 0;
//next two for loops fill Y with points that have X value < = than d
		 for(int i = median; Math.abs(array[i].getX())<=d; i--){
			 Y[index]=array[i];
			 index++;
		 }
		 for(int i=median+1; Math.abs(array[i].getX())<=d; i++){
			Y[index]=array[i];
			index++;
		 }
		 Arrays.sort(Y, new Point.CompY());  //sort Y by increasing Y coordinate
		 double dY = Double.POSITIVE_INFINITY;
		 for(int i=0; i<=array.length-2; i++)
	        {
	        	for(int j=i+1; j<=array.length-1; j++)
	        	{
//xx, yy initialized alreay doubles xx = Math.pow((array[i].getX()-array[j].getX()), 2);
	        		 yy = Math.pow((array[i].getY()-array[j].getY()), 2);    		
	        		 dY = Math.sqrt(xx+yy);
	        		 if(dY < d)
	        		 {
	        			 d = dY;
 
	        		 }
	        	}
	        }
		 return d;
	}
 
 
-----------------------------------------------------------
 
 
	public static void main(String[] args) {
 
		MyClass class = new MyClass("Points.txt");
		Arrays.sort(driver.array, new Point.CompX());
		MyClass.closestPoints(0, MyClass.array.length);

Open in new window

0
Comment
Question by:ubuntuguy
  • 2
  • 2
4 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 22975112
>>I couldn't check my method completely because I got a null pointer exception when I sorted my array by the X-coordinate.

Start with that. Please post the stack trace of that exception
0
 
LVL 1

Author Comment

by:ubuntuguy
ID: 22977376
OK I figured that I wasn't filling the array with the values. Sorry I was tired last night.  This is what I get:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
    at project.myClass.closestPoints(myClass.java:140)
    at project.myClass.closestPoints(myClass.java:131)
    at project.myClass.closestPoints(myClass.java:131)
    at project.myClass.closestPoints(myClass.java:131)
    at project.myClass.closestPoints(myClass.java:131)
    at project.myClass.closestPoints(myClass.java:131)
    at project.myClass.closestPoints(myClass.java:131)
    at project.myClass.closestPoints(myClass.java:131)
    at project.myClass.closestPoints(myClass.java:131)
    at project.myClass.closestPoints(myClass.java:131)
    at project.FindPoints.main(closestPoints.java:14)

where line 131:
         dL = closestPoints(l, median);
line 140:
         dR = fcp(median+1, r);

line 14 on main class
        myClass.closestPoints(0, myClass.array.length);
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 1500 total points
ID: 22977425
>>myClass.array.length

Any attempt to index a Java array at array.length will fail since array[array.length - 1] is the upper bound. Is that what you're doing?
0
 
LVL 1

Author Comment

by:ubuntuguy
ID: 22977444
No I actually I changed it to array.length-1.  I still get the same error.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses
Course of the Month14 days, 6 hours left to enroll

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question