Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
big-data-project/src/Main.java
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
132 lines (109 sloc)
3.74 KB
This file contains 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
import java.io.File; | |
import java.io.FileNotFoundException; | |
import java.util.Comparator; | |
import java.util.Scanner; | |
public class Main { | |
public static void main(String[] args) { | |
runTests(); | |
} | |
/** | |
* | |
*/ | |
public static void runTests() { | |
System.out.println("For 1,000 points: "); | |
PointList p1 = readFile(1000); | |
runClosetDistanceAlgorithm(p1); | |
testBrute(p1); | |
System.out.println(); | |
System.out.println("For 10,000 points: "); | |
PointList p2 = readFile(10000); | |
runClosetDistanceAlgorithm(p2); | |
testBrute(p2); | |
System.out.println(); | |
System.out.println("For 100,000 points: "); | |
PointList p3 = readFile(100000); | |
runClosetDistanceAlgorithm(p3); | |
testBrute(p3); | |
System.out.println("Tests completed"); | |
} | |
/** | |
* Reads the CSV file, creating a MyPoint instance for each coordinate it reads and adding each of | |
* these MyPoints to a PointList | |
* @return PointList with all points read | |
*/ | |
public static PointList readFile() { | |
PointList plist = new PointList(); | |
String csvFile = "src/xy.csv"; | |
Scanner scanner = null; | |
String csvRegex = ","; | |
try { | |
scanner = new Scanner(new File(csvFile)); | |
// Read over first line | |
scanner.nextLine(); | |
// Read the next line while there is a next line and create new MyPoint objects for each | |
while(scanner.hasNext()) { | |
String s = scanner.nextLine(); | |
String[] values = s.split(csvRegex); | |
double xVal = Double.parseDouble(values[0]); | |
double yVal = Double.parseDouble(values[1]); | |
MyPoint p = new MyPoint(xVal, yVal); | |
plist.add(p); | |
} | |
} catch (FileNotFoundException e) { | |
e.printStackTrace(); | |
} | |
return plist; | |
} | |
/** | |
* Reads the CSV files to a desired depth. Used mostly for comparing run times of the 2 algorithms used | |
* @param depth at which file reading will stop | |
* @return PointList with all points up to depth read | |
*/ | |
public static PointList readFile(int depth) { | |
PointList plist = new PointList(); | |
String csvFile = "src/xy.csv"; | |
Scanner scanner = null; | |
String csvRegex = ","; | |
try { | |
scanner = new Scanner(new File(csvFile)); | |
// Read over first line | |
scanner.nextLine(); | |
// Read the next line until specified depth is reached | |
for(int i=0; i<depth; i++) { | |
String s = scanner.nextLine(); | |
String[] values = s.split(csvRegex); | |
double xVal = Double.parseDouble(values[0]); | |
double yVal = Double.parseDouble(values[1]); | |
MyPoint p = new MyPoint(xVal, yVal); | |
plist.add(p); | |
} | |
} catch (FileNotFoundException e) { | |
e.printStackTrace(); | |
} | |
return plist; | |
} | |
/** | |
* Runs the brute force algorithm and prints the execution time | |
* @param plist to be tested | |
*/ | |
public static void testBrute(PointList plist) { | |
long tStart = System.currentTimeMillis(); | |
System.out.println("Result: " + PointList.getClosestDistanceBrute(plist)); | |
long tEnd = System.currentTimeMillis(); | |
long tDelta = tEnd - tStart; | |
double elapsedSeconds = tDelta / 1000.0; | |
System.out.println("Brute force takes " + elapsedSeconds + " seconds"); | |
} | |
/** | |
* Calls parent method for divide and conquer method and prints execution time | |
* @param plist to be tested | |
*/ | |
public static void runClosetDistanceAlgorithm(PointList plist) { | |
long tStart = System.currentTimeMillis(); | |
System.out.println("Result: " + PointList.closestDistance(plist)); | |
long tEnd = System.currentTimeMillis(); | |
long tDelta = tEnd - tStart; | |
double elapsedSeconds = tDelta / 1000.0; | |
System.out.println("Divide and conquer algorithm takes " + elapsedSeconds + " seconds."); | |
} | |
} |