ポイント間の最短距離アルゴリズム
-
05-07-2019 - |
質問
平面上の一連の点を与え、これらの点のいずれか2つによって形成される最短線分を見つけます。
どうすればそれができますか?簡単な方法は明らかに各距離を計算することですが、比較するには別のアルゴリズムが必要です。
解決
http://en.wikipedia.org/wiki/Closest_pair_of_points
問題は、次のように、再帰的な分割統治アプローチを使用してO(n log n)時間で解決できます:
- x座標に沿ったポイントのソート
- 点のセットを垂直線x = xmidで2つの等しいサイズのサブセットに分割します
- 左および右のサブセットで問題を再帰的に解決します。これにより、左側と右側の最小距離dLminとdRminがそれぞれ得られます。
- 1つのポイントが分割する垂直線の左側にあり、2番目のポイントが右側にあるポイントのペア間の最小距離dLRminを見つけます。
- 最終的な答えは、dLmin、dRmin、およびdLRminの最小値です。
他のヒント
ブルートフォース手法よりも迅速な代替策をすぐに思いつくことはできません(ただし、十分な量が必要です)が、選択したアルゴリズムはすべて、各ポイント間の距離を計算しません 。距離を比較する必要がある場合は、距離の平方を比較するだけで、高価で完全に冗長な平方根を回避できます。
1つの可能性は、X座標でポイントを並べ替えることです(またはY-どちらでもかまいませんが、一貫性があるだけです)。その後、それを使用して、他の多くのポイントとの比較を排除できます。 point [i]とpoint [j]の間の距離を見ているときに、X距離だけが現在の最短距離よりも大きい場合、point [j + 1] ... point [N]は次のように削除できます。よく(i<j
を想定-j<i
の場合、削除されるのはpoint [0] ... point [i]です)。
ポイントが極座標として始まる場合、同じもののバリエーションを使用できます-原点からの距離でソートし、原点からの距離の差が現在の最短距離よりも大きい場合、排除することができますその点、および現在検討しているものよりも原点から遠い(または近い)他のすべての点。
Delaunay三角形分割から、および< href = "http://en.wikipedia.org/wiki/Voronoi_diagram" rel = "nofollow noreferrer">ボロノイ図。
この問題には標準のアルゴリズムがあります。ここで見つけることができます: http://www.cs.mcgill.ca/~cs251/ClosestPair/ ClosestPairPS.html
そして、このアルゴリズムの実装です。コメントなしで申し訳ありません。
static long distSq(Point a, Point b) {
return ((long) (a.x - b.x) * (long) (a.x - b.x) + (long) (a.y - b.y) * (long) (a.y - b.y));
}
static long ccw(Point p1, Point p2, Point p3) {
return (long) (p2.x - p1.x) * (long) (p3.y - p1.y) - (long) (p2.y - p1.y) * (long) (p3.x - p1.x);
}
static List<Point> convexHull(List<Point> P) {
if (P.size() < 3) {
//WTF
return null;
}
int k = 0;
for (int i = 0; i < P.size(); i++) {
if (P.get(i).y < P.get(k).y || (P.get(i).y == P.get(k).y && P.get(i).x < P.get(k).x)) {
k = i;
}
}
Collections.swap(P, k, P.size() - 1);
final Point o = P.get(P.size() - 1);
P.remove(P.size() - 1);
Collections.sort(P, new Comparator() {
public int compare(Object o1, Object o2) {
Point a = (Point) o1;
Point b = (Point) o2;
long t1 = (long) (a.y - o.y) * (long) (b.x - o.x) - (long) (a.x - o.x) * (long) (b.y - o.y);
if (t1 == 0) {
long tt = distSq(o, a);
tt -= distSq(o, b);
if (tt > 0) {
return 1;
} else if (tt < 0) {
return -1;
}
return 0;
}
if (t1 < 0) {
return -1;
}
return 1;
}
});
List<Point> hull = new ArrayList<Point>();
hull.add(o);
hull.add(P.get(0));
for (int i = 1; i < P.size(); i++) {
while (hull.size() >= 2 &&
ccw(hull.get(hull.size() - 2), hull.get(hull.size() - 1), P.get(i)) <= 0) {
hull.remove(hull.size() - 1);
}
hull.add(P.get(i));
}
return hull;
}
static long nearestPoints(List<Point> P, int l, int r) {
if (r - l == P.size()) {
Collections.sort(P, new Comparator() {
public int compare(Object o1, Object o2) {
int t = ((Point) o1).x - ((Point) o2).x;
if (t == 0) {
return ((Point) o1).y - ((Point) o2).y;
}
return t;
}
});
}
if (r - l <= 100) {
long ret = distSq(P.get(l), P.get(l + 1));
for (int i = l; i < r; i++) {
for (int j = i + 1; j < r; j++) {
ret = Math.min(ret, distSq(P.get(i), P.get(j)));
}
}
return ret;
}
int c = (l + r) / 2;
long lD = nearestPoints(P, l, c);
long lR = nearestPoints(P, c + 1, r);
long ret = Math.min(lD, lR);
Set<Point> set = new TreeSet<Point>(new Comparator<Point>() {
public int compare(Point o1, Point o2) {
int t = o1.y - o2.y;
if (t == 0) {
return o1.x - o2.x;
}
return t;
}
});
for (int i = l; i < r; i++) {
set.add(P.get(i));
}
int x = P.get(c).x;
double theta = Math.sqrt(ret);
Point[] Q = set.toArray(new Point[0]);
Point[] T = new Point[Q.length];
int pos = 0;
for (int i = 0; i < Q.length; i++) {
if (Q[i].x - x + 1 > theta) {
continue;
}
T[pos++] = Q[i];
}
for (int i = 0; i < pos; i++) {
for (int j = 1; j < 7 && i + j < pos; j++) {
ret = Math.min(ret, distSq(T[i], T[j + i]));
}
}
return ret;
}
質問から、セグメントの距離を探しているのか、セグメント自体を探しているのかは明確ではありません。距離を探していると仮定すると(1から5までの番号が付けられた5点が与えられ、距離が最小の2点であることがわかったら、単純な修正のセグメント)、
compare 1 with 2,3,4,5, then
compare 2, with 3,4,5, then
compare 3 with 4,5, then
compare 4 with 5.
距離の可換性を考えると、他の比較を実行する必要はありません。 Pythonでは、何かのように聞こえるかもしれません
import numpy as np
def find_min_distance_of_a_cloud(cloud):
"""
Given a cloud of points in the n-dim space, provides the minimal distance.
:param cloud: list of nX1-d vectors, as ndarray.
:return:
"""
dist_min = None
for i, p_i in enumerate(cloud[:-1]):
new_dist_min = np.min([np.linalg.norm(p_i - p_j) for p_j in cloud[(i + 1):]])
if dist_min is None or dist_min > new_dist_min:
dist_min = new_dist_min
return dist_min
次のコードのようなものでテストできます:
from nose.tools import assert_equal
def test_find_min_distance_of_a_cloud_1pt():
cloud = [np.array((1, 1, 1)), np.array((0, 0, 0))]
min_out = find_min_distance_of_a_cloud(cloud)
assert_equal(min_out, np.sqrt(3))
def test_find_min_distance_of_a_cloud_5pt():
cloud = [np.array((0, 0, 0)),
np.array((1, 1, 0)),
np.array((2, 1, 4)),
np.array((3, 4, 4)),
np.array((5, 3, 4))]
min_out = find_min_distance_of_a_cloud(cloud)
assert_equal(min_out, np.sqrt(2))
2つ以上のポイントが同じ最小距離を持つことができ、セグメントを探している場合、提案されたコードを再度変更する必要があり、出力は距離が最小のポイントのリスト(またはポイントのカップル)になります)。役に立てば幸いです!