In this problem we will develop a divide-and-conquer algorithm for the following geometric task.
CLOSEST PAIR Input: A set of points in the plane, $\left\{p_{1}=\left(x_{1}, y_{1}\right), p_{2}=\left(x_{2}, y_{2}\right), \ldots, p_{n}=\left(x_{n}, y_{n}\right)\right\}$
Output: The closest pair of points: that is, the pair $p_{i} \neq p_{j}$ for which the distance between $p_{i}$ and $p_{j},$ that is,
\[
\sqrt{\left(x_{i}-x_{j}\right)^{2}+\left(y_{i}-y_{j}\right)^{2}}
\]
is minimized.
For simplicity, assume that $n$ is a power of two, and that all the $x$ -coordinates $x_{i}$ are distinct, as are the $y$ -coordinates. Here's a high-level overview of the algorithm:
Find a value $x$ for which exactly half the points have $x_{i}<x,$ and half have $x_{i}>x .$ On this basis, split the points into two groups, $L$ and $R$ Recursively find the closest pair in $L$ and in $R .$ Say these pairs are $p_{L}, q_{L} \in L$ and $p_{R}, q_{R} \in R$ with distances $d_{L}$ and $d_{R}$ respectively. Let $d$ be the smaller of these two distances. It remains to be seen whether there is a point in $L$ and a point in $R$ that are less than distance $d$ apart from each other. To this end, discard all points with $x_{i}<x-d$ or $x_{i}>x+d$ and sort the remaining points by $y$ -coordinate.
Now, go through this sorted list, and for each point, compute its distance to the seven subsequent points in the list. Let $p_{M}, q_{M}$ be the closest pair found in this way.
The answer is one of the three pairs $\left\{p_{L}, q_{L}\right\},\left\{p_{R}, q_{R}\right\},\left\{p_{M}, q_{M}\right\},$ whichever is closest.
(a) In order to prove the correctness of this algorithm, start by showing the following property:
any square of size $d \times d$ in the plane contains at most four points of $L$
(b) Now show that the algorithm is correct. The only case which needs careful consideration is when the closest pair is split between $L$ and $R$
(c) Write down the pseudocode for the algorithm, and show that its running time is given by the recurrence:
\[
T(n)=2 T(n / 2)+O(n \log n)
\]
Show that the solution to this recurrence is $O\left(n \log ^{2} n\right)$
(d) Can you bring the running time down to $O(n \log n) ?$