ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
ClosestPair.h File Reference

The closest pair of a set \( P \) of \( n \ge 2 \) points of the plane, the two points at the least Euclidean distance, by divide et impera in \( O( n \log n ) \) time, against the \( \Theta( n^2 ) \) of the comparison of all the pairs (closestPairBrute()). More...

#include "ADS/Contract.h"
#include "ADS/geometry/ConvexHull.h"
#include "ADS/numeric/RandomGenerators.h"
#include "ADS/sort/Sort.h"
#include <algorithm>
#include <cmath>
#include <cstddef>
#include <cstdint>
#include <cstdlib>
#include <limits>
#include <numeric>
#include <optional>
#include <tuple>
#include <utility>
#include <vector>
#include "ADS/geometry/impl/ClosestPair.tpp"

Go to the source code of this file.

Namespaces

namespace  ADS

Functions

std::int64_t ADS::squaredDistance (const Point &p, const Point &q)
 the square \( ( x_p - x_q )^2 + ( y_p - y_q )^2 \) of the distance of \( p \) and \( q \)
std::pair< std::pair< std::size_t, std::size_t >, std::int64_t > ADS::closestPair (const std::vector< Point > &P)
 the places \( i < j \) of the two closest points of \( P \) and the square of their distance, by divide et impera; \( O( n \log n ) \)
std::pair< std::pair< std::size_t, std::size_t >, std::int64_t > ADS::closestPairSweep (const std::vector< Point > &P)
 the pair of closestPair() by the sweep on the points sorted by \( x \), and by the randomized grid when the sweep takes too long; a pair of the least distance, not always the one of closestPair()
std::pair< std::pair< std::size_t, std::size_t >, std::int64_t > ADS::closestPairBrute (const std::vector< Point > &P)
 a closest pair, the first of the least distance in the order of the loops (not always the one of closestPair()), by all the pairs; \( \Theta( n^2 ) \)

Detailed description

The closest pair of a set \( P \) of \( n \ge 2 \) points of the plane, the two points at the least Euclidean distance, by divide et impera in \( O( n \log n ) \) time, against the \( \Theta( n^2 ) \) of the comparison of all the pairs (closestPairBrute()).

closestPair() sorts the points by \( x \) and splits them at the median \( x \) into two halves of \( n / 2 \) points, solved recursively; if \( \delta \) is the smaller of their two distances, a closer pair crossing the split has both points in the strip of width \( 2 \delta \) about it, and a point of the strip has at most 7 others of the strip within \( \delta \) above it, since a \( \delta \times 2 \delta \) rectangle astride the split holds at most 8 points when those of each half are at mutual distances at least \( \delta \); the strip, sorted by \( y \), is scanned comparing each point with the next ones until their difference in \( y \) reaches \( \delta \), \( O( n ) \). The sort by \( y \) comes from the merge of the halves, as in mergesort, so that \( T( n ) = 2 T( n / 2 ) + O( n ) = O( n \log n ) \), which is optimal for the algebraic decision trees, since the closest pair decides whether \( n \) numbers are distinct, \( \Omega( n \log n ) \). The squares of the distances are compared, exactly, with the integers of 64 bits. Rabin's randomized algorithm, and the grid of Khuller and Matias, solve the problem in \( O( n ) \) expected time, with the floor function out of the decision tree model.

closestPairSweep(), beyond the book, is the sweep of the plane of Hinrichs, Nievergelt and Schorn with no tree: the points, each made a key of 64 bits \( ( x , y ) \), sorted by sort() [see Sort.h], \( O( n \log n ) \) and \( O( n ) \) when it takes the radix sort, and each one compared with those before it while their difference in \( x \) is below \( \delta \), the least distance so far (all on squares, exact on 64 bits), a point of the same \( x \) ending the scan of its column as soon as the difference in \( y \) reaches \( \delta \), since the column is sorted by \( y \). A point meets the points in the strip of width \( \delta \) before it, \( O( 1 ) \) of them on points spread in the plane, whence \( O( n ) \) after the sort; some inputs make the strips full of points far in \( y \): after \( 16 n \) comparisons the sweep gives up and starts again along \( y \), and if that fails too the randomized grid of Golin, Raman, Schwarz and Smid solves the problem: the points in a random order (a shuffle by Xoshiro256 seeded with \( n \)), the \( i \)-th looked for in the 9 cells around its own of a grid of side \( \lfloor \delta_i \rfloor + 1 \) hashed by the cells, each cell with \( O( 1 ) \) points of the grid; the grid is built again with the \( i \) points when \( \delta \) falls, which happens with probability at most \( 2 / i \) over the random order, whence \( \sum_i ( 1 + i \cdot 2 / i ) = O( n ) \) expected time on any input. The time is thus that of the sort plus \( O( n ) \) when a sweep succeeds, and \( O( n ) \) expected always beyond the sorts: with the radix sort, below the \( \Omega( n \log n ) \) of the algebraic decision trees, which the radix sort and the grid escape by reading bytes and by the floor function.

The contracts [see Contract.h]: at least two points, coordinates of absolute value at most \( 10^9 \), so that the square of a distance fits in 63 bits; the distance found is that of its two points, a postcondition. The functions are defined in ClosestPair.tpp.

See also
A. Montresor, Algoritmi e Strutture Dati, 2025, chapter 12, https://algoritmicamente.it/materiali/libro/capitoli/cap12.pdf
M. I. Shamos, D. Hoey, Closest-point problems, 16th Annual Symposium on Foundations of Computer Science, 151-162, 1975, doi:10.1109/SFCS.1975.8
K. Hinrichs, J. Nievergelt, P. Schorn, Plane-sweep solves the closest pair problem elegantly, Information Processing Letters 26(5), 255-261, 1988, doi:10.1016/0020-0190(88)90150-0
M. Golin, R. Raman, C. Schwarz, M. Smid, Simple randomized algorithms for closest pair problems, Nordic Journal of Computing 2(1), 3-27, 1995
S. Khuller, Y. Matias, A simple randomized sieve algorithm for the closest-pair problem, Information and Computation 118(1), 34-37, 1995, doi:10.1006/inco.1995.1049
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa