Range minimum query explained

In computer science, a range minimum query (RMQ) solves the problem of finding the minimal value in a sub-array of an array of comparable objects. Range minimum queries have several use cases in computer science, such as the lowest common ancestor problem and the longest common prefix problem (LCP).

Definition

Given an array of objects taken from a totally ordered set, such as integers, the range minimum query (with) returns the position of the minimal element in the specified sub-array .

For example, when, then the answer to the range minimum query for the sub-array is, as .

Algorithms

Naive solution

In a typical setting, the array is static, i.e., elements are not inserted or deleted during a series of queries, and the queries to be answered on-line (i.e., the whole set of queries are not known in advance to the algorithm). In this case a suitable preprocessing of the array into a data structure ensures faster query answering. A naive solution is to precompute all possible queries, i.e. the minimum of all sub-arrays of, and store these in an array such that ; then a range min query can be solved in constant time by array lookup in . There are possible queries for a length- array, and the answers to these can be computed in time by dynamic programming.[1]

Solution using constant time after linearithmic space and time pre-computation

As in the solution above, answering queries in constant time will be achieved by pre-computing results. However, the array will store pre-computed range minimum queries not for every range, but only for ranges whose size is a power of two. There are such queries for each start position, so the size of the dynamic programming table is . The value of is the index of the minimum of the range . Filling the table takes time, with the indices of minima using the following recurrence

If, then ;

else, .

After this pre-computing step, a query can now be answered in constant time by splitting it into two separate queries: one is the pre-computed query with range from to the largest memoized value smaller than . The other is the query of an interval of the same length that has as its right boundary. These intervals may overlap, but since we are trying to compute the minimum rather than, for example, the sum of the numbers in the array, this does not matter. The overall result can thus be obtained, after the linearithmic time pre-computing, in constant time: the two queries can be answered in constant time and the only thing left to do is to choose the smaller of the two results.

Result table for
 
0123
11 1 1 1
2 2 3 3 7
3 3 3 3 7
4 4 5 6 7
5 5 6 7 7
6 6 7 7 7
7 7 7 7 7
8 8 7 7 7
9 9 7 7 7

Solution using logarithmic query time after linear time and space pre-computation

This solution does pre-computation in time. Its data structures use space and its data structures can be used to answer queries in logarithmic time. The array is first conceptually divided into blocks of size . Then the minimum for each block can be computed in time overall and the minima are stored in a new array.

RMQs can now be answered in logarithmic time by looking at the blocks containing the left query boundary, the right query boundary and all the blocks in between:

For example, using the array and a block size of (for illustrative purposes only) yields the minimum array .

Solution using constant time and linear space

Using the solution above, the sub-queries inside the blocks that are not fully contained in the query still need to be answered in constant time. There are at most two of those blocks: the block containing and the block containing . Constant time is achieved by storing the Cartesian trees for all the blocks in the array. A few observations:

For every such tree, the possible result for all queries need to be stored. This comes down to or entries. This means the overall size of the table is .

To look up results efficiently, the Cartesian tree (row) corresponding to a specific block must be addressable in constant time. The solution is to store the results for all trees in an array and find a unique projection from binary trees to integers to address the entries. This can be achieved by doing a breadth-first-search through the tree and adding leaf nodes so that every existing node in the Cartesian tree has exactly two children. The integer is then generated by representing every inner node as a 0-bit and every leaf as a 1-bit in a bit-word (by traversing the tree in level-order again). This leads to a size of for every tree. To enable random access in constant time to any tree, the trees not contained in the original array need to be included as well. An array with indices of bits length has size .

Pre-computed results for the three Cartesian block trees of
Index 1 2 3
1 2 3 1 2 3 1 2 3
0 colspan="9"
23 (Bitword 0010111) 1 2 3 2 3 3
39 (Bitword 0100111) 1 1 1 2 3 3
127

Applications

RMQs are used as a tool for many tasks in exact and approximate string matching. Several applications can be found in Fischer and Heun (2007).

Computing the lowest common ancestor in a tree

RMQs can be used to solve the lowest common ancestor problem[1] and are used as a tool for many tasks in exact and approximate string matching.The LCA query of a rooted tree and two nodes returns the deepest node (which may be or) on paths from the root to both and .Gabow, Bentley, and Tarjan (1984) showed that the LCA Problem can be reduced in linear time to the RMQ problem. It follows that, like the RMQ problem, the LCA problem can be solved in constant time and linear space.

Computing the longest common prefix in a string

In the context of text indexing, RMQs can be used to find the LCP (longest common prefix), where computes the LCP of the suffixes that start at indexes and in .To do this we first compute the suffix array, and the inverse suffix array . We then compute the LCP array giving the LCP of adjacent suffixes in . Once these data structures are computed, and RMQ preprocessing is complete, the length of the general LCP can be computed in constant time by the formula:, where we assume for simplicity that (otherwise swap).[2]

See also

References

External links

Notes and References

  1. Bender . Michael A. . Martín . Farach-Colton. Martin Farach-Colton . Giridhar . Pemmasani . Steven . Skiena . Steven Skiena . Pavel . Sumazin . Lowest common ancestors in trees and directed acyclic graphs . Journal of Algorithms . 57 . 2 . 2005 . 75–94 . 10.1016/j.jalgor.2005.08.001.
  2. Book: Fischer, J. and V. Heun. Combinatorial Pattern Matching. Theoretical and Practical Improvements on the RMQ-Problem, with Applications to LCA and LCE. 2006. 4009. 36–48. 10.1007/11780441_5. Lecture Notes in Computer Science. 978-3-540-35455-0. 10.1.1.64.5439.
  3. Bender . Michael . Farach-Colton . Martín . LATIN 2000: Theoretical Informatics . The LCA Problem Revisited . LATIN 2000: Theoretical Informatics . Springer . LNCS . 1776 . 2000 . 88–94 . 10.1007/10719839_9. 978-3-540-67306-4 .