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

The range minimum query, beyond the book. More...

Files

file  ADS/search/RangeMinimum.h
 The range minimum query, beyond the book: given an array \( A \) of \( n \) items of an ordered universe, the place of a minimum of \( A[ i .. j ] \) for any \( i \le j \), the leftmost one when the minimum repeats.

Classes

class  ADS::SparseTable< T, Compare >
 the range minima of an array by the sparse table More...
class  ADS::PlusMinusOneRangeMinimum
 the range minima of an array whose adjacent items differ by 1 More...
class  ADS::RangeMinimum< T, Compare >
 the range minima of an array by Bender and Farach-Colton: the Cartesian tree, its Euler tour, and the minima of the depths along the tour More...

Functions

template<class T, class Compare = std::less< T >>
std::vector< std::size_t > ADS::cartesianTree (const std::vector< T > &A, Compare less={})
 the parent of each item of \( A \) in its Cartesian tree, the root (the leftmost minimum) having \( n \); equal items go to the right

Detailed description

The range minimum query, beyond the book.

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

Function documentation

◆ cartesianTree()

template<class T, class Compare = std::less< T >>
std::vector< std::size_t > ADS::cartesianTree ( const std::vector< T > & A,
Compare less = {} )
nodiscard

the parent of each item of \( A \) in its Cartesian tree, the root (the leftmost minimum) having \( n \); equal items go to the right

In pseudocode (Vuillemin, beyond the book), the stack \( S \) holding the right spine of the tree of \( A[ 0 .. i - 1 ] \), increasing:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{cartesianTree}( A ) \cr 2 & \quad \textbf{foreach}\ i\ \textbf{do}\ P[ i ] \gets n \cr 3 & \quad S \gets \text{an empty stack} \cr 4 & \quad \textbf{for}\ i \gets 0\ \textbf{to}\ n - 1\ \textbf{do} \cr 5 & \qquad \ell \gets \textbf{nil} \cr 6 & \qquad \textbf{while}\ S \ne \emptyset\ \textbf{and}\ A[ i ] < A[ \mathrm{top}( S ) ]\ \textbf{do}\ \ell \gets \mathrm{pop}( S ) \cr 7 & \qquad \textbf{if}\ \ell \ne \textbf{nil}\ \textbf{then}\ P[ \ell ] \gets i\quad \triangleright\ \text{the left child of } i \cr 8 & \qquad \textbf{if}\ S \ne \emptyset\ \textbf{then}\ P[ i ] \gets \mathrm{top}( S ) \cr 9 & \qquad \mathrm{push}( S , i ) \cr 10 & \quad \textbf{return}\ P \end{array}} \]

Postcondition
a heap: no item is smaller than its parent (audit)