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

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. More...

#include "ADS/Contract.h"
#include "ADS/sort/Sorted.h"
#include <concepts>
#include <cstddef>
#include <limits>
#include <type_traits>
#include "ADS/search/impl/MonotoneSearch.tpp"

Go to the source code of this file.

Namespaces

namespace  ADS

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 \)

Detailed description

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.

  • exponentialSearch(): the least \( i \ge \mathit{lo} \) with \( p( i ) \), for a predicate false and then true; the probes \( \mathit{lo} \), \( \mathit{lo} + 1 \), \( \mathit{lo} + 3 \), \( \mathit{lo} + 7 \), and so on, double the distance from \( \mathit{lo} \) until \( p \) holds, and a binary search between the last two probes finds the place \( h \), in \( 2 \lceil \log_2 ( h - \mathit{lo} + 1 ) \rceil + 1 \) evaluations at most, the method \( B_1 \) of Bentley and Yao; their methods \( B_k \), which find the bound itself by an unbounded search of the previous level, reach \( \log_2 h + \log_2 \log_2 h + \cdots \), close to the lower bound. The probes never overflow the type of the integers, and the middle of the binary search is computed on the unsigned distance;
  • unboundedSearch(): the first place of a sequence read one item at a time, get( i ) giving no item past its end, at which there is no item or the item satisfies a predicate false and then true on the items; the count of the items before the first one that is not smaller than a value, in a sorted sequence of unknown length, is the classic case;
  • integerSqrt(): \( \lfloor \sqrt n \rfloor \), the largest \( r \) with \( r^2 \le n \), by a binary search on \( [ 0 , 2^{w / 2} ) \) for \( w \) bits, where \( m^2 \le n \) is tested as \( m \le \lfloor n / m \rfloor \), so that no product overflows; \( w / 2 \) steps. Newton's iteration \( x \leftarrow \lfloor ( x + \lfloor n / x \rfloor ) / 2 \rfloor \) converges quadratically from a start above the root, in \( O( \log \log n ) \) steps from a start within a constant factor; isPerfectSquare() compares the square of the root with \( n \).

The contracts [see Contract.h]: exponentialSearch() needs a place up to the largest integer where the predicate holds, a precondition checked when the probes reach it, and checks that \( p \) holds at the result and not before it, evaluations made with checkingContract() true [see Sorted.h]; integerSqrt() checks \( r^2 \le n < ( r + 1 )^2 \). The functions are defined in MonotoneSearch.tpp.

See also
A. Montresor, Algoritmi e Strutture Dati, 2025, Esercizi Divide-et-impera, Raccolte da compiti scritti
J. L. Bentley, A. C.-C. Yao, An almost optimal algorithm for unbounded searching, Information Processing Letters 5(3), 82-87, 1976, doi:10.1016/0020-0190(76)90071-5
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa