Talk:Closest-pair problem/C
your code does NOT RUN when i compile with devc or Turbo C???? (unsigned comment added by 113.22.126.190 at 21:06, 24 October 2010)
- Correct it? Try setting compiler flags for compatibility with a specific C standard? --Michael Mol 13:11, 25 October 2010 (UTC)
- "The code does not run"... is like pretending to derive solar physics by the statement "the sun shines"... By the way "devc" (Dev C++ IDE?) usually is used with (an old version of) gcc. On my test machine (GNU/Linux) it runs, except for some "evil dataset" that someone gave me once upon a time... I've inspected the code with valgrind, debugged it, ... but I was not able to unwind the flow that gives the problem... I know this code hides some oddity somewhere. Likely the better thing is to rewrite it from scratch, but I've not the courage yet! :) — ShinTakezou 17:52, 30 May 2011 (UTC)
Propose replacing code
I suggest replace current code sample with rewritten code below. Reasons: 1. It doesn't segfault with 200,000 points or more; 2. It's shorter and quite a bit faster; 3. It's cleaner IMO. <lang C>#include <stdio.h>
- include <stdlib.h>
- include <values.h>
- include <math.h>
typedef struct { double x, y; } point_t, *point;
/* note: even though l_list and r_list are used by each recursion of closest(), they
are always used in sequence, no need to allocate them repeatedly */
int *l_list, *r_list;
inline double dist(point a, point b) {
double dx = a->x - b->x, dy = a->y - b->y; return dx * dx + dy * dy;
}
inline int cmp_dbl(double a, double b) {
return a < b ? -1 : a > b ? 1 : 0;
}
double brute_force(point pts, int max_n, int *a, int *b)
{
int i, j; point pi; double d, min_dist = MAXDOUBLE;
for (i = 0, pi = pts; i < max_n; pi++, i++) { for (j = i + 1; j < max_n; j++) { if ( (d = dist(pi, pts + j)) < min_dist ) { *a = i; *b = j; min_dist = d; } } } return min_dist;
}
void sort_y(point pts, int n, int *list) {
int cmp_y(const void *a, const void *b) { return cmp_dbl(pts[*(int*)a].y, pts[*(int*)b].y); } qsort(list, n, sizeof(int), cmp_y);
}
double closest(point pts, int n, point *a, point *b) {
int left, right, lsize, rsize, i; point pl, a1, b1; double min_d, d, dsqrt, median;
/* don't divide if there aren't enough points to divide */ if (n == 2) { *a = pts; *b = pts + 1; return dist(pts, pts + 1); }
if (n == 3) { min_d = dist(pts, pts + 1); *a = pts; *b = pts + 1;
if (min_d > (d = dist(pts, pts + 2))) { min_d = d; *b = pts + 2; } if (min_d > (d = dist(pts + 1, pts + 2))) { min_d = d; *a = pts + 1; *b = pts + 2; } return min_d; }
/* get left and right results */ left = right = n / 2; if ((min_d = closest(pts, left, a, b)) > (d = closest(pts + left, n - left, &a1, &b1))) { min_d = d; *a = a1; *b = b1; }
median = pts[left].x; dsqrt = sqrt(min_d);
/* find points within +- min distance on X from the center line, list their indices */ for ( lsize = 0, left--; median - pts[left].x < dsqrt && left; l_list[lsize++] = left--);
for ( rsize = 0; pts[right].x - median < dsqrt && right < n; r_list[rsize++] = right++);
/* sort the indices by y: don't touch the point data which is sorted by x */ sort_y(pts, lsize, l_list); sort_y(pts, rsize, r_list);
/* climb up left and right list and compare distance */ for (left = right = 0; left < lsize; left ++) { /* next point in left list */ pl = pts + l_list[left]; median = pl->y;
/* climb up right list until the y is not too low */ while (median > dsqrt + pts[r_list[right]].y && right < rsize) right++;
if (right >= rsize) break;
for (i = right; i < rsize; i++) { /* right y is too high, break */ if (pts[ r_list[i] ].y > median + dsqrt) break;
if (min_d > (d = dist(pts + r_list[i], pl))) { *a = pl; *b = pts + r_list[i]; min_d = d; dsqrt = sqrt(min_d); } } }
return min_d;
}
- define NP 1000000
int main(int argc, char **argv) {
int i, j; double d; point a, b;
point pts = malloc(sizeof(point_t) * NP); l_list = malloc(sizeof(int) * NP); r_list = malloc(sizeof(int) * NP);
for(i = 0; i < NP; i++) { pts[i].x = 20 * (double) rand()/RAND_MAX; pts[i].y = 20 * (double) rand()/RAND_MAX; }
d = brute_force(pts, NP, &i, &j); printf("brute force: %g, between (%f,%f) and (%f,%f)\n", d, pts[i].x, pts[i].y, pts[j].x, pts[j].y);
int cmp_x(const void *a, const void *b) { return cmp_dbl( ((point)a)->x, ((point)b)->x ); } qsort(pts, NP, sizeof(point_t), cmp_x);
printf("min: %g; ", sqrt(closest(pts, NP, &a, &b))); printf("point (%f,%f) and (%f,%f)\n", a->x, a->y, b->x, b->y);
return 0;
}</lang> --Ledrug 21:34, 17 June 2011 (UTC)