ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
LocalOptima.h

The local optima of a vector and of a binary tree, found without looking at all the items. More...

Files

file  ADS/search/LocalOptima.h
 The local optima of a vector and of a binary tree, found without looking at all the items.

Functions

template<class T, class C = std::less<>>
std::size_t ADS::findPeak (const std::vector< T > &A, C less=C())
 the place \( r \) of an item of \( A \) not smaller than its neighbors, by a binary search; \( \lceil \log_2 n \rceil \) comparisons
template<class T, class C = std::less<>>
std::size_t ADS::localMinimum (const std::vector< T > &A, C less=C())
 the place \( r \) of an item of \( A \) not larger than its neighbors, by a binary search; \( \lceil \log_2 n \rceil \) comparisons
template<class T, class C = std::less<>>
BinaryTree< T > * ADS::localMinimum (BinaryTree< T > *t, C less=C())
 a node of the tree \( t \) not larger than its children and, if it is not \( t \), smaller than its parent: from \( t \) down to the first child smaller than the current node, the left one first; \( O( h ) \) comparisons

Detailed description

The local optima of a vector and of a binary tree, found without looking at all the items.

The definitions, the invariants and the costs of the area are in Theory of searching.

Function documentation

◆ findPeak()

template<class T, class C = std::less<>>
std::size_t ADS::findPeak ( const std::vector< T > & A,
C less = C() )

the place \( r \) of an item of \( A \) not smaller than its neighbors, by a binary search; \( \lceil \log_2 n \rceil \) comparisons

In pseudocode (Esercizi Divide-et-impera of the book), the invariant being that \( A[ \mathit{lo} .. \mathit{hi} ] \) holds a peak of \( A \):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{findPeak}( A ) \cr 2 & \quad ( \mathit{lo} , \mathit{hi} ) \gets ( 0 , n - 1 )\quad \triangleright\ A[ \mathit{lo} .. \mathit{hi} ] \text{ holds a peak} \cr 3 & \quad \textbf{while}\ \mathit{lo} < \mathit{hi}\ \textbf{do} \cr 4 & \qquad m \gets \mathit{lo} + \lfloor ( \mathit{hi} - \mathit{lo} ) / 2 \rfloor \cr 5 & \qquad \textbf{if}\ A[ m + 1 ] \le A[ m ]\ \textbf{then}\ \mathit{hi} \gets m\ \textbf{else}\ \mathit{lo} \gets m + 1 \cr 6 & \quad \textbf{return}\ \mathit{lo} \end{array}} \]

Precondition
\( A \) is not empty
Postcondition
\( A[ r ] \) not smaller than \( A[ r - 1 ] \) and \( A[ r + 1 ] \), where they are

◆ localMinimum() [1/2]

template<class T, class C = std::less<>>
BinaryTree< T > * ADS::localMinimum ( BinaryTree< T > * t,
C less = C() )

a node of the tree \( t \) not larger than its children and, if it is not \( t \), smaller than its parent: from \( t \) down to the first child smaller than the current node, the left one first; \( O( h ) \) comparisons

In pseudocode (Esercizi Alberi of the book):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{localMinimum}( t ) \cr 2 & \quad \textbf{loop}\quad \triangleright\ \text{down to a smaller child while there is one} \cr 3 & \qquad \textbf{if}\ t.\mathit{left} \ne \textbf{nil}\ \textbf{and} \ t.\mathit{left}.\mathit{key} < t.\mathit{key}\ \textbf{then}\ t \gets t.\mathit{left} \cr 4 & \qquad \textbf{else if}\ t.\mathit{right} \ne \textbf{nil} \ \textbf{and}\ t.\mathit{right}.\mathit{key} < t.\mathit{key} \ \textbf{then}\ t \gets t.\mathit{right} \cr 5 & \qquad \textbf{else return}\ t \end{array}} \]

Precondition
\( t \) is not nil
Postcondition
no child of \( r \) smaller than \( r \), and \( r \) smaller than its parent if \( r \) is not \( t \)

◆ localMinimum() [2/2]

template<class T, class C = std::less<>>
std::size_t ADS::localMinimum ( const std::vector< T > & A,
C less = C() )

the place \( r \) of an item of \( A \) not larger than its neighbors, by a binary search; \( \lceil \log_2 n \rceil \) comparisons

In pseudocode (Esercizi Divide-et-impera of the book), the peak of the reversed order:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{localMinimum}( A ) \cr 2 & \quad \textbf{return}\ \text{the index found by } \mathrm{findPeak}( A ) \text{ with the order reversed} \end{array}} \]

Precondition
\( A \) is not empty
Postcondition
\( A[ r ] \) not larger than \( A[ r - 1 ] \) and \( A[ r + 1 ] \), where they are