|
| 1 | +import java.util.ArrayList; |
| 2 | +import java.util.Comparator; |
| 3 | + |
| 4 | +/** |
| 5 | + * @author dimgrichr |
| 6 | + * <p> |
| 7 | + * Space complexity: O(n) |
| 8 | + * Time complexity: O(nlogn), because it is a divide and conquer algorithm |
| 9 | + */ |
| 10 | +public class SkylineAlgorithm { |
| 11 | + private ArrayList<Point> points; |
| 12 | + |
| 13 | + /** |
| 14 | + * Main constructor of the application. |
| 15 | + * ArrayList points gets created, which represents the sum of all edges. |
| 16 | + */ |
| 17 | + public SkylineAlgorithm() { |
| 18 | + points = new ArrayList<>(); |
| 19 | + } |
| 20 | + |
| 21 | + |
| 22 | + /** |
| 23 | + * @return points, the ArrayList that includes all points. |
| 24 | + */ |
| 25 | + public ArrayList<Point> getPoints() { |
| 26 | + return points; |
| 27 | + } |
| 28 | + |
| 29 | + |
| 30 | + /** |
| 31 | + * The main divide and conquer, and also recursive algorithm. |
| 32 | + * It gets an ArrayList full of points as an argument. |
| 33 | + * If the size of that ArrayList is 1 or 2, |
| 34 | + * the ArrayList is returned as it is, or with one less point |
| 35 | + * (if the initial size is 2 and one of it's points, is dominated by the other one). |
| 36 | + * On the other hand, if the ArrayList's size is bigger than 2, |
| 37 | + * the function is called again, twice, |
| 38 | + * with arguments the corresponding half of the initial ArrayList each time. |
| 39 | + * Once the flashback has ended, the function produceFinalSkyLine gets called, |
| 40 | + * in order to produce the final skyline, and return it. |
| 41 | + * |
| 42 | + * @param list, the initial list of points |
| 43 | + * @return leftSkyLine, the combination of first half's and second half's skyline |
| 44 | + * @see Point |
| 45 | + * @see produceFinalSkyLine |
| 46 | + */ |
| 47 | + public ArrayList<Point> produceSubSkyLines(ArrayList<Point> list) { |
| 48 | + |
| 49 | + // part where function exits flashback |
| 50 | + int size = list.size(); |
| 51 | + if (size == 1) { |
| 52 | + return list; |
| 53 | + } else if (size == 2) { |
| 54 | + if (list.get(0).dominates(list.get(1))) { |
| 55 | + list.remove(1); |
| 56 | + } else { |
| 57 | + if (list.get(1).dominates(list.get(0))) { |
| 58 | + list.remove(0); |
| 59 | + } |
| 60 | + } |
| 61 | + return list; |
| 62 | + } |
| 63 | + |
| 64 | + // recursive part of the function |
| 65 | + ArrayList<Point> leftHalf = new ArrayList<>(); |
| 66 | + ArrayList<Point> rightHalf = new ArrayList<>(); |
| 67 | + for (int i = 0; i < list.size(); i++) { |
| 68 | + if (i < list.size() / 2) { |
| 69 | + leftHalf.add(list.get(i)); |
| 70 | + } else { |
| 71 | + rightHalf.add(list.get(i)); |
| 72 | + } |
| 73 | + } |
| 74 | + ArrayList<Point> leftSubSkyLine = produceSubSkyLines(leftHalf); |
| 75 | + ArrayList<Point> rightSubSkyLine= produceSubSkyLines(rightHalf); |
| 76 | + |
| 77 | + // skyline is produced |
| 78 | + return produceFinalSkyLine(leftSubSkyLine, rightSubSkyLine); |
| 79 | + } |
| 80 | + |
| 81 | + |
| 82 | + /** |
| 83 | + * The first half's skyline gets cleared |
| 84 | + * from some points that are not part of the final skyline |
| 85 | + * (Points with same x-value and different y=values. The point with the smallest y-value is kept). |
| 86 | + * Then, the minimum y-value of the points of first half's skyline is found. |
| 87 | + * That helps us to clear the second half's skyline, because, the points |
| 88 | + * of second half's skyline that have greater y-value of the minimum y-value that we found before, |
| 89 | + * are dominated, so they are not part of the final skyline. |
| 90 | + * Finally, the "cleaned" first half's and second half's skylines, are combined, |
| 91 | + * producing the final skyline, which is returned. |
| 92 | + * |
| 93 | + * @param left the skyline of the left part of points |
| 94 | + * @param right the skyline of the right part of points |
| 95 | + * @return left the final skyline |
| 96 | + */ |
| 97 | + public ArrayList<Point> produceFinalSkyLine(ArrayList<Point> left, ArrayList<Point> right) { |
| 98 | + |
| 99 | + // dominated points of ArrayList left are removed |
| 100 | + for (int i = 0; i < left.size() - 1; i++) { |
| 101 | + if (left.get(i).x == left.get(i + 1).x && left.get(i).y > left.get(i + 1).y) { |
| 102 | + left.remove(i); |
| 103 | + i--; |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + // minimum y-value is found |
| 108 | + int min = left.get(0).y; |
| 109 | + for (int i = 1; i < left.size(); i++) { |
| 110 | + if (min > left.get(i).y) { |
| 111 | + min = left.get(i).y; |
| 112 | + if (min == 1) { |
| 113 | + i = left.size(); |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + // dominated points of ArrayList right are removed |
| 119 | + for (int i = 0; i < right.size(); i++) { |
| 120 | + if (right.get(i).y >= min) { |
| 121 | + right.remove(i); |
| 122 | + i--; |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + // final skyline found and returned |
| 127 | + left.addAll(right); |
| 128 | + return left; |
| 129 | + } |
| 130 | + |
| 131 | + |
| 132 | + public static class Point { |
| 133 | + private int x; |
| 134 | + private int y; |
| 135 | + |
| 136 | + /** |
| 137 | + * The main constructor of Point Class, used to represent the 2 Dimension points. |
| 138 | + * |
| 139 | + * @param x the point's x-value. |
| 140 | + * @param y the point's y-value. |
| 141 | + */ |
| 142 | + public Point(int x, int y) { |
| 143 | + this.x = x; |
| 144 | + this.y = y; |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * @return x, the x-value |
| 149 | + */ |
| 150 | + public int getX() { |
| 151 | + return x; |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * @return y, the y-value |
| 156 | + */ |
| 157 | + public int getY() { |
| 158 | + return y; |
| 159 | + } |
| 160 | + |
| 161 | + /** |
| 162 | + * Based on the skyline theory, |
| 163 | + * it checks if the point that calls the function dominates the argument point. |
| 164 | + * |
| 165 | + * @param p1 the point that is compared |
| 166 | + * @return true if the point wich calls the function dominates p1 |
| 167 | + * false otherwise. |
| 168 | + */ |
| 169 | + public boolean dominates(Point p1) { |
| 170 | + // checks if p1 is dominated |
| 171 | + return (this.x < p1.x && this.y <= p1.y) || (this.x <= p1.x && this.y < p1.y); |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + /** |
| 176 | + * It is used to compare the 2 Dimension points, |
| 177 | + * based on their x-values, in order get sorted later. |
| 178 | + */ |
| 179 | + class XComparator implements Comparator<Point> { |
| 180 | + @Override |
| 181 | + public int compare(Point a, Point b) { |
| 182 | + return Integer.compare(a.x, b.x); |
| 183 | + } |
| 184 | + } |
| 185 | +} |
0 commit comments