Here is an algorithm which requires O(n2)
time and O(n)
space.
It's based on the observation that if you partition the points into two sets, then the connectivity distance cannot be less than the distance of the closest pair of points one from each set in the partition. In other words, if we build up the connected graph by always adding the closest point, then the largest distance we add will be the connectivity distance.
Create two sets,
A
andB
. Put a random point intoA
and all the remaining points intoB
.Initialize
r
(the connectivity distance) to 0.Initialize a map
M
with the distance to every point inB
of the point inA
.While there are still points in
B
:Select the point
b
inB
whose distanceM[b]
is the smallest.If
M[b]
is greater thanr
, setr
toM[b]
Remove
b
fromB
and add it toA
.For each point
p
inM
:If
p
isb
, remove it fromM
.Otherwise, if the distance from
b
top
is less thanM[p]
, setM[p]
to that distance.
When all the points are in A
, r
will be the connectivity distance.
Each iteration of the while
loop takes O(|B|)
time, first to find the minimum value in M
(whose size is equal to the size of B
); second, to update the values in M
. Since a point is moved from B
to A
in each iteration, there will be exactly n
iterations, and thus the total execution time is O(n2)
.
The algorithm presented above is an improvement to a previous algorithm, which used an (unspecified) solution to the bichromatic closest pair (BCP) problem to recompute the closest neighbour to A
in every cycle. Since there is an O(n log n)
solution to BCP, this implied a solution to the original problem in O(n2 log n)
. However, maintaining and updating the list of closest points is actually much simpler, and only requires O(n)
. Thanks to @LajosArpad for a question which triggered this line of thought.