Browse Source

add closest pair

Olivier Marty 8 years ago
parent
commit
70254132cf
3 changed files with 48 additions and 0 deletions
  1. 44 0
      code/ClosestPair.cpp
  2. 1 0
      code/MaxBipartiteMatching_sqrt.cc
  3. 3 0
      main.tex

+ 44 - 0
code/ClosestPair.cpp

@@ -0,0 +1,44 @@
+struct vect {
+  double x, y;
+  vect(double a=0.0, double b=0.0) : x(a), y(b) {}
+  double length() {
+    return sqrt(x*x+y*y);
+  }
+  vect operator-(vect a) {
+    return vect(x-a.x, y-a.y);
+  }
+};
+double CP_Search(vect *s, vect *x[], vect *y[], int n, int &a, int &b, double d) {
+  if(n == 1) return d;
+  vect *z[maxn];
+  int m = n>>1, p = 0;
+  static bool u[maxn];
+  for(int i = 0; i < m; i++) u[x[i]-s] = true;
+  for(int i = 0, j = 0, k = m; i < n; i++)
+    if(u[y[i]-s]) z[j++]=y[i]; else z[k++]=y[i];
+  for(int i = 0; i < m; i++) u[x[i]-s] = false;
+  d = CP_Search(s, x, z, m, a, b, d);
+  d = CP_Search(s, x+m, z+m, n-m, a, b, d);
+  for(int i = 0; i < n; i++)
+    if(fabs(y[i]->x - x[m]->x)<d) z[p++] = y[i];
+  for(int i = 0; i < p; i++)
+    for(int j = i+1; j < p && z[j]->y - z[i]->y < d; j++) {
+      double tmp = (*z[j] - *z[i]).length();
+      if(d>tmp) { d=tmp; a=z[j]-s; b=z[i]-s; }
+    }
+  return d;
+}
+bool CP_cmpx(const vect *a, const vect *b) {
+  return a->x < b->x;
+}
+bool CP_cmpy(const vect *a, const vect *b) {
+  return a->y < b->y;
+}
+double ClosestPair(vect s[], int n, int &a, int &b) {
+  vect *x[maxn], *y[maxn];
+  for(int i = 0; i < n; i++)
+    x[i] = y[i] = s+i;
+  sort(x, x+n, CP_cmpx);
+  sort(y, y+n, CP_cmpy);
+  return CP_Search(s,x,y,n,a,b,INF);
+}

+ 1 - 0
code/MaxBipartiteMatching_sqrt.cc

@@ -1,3 +1,4 @@
+// Hopcroft–Karp algorithm (not sure)
 // O(sqrt(n)*E)
 // Use : set G (adjacency list), set uN, then m = MaxMatch() and the result is in Mx and My
 const int MAXN = 1000; const int INF = 0x3f3f3f3f; vector<int>G [MAXN];

+ 3 - 0
main.tex

@@ -107,6 +107,9 @@ Temps de cuisson : $O(n)$
 % OK RotateCCW
 % OK ComputeSignedArea
 
+\subsection{Closest Pair}
+{\scriptsize\lstinputlisting{code/ClosestPair.cpp}}
+
 \subsection{Slow Delaunay triangulation}
 {\scriptsize\lstinputlisting{code/Delaunay.cpp}} % TODO chercher algo en N (incremental flip)