|
ADS
Algorithms and Data Structures
|
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 | |
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.
| 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}} \]
| 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}} \]
| 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}} \]