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

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. More...

#include "ADS/Contract.h"
#include <algorithm>
#include <bit>
#include <cstddef>
#include <cstdint>
#include <functional>
#include <limits>
#include <utility>
#include <vector>
#include "ADS/search/impl/RangeMinimum.tpp"

Go to the source code of this file.

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...

Namespaces

namespace  ADS

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: 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.

The problem is the backbone of the longest common extensions of a text [see SuffixArray.h] and of the lowest common ancestors of a tree [see SuffixTree.h].

  • SparseTable stores, for every place \( i \) and every \( L \), the place of the minimum of \( A[ i .. i + 2^L - 1 ] \), computed from the two halves of the range; a query covers \( [ i , j ] \) by the two (possibly overlapping) ranges of length \( 2^L \), \( L = \lfloor \log_2( j - i + 1 ) \rfloor \), that start at \( i \) and end at \( j \). It takes \( O( n \log n ) \) time and space and answers in \( O( 1 ) \) time, against the \( O( n^2 ) \) of the table of all the answers;
  • cartesianTree() builds the Cartesian tree of \( A \) with a stack in \( O( n ) \): its root is the leftmost minimum of \( A \), its left and right subtrees the Cartesian trees of the parts before and after it; a range of \( A \) is a subtree, and the minimum of \( A[ i .. j ] \) is the lowest common ancestor of \( i \) and \( j \);
  • PlusMinusOneRangeMinimum answers the queries on an array whose adjacent items differ by exactly 1, as the depths along an Euler tour of a tree: the array is cut into blocks of \( d = \max\{ 1 , \lfloor \log_2 n \rfloor / 2 \} \) items, a sparse table on the minima of the blocks answers for the blocks covered entirely, and a block is described by the \( d - 1 \) signs of its differences, so that a table of the \( 2^{d-1} \le \sqrt{n} \) signatures times the \( d^2 \) pairs of offsets (the trick of the Four Russians) answers inside a block: \( O( n ) \) time and space, \( O( 1 ) \) time per query;
  • RangeMinimum is the solution of Bender and Farach-Colton for any array: the minimum of \( A[ i .. j ] \) is the lowest common ancestor of \( i \) and \( j \) in the Cartesian tree, i.e., the node of least depth on the Euler tour of the tree between the first visits of \( i \) and \( j \), a query on the \( 2 n - 1 \) depths of the tour, which differ by 1. Its time and space are \( O( n ) \), \( O( 1 ) \) time per query (Searching Strings by Substring, chapter 10 of Ferragina), and the same reduction answers the lowest common ancestors of any tree (ibid.).

The bound \( O( n ) \) of Bender and Farach-Colton was first reached by Harel and Tarjan on the lowest common ancestors, with a far more involved structure; Fischer and Heun need only \( 2 n + o( n ) \) bits besides \( A \), by describing each block by its own Cartesian tree, which removes the Euler tour and its factor 2. The sparse table is the simplest and, on the arrays that fit in a cache, often the fastest in practice.

The contracts [see Contract.h]: a query asks for \( i \le j < n \), and its answer is a leftmost minimum of the range (an audit, linear in the range); the items of a PlusMinusOneRangeMinimum differ by 1. The functions are defined in RangeMinimum.tpp.

See also
P. Ferragina, Pearls of Algorithm Engineering, Cambridge University Press, 2023, chapter 10 (Searching Strings by Substring), doi:10.1017/9781009128933
M. A. Bender, M. Farach-Colton, The LCA problem revisited, Proceedings of the 4th Latin American Symposium on Theoretical Informatics (LATIN), LNCS 1776, 88-94, 2000, doi:10.1007/10719839_9
D. Harel, R. E. Tarjan, Fast algorithms for finding nearest common ancestors, SIAM Journal on Computing 13(2), 338-355, 1984, doi:10.1137/0213024
J. Fischer, V. Heun, Space-efficient preprocessing schemes for range minimum queries on static arrays, SIAM Journal on Computing 40(2), 465-492, 2011, doi:10.1137/090779759
J. Vuillemin, A unifying look at data structures, Communications of the ACM 23(4), 229-239, 1980, doi:10.1145/358841.358852
V. L. Arlazarov, E. A. Dinic, M. A. Kronrod, I. A. Faradzev, On economical construction of the transitive closure of an oriented graph, Soviet Mathematics Doklady 11, 1209-1210, 1970 (the trick of the Four Russians)
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa