-
Notifications
You must be signed in to change notification settings - Fork 8
/
closest.c
274 lines (251 loc) · 6.68 KB
/
closest.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/*
* Author: Leandro Augusto Lacerda Campos <llacerdacampos@gmail.com>
*
* Data Structures and Algorithms Specialization,
* by University of California, San Diego,
* and National Research University Higher School of Economics
*
* Course 1: Algorithmic Toolbox
*
* Solution for Closest Points Problem
*/
#include <stdlib.h>
#include <stdio.h>
#include <limits.h>
#include <math.h>
typedef struct point
{
double x;
double y;
} point;
#define min(a, b) ((a) < (b) ? (a) : (b))
#define MAX_DIST UINT_MAX
double
minimal_distance(const point *, const point *);
const point *
middle_range(const point *, const point *, const point *, double,
unsigned int *);
double
minimal_distance_naive(const point *, const point *);
double
distance(const point *, const point *);
void sort_by_x(point *, const point *);
int compare_by_x(const point *, const point *);
void sort_by_y(point *, const point *);
int compare_by_y(const point *, const point *);
void scan(point *, point *);
void print(const point *, const point *);
void stress_test(unsigned int);
int main()
{
#if defined(STRESS_TEST)
stress_test(0);
#else
unsigned long n;
point *points;
double dist;
scanf("%lu", &n);
points = malloc(n * sizeof(point));
scan(&points[0], &points[n - 1]);
sort_by_x(&points[0], &points[n - 1]);
dist = minimal_distance(&points[0], &points[n - 1]);
printf("%.6lf\n", dist);
free(points);
return 0;
#endif
}
/*
* minimal_distance: finds the smallest distance between a pair of two points
* of the sequence *left <= ... <= *right.
*/
double
minimal_distance(const point *left, const point *right)
{
const point *middle, *lower_middle, *upper_middle, *p1, *p2;
double dist, dist_lt, dist_rt, dist_mid;
unsigned int nel;
if (right - left < 1)
return MAX_DIST;
if (right - left == 1)
return distance(left, right);
middle = left + (right - left) / 2;
dist_lt = minimal_distance(left, middle);
dist_rt = minimal_distance(middle + 1, right);
dist = min(dist_lt, dist_rt);
/* checks whether there exist points in the left and the right side such that
the distance between them is smaller than dist */
lower_middle = middle_range(left, right, middle, dist, &nel);
if (nel > 1)
{
upper_middle = lower_middle + nel - 1;
sort_by_y((point *)lower_middle, (point *)upper_middle);
p1 = lower_middle;
while (p1 < upper_middle)
{
p2 = p1 + 1;
while (p2 <= upper_middle && (p2 - p1) <= 7)
{
dist_mid = distance(p1, p2);
if (dist_mid < dist)
dist = dist_mid;
p2++;
}
p1++;
}
sort_by_x((point *)lower_middle, (point *)upper_middle);
}
return dist;
}
/*
* middle_range: returns a pointer to the left endpoint of the sequence (middle->x - radius)
* <= *middle_range <= ... <= (*middle_range + *nel - 1) <= (middle->x + radius).
*/
const point *
middle_range(const point *left, const point *right, const point *middle, double radius,
unsigned int *nel)
{
const point *p;
double lt_endpoint, rt_endpoint;
lt_endpoint = middle->x - radius;
rt_endpoint = middle->x + radius;
while (middle > left)
if ((middle - 1)->x >= lt_endpoint)
middle--;
else
break;
p = middle;
while (p < right)
if ((p + 1)->x <= rt_endpoint)
p++;
else
break;
*nel = p - middle + 1;
return middle;
}
double
minimal_distance_naive(const point *left, const point *right)
{
const point *p1, *p2;
double dist, tmp;
if (right - left < 1)
return MAX_DIST;
if (right - left == 1)
return distance(left, right);
dist = MAX_DIST;
p1 = left;
while (p1 <= right)
{
p2 = left;
while (p2 <= right)
{
if (p1 != p2)
{
tmp = distance(p1, p2);
if (tmp < dist)
dist = tmp;
}
p2++;
}
p1++;
}
return dist;
}
double
distance(const point *a, const point *b)
{
return sqrt(pow(a->x - b->x, 2) + pow(a->y - b->y, 2));
}
void sort_by_x(point *left, const point *right)
{
qsort(left, right - left + 1, sizeof(point),
(int (*)(const void *, const void *))compare_by_x);
}
int compare_by_x(const point *a, const point *b)
{
if (a->x < b->x)
return -1;
else if (a->x == b->x)
return 0;
else
return 1;
}
void sort_by_y(point *left, const point *right)
{
qsort(left, right - left + 1, sizeof(point),
(int (*)(const void *, const void *))compare_by_y);
}
int compare_by_y(const point *a, const point *b)
{
if (a->y < b->y)
return -1;
else if (a->y == b->y)
return 0;
else
return 1;
}
void scan(point *left, point *right)
{
while (left <= right)
{
scanf("%lf %lf", &(left->x), &(left->y));
left++;
}
}
void print(const point *left, const point *right)
{
while (left <= right)
{
printf("(%.0lf, %.0lf)\n", left->x, left->y);
left++;
}
}
#if defined(STRESS_TEST)
/*
* stress_test: proves that the solution implemented with a naive solution.
*/
#define MAX_ITER 100000
#define MIN_SEQUENCE_SIZE 2 /* 1 */
#define MAX_SEQUENCE_SIZE 10 /* 100000 */
#define MIN_INTEGER_VALUE -10 /* 1 */
#define MAX_INTEGER_VALUE 10 /* 1000000000 */
void stress_test(unsigned int seed)
{
unsigned int n;
point *base;
unsigned int iter, i, j;
double r_dist, t_dist;
srand(seed);
iter = 0;
while (iter < MAX_ITER)
{
printf("test %u\n", iter + 1);
n = rand() % (MAX_SEQUENCE_SIZE - MIN_SEQUENCE_SIZE) + MIN_SEQUENCE_SIZE;
base = malloc(n * sizeof(point));
for (i = 0; i < n; i++)
{
base[i].x = rand() % (MAX_INTEGER_VALUE - MIN_INTEGER_VALUE) + MIN_INTEGER_VALUE;
base[i].y = rand() % (MAX_INTEGER_VALUE - MIN_INTEGER_VALUE) + MIN_INTEGER_VALUE;
}
t_dist = minimal_distance_naive(&base[0], &base[n - 1]);
sort_by_x(&base[0], &base[n - 1]);
r_dist = minimal_distance(&base[0], &base[n - 1]);
if (fabs(r_dist - t_dist) <= pow(10, -3))
{
printf("ok!\n");
printf("result = %.6lf\n", r_dist);
printf("test = %.6lf\n", t_dist);
}
else
{
printf("error!\n");
print(&base[0], &base[n - 1]);
printf("\nresult = %.6lf\n", r_dist);
printf("test = %.6lf\n", t_dist);
break;
}
free(base);
iter++;
putchar('\n');
}
}
#endif