|
ADS
Algorithms and Data Structures
|
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...
Files | |
| file | ADS/geometry/ClosestPair.h |
| 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()). | |
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 ) \) | |
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()).
The definitions, the invariants and the costs of the area are in Theory of geometry.
|
inline |
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 ) \)
In pseudocode (Shamos and Hoey, beyond the book), \( I \) the points sorted by \( x \) and \( \delta^2 \) the least square distance found so far, a global:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{closestRec}( I , \mathit{lo} , \mathit{hi} )\quad \triangleright\ \text{sorts } I[ \mathit{lo} .. \mathit{hi} - 1 ] \text{ by } y \cr 2 & \quad \textbf{if}\ \mathit{hi} - \mathit{lo} \le 3\ \textbf{then} \cr 3 & \qquad \text{offer all the pairs};\ \text{sort the range by } y; \ \textbf{return} \cr 4 & \quad \mathit{mid} \gets \lfloor ( \mathit{lo} + \mathit{hi} ) / 2 \rfloor;\ x_m \gets x( I[ \mathit{mid} ] ) \cr 5 & \quad \mathrm{closestRec}( I , \mathit{lo} , \mathit{mid} ); \ \mathrm{closestRec}( I , \mathit{mid} , \mathit{hi} ) \cr 6 & \quad \text{merge the two halves by } y \cr 7 & \quad B \gets \text{the points } p \text{ of the range with } ( x( p ) - x_m )^2 < \delta^2 \text{, in order of } y \cr 8 & \quad \textbf{for}\ u \gets 1\ \textbf{to}\ |B|\ \textbf{do} \cr 9 & \qquad \textbf{for}\ v \gets u + 1\ \textbf{to}\ |B|\ \textbf{do} \cr 10 & \qquad\quad \textbf{if}\ ( y( B[ v ] ) - y( B[ u ] ) )^2 \ge \delta^2\ \textbf{then}\ \textbf{break} \cr 11 & \qquad\quad \mathrm{offer}( B[ u ] , B[ v ] ) \cr 12 & \textbf{function}\ \mathrm{closestPair}( P ) \cr 13 & \quad \delta^2 \gets \infty;\ I \gets \text{the places of } P \text{ sorted by } x \cr 14 & \quad \mathrm{closestRec}( I , 0 , n ) \cr 15 & \quad \textbf{return}\ \text{the pair offered at } \delta^2 \text{, and } \delta^2 \end{array}} \]
where \( \mathrm{offer}( a , b ) \) keeps \( ( a , b ) \) if its square distance is below \( \delta^2 \).
|
inline |
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 ) \)
In pseudocode:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{closestPairBrute}( P ) \cr 2 & \quad \delta^2 \gets \infty \cr 3 & \quad \textbf{for}\ i \gets 1\ \textbf{to}\ n\ \textbf{do} \cr 4 & \qquad \textbf{for}\ j \gets i + 1\ \textbf{to}\ n\ \textbf{do} \cr 5 & \qquad\quad e \gets \mathrm{squaredDistance}( P[ i ] , P[ j ] ) \cr 6 & \qquad\quad \textbf{if}\ e < \delta^2\ \textbf{then}\ \delta^2 \gets e;\ ( a , b ) \gets ( i , j ) \cr 7 & \quad \textbf{return}\ ( ( a , b ) , \delta^2 ) \end{array}} \]
|
inline |
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()
In pseudocode (the sweep of Hinrichs, Nievergelt and Schorn and the grid of Golin, Raman, Schwarz and Smid, beyond the book):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{sweep}( P , \mathit{axis} , \mathit{budget} ) \cr 2 & \quad K \gets \text{the points sorted by radix along } \mathit{axis} \text{, then along the other axis} \cr 3 & \quad \delta^2 \gets \infty \cr 4 & \quad \textbf{for}\ u \gets 2\ \textbf{to}\ n\ \textbf{do} \cr 5 & \qquad \textbf{for}\ v \gets u - 1\ \textbf{downto}\ 1\ \textbf{do} \cr 6 & \qquad\quad \textbf{if}\ \mathit{dx}^2 \ge \delta^2\ \textbf{then} \ \textbf{break}\quad \triangleright\ \mathit{dx} , \mathit{dy} \text{ the differences of } K[ u ] \text{ and } K[ v ] \cr 7 & \qquad\quad \textbf{if}\ \mathit{dx}^2 + \mathit{dy}^2 < \delta^2 \ \textbf{then}\ \delta^2 \gets \mathit{dx}^2 + \mathit{dy}^2 \cr 8 & \qquad\quad \textbf{else if}\ \mathit{dy}^2 \ge \delta^2 \text{ and } \mathit{dx} = 0\ \textbf{then}\ v \gets \text{the first of the } \text{column of } u \cr 9 & \qquad\quad \mathit{budget} \gets \mathit{budget} - 1; \ \textbf{if}\ \mathit{budget} = 0\ \textbf{then return nil} \cr 10 & \quad \textbf{return}\ \delta^2 \text{ and its pair} \cr 11 & \textbf{function}\ \mathrm{closestPairSweep}( P ) \cr 12 & \quad r \gets \mathrm{sweep}( P , x , 16 n ) \cr 13 & \quad \textbf{if}\ r = \textbf{nil}\ \textbf{then}\ r \gets \mathrm{sweep}( P , y , 16 n ) \cr 14 & \quad \textbf{if}\ r = \textbf{nil}\ \textbf{then return} \ \mathrm{closestGrid}( P ) \cr 15 & \quad \textbf{return}\ r \cr 16 & \textbf{function}\ \mathrm{closestGrid}( P ) \cr 17 & \quad \text{shuffle } P;\ \delta^2 \gets \text{the square } \text{distance of } P[ 1 ] \text{ and } P[ 2 ] \cr 18 & \quad G \gets \text{a grid of side } \lfloor \delta \rfloor + 1 \text{ hashed by the cells, with } P[ 1 ] \text{ and } P[ 2 ] \cr 19 & \quad \textbf{for}\ i \gets 3\ \textbf{to}\ n\ \textbf{do} \cr 20 & \qquad \text{offer the pairs of } P[ i ] \text{ with the points of } \text{the 9 cells around its own} \cr 21 & \qquad \textbf{if}\ \delta^2 = 0\ \textbf{then}\ \textbf{break} \cr 22 & \qquad \textbf{if}\ \delta \text{ fell}\ \textbf{then}\ G \gets \text{a grid of side } \lfloor \delta \rfloor + 1 \text{ with } P[ 1 .. i ] \cr 23 & \qquad \textbf{else}\ \text{add } P[ i ] \text{ to } G \cr 24 & \quad \textbf{return}\ \text{the pair offered at } \delta^2 \text{, and } \delta^2 \end{array}} \]