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

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

Detailed description

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.

  • findPeak(): a binary search on the invariant that \( A[ \mathit{lo} .. \mathit{hi} ] \) holds a peak of \( A \), true when \( A[ \mathit{lo} - 1 ] < A[ \mathit{lo} ] \) (or \( \mathit{lo} = 0 \)) and \( A[ \mathit{hi} ] \) is not smaller than \( A[ \mathit{hi} + 1 ] \) (or \( \mathit{hi} = n - 1 \)): the middle item \( A[ m ] \) is compared with \( A[ m + 1 ] \), and the search goes on in \( A[ m + 1 .. \mathit{hi} ] \) if it is smaller and in \( A[ \mathit{lo} .. m ] \) otherwise, both keeping the invariant; \( \lceil \log_2 n \rceil \) comparisons, and \( \Omega( \log n ) \) are needed in the worst case. With distinct items the peak is strict;
  • localMinimum() of a vector: findPeak() with the order reversed;
  • localMinimum() of a binary tree: a node not larger than its parent and its children, found by going down from the root to the first child smaller than the current node, until no child is smaller; each node reached is smaller than its parent, the last one is not larger than its children, \( O( h ) \) comparisons for a tree of height \( h \), i.e., \( O( \log n ) \) on a complete tree.

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.

See also
A. Montresor, Algoritmi e Strutture Dati, 2025, Esercizi Divide-et-impera and Esercizi Alberi, Raccolte da compiti scritti
D. Aldous, Minimization algorithms and random walk on the d-cube, The Annals of Probability 11(2), 403-413, 1983, doi:10.1214/aop/1176993605
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa