Question: Just comment on what line is doing what in this code. (This Closest Pair of Points divide and conquer problem) double closestPair(int l, int r)
Just comment on what line is doing what in this code. (This Closest Pair of Points divide and conquer problem)
double closestPair(int l, int r) { if (l >= r) return 1e9; if (l + 1 == r) return dist(points[l], points[r]);
int mid = l + r >> 1; double d = min(closestPair(l, mid), closestPair(mid + 1, r));
int i = l, j = mid + 1, k = 0; //? Point strip[r - l + 1]; //? while (i <= mid && j <= r) { if (points[i] < points[j]) strip[k++] = points[i++]; //? else strip[k++] = points[j++]; } while (i <= mid) strip[k++] = points[i++]; //? while (j <= r) strip[k++] = points[j++]; //? for (int i = 0; i < k; i++) points[l + i] = strip[I]; //?
i = 0; for (int j = l; j <= r; j++) { //? if (abs(points[j].x - points[mid].x) >= d) continue; //? while (i < k - 1 && points[l + i + 1].y < points[j].y) i++; //? for (int t = i; t < k && points[l + t].y - points[j].y < d; t++) { //? if (mid - l >= 1 && r - mid >= 1) { d = min(d, dist(points[mid - 1], points[mid])); d = min(d, dist(points[mid], points[mid + 1])); } } }
return d; } input:
5
0 0
1 1
2 2
3 3
4 4
Output:
1.4.....
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
