|
ADS
Algorithms and Data Structures
|
The search of the first place where a monotone predicate becomes true, when no upper bound on that place is known. More...
Files | |
| file | ADS/search/MonotoneSearch.h |
| The search of the first place where a monotone predicate becomes true, when no upper bound on that place is known: on the integers, on a sequence of unknown length, and on the integer square root. | |
Functions | |
| template<std::integral I, class P> | |
| I | ADS::exponentialSearch (P &&p, I lo=I(0)) |
| the least \( i \ge \mathit{lo} \) with \( p( i ) \), \( p \) false and then true from \( \mathit{lo} \); \( O( \log( i - \mathit{lo} + 1 ) ) \) evaluations of \( p \) | |
| template<class G, class P> | |
| std::size_t | ADS::unboundedSearch (G &&get, P &&pred) |
| the first place \( i \) where get( i ), which gives an optional item, empty past the end of the sequence, gives no item or an item \( x \) with pred( x ); \( O( \log i ) \) calls of get() | |
| template<std::unsigned_integral U> | |
| U | ADS::integerSqrt (U n) |
| the largest \( r \) with \( r^2 \le n \), by a binary search with no overflow; \( w / 2 \) steps for \( w \) bits, none for a bool, which is its own root | |
| template<std::unsigned_integral U> | |
| bool | ADS::isPerfectSquare (U n) |
| true if \( n = k^2 \) for an integer \( k \), i.e., if the square of integerSqrt( n ) is \( n \) | |
The search of the first place where a monotone predicate becomes true, when no upper bound on that place is known.
The definitions, the invariants and the costs of the area are in Theory of searching.
| I ADS::exponentialSearch | ( | P && | p, |
| I | lo = I(0) ) |
the least \( i \ge \mathit{lo} \) with \( p( i ) \), \( p \) false and then true from \( \mathit{lo} \); \( O( \log( i - \mathit{lo} + 1 ) ) \) evaluations of \( p \)
In pseudocode (the method \( B_1 \) of Bentley and Yao, as in exercise 12.9), \( p \) false on \( [ \mathit{lo} , a ) \) at all times, the probes cut at the largest integer:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{exponentialSearch}( p , \mathit{lo} ) \cr 2 & \quad ( a , b , s ) \gets ( \mathit{lo} , \mathit{lo} , 1 )\quad \triangleright\ p \text{ false before } a \cr 3 & \quad \textbf{while not}\ p( b )\ \textbf{do}\ ( a , b , s ) \gets ( b + 1 , b + s , 2 s ) \cr 4 & \quad \textbf{while}\ a < b\ \textbf{do}\quad \triangleright\ p( b ) \text{ is true} \cr 5 & \qquad m \gets a + \lfloor ( b - a ) / 2 \rfloor \cr 6 & \qquad \textbf{if}\ p( m )\ \textbf{then}\ b \gets m\ \textbf{else} \ a \gets m + 1 \cr 7 & \quad \textbf{return}\ a \end{array}} \]
| U ADS::integerSqrt | ( | U | n | ) |
the largest \( r \) with \( r^2 \le n \), by a binary search with no overflow; \( w / 2 \) steps for \( w \) bits, none for a bool, which is its own root
In pseudocode (Esercizi Divide-et-impera of the book), the invariant being \( \mathit{lo}^2 \le n < \mathit{hi}^2 \):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{integerSqrt}( n ) \cr 2 & \quad ( \mathit{lo} , \mathit{hi} ) \gets ( 0 , 2^{w / 2} )\quad \triangleright\ \mathit{lo}^2 \le n < \mathit{hi}^2 \cr 3 & \quad \textbf{while}\ \mathit{lo} + 1 < \mathit{hi}\ \textbf{do} \cr 4 & \qquad m \gets \mathit{lo} + \lfloor ( \mathit{hi} - \mathit{lo} ) / 2 \rfloor \cr 5 & \qquad \textbf{if}\ \lfloor n / m \rfloor < m\ \textbf{then} \ \mathit{hi} \gets m\ \textbf{else}\ \mathit{lo} \gets m \cr 6 & \quad \textbf{return}\ \mathit{lo} \end{array}} \]
| bool ADS::isPerfectSquare | ( | U | n | ) |
true if \( n = k^2 \) for an integer \( k \), i.e., if the square of integerSqrt( n ) is \( n \)
| std::size_t ADS::unboundedSearch | ( | G && | get, |
| P && | pred ) |
the first place \( i \) where get( i ), which gives an optional item, empty past the end of the sequence, gives no item or an item \( x \) with pred( x ); \( O( \log i ) \) calls of get()
In pseudocode:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{unboundedSearch}( \mathit{get} , \mathit{pred} ) \cr 2 & \quad \textbf{return}\ \mathrm{exponentialSearch}( i \mapsto \mathit{get}( i ) = \textbf{nil}\ \textbf{or}\ \mathit{pred}( \mathit{get}( i ) ) , 0 ) \end{array}} \]