|
ADS
Algorithms and Data Structures
|
The local optima of a vector and of a binary tree, found without looking at all the items. More...
#include "ADS/Contract.h"#include "ADS/sort/Sorted.h"#include "ADS/tree/BinaryTree.h"#include <cstddef>#include <functional>#include <initializer_list>#include <vector>#include "ADS/search/impl/LocalOptima.tpp"Go to the source code of this file.
Namespaces | |
| namespace | ADS |
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.
A peak of \( A[ 0 .. n - 1 ] \) is an item not smaller than its neighbors, the items out of \( A \) counting as smaller than any item, and a local minimum is an item not larger than its neighbors; the maximum is a peak, so that a peak always exists, but finding the maximum needs \( n - 1 \) comparisons while a peak needs only a logarithmic number of them.
The same idea works in two dimensions, where a peak of an \( n \times n \) matrix costs \( \Theta( n ) \) probes by the search on the middle row and the middle column alternately, but not on general graphs: on the hypercube of dimension \( d \), Aldous showed that every randomized algorithm needs about \( 2^{d / 2} \) evaluations to find a local minimum.
The contracts [see Contract.h]: findPeak() and localMinimum() of a vector need a vector that is not empty and localMinimum() of a tree a tree; the result is checked against its neighbors, a postcondition. The functions are defined in LocalOptima.tpp.