Given list of 2d points, find the point closest to all other points | Hello World
Lets analysis this problem. We need to find the point closest to all other points in a 2d plane. What if it is only 1d plane? The point is the median.
What about 2d plane, we can first map all the points to the x axis and sort the points and thus forms a 1d array. Then we can calculate the sum of distance to all the other points for each point (by scanning the array from left to right then right to left). Then we do the same on y axis. The distance between two points is x^2 + y^2 and because x and y are all distance, which means x and y are all >= 1, so if x1 + y1 >= x2 + y2 => x1^2 + y1^2 >= x2^2 + y2^2.
http://stackoverflow.com/questions/12905663/given-list-of-2d-points-find-the-point-closest-to-all-other-points
Read full article from Given list of 2d points, find the point closest to all other points | Hello World
Lets analysis this problem. We need to find the point closest to all other points in a 2d plane. What if it is only 1d plane? The point is the median.
What about 2d plane, we can first map all the points to the x axis and sort the points and thus forms a 1d array. Then we can calculate the sum of distance to all the other points for each point (by scanning the array from left to right then right to left). Then we do the same on y axis. The distance between two points is x^2 + y^2 and because x and y are all distance, which means x and y are all >= 1, so if x1 + y1 >= x2 + y2 => x1^2 + y1^2 >= x2^2 + y2^2.
http://stackoverflow.com/questions/12905663/given-list-of-2d-points-find-the-point-closest-to-all-other-points
Note that in 1-D the point that minimizes the sum of distances to all the points is the median.
In 2-D the problem can be solved in
O(n log n)
as follows:
Create a sorted array of x-coordinates and for each element in the array compute the "horizontal" cost of choosing that coordinate. The horizontal cost of an element is the sum of distances to all the points projected onto the X-axis. This can be computed in linear time by scanning the array twice (once from left to right and once in the reverse direction). Similarly create a sorted array of y-coordinates and for each element in the array compute the "vertical" cost of choosing that coordinate.
Now for each point in the original array, we can compute the total cost to all other points in
O(1)
time by adding the horizontal and vertical costs. So we can compute the optimal point in O(n)
. Thus the total running time is O(n log n)
.
public
Point findMin(Point[] p) {
HashMap<Point, Integer> map =
new
HashMap<Point, Integer>();
for
(
int
i =
0
; i < p.length; i++) {
map.add(p[i],
0
);
}
Arrays.sort(p,
new
Comparator<Point>() {
@Override
public
int
compare(Point p1, Point p2) {
if
(p1.x > p2.x)
return
1
;
if
(p1.x < p2.x)
return
-
1
;
return
0
;
}
});
int
[] disX =
new
int
[p.length];
//find distance from the left
disX[
0
] =
0
;
for
(
int
i =
1
; i < disX.length; i++) {
disX[i] = i*(p[i] - p[i-
1
]) + disX[i-
1
];
}
//find distance from the right
int
[] tmpX =
new
int
[p.length];
tmpX[p.length -
1
] =
0
;
for
(
int
i = tmpX.length -
2
; i >=
0
; i--) {
tmpX[i] = (p.length - i)*(p[i]-p[i+
1
]) + tmpX[i+
1
];
disX[i] += tmpX[i];
map.put(p[i], disX[i]);
}
//do the same for y
Arrays.sort(p,
new
Comparator<Point>() {
@Override
public
int
compare(Point p1, Point p2) {
if
(p1.y > p2.y)
return
1
;
if
(p1.y < p2.y)
return
-
1
;
return
0
;
}
});
int
[] disY =
new
int
[p.length];
//find distance from the left
disY[
0
] =
0
;
for
(
int
i =
1
; i < disY.length; i++) {
disY[i] = i*(p[i] - p[i-
1
]) + disY[i-
1
];
}
//find distance from the right
int
[] tmpY =
new
int
[p.length];
tmpY[p.length -
1
] =
0
;
for
(
int
i = tmpY.length -
2
; i >=
0
; i--) {
tmpY[i] = (p.length - i)*(p[i]-p[i+
1
]) + tmpY[i+
1
];
disY[i] += tmpY[i];
map.put(p[i], map.get(p[i]) + disY[i]);
}
//calculate the 2d distance by adding x and y
Point min = p[
0
];
int
minDis = map.get(p[
0
]);
for
(
int
i =
1
; i < p.length; i++) {
if
(map.get(p[i]) < minDis) {
minDis = map.get(p[i]);
min = p[i];
}
}
return
min;
}