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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started