1- package skylinealgorithm ;
2-
31import java .util .ArrayList ;
42import java .util .Comparator ;
53
64/**
7- *
85 * @author dimgrichr
9- *
6+ * <p>
107 * Space complexity: O(n)
118 * Time complexity: O(nlogn), because it is a divide and conquer algorithm
129 */
1310public class SkylineAlgorithm {
1411 private ArrayList <Point > points ;
15-
16-
12+
1713 /**
1814 * Main constructor of the application.
1915 * ArrayList points gets created, which represents the sum of all edges.
2016 */
21- public SkylineAlgorithm (){
17+ public SkylineAlgorithm () {
2218 points = new ArrayList <>();
2319 }
24-
25-
20+
21+
2622 /**
2723 * @return points, the ArrayList that includes all points.
2824 */
29- public ArrayList <Point > getPoints (){
25+ public ArrayList <Point > getPoints () {
3026 return points ;
3127 }
32-
33-
28+
29+
3430 /**
3531 * The main divide and conquer, and also recursive algorithm.
3632 * It gets an ArrayList full of points as an argument.
3733 * If the size of that ArrayList is 1 or 2,
3834 * the ArrayList is returned as it is, or with one less point
3935 * (if the initial size is 2 and one of it's points, is dominated by the other one).
4036 * On the other hand, if the ArrayList's size is bigger than 2,
41- * the function is called again, twice,
37+ * the function is called again, twice,
4238 * with arguments the corresponding half of the initial ArrayList each time.
4339 * Once the flashback has ended, the function produceFinalSkyLine gets called,
4440 * in order to produce the final skyline, and return it.
41+ *
4542 * @param list, the initial list of points
4643 * @return leftSkyLine, the combination of first half's and second half's skyline
4744 * @see Point
4845 * @see produceFinalSkyLine
4946 */
50- public ArrayList <Point > produceSubSkyLines (ArrayList <Point > list ){
51-
52- //part where function exits flashback
47+ public ArrayList <Point > produceSubSkyLines (ArrayList <Point > list ) {
48+
49+ // part where function exits flashback
5350 int size = list .size ();
54- if (size == 1 ){
51+ if (size == 1 ) {
5552 return list ;
56- }
57- else if (size ==2 ){
58- if (list .get (0 ).dominates (list .get (1 ))){
53+ } else if (size == 2 ) {
54+ if (list .get (0 ).dominates (list .get (1 ))) {
5955 list .remove (1 );
60- }
61- else {
62- if (list .get (1 ).dominates (list .get (0 ))){
56+ } else {
57+ if (list .get (1 ).dominates (list .get (0 ))) {
6358 list .remove (0 );
6459 }
6560 }
6661 return list ;
6762 }
68-
69- //recursive part of the function
70- ArrayList <Point > leftHalf = new ArrayList <>();
71- ArrayList <Point > rightHalf = new ArrayList <>();
72- for (int i = 0 ; i < list .size ();i ++){
73- if ( i < list .size ()/ 2 ) {
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 ) {
7469 leftHalf .add (list .get (i ));
75- }
76- else {
70+ } else {
7771 rightHalf .add (list .get (i ));
7872 }
7973 }
80- ArrayList <Point > leftSubSkyLine =new ArrayList <>();
81- ArrayList <Point > rightSubSkyLine =new ArrayList <>();
82- leftSubSkyLine =produceSubSkyLines (leftHalf );
83- rightSubSkyLine =produceSubSkyLines (rightHalf );
84-
85- //skyline is produced
86- return produceFinalSkyLine (leftSubSkyLine ,rightSubSkyLine );
74+ ArrayList <Point > leftSubSkyLine = produceSubSkyLines (leftHalf );
75+ ArrayList <Point > rightSubSkyLine = produceSubSkyLines (rightHalf );
76+
77+ // skyline is produced
78+ return produceFinalSkyLine (leftSubSkyLine , rightSubSkyLine );
8779 }
88-
89-
80+
81+
9082 /**
9183 * The first half's skyline gets cleared
9284 * from some points that are not part of the final skyline
@@ -97,94 +89,97 @@ else if(size==2){
9789 * are dominated, so they are not part of the final skyline.
9890 * Finally, the "cleaned" first half's and second half's skylines, are combined,
9991 * producing the final skyline, which is returned.
100- * @param left the skyline of the left part of points
92+ *
93+ * @param left the skyline of the left part of points
10194 * @param right the skyline of the right part of points
10295 * @return left the final skyline
10396 */
104- public ArrayList <Point > produceFinalSkyLine (ArrayList <Point > left , ArrayList <Point > right ){
105-
106- //dominated points of ArrayList left are removed
107- for (int i = 0 ; i < left .size ()- 1 ; i ++){
108- if (left .get (i ).x == left .get (i + 1 ).x && left .get (i ).y > left .get (i + 1 ).y ){
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 ) {
109102 left .remove (i );
110103 i --;
111104 }
112105 }
113-
114- //minimum y-value is found
115- int min = left .get (0 ).y ;
116- for (int i = 1 ; i < left .size ();i ++){
117- if (min > left .get (i ).y ){
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 ) {
118111 min = left .get (i ).y ;
119- if (min == 1 ) {
120- i = left .size ();
112+ if (min == 1 ) {
113+ i = left .size ();
121114 }
122115 }
123116 }
124-
125- //dominated points of ArrayList right are removed
126- for (int i = 0 ; i < right .size ();i ++){
127- if (right .get (i ).y >= min ){
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 ) {
128121 right .remove (i );
129122 i --;
130123 }
131124 }
132-
133- //final skyline found and returned
125+
126+ // final skyline found and returned
134127 left .addAll (right );
135128 return left ;
136129 }
137-
138-
139130
140- public static class Point {
131+
132+ public static class Point {
141133 private int x ;
142134 private int y ;
135+
143136 /**
144137 * The main constructor of Point Class, used to represent the 2 Dimension points.
138+ *
145139 * @param x the point's x-value.
146140 * @param y the point's y-value.
147141 */
148- public Point (int x , int y ){
149- this .x = x ;
150- this .y = y ;
142+ public Point (int x , int y ) {
143+ this .x = x ;
144+ this .y = y ;
151145 }
146+
152147 /**
153148 * @return x, the x-value
154149 */
155- public int getX (){
150+ public int getX () {
156151 return x ;
157152 }
153+
158154 /**
159155 * @return y, the y-value
160156 */
161- public int getY (){
157+ public int getY () {
162158 return y ;
163159 }
160+
164161 /**
165162 * Based on the skyline theory,
166163 * it checks if the point that calls the function dominates the argument point.
164+ *
167165 * @param p1 the point that is compared
168166 * @return true if the point wich calls the function dominates p1
169- * false otherwise.
167+ * false otherwise.
170168 */
171- public boolean dominates (Point p1 ){
172-
173- //checks if p1 is dominated
174- if ((this .x <p1 .x && this .y <=p1 .y ) || (this .x <=p1 .x && this .y <p1 .y )){
175- return true ;
176- }
177- return false ;
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 );
178172 }
179173 }
174+
180175 /**
181176 * It is used to compare the 2 Dimension points,
182177 * based on their x-values, in order get sorted later.
183178 */
184179 class XComparator implements Comparator <Point > {
185- @ Override
186- public int compare (Point a , Point b ) {
187- return a . x < b . x ? - 1 : a .x == b .x ? 0 : 1 ;
188- }
180+ @ Override
181+ public int compare (Point a , Point b ) {
182+ return Integer . compare ( a .x , b .x ) ;
183+ }
189184 }
190185}
0 commit comments