-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclosest_pair.rs
260 lines (221 loc) · 6.73 KB
/
closest_pair.rs
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
use std::f64;
use std::cmp::min;
use crate::collections::{Axis, Point, PointPair, Plane, f64_min, vec_range_midpoint, euclidean_distance, get_point_value_by_axis};
/// Iterate Closest Pair via brute force method
fn iterate_closest_pair(x: &[PointPair]) -> &PointPair {
let mut closest_pair: &(Point, Point) = &x[0];
let mut best_euclidean_distance: f64 = f64::MAX;
for (i, pair) in x.iter().enumerate() {
let delta = euclidean_distance(pair.0, pair.1);
if i == 0 {
closest_pair = pair;
best_euclidean_distance = delta;
continue;
}
if f64_min(&[delta, best_euclidean_distance]) == delta {
closest_pair = pair;
best_euclidean_distance = delta;
continue;
}
}
closest_pair
}
/// Merge Pairs using merge from merge sort on types Plane
fn merge_pairs(a: Plane, b: Plane, axis: &Axis) -> Plane {
let mut merged: Plane = Vec::new();
let mut a_index = 0;
let mut b_index = 0;
while a_index < a.len() && b_index < b.len() {
let a_axis = get_point_value_by_axis(a[a_index], &axis);
let b_axis = get_point_value_by_axis(b[b_index], &axis);
if a_axis <= b_axis {
merged.push(a[a_index]);
a_index += 1;
} else {
merged.push(b[b_index]);
b_index += 1;
}
}
for element in a[a_index..].to_vec() {
merged.push(element);
}
for element in b[b_index..].to_vec() {
merged.push(element);
}
merged
}
/// Sort Pairs using merge sort on Plane type
fn sort_pairs(x: Plane, axis: &Axis) -> Plane {
if x.len() <= 1 {
return x;
}
let midpoint = vec_range_midpoint(&x);
let a = &x[..midpoint];
let b = &x[midpoint..];
let c = sort_pairs(a.to_vec(), &axis);
let d = sort_pairs(b.to_vec(), &axis);
merge_pairs(c, d, &axis)
}
/// Closest Split Pair
///
/// Input: Plane x, y sorted by respective axis and minimum delta of halves
/// Output: Option Point Pair from x that are closest
///
/// ================================================================================================
///
/// x_median = x value at median point sorted by x-axis
/// sy = points in py where point x value is within x_median +/- delta
///
/// closest_pair = none
/// best_euclidean_distance = delta
///
/// for point pairs in sy (brute force within min 7 or remainder)
/// if euclidean_distance is less than best_euclidean_distance
/// closest_pair = current point pair
///
/// return closest_pair
fn closest_split_pair(px: Plane, py: Plane, delta: f64) -> Option<PointPair> {
let midpoint = vec_range_midpoint(&px);
let pxl = &px[..midpoint];
let x_median = pxl.last().unwrap().0 as f64;
let sy: Vec<&Point> = py.iter().filter(|p| {
let x = p.0 as f64;
x >= (x_median - delta) && x <= (x_median + delta)
}).collect();
let mut closest_pair: Option<PointPair> = None;
let mut best_euclidean_distance = delta;
for i in 0..(sy.len() - 1) {
let max_range = min(7, sy.len() - i);
for j in 1..max_range {
let euclidean_distance = euclidean_distance(*sy[i], *sy[i + j]);
if euclidean_distance < best_euclidean_distance {
closest_pair = Some(
(sy[i].clone(), sy[i + j].clone())
);
best_euclidean_distance = euclidean_distance;
}
}
}
closest_pair
}
/// Closest Pair implementation
fn closest_pair(px: Plane, py: Plane) -> (Point, Point) {
if px.len() == 2 {
return (px[0], px[1]);
}
if px.len() == 3 {
return *iterate_closest_pair(&[
(px[0], px[1]),
(px[1], px[2]),
]);
}
let midpoint = vec_range_midpoint(&px);
let lx = &px[..midpoint];
let ly = &py[..midpoint];
let rx = &px[midpoint..];
let ry = &py[midpoint..];
// todo - to_vec expensive; work in slices
let (l1, l2) = closest_pair(lx.to_vec(), ly.to_vec());
let (r1, r2) = closest_pair(rx.to_vec(), ry.to_vec());
let delta = f64_min(
&[
euclidean_distance(l1, l2),
euclidean_distance(r1, r2)
]
);
if let Some((s1, s2)) = closest_split_pair(px, py, delta) {
return *iterate_closest_pair(&[
(l1, l2),
(r1, r2),
(s1, s2),
]);
}
*iterate_closest_pair(&[
(l1, l2),
(r1, r2),
])
}
/// Closest Pair
///
/// Input: Plane x of n Point elements
/// Output: pair of Point elements from x that are closest
///
/// ================================================================================================
///
/// if px length equals 2 then
/// base case: return px (already closest pair)
/// if px length equals 3 then
/// base case: return brute force closest pair search
///
/// lx, ly = first halves of px and py
/// rx, ry = second halves of px and py
///
/// l1, l2 = closest pair in first halve
/// r1, r2 = closest pair in second halve
///
/// delta = minimum euclidean distance between first and second halve closest pairs (left and right)
/// s1, s2 = closest split pair between first and second halves using delta
///
/// return closest pair amongst left, right or split pairs
pub fn find(x: Plane) -> PointPair {
let px: Plane = sort_pairs(x.clone(), &Axis::X); // todo - optimize O(n) clone w/borrow
let py: Plane = sort_pairs(x, &Axis::Y);
closest_pair(px, py)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_iterate_closest_pair() {
let pairs: [(Point, Point); 3] = [
((0, 0), (1, 10)),
((1, 1), (2, 15)),
((2, 2), (3, 3)),
];
let expectation = ((2, 2), (3, 3));
let pair = iterate_closest_pair(&pairs);
assert_eq!(pair, &expectation);
}
#[test]
fn test_merge_pairs() {
let a: Plane = vec![
(0, 1),
(1, 2)
];
let b: Plane = vec![
(2, 3),
(3, 4),
(4, 5)
];
let axis = Axis::X;
let expectation: Plane = vec![
(0, 1),
(1, 2),
(2, 3),
(3, 4),
(4, 5)
];
let result = merge_pairs(a, b, &axis);
assert_eq!(result, expectation);
}
#[test]
fn test_sort_pairs_x() {
let pairs: Plane = vec![
(4, 5),
(3, 4),
(2, 3),
(1, 2),
(0, 1)
];
let axis = Axis::X;
let expectation: Plane = vec![
(0, 1),
(1, 2),
(2, 3),
(3, 4),
(4, 5)
];
let result = sort_pairs(pairs, &axis);
assert_eq!(result, expectation);
}
}