In finding the closest pair of points in O(nlgn) time, the pseudocode for splitting a sorted list into two sorted lists (CLRS 3rd ed pg 1043) is said to run in O(n) time.
However, this assumes that line 4 runs in constant time, which I find hard to believe (I'd assume it runs in O(lgn) time if it were stored as a binary tree, giving a total running time of O(nlgn).
Y is a sorted array, YL and YR are the two new sub-arrays. PL is a subset of Y in random order, and YL is the same subset, but in sorted order.
Where am I going wrong with my reasoning?
I don't know how it is supposed to work in the book, but thinking about the way the algorithm looks like, you can come up with the following idea:
Y[i]
,X[i]
,YL[i]
,XL[i]
,YR[i]
andXR[i]
are integers, corresponding to the index of thei
th-point (so you just have to store some global array which, given the index, returns thex
ory
coordinate).PL[i]
is a boolean,true
if thei
-th point is in the left part,false
otherwise.At each recursion step, you can compute
PL[i]
usingy
coordinates (O(n)
time). Then you separate the set of points in two sets "left" and "right" using the algorithm from the book, replacing the lineif Y[i] in PL
byif PL[Y[i]]
(such access isO(1)
, so in overall we getO(n)
).This has
O(n)
time complexity and usesO(n)
memory.Thus the closest pair problem is solved that way in
T(n) = O(n log n)
.