2019-05-09 19:32:54 +08:00
|
|
|
package divideconquer;
|
|
|
|
|
2019-01-15 18:41:26 +08:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Comparator;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author dimgrichr
|
2019-01-15 19:54:43 +08:00
|
|
|
* <p>
|
2019-01-15 18:41:26 +08:00
|
|
|
* Space complexity: O(n)
|
|
|
|
* Time complexity: O(nlogn), because it is a divide and conquer algorithm
|
|
|
|
*/
|
|
|
|
public class SkylineAlgorithm {
|
|
|
|
private ArrayList<Point> points;
|
2019-01-15 19:54:43 +08:00
|
|
|
|
2019-01-15 18:41:26 +08:00
|
|
|
/**
|
|
|
|
* Main constructor of the application.
|
|
|
|
* ArrayList points gets created, which represents the sum of all edges.
|
|
|
|
*/
|
2019-01-15 19:54:43 +08:00
|
|
|
public SkylineAlgorithm() {
|
2019-01-15 18:41:26 +08:00
|
|
|
points = new ArrayList<>();
|
|
|
|
}
|
2019-01-15 19:54:43 +08:00
|
|
|
|
|
|
|
|
2019-01-15 18:41:26 +08:00
|
|
|
/**
|
|
|
|
* @return points, the ArrayList that includes all points.
|
|
|
|
*/
|
2019-01-15 19:54:43 +08:00
|
|
|
public ArrayList<Point> getPoints() {
|
2019-01-15 18:41:26 +08:00
|
|
|
return points;
|
|
|
|
}
|
2019-01-15 19:54:43 +08:00
|
|
|
|
|
|
|
|
2019-01-15 18:41:26 +08:00
|
|
|
/**
|
|
|
|
* The main divide and conquer, and also recursive algorithm.
|
|
|
|
* It gets an ArrayList full of points as an argument.
|
|
|
|
* If the size of that ArrayList is 1 or 2,
|
|
|
|
* the ArrayList is returned as it is, or with one less point
|
|
|
|
* (if the initial size is 2 and one of it's points, is dominated by the other one).
|
|
|
|
* On the other hand, if the ArrayList's size is bigger than 2,
|
2019-01-15 19:54:43 +08:00
|
|
|
* the function is called again, twice,
|
2019-01-15 18:41:26 +08:00
|
|
|
* 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.
|
2019-01-15 19:54:43 +08:00
|
|
|
*
|
2019-01-15 18:41:26 +08:00
|
|
|
* @param list, the initial list of points
|
|
|
|
* @return leftSkyLine, the combination of first half's and second half's skyline
|
|
|
|
* @see Point
|
|
|
|
*/
|
2019-01-15 19:54:43 +08:00
|
|
|
public ArrayList<Point> produceSubSkyLines(ArrayList<Point> list) {
|
|
|
|
|
|
|
|
// part where function exits flashback
|
2019-01-15 18:41:26 +08:00
|
|
|
int size = list.size();
|
2019-01-15 19:54:43 +08:00
|
|
|
if (size == 1) {
|
2019-01-15 18:41:26 +08:00
|
|
|
return list;
|
2019-01-15 19:54:43 +08:00
|
|
|
} else if (size == 2) {
|
|
|
|
if (list.get(0).dominates(list.get(1))) {
|
2019-01-15 18:41:26 +08:00
|
|
|
list.remove(1);
|
2019-01-15 19:54:43 +08:00
|
|
|
} else {
|
|
|
|
if (list.get(1).dominates(list.get(0))) {
|
2019-01-15 18:41:26 +08:00
|
|
|
list.remove(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return list;
|
|
|
|
}
|
2019-01-15 19:54:43 +08:00
|
|
|
|
|
|
|
// recursive part of the function
|
|
|
|
ArrayList<Point> leftHalf = new ArrayList<>();
|
|
|
|
ArrayList<Point> rightHalf = new ArrayList<>();
|
|
|
|
for (int i = 0; i < list.size(); i++) {
|
|
|
|
if (i < list.size() / 2) {
|
2019-01-15 18:41:26 +08:00
|
|
|
leftHalf.add(list.get(i));
|
2019-01-15 19:54:43 +08:00
|
|
|
} else {
|
2019-01-15 18:41:26 +08:00
|
|
|
rightHalf.add(list.get(i));
|
|
|
|
}
|
|
|
|
}
|
2019-01-15 19:54:43 +08:00
|
|
|
ArrayList<Point> leftSubSkyLine = produceSubSkyLines(leftHalf);
|
2019-05-09 19:32:54 +08:00
|
|
|
ArrayList<Point> rightSubSkyLine = produceSubSkyLines(rightHalf);
|
2019-01-15 19:54:43 +08:00
|
|
|
|
|
|
|
// skyline is produced
|
|
|
|
return produceFinalSkyLine(leftSubSkyLine, rightSubSkyLine);
|
2019-01-15 18:41:26 +08:00
|
|
|
}
|
2019-01-15 19:54:43 +08:00
|
|
|
|
|
|
|
|
2019-01-15 18:41:26 +08:00
|
|
|
/**
|
|
|
|
* The first half's skyline gets cleared
|
|
|
|
* from some points that are not part of the final skyline
|
|
|
|
* (Points with same x-value and different y=values. The point with the smallest y-value is kept).
|
|
|
|
* Then, the minimum y-value of the points of first half's skyline is found.
|
|
|
|
* That helps us to clear the second half's skyline, because, the points
|
|
|
|
* of second half's skyline that have greater y-value of the minimum y-value that we found before,
|
|
|
|
* 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.
|
2019-01-15 19:54:43 +08:00
|
|
|
*
|
|
|
|
* @param left the skyline of the left part of points
|
2019-01-15 18:41:26 +08:00
|
|
|
* @param right the skyline of the right part of points
|
|
|
|
* @return left the final skyline
|
|
|
|
*/
|
2019-01-15 19:54:43 +08:00
|
|
|
public ArrayList<Point> produceFinalSkyLine(ArrayList<Point> left, ArrayList<Point> right) {
|
|
|
|
|
|
|
|
// dominated points of ArrayList left are removed
|
|
|
|
for (int i = 0; i < left.size() - 1; i++) {
|
|
|
|
if (left.get(i).x == left.get(i + 1).x && left.get(i).y > left.get(i + 1).y) {
|
2019-01-15 18:41:26 +08:00
|
|
|
left.remove(i);
|
|
|
|
i--;
|
|
|
|
}
|
|
|
|
}
|
2019-01-15 19:54:43 +08:00
|
|
|
|
|
|
|
// minimum y-value is found
|
|
|
|
int min = left.get(0).y;
|
|
|
|
for (int i = 1; i < left.size(); i++) {
|
|
|
|
if (min > left.get(i).y) {
|
2019-01-15 18:41:26 +08:00
|
|
|
min = left.get(i).y;
|
2019-01-15 19:54:43 +08:00
|
|
|
if (min == 1) {
|
|
|
|
i = left.size();
|
2019-01-15 18:41:26 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-01-15 19:54:43 +08:00
|
|
|
|
|
|
|
// dominated points of ArrayList right are removed
|
|
|
|
for (int i = 0; i < right.size(); i++) {
|
|
|
|
if (right.get(i).y >= min) {
|
2019-01-15 18:41:26 +08:00
|
|
|
right.remove(i);
|
|
|
|
i--;
|
|
|
|
}
|
|
|
|
}
|
2019-01-15 19:54:43 +08:00
|
|
|
|
|
|
|
// final skyline found and returned
|
2019-01-15 18:41:26 +08:00
|
|
|
left.addAll(right);
|
|
|
|
return left;
|
|
|
|
}
|
|
|
|
|
2019-01-15 19:54:43 +08:00
|
|
|
|
|
|
|
public static class Point {
|
2019-01-15 18:41:26 +08:00
|
|
|
private int x;
|
|
|
|
private int y;
|
2019-01-15 19:54:43 +08:00
|
|
|
|
2019-01-15 18:41:26 +08:00
|
|
|
/**
|
|
|
|
* The main constructor of Point Class, used to represent the 2 Dimension points.
|
2019-01-15 19:54:43 +08:00
|
|
|
*
|
2019-01-15 18:41:26 +08:00
|
|
|
* @param x the point's x-value.
|
|
|
|
* @param y the point's y-value.
|
|
|
|
*/
|
2019-01-15 19:54:43 +08:00
|
|
|
public Point(int x, int y) {
|
|
|
|
this.x = x;
|
|
|
|
this.y = y;
|
2019-01-15 18:41:26 +08:00
|
|
|
}
|
2019-01-15 19:54:43 +08:00
|
|
|
|
2019-01-15 18:41:26 +08:00
|
|
|
/**
|
|
|
|
* @return x, the x-value
|
|
|
|
*/
|
2019-01-15 19:54:43 +08:00
|
|
|
public int getX() {
|
2019-01-15 18:41:26 +08:00
|
|
|
return x;
|
|
|
|
}
|
2019-01-15 19:54:43 +08:00
|
|
|
|
2019-01-15 18:41:26 +08:00
|
|
|
/**
|
|
|
|
* @return y, the y-value
|
|
|
|
*/
|
2019-01-15 19:54:43 +08:00
|
|
|
public int getY() {
|
2019-01-15 18:41:26 +08:00
|
|
|
return y;
|
|
|
|
}
|
2019-01-15 19:54:43 +08:00
|
|
|
|
2019-01-15 18:41:26 +08:00
|
|
|
/**
|
|
|
|
* Based on the skyline theory,
|
|
|
|
* it checks if the point that calls the function dominates the argument point.
|
2019-01-15 19:54:43 +08:00
|
|
|
*
|
2019-01-15 18:41:26 +08:00
|
|
|
* @param p1 the point that is compared
|
|
|
|
* @return true if the point wich calls the function dominates p1
|
2019-01-15 19:54:43 +08:00
|
|
|
* false otherwise.
|
2019-01-15 18:41:26 +08:00
|
|
|
*/
|
2019-01-15 19:54:43 +08:00
|
|
|
public boolean dominates(Point p1) {
|
|
|
|
// checks if p1 is dominated
|
|
|
|
return (this.x < p1.x && this.y <= p1.y) || (this.x <= p1.x && this.y < p1.y);
|
2019-01-15 18:41:26 +08:00
|
|
|
}
|
|
|
|
}
|
2019-01-15 19:54:43 +08:00
|
|
|
|
2019-01-15 18:41:26 +08:00
|
|
|
/**
|
|
|
|
* It is used to compare the 2 Dimension points,
|
|
|
|
* based on their x-values, in order get sorted later.
|
|
|
|
*/
|
|
|
|
class XComparator implements Comparator<Point> {
|
2019-01-15 19:54:43 +08:00
|
|
|
@Override
|
|
|
|
public int compare(Point a, Point b) {
|
|
|
|
return Integer.compare(a.x, b.x);
|
|
|
|
}
|
2019-01-15 18:41:26 +08:00
|
|
|
}
|
|
|
|
}
|