mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-25 13:34:54 +08:00
Update SkylineAlgorithm.java
This commit is contained in:
@ -1,36 +1,32 @@
|
|||||||
package skylinealgorithm;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Comparator;
|
import java.util.Comparator;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* @author dimgrichr
|
* @author dimgrichr
|
||||||
*
|
* <p>
|
||||||
* Space complexity: O(n)
|
* Space complexity: O(n)
|
||||||
* Time complexity: O(nlogn), because it is a divide and conquer algorithm
|
* Time complexity: O(nlogn), because it is a divide and conquer algorithm
|
||||||
*/
|
*/
|
||||||
public class SkylineAlgorithm {
|
public class SkylineAlgorithm {
|
||||||
private ArrayList<Point> points;
|
private ArrayList<Point> points;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Main constructor of the application.
|
* Main constructor of the application.
|
||||||
* ArrayList points gets created, which represents the sum of all edges.
|
* ArrayList points gets created, which represents the sum of all edges.
|
||||||
*/
|
*/
|
||||||
public SkylineAlgorithm(){
|
public SkylineAlgorithm() {
|
||||||
points = new ArrayList<>();
|
points = new ArrayList<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return points, the ArrayList that includes all points.
|
* @return points, the ArrayList that includes all points.
|
||||||
*/
|
*/
|
||||||
public ArrayList<Point> getPoints(){
|
public ArrayList<Point> getPoints() {
|
||||||
return points;
|
return points;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The main divide and conquer, and also recursive algorithm.
|
* The main divide and conquer, and also recursive algorithm.
|
||||||
* It gets an ArrayList full of points as an argument.
|
* It gets an ArrayList full of points as an argument.
|
||||||
@ -38,55 +34,51 @@ public class SkylineAlgorithm {
|
|||||||
* the ArrayList is returned as it is, or with one less point
|
* 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).
|
* (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,
|
* On the other hand, if the ArrayList's size is bigger than 2,
|
||||||
* the function is called again, twice,
|
* the function is called again, twice,
|
||||||
* with arguments the corresponding half of the initial ArrayList each time.
|
* with arguments the corresponding half of the initial ArrayList each time.
|
||||||
* Once the flashback has ended, the function produceFinalSkyLine gets called,
|
* Once the flashback has ended, the function produceFinalSkyLine gets called,
|
||||||
* in order to produce the final skyline, and return it.
|
* in order to produce the final skyline, and return it.
|
||||||
|
*
|
||||||
* @param list, the initial list of points
|
* @param list, the initial list of points
|
||||||
* @return leftSkyLine, the combination of first half's and second half's skyline
|
* @return leftSkyLine, the combination of first half's and second half's skyline
|
||||||
* @see Point
|
* @see Point
|
||||||
* @see produceFinalSkyLine
|
* @see produceFinalSkyLine
|
||||||
*/
|
*/
|
||||||
public ArrayList<Point> produceSubSkyLines(ArrayList<Point> list){
|
public ArrayList<Point> produceSubSkyLines(ArrayList<Point> list) {
|
||||||
|
|
||||||
//part where function exits flashback
|
// part where function exits flashback
|
||||||
int size = list.size();
|
int size = list.size();
|
||||||
if(size == 1){
|
if (size == 1) {
|
||||||
return list;
|
return list;
|
||||||
}
|
} else if (size == 2) {
|
||||||
else if(size==2){
|
if (list.get(0).dominates(list.get(1))) {
|
||||||
if(list.get(0).dominates(list.get(1))){
|
|
||||||
list.remove(1);
|
list.remove(1);
|
||||||
}
|
} else {
|
||||||
else{
|
if (list.get(1).dominates(list.get(0))) {
|
||||||
if(list.get(1).dominates(list.get(0))){
|
|
||||||
list.remove(0);
|
list.remove(0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
//recursive part of the function
|
// recursive part of the function
|
||||||
ArrayList<Point> leftHalf=new ArrayList<>();
|
ArrayList<Point> leftHalf = new ArrayList<>();
|
||||||
ArrayList<Point> rightHalf=new ArrayList<>();
|
ArrayList<Point> rightHalf = new ArrayList<>();
|
||||||
for (int i=0;i<list.size();i++){
|
for (int i = 0; i < list.size(); i++) {
|
||||||
if(i<list.size()/2){
|
if (i < list.size() / 2) {
|
||||||
leftHalf.add(list.get(i));
|
leftHalf.add(list.get(i));
|
||||||
}
|
} else {
|
||||||
else{
|
|
||||||
rightHalf.add(list.get(i));
|
rightHalf.add(list.get(i));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ArrayList<Point> leftSubSkyLine=new ArrayList<>();
|
ArrayList<Point> leftSubSkyLine = produceSubSkyLines(leftHalf);
|
||||||
ArrayList<Point> rightSubSkyLine=new ArrayList<>();
|
ArrayList<Point> rightSubSkyLine= produceSubSkyLines(rightHalf);
|
||||||
leftSubSkyLine=produceSubSkyLines(leftHalf);
|
|
||||||
rightSubSkyLine=produceSubSkyLines(rightHalf);
|
// skyline is produced
|
||||||
|
return produceFinalSkyLine(leftSubSkyLine, rightSubSkyLine);
|
||||||
//skyline is produced
|
|
||||||
return produceFinalSkyLine(leftSubSkyLine,rightSubSkyLine);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The first half's skyline gets cleared
|
* The first half's skyline gets cleared
|
||||||
* from some points that are not part of the final skyline
|
* from some points that are not part of the final skyline
|
||||||
@ -97,94 +89,97 @@ public class SkylineAlgorithm {
|
|||||||
* are dominated, so they are not part of the final skyline.
|
* are dominated, so they are not part of the final skyline.
|
||||||
* Finally, the "cleaned" first half's and second half's skylines, are combined,
|
* Finally, the "cleaned" first half's and second half's skylines, are combined,
|
||||||
* producing the final skyline, which is returned.
|
* producing the final skyline, which is returned.
|
||||||
* @param left the skyline of the left part of points
|
*
|
||||||
|
* @param left the skyline of the left part of points
|
||||||
* @param right the skyline of the right part of points
|
* @param right the skyline of the right part of points
|
||||||
* @return left the final skyline
|
* @return left the final skyline
|
||||||
*/
|
*/
|
||||||
public ArrayList<Point> produceFinalSkyLine(ArrayList<Point> left, ArrayList<Point> right){
|
public ArrayList<Point> produceFinalSkyLine(ArrayList<Point> left, ArrayList<Point> right) {
|
||||||
|
|
||||||
//dominated points of ArrayList left are removed
|
// dominated points of ArrayList left are removed
|
||||||
for(int i=0;i<left.size()-1;i++){
|
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){
|
if (left.get(i).x == left.get(i + 1).x && left.get(i).y > left.get(i + 1).y) {
|
||||||
left.remove(i);
|
left.remove(i);
|
||||||
i--;
|
i--;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//minimum y-value is found
|
// minimum y-value is found
|
||||||
int min=left.get(0).y;
|
int min = left.get(0).y;
|
||||||
for(int i=1;i<left.size();i++){
|
for (int i = 1; i < left.size(); i++) {
|
||||||
if(min>left.get(i).y){
|
if (min > left.get(i).y) {
|
||||||
min = left.get(i).y;
|
min = left.get(i).y;
|
||||||
if(min==1){
|
if (min == 1) {
|
||||||
i=left.size();
|
i = left.size();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//dominated points of ArrayList right are removed
|
// dominated points of ArrayList right are removed
|
||||||
for(int i=0;i<right.size();i++){
|
for (int i = 0; i < right.size(); i++) {
|
||||||
if(right.get(i).y>=min){
|
if (right.get(i).y >= min) {
|
||||||
right.remove(i);
|
right.remove(i);
|
||||||
i--;
|
i--;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//final skyline found and returned
|
// final skyline found and returned
|
||||||
left.addAll(right);
|
left.addAll(right);
|
||||||
return left;
|
return left;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public static class Point{
|
|
||||||
|
public static class Point {
|
||||||
private int x;
|
private int x;
|
||||||
private int y;
|
private int y;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The main constructor of Point Class, used to represent the 2 Dimension points.
|
* The main constructor of Point Class, used to represent the 2 Dimension points.
|
||||||
|
*
|
||||||
* @param x the point's x-value.
|
* @param x the point's x-value.
|
||||||
* @param y the point's y-value.
|
* @param y the point's y-value.
|
||||||
*/
|
*/
|
||||||
public Point(int x, int y){
|
public Point(int x, int y) {
|
||||||
this.x=x;
|
this.x = x;
|
||||||
this.y=y;
|
this.y = y;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return x, the x-value
|
* @return x, the x-value
|
||||||
*/
|
*/
|
||||||
public int getX(){
|
public int getX() {
|
||||||
return x;
|
return x;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return y, the y-value
|
* @return y, the y-value
|
||||||
*/
|
*/
|
||||||
public int getY(){
|
public int getY() {
|
||||||
return y;
|
return y;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Based on the skyline theory,
|
* Based on the skyline theory,
|
||||||
* it checks if the point that calls the function dominates the argument point.
|
* it checks if the point that calls the function dominates the argument point.
|
||||||
|
*
|
||||||
* @param p1 the point that is compared
|
* @param p1 the point that is compared
|
||||||
* @return true if the point wich calls the function dominates p1
|
* @return true if the point wich calls the function dominates p1
|
||||||
* false otherwise.
|
* false otherwise.
|
||||||
*/
|
*/
|
||||||
public boolean dominates(Point p1){
|
public boolean dominates(Point p1) {
|
||||||
|
// checks if p1 is dominated
|
||||||
//checks if p1 is dominated
|
return (this.x < p1.x && this.y <= p1.y) || (this.x <= p1.x && this.y < p1.y);
|
||||||
if((this.x<p1.x && this.y<=p1.y) || (this.x<=p1.x && this.y<p1.y)){
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* It is used to compare the 2 Dimension points,
|
* It is used to compare the 2 Dimension points,
|
||||||
* based on their x-values, in order get sorted later.
|
* based on their x-values, in order get sorted later.
|
||||||
*/
|
*/
|
||||||
class XComparator implements Comparator<Point> {
|
class XComparator implements Comparator<Point> {
|
||||||
@Override
|
@Override
|
||||||
public int compare(Point a, Point b) {
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user