Update SkylineAlgorithm.java

This commit is contained in:
Libin Yang
2019-01-15 19:54:43 +08:00
committed by GitHub
parent 8f78a3fe6c
commit 0a3717957c

View File

@ -1,19 +1,15 @@
package skylinealgorithm;
import java.util.ArrayList;
import java.util.Comparator;
/**
*
* @author dimgrichr
*
* <p>
* Space complexity: O(n)
* Time complexity: O(nlogn), because it is a divide and conquer algorithm
*/
public class SkylineAlgorithm {
private ArrayList<Point> points;
/**
* Main constructor of the application.
* ArrayList points gets created, which represents the sum of all edges.
@ -42,6 +38,7 @@ public class SkylineAlgorithm {
* with arguments the corresponding half of the initial ArrayList each time.
* Once the flashback has ended, the function produceFinalSkyLine gets called,
* in order to produce the final skyline, and return it.
*
* @param list, the initial list of points
* @return leftSkyLine, the combination of first half's and second half's skyline
* @see Point
@ -53,12 +50,10 @@ public class SkylineAlgorithm {
int size = list.size();
if (size == 1) {
return list;
}
else if(size==2){
} else if (size == 2) {
if (list.get(0).dominates(list.get(1))) {
list.remove(1);
}
else{
} else {
if (list.get(1).dominates(list.get(0))) {
list.remove(0);
}
@ -72,15 +67,12 @@ public class SkylineAlgorithm {
for (int i = 0; i < list.size(); i++) {
if (i < list.size() / 2) {
leftHalf.add(list.get(i));
}
else{
} else {
rightHalf.add(list.get(i));
}
}
ArrayList<Point> leftSubSkyLine=new ArrayList<>();
ArrayList<Point> rightSubSkyLine=new ArrayList<>();
leftSubSkyLine=produceSubSkyLines(leftHalf);
rightSubSkyLine=produceSubSkyLines(rightHalf);
ArrayList<Point> leftSubSkyLine = produceSubSkyLines(leftHalf);
ArrayList<Point> rightSubSkyLine= produceSubSkyLines(rightHalf);
// skyline is produced
return produceFinalSkyLine(leftSubSkyLine, rightSubSkyLine);
@ -97,6 +89,7 @@ public class SkylineAlgorithm {
* are dominated, so they are not part of the final skyline.
* Finally, the "cleaned" first half's and second half's skylines, are combined,
* producing the final skyline, which is returned.
*
* @param left the skyline of the left part of points
* @param right the skyline of the right part of points
* @return left the final skyline
@ -136,12 +129,13 @@ public class SkylineAlgorithm {
}
public static class Point {
private int x;
private int y;
/**
* The main constructor of Point Class, used to represent the 2 Dimension points.
*
* @param x the point's x-value.
* @param y the point's y-value.
*/
@ -149,34 +143,35 @@ public class SkylineAlgorithm {
this.x = x;
this.y = y;
}
/**
* @return x, the x-value
*/
public int getX() {
return x;
}
/**
* @return y, the y-value
*/
public int getY() {
return y;
}
/**
* Based on the skyline theory,
* it checks if the point that calls the function dominates the argument point.
*
* @param p1 the point that is compared
* @return true if the point wich calls the function dominates p1
* false otherwise.
*/
public boolean dominates(Point p1) {
// checks if p1 is dominated
if((this.x<p1.x && this.y<=p1.y) || (this.x<=p1.x && this.y<p1.y)){
return true;
}
return false;
return (this.x < p1.x && this.y <= p1.y) || (this.x <= p1.x && this.y < p1.y);
}
}
/**
* It is used to compare the 2 Dimension points,
* based on their x-values, in order get sorted later.
@ -184,7 +179,7 @@ public class SkylineAlgorithm {
class XComparator implements Comparator<Point> {
@Override
public int compare(Point a, Point b) {
return a.x < b.x ? -1 : a.x == b.x ? 0 : 1;
return Integer.compare(a.x, b.x);
}
}
}