Misplaced Pages

Range query (computer science)

Article snapshot taken from Wikipedia with creative commons attribution-sharealike license. Give it a read and then ask your questions in the chat. We can research this topic together.
(Redirected from Range query (data structures)) For finding items that fall within a range, see range query (database).
This article's tone or style may not reflect the encyclopedic tone used on Misplaced Pages. See Misplaced Pages's guide to writing better articles for suggestions. (December 2017) (Learn how and when to remove this message)

In computer science, the range query problem consists of efficiently answering several queries regarding a given interval of elements within an array. For example, a common task, known as range minimum query, is finding the smallest value inside a given range within a list of numbers.

Definition

Given a function f {\displaystyle f} that accepts an array, a range query f q ( l , r ) {\displaystyle f_{q}(l,r)} on an array a = [ a 1 , . . , a n ] {\displaystyle a=} takes two indices l {\displaystyle l} and r {\displaystyle r} and returns the result of f {\displaystyle f} when applied to the subarray [ a l , , a r ] {\displaystyle } . For example, for a function sum {\displaystyle \operatorname {sum} } that returns the sum of all values in an array, the range query sum q ( l , r ) {\displaystyle \operatorname {sum} _{q}(l,r)} returns the sum of all values in the range [ l , r ] {\displaystyle } .

Solutions

Prefix sum array

Main article: Prefix sum

Range sum queries may be answered in constant time and linear space by pre-computing an array p of same length as the input such that for every index i, the element pi is the sum of the first i elements of a. Any query may then be computed as follows: sum q ( l , r ) = p r p l 1 . {\displaystyle \operatorname {sum} _{q}(l,r)=p_{r}-p_{l-1}.}

This strategy may be extended to any other binary operation f {\displaystyle f} whose inverse function f 1 {\displaystyle f^{-1}} is well-defined and easily computable. It can also be extended to higher dimensions with a similar pre-processing. For example, if pi,j contains the sum of the first i × j elements of a, then sum q ( l , r , t , b ) = p r , b p l 1 , b p r , t 1 + p l 1 , t 1 . {\displaystyle \operatorname {sum} _{q}(l,r,t,b)=p_{r,b}-p_{l-1,b}-p_{r,t-1}+p_{l-1,t-1}.}

Dynamic range queries

A more difficult subset of the problem consists of executing range queries on dynamic data; that is, data that may mutate between each query. In order to efficiently update array values, more sophisticated data structures like the segment tree or Fenwick tree are necessary.

Examples

Semigroup operators

Constructing the corresponding Cartesian tree to solve a range minimum query.
Range minimum query reduced to the lowest common ancestor problem.
Main article: Range minimum query

When the function of interest in a range query is a semigroup operator, the notion of f 1 {\displaystyle f^{-1}} is not always defined, so the strategy in the previous section does not work. Andrew Yao showed that there exists an efficient solution for range queries that involve semigroup operators. He proved that for any constant c, a pre-processing of time and space Θ ( c n ) {\displaystyle \Theta (c\cdot n)} allows to answer range queries on lists where f is a semigroup operator in θ ( α c ( n ) ) {\displaystyle \theta (\alpha _{c}(n))} time, where α c {\displaystyle \alpha _{c}} is a certain functional inverse of the Ackermann function.

There are some semigroup operators that admit slightly better solutions. For instance when f { max , min } {\displaystyle f\in \{\max ,\min \}} . Assume f = min {\displaystyle f=\min } then min ( A [ 1.. n ] ) {\displaystyle \min(A)} returns the index of the minimum element of A [ 1.. n ] {\displaystyle A} . Then min i , j ( A ) {\textstyle \min _{i,j}(A)} denotes the corresponding minimum range query. There are several data structures that allow to answer a range minimum query in O ( 1 ) {\displaystyle O(1)} time using a pre-processing of time and space O ( n ) {\displaystyle O(n)} . One such solution is based on the equivalence between this problem and the lowest common ancestor problem.

The Cartesian tree T A {\displaystyle T_{A}} of an array A [ 1 , n ] {\displaystyle A} has as root a i = min { a 1 , a 2 , , a n } {\displaystyle a_{i}=\min\{a_{1},a_{2},\ldots ,a_{n}\}} and as left and right subtrees the Cartesian tree of A [ 1 , i 1 ] {\displaystyle A} and the Cartesian tree of A [ i + 1 , n ] {\displaystyle A} respectively. A range minimum query min i , j ( A ) {\textstyle \min _{i,j}(A)} is the lowest common ancestor in T A {\displaystyle T_{A}} of a i {\displaystyle a_{i}} and a j {\displaystyle a_{j}} . Because the lowest common ancestor can be solved in constant time using a pre-processing of time and space O ( n ) {\displaystyle O(n)} , range minimum query can as well. The solution when f = max {\displaystyle f=\max } is analogous. Cartesian trees can be constructed in linear time.

Mode

Main article: Range mode query

The mode of an array is the element that appears the most in it. For instance the mode of a = [ 4 , 5 , 6 , 7 , 4 ] {\displaystyle a=} is 4. In case of a tie, any of the most frequent elements might be picked as the mode. A range mode query consists in pre-processing A [ 1 , n ] {\displaystyle A} such that we can find the mode in any range of A [ 1 , n ] {\displaystyle A} . Several data structures have been devised to solve this problem, we summarize some of the results in the following table.

Range Mode Queries
Space Query Time Restrictions
O ( n 2 2 ϵ ) {\displaystyle O(n^{2-2\epsilon })} O ( n ϵ log n ) {\displaystyle O(n^{\epsilon }\log n)} 0 ϵ 1 2 {\displaystyle 0\leq \epsilon \leq {\frac {1}{2}}}
O ( n 2 log log n log n ) {\displaystyle O\left({\frac {n^{2}\log \log n}{\log n}}\right)} O ( 1 ) {\displaystyle O(1)}

Recently Jørgensen et al. proved a lower bound on the cell-probe model of Ω ( log n log ( S w / n ) ) {\displaystyle \Omega \left({\tfrac {\log n}{\log(Sw/n)}}\right)} for any data structure that uses S cells.

Median

This particular case is of special interest since finding the median has several applications. On the other hand, the median problem, a special case of the selection problem, is solvable in O(n), using the median of medians algorithm. However its generalization through range median queries is recent. A range median query median ( A , i , j ) {\displaystyle \operatorname {median} (A,i,j)} where A,i and j have the usual meanings returns the median element of A [ i , j ] {\displaystyle A} . Equivalently, median ( A , i , j ) {\displaystyle \operatorname {median} (A,i,j)} should return the element of A [ i , j ] {\displaystyle A} of rank j i 2 {\displaystyle {\frac {j-i}{2}}} . Range median queries cannot be solved by following any of the previous methods discussed above including Yao's approach for semigroup operators.

There have been studied two variants of this problem, the offline version, where all the k queries of interest are given in a batch, and a version where all the pre-processing is done up front. The offline version can be solved with O ( n log k + k log n ) {\displaystyle O(n\log k+k\log n)} time and O ( n log k ) {\displaystyle O(n\log k)} space.

The following pseudocode of the quickselect algorithm shows how to find the element of rank r in A [ i , j ] {\displaystyle A} an unsorted array of distinct elements, to find the range medians we set r = j i 2 {\displaystyle r={\frac {j-i}{2}}} .

rangeMedian(A, i, j, r) {
    if A.length() == 1
        return A
    if A.low is undefined then
        m = median(A)
        A.low  = 
        A.high = 
    calculate t the number of elements of A that belong to A.low
    if r <= t then
        return rangeMedian(A.low, i, j, r)
    else
        return rangeMedian(A.high, i, j, r-t)
}

Procedure rangeMedian partitions A, using A's median, into two arrays A.low and A.high, where the former contains the elements of A that are less than or equal to the median m and the latter the rest of the elements of A. If we know that the number of elements of A [ i , j ] {\displaystyle A} that end up in A.low is t and this number is bigger than r then we should keep looking for the element of rank r in A.low; otherwise we should look for the element of rank ( r t ) {\displaystyle (r-t)} in A.high. To find t, it is enough to find the maximum index m i 1 {\displaystyle m\leq i-1} such that a m {\displaystyle a_{m}} is in A.low and the maximum index l j {\displaystyle l\leq j} such that a l {\displaystyle a_{l}} is in A.high. Then t = l m {\displaystyle t=l-m} . The total cost for any query, without considering the partitioning part, is log n {\displaystyle \log n} since at most log n {\displaystyle \log n} recursion calls are done and only a constant number of operations are performed in each of them (to get the value of t fractional cascading should be used). If a linear algorithm to find the medians is used, the total cost of pre-processing for k range median queries is n log k {\displaystyle n\log k} . The algorithm can also be modified to solve the online version of the problem.

Majority

Finding frequent elements in a given set of items is one of the most important tasks in data mining. Finding frequent elements might be a difficult task to achieve when most items have similar frequencies. Therefore, it might be more beneficial if some threshold of significance was used for detecting such items. One of the most famous algorithms for finding the majority of an array was proposed by Boyer and Moore which is also known as the Boyer–Moore majority vote algorithm. Boyer and Moore proposed an algorithm to find the majority element of a string (if it has one) in O ( n ) {\displaystyle O(n)} time and using O ( 1 ) {\displaystyle O(1)} space. In the context of Boyer and Moore’s work and generally speaking, a majority element in a set of items (for example string or an array) is one whose number of instances is more than half of the size of that set. Few years later, Misra and Gries proposed a more general version of Boyer and Moore's algorithm using O ( n log ( 1 τ ) ) {\displaystyle O\left(n\log \left({\frac {1}{\tau }}\right)\right)} comparisons to find all items in an array whose relative frequencies are greater than some threshold 0 < τ < 1 {\displaystyle 0<\tau <1} . A range τ {\displaystyle \tau } -majority query is one that, given a subrange of a data structure (for example an array) of size | R | {\displaystyle |R|} , returns the set of all distinct items that appear more than (or in some publications equal to) τ | R | {\displaystyle \tau |R|} times in that given range. In different structures that support range τ {\displaystyle \tau } -majority queries, τ {\displaystyle \tau } can be either static (specified during pre-processing) or dynamic (specified at query time). Many of such approaches are based on the fact that, regardless of the size of the range, for a given τ {\displaystyle \tau } there could be at most O ( 1 / τ ) {\displaystyle O(1/\tau )} distinct candidates with relative frequencies at least τ {\displaystyle \tau } . By verifying each of these candidates in constant time, O ( 1 / τ ) {\displaystyle O(1/\tau )} query time is achieved. A range τ {\displaystyle \tau } -majority query is decomposable in the sense that a τ {\displaystyle \tau } -majority in a range R {\displaystyle R} with partitions R 1 {\displaystyle R_{1}} and R 2 {\displaystyle R_{2}} must be a τ {\displaystyle \tau } -majority in either R 1 {\displaystyle R_{1}} or R 2 {\displaystyle R_{2}} . Due to this decomposability, some data structures answer τ {\displaystyle \tau } -majority queries on one-dimensional arrays by finding the Lowest common ancestor (LCA) of the endpoints of the query range in a Range tree and validating two sets of candidates (of size O ( 1 / τ ) {\displaystyle O(1/\tau )} ) from each endpoint to the lowest common ancestor in constant time resulting in O ( 1 / τ ) {\displaystyle O(1/\tau )} query time.

Two-dimensional arrays

Gagie et al. proposed a data structure that supports range τ {\displaystyle \tau } -majority queries on an m × n {\displaystyle m\times n} array A {\displaystyle A} . For each query Q = ( R , τ ) {\displaystyle \operatorname {Q} =(\operatorname {R} ,\tau )} in this data structure a threshold 0 < τ < 1 {\displaystyle 0<\tau <1} and a rectangular range R {\displaystyle \operatorname {R} } are specified, and the set of all elements that have relative frequencies (inside that rectangular range) greater than or equal to τ {\displaystyle \tau } are returned as the output. This data structure supports dynamic thresholds (specified at query time) and a pre-processing threshold α {\displaystyle \alpha } based on which it is constructed. During the pre-processing, a set of vertical and horizontal intervals are built on the m × n {\displaystyle m\times n} array. Together, a vertical and a horizontal interval form a block. Each block is part of a superblock nine times bigger than itself (three times the size of the block's horizontal interval and three times the size of its vertical one). For each block a set of candidates (with 9 α {\displaystyle {\frac {9}{\alpha }}} elements at most) is stored which consists of elements that have relative frequencies at least α 9 {\displaystyle {\frac {\alpha }{9}}} (the pre-processing threshold as mentioned above) in its respective superblock. These elements are stored in non-increasing order according to their frequencies and it is easy to see that, any element that has a relative frequency at least α {\displaystyle \alpha } in a block must appear its set of candidates. Each τ {\displaystyle \tau } -majority query is first answered by finding the query block, or the biggest block that is contained in the provided query rectangle in O ( 1 ) {\displaystyle O(1)} time. For the obtained query block, the first 9 τ {\displaystyle {\frac {9}{\tau }}} candidates are returned (without being verified) in O ( 1 / τ ) {\displaystyle O(1/\tau )} time, so this process might return some false positives. Many other data structures (as discussed below) have proposed methods for verifying each candidate in constant time and thus maintaining the O ( 1 / τ ) {\displaystyle O(1/\tau )} query time while returning no false positives. The cases in which the query block is smaller than 1 / α {\displaystyle 1/\alpha } are handled by storing log ( 1 α ) {\displaystyle \log \left({\frac {1}{\alpha }}\right)} different instances of this data structure of the following form:

β = 2 i , i { 1 , , log ( 1 α ) } {\displaystyle \beta =2^{-i},\;\;i\in \left\{1,\dots ,\log \left({\frac {1}{\alpha }}\right)\right\}}

where β {\displaystyle \beta } is the pre-processing threshold of the i {\displaystyle i} -th instance. Thus, for query blocks smaller than 1 / α {\displaystyle 1/\alpha } the log ( 1 / τ ) {\displaystyle \lceil \log(1/\tau )\rceil } -th instance is queried. As mentioned above, this data structure has query time O ( 1 / τ ) {\displaystyle O(1/\tau )} and requires O ( m n ( H + 1 ) log 2 ( 1 α ) ) {\displaystyle O\left(mn(H+1)\log ^{2}\left({\frac {1}{\alpha }}\right)\right)} bits of space by storing a Huffman-encoded copy of it (note the log ( 1 α ) {\displaystyle \log({\frac {1}{\alpha }})} factor and also see Huffman coding).

One-dimensional arrays

Chan et al. proposed a data structure that given a one-dimensional array A {\displaystyle A} , a subrange R {\displaystyle R} of A {\displaystyle A} (specified at query time) and a threshold τ {\displaystyle \tau } (specified at query time), is able to return the list of all τ {\displaystyle \tau } -majorities in O ( 1 / τ ) {\displaystyle O(1/\tau )} time requiring O ( n log n ) {\displaystyle O(n\log n)} words of space. To answer such queries, Chan et al. begin by noting that there exists a data structure capable of returning the top-k most frequent items in a range in O ( k ) {\displaystyle O(k)} time requiring O ( n ) {\displaystyle O(n)} words of space. For a one-dimensional array A [ 0 , . . , n 1 ] {\displaystyle A} , let a one-sided top-k range query to be of form A [ 0.. i ]  for  0 i n 1 {\displaystyle A{\text{ for }}0\leq i\leq n-1} . For a maximal range of ranges A [ 0.. i ]  through  A [ 0.. j ] {\displaystyle A{\text{ through }}A} in which the frequency of a distinct element e {\displaystyle e} in A {\displaystyle A} remains unchanged (and equal to f {\displaystyle f} ), a horizontal line segment is constructed. The x {\displaystyle x} -interval of this line segment corresponds to [ i , j ] {\displaystyle } and it has a y {\displaystyle y} -value equal to f {\displaystyle f} . Since adding each element to A {\displaystyle A} changes the frequency of exactly one distinct element, the aforementioned process creates O ( n ) {\displaystyle O(n)} line segments. Moreover, for a vertical line x = i {\displaystyle x=i} all horizonal line segments intersecting it are sorted according to their frequencies. Note that, each horizontal line segment with x {\displaystyle x} -interval [ , r ] {\displaystyle } corresponds to exactly one distinct element e {\displaystyle e} in A {\displaystyle A} , such that A [ ] = e {\displaystyle A=e} . A top-k query can then be answered by shooting a vertical ray x = i {\displaystyle x=i} and reporting the first k {\displaystyle k} horizontal line segments that intersect it (remember from above that these line segments are already sorted according to their frequencies) in O ( k ) {\displaystyle O(k)} time.

Chan et al. first construct a range tree in which each branching node stores one copy of the data structure described above for one-sided range top-k queries and each leaf represents an element from A {\displaystyle A} . The top-k data structure at each node is constructed based on the values existing in the subtrees of that node and is meant to answer one-sided range top-k queries. Please note that for a one-dimensional array A {\displaystyle A} , a range tree can be constructed by dividing A {\displaystyle A} into two halves and recursing on both halves; therefore, each node of the resulting range tree represents a range. It can also be seen that this range tree requires O ( n log n ) {\displaystyle O(n\log n)} words of space, because there are O ( log n ) {\displaystyle O(\log n)} levels and each level {\displaystyle \ell } has 2 {\displaystyle 2^{\ell }} nodes. Moreover, since at each level {\displaystyle \ell } of a range tree all nodes have a total of n {\displaystyle n} elements of A {\displaystyle A} at their subtrees and since there are O ( log n ) {\displaystyle O(\log n)} levels, the space complexity of this range tree is O ( n log n ) {\displaystyle O(n\log n)} .

Using this structure, a range τ {\displaystyle \tau } -majority query A [ i . . j ] {\displaystyle A} on A [ 0.. n 1 ] {\displaystyle A} with 0 i j n {\displaystyle 0\leq i\leq j\leq n} is answered as follows. First, the lowest common ancestor (LCA) of leaf nodes i {\displaystyle i} and j {\displaystyle j} is found in constant time. Note that there exists a data structure requiring O ( n ) {\displaystyle O(n)} bits of space that is capable of answering the LCA queries in O ( 1 ) {\displaystyle O(1)} time. Let z {\displaystyle z} denote the LCA of i {\displaystyle i} and j {\displaystyle j} , using z {\displaystyle z} and according to the decomposability of range τ {\displaystyle \tau } -majority queries (as described above and in ), the two-sided range query A [ i . . j ] {\displaystyle A} can be converted into two one-sided range top-k queries (from z {\displaystyle z} to i {\displaystyle i} and j {\displaystyle j} ). These two one-sided range top-k queries return the top-( 1 / τ {\displaystyle 1/\tau } ) most frequent elements in each of their respective ranges in O ( 1 / τ ) {\displaystyle O(1/\tau )} time. These frequent elements make up the set of candidates for τ {\displaystyle \tau } -majorities in A [ i . . j ] {\displaystyle A} in which there are O ( 1 / τ ) {\displaystyle O(1/\tau )} candidates some of which might be false positives. Each candidate is then assessed in constant time using a linear-space data structure (as described in Lemma 3 in ) that is able to determine in O ( 1 ) {\displaystyle O(1)} time whether or not a given subrange of an array A {\displaystyle A} contains at least q {\displaystyle q} instances of a particular element e {\displaystyle e} .

Tree paths

Gagie et al. proposed a data structure which supports queries such that, given two nodes u {\displaystyle u} and v {\displaystyle v} in a tree, are able to report the list of elements that have a greater relative frequency than τ {\displaystyle \tau } on the path from u {\displaystyle u} to v {\displaystyle v} . More formally, let T {\displaystyle T} be a labelled tree in which each node has a label from an alphabet of size σ {\displaystyle \sigma } . Let l a b e l ( u ) [ 1 , , σ ] {\displaystyle label(u)\in } denote the label of node u {\displaystyle u} in T {\displaystyle T} . Let P u v {\displaystyle P_{uv}} denote the unique path from u {\displaystyle u} to v {\displaystyle v} in T {\displaystyle T} in which middle nodes are listed in the order they are visited. Given T {\displaystyle T} , and a fixed (specified during pre-processing) threshold 0 < τ < 1 {\displaystyle 0<\tau <1} , a query Q ( u , v ) {\displaystyle Q(u,v)} must return the set of all labels that appear more than τ | P u v | {\displaystyle \tau |P_{uv}|} times in P u v {\displaystyle P_{uv}} .

To construct this data structure, first O ( τ n ) {\displaystyle {O}(\tau n)} nodes are marked. This can be done by marking any node that has distance at least 1 / τ {\displaystyle \lceil 1/\tau \rceil } from the bottom of the three (height) and whose depth is divisible by 1 / τ {\displaystyle \lceil 1/\tau \rceil } . After doing this, it can be observed that the distance between each node and its nearest marked ancestor is less than 2 1 / τ {\displaystyle 2\lceil 1/\tau \rceil } . For a marked node x {\displaystyle x} , log ( d e p t h ( x ) ) {\displaystyle \log(depth(x))} different sequences (paths towards the root) P i ( x ) {\displaystyle P_{i}(x)} are stored,

P i ( x ) = label ( x ) , par ( x ) , par 2 ( x ) , , par 2 i ( x ) {\displaystyle P_{i}(x)=\left\langle \operatorname {label} (x),\operatorname {par} (x),\operatorname {par} ^{2}(x),\ldots ,\operatorname {par} ^{2^{i}}(x)\right\rangle }

for 0 i log ( d e p t h ( x ) ) {\displaystyle 0\leq i\leq \log(depth(x))} where par ( x ) {\displaystyle \operatorname {par} (x)} returns the label of the direct parent of node x {\displaystyle x} . Put another way, for each marked node, the set of all paths with a power of two length (plus one for the node itself) towards the root is stored. Moreover, for each P i ( x ) {\displaystyle P_{i}(x)} , the set of all majority candidates C i ( x ) {\displaystyle C_{i}(x)} are stored. More specifically, C i ( x ) {\displaystyle C_{i}(x)} contains the set of all ( τ / 2 ) {\displaystyle (\tau /2)} -majorities in P i ( x ) {\displaystyle P_{i}(x)} or labels that appear more than ( τ / 2 ) . ( 2 i + 1 ) {\displaystyle (\tau /2).(2^{i}+1)} times in P i ( x ) {\displaystyle P_{i}(x)} . It is easy to see that the set of candidates C i ( x ) {\displaystyle C_{i}(x)} can have at most 2 / τ {\displaystyle 2/\tau } distinct labels for each i {\displaystyle i} . Gagie et al. then note that the set of all τ {\displaystyle \tau } -majorities in the path from any marked node x {\displaystyle x} to one of its ancestors z {\displaystyle z} is included in some C i ( x ) {\displaystyle C_{i}(x)} (Lemma 2 in ) since the length of P i ( x ) {\displaystyle P_{i}(x)} is equal to ( 2 i + 1 ) {\displaystyle (2^{i}+1)} thus there exists a P i ( x ) {\displaystyle P_{i}(x)} for 0 i log ( d e p t h ( x ) ) {\displaystyle 0\leq i\leq \log(depth(x))} whose length is between d x z  and  2 d x z {\displaystyle d_{xz}{\text{ and }}2d_{xz}} where d x z {\displaystyle d_{xz}} is the distance between x and z. The existence of such P i ( x ) {\displaystyle P_{i}(x)} implies that a τ {\displaystyle \tau } -majority in the path from x {\displaystyle x} to z {\displaystyle z} must be a ( τ / 2 ) {\displaystyle (\tau /2)} -majority in P i ( x ) {\displaystyle P_{i}(x)} , and thus must appear in C i ( x ) {\displaystyle C_{i}(x)} . It is easy to see that this data structure require O ( n log n ) {\displaystyle O(n\log n)} words of space, because as mentioned above in the construction phase O ( τ n ) {\displaystyle O(\tau n)} nodes are marked and for each marked node some candidate sets are stored. By definition, for each marked node O ( log n ) {\displaystyle O(\log n)} of such sets are stores, each of which contains O ( 1 / τ ) {\displaystyle O(1/\tau )} candidates. Therefore, this data structure requires O ( log n × ( 1 / τ ) × τ n ) = O ( n log n ) {\displaystyle O(\log n\times (1/\tau )\times \tau n)=O(n\log n)} words of space. Please note that each node x {\displaystyle x} also stores c o u n t ( x ) {\displaystyle count(x)} which is equal to the number of instances of l a b e l ( x ) {\displaystyle label(x)} on the path from x {\displaystyle x} to the root of T {\displaystyle T} , this does not increase the space complexity since it only adds a constant number of words per node.

Each query between two nodes u {\displaystyle u} and v {\displaystyle v} can be answered by using the decomposability property (as explained above) of range τ {\displaystyle \tau } -majority queries and by breaking the query path between u {\displaystyle u} and v {\displaystyle v} into four subpaths. Let z {\displaystyle z} be the lowest common ancestor of u {\displaystyle u} and v {\displaystyle v} , with x {\displaystyle x} and y {\displaystyle y} being the nearest marked ancestors of u {\displaystyle u} and v {\displaystyle v} respectively. The path from u {\displaystyle u} to v {\displaystyle v} is decomposed into the paths from u {\displaystyle u} and v {\displaystyle v} to x {\displaystyle x} and y {\displaystyle y} respectively (the size of these paths are smaller than 2 1 / τ {\displaystyle 2\lceil 1/\tau \rceil } by definition, all of which are considered as candidates), and the paths from x {\displaystyle x} and y {\displaystyle y} to z {\displaystyle z} (by finding the suitable C i ( x ) {\displaystyle C_{i}(x)} as explained above and considering all of its labels as candidates). Please note that, boundary nodes have to be handled accordingly so that all of these subpaths are disjoint and from all of them a set of O ( 1 / τ ) {\displaystyle O(1/\tau )} candidates is derived. Each of these candidates is then verified using a combination of the l a b e l a n c ( x , ) {\displaystyle labelanc(x,\ell )} query which returns the lowest ancestor of node x {\displaystyle x} that has label {\displaystyle \ell } and the c o u n t ( x ) {\displaystyle count(x)} fields of each node. On a w {\displaystyle w} -bit RAM and an alphabet of size σ {\displaystyle \sigma } , the l a b e l a n c ( x , ) {\displaystyle labelanc(x,\ell )} query can be answered in O ( log log w σ ) {\displaystyle O\left(\log \log _{w}\sigma \right)} time whilst having linear space requirements. Therefore, verifying each of the O ( 1 / τ ) {\displaystyle O(1/\tau )} candidates in O ( log log w σ ) {\displaystyle O\left(\log \log _{w}\sigma \right)} time results in O ( ( 1 / τ ) log log w σ ) {\displaystyle O\left((1/\tau )\log \log _{w}\sigma \right)} total query time for returning the set of all τ {\displaystyle \tau } -majorities on the path from u {\displaystyle u} to v {\displaystyle v} .

Related problems

All the problems described above have been studied for higher dimensions as well as their dynamic versions. On the other hand, range queries might be extended to other data structures like trees, such as the level ancestor problem. A similar family of problems are orthogonal range queries, also known as counting queries.

See also

References

  1. ^ Krizanc, Danny; Morin, Pat; Smid, Michiel H. M. (2003). "Range Mode and Range Median Queries on Lists and Trees". ISAAC: 517–526. arXiv:cs/0307034. Bibcode:2003cs........7034K.
  2. Meng, He; Munro, J. Ian; Nicholson, Patrick K. (2011). "Dynamic Range Selection in Linear Space". ISAAC: 160–169. arXiv:1106.5076.
  3. Yao, Andrew C. (1982). "Space-time tradeoff for answering range queries (Extended Abstract)". Proceedings of the fourteenth annual ACM symposium on Theory of computing - STOC '82. pp. 128–136. doi:10.1145/800070.802185. ISBN 0-89791-070-2.
  4. Greve, Mark; Jørgensen, Allan Grønlund; Larsen, Kasper Dalgaard; Truelsen, Jakob (2010). "Cell Probe Lower Bounds and Approximations for Range Mode". Automata, Languages and Programming. Lecture Notes in Computer Science. Vol. 6198. pp. 605–616. doi:10.1007/978-3-642-14165-2_51. ISBN 978-3-642-14164-5.
  5. Har-Peled, Sariel; Muthukrishnan, S. (2008). "Range Medians". Algorithms - ESA 2008. Lecture Notes in Computer Science. Vol. 5193. pp. 503–514. arXiv:0807.0222. doi:10.1007/978-3-540-87744-8_42. ISBN 978-3-540-87743-1.
  6. Blum, M.; Floyd, R. W.; Pratt, V. R.; Rivest, R. L.; Tarjan, R. E. (August 1973). "Time bounds for selection" (PDF). Journal of Computer and System Sciences. 7 (4): 448–461. doi:10.1016/S0022-0000(73)80033-9.
  7. ^ Gfeller, Beat; Sanders, Peter (2009). "Towards Optimal Range Medians". Automata, Languages and Programming. Lecture Notes in Computer Science. Vol. 5555. pp. 475–486. arXiv:0901.1761. doi:10.1007/978-3-642-02927-1_40. ISBN 978-3-642-02926-4.
  8. ^ Bose, Prosenjit; Kranakis, Evangelos; Morin, Pat; Tang, Yihui (2005). "Approximate Range Mode and Range Median Queries" (PDF). Stacs 2005. Lecture Notes in Computer Science. Vol. 3404. pp. 377–388. doi:10.1007/978-3-540-31856-9_31. ISBN 978-3-540-24998-6.
  9. Boyer, Robert S.; Moore, J. Strother (1991). "MJRTY—A Fast Majority Vote Algorithm". Automated Reasoning. Automated Reasoning Series. Vol. 1. Dordrecht: Springer Netherlands. pp. 105–117. doi:10.1007/978-94-011-3488-0_5. ISBN 978-94-010-5542-0. Retrieved 2021-12-18.
  10. Misra, J.; Gries, David (November 1982). "Finding repeated elements". Science of Computer Programming. 2 (2): 143–152. doi:10.1016/0167-6423(82)90012-0. hdl:1813/6345. ISSN 0167-6423.
  11. ^ Karpiński, Marek. Searching for frequent colors in rectangles. OCLC 277046650.
  12. Gagie, Travis; He, Meng; Munro, J. Ian; Nicholson, Patrick K. (2011). "Finding Frequent Elements in Compressed 2D Arrays and Strings". String Processing and Information Retrieval. Lecture Notes in Computer Science. Vol. 7024. Berlin, Heidelberg: Springer Berlin Heidelberg. pp. 295–300. doi:10.1007/978-3-642-24583-1_29. ISBN 978-3-642-24582-4. Retrieved 2021-12-18.
  13. ^ Chan, Timothy M.; Durocher, Stephane; Skala, Matthew; Wilkinson, Bryan T. (2012). "Linear-Space Data Structures for Range Minority Query in Arrays". Algorithm Theory – SWAT 2012. Lecture Notes in Computer Science. Vol. 7357. Berlin, Heidelberg: Springer Berlin Heidelberg. pp. 295–306. doi:10.1007/978-3-642-31155-0_26. ISBN 978-3-642-31154-3. Retrieved 2021-12-20.
  14. Sadakane, Kunihiko; Navarro, Gonzalo (2010-01-17). "Fully-Functional Succinct Trees". Proceedings of the Twenty-First Annual ACM-SIAM Symposium on Discrete Algorithms. Philadelphia, PA: Society for Industrial and Applied Mathematics. pp. 134–149. doi:10.1137/1.9781611973075.13. ISBN 978-0-89871-701-3. S2CID 3189222.
  15. Chan, Timothy M.; Durocher, Stephane; Larsen, Kasper Green; Morrison, Jason; Wilkinson, Bryan T. (2013-03-08). "Linear-Space Data Structures for Range Mode Query in Arrays". Theory of Computing Systems. 55 (4): 719–741. doi:10.1007/s00224-013-9455-2. ISSN 1432-4350. S2CID 253747004.
  16. ^ Gagie, Travis; He, Meng; Navarro, Gonzalo; Ochoa, Carlos (September 2020). "Tree path majority data structures". Theoretical Computer Science. 833: 107–119. arXiv:1806.01804. doi:10.1016/j.tcs.2020.05.039. ISSN 0304-3975.
  17. He, Meng; Munro, J. Ian; Zhou, Gelin (2014-07-08). "A Framework for Succinct Labeled Ordinal Trees over Large Alphabets". Algorithmica. 70 (4): 696–717. doi:10.1007/s00453-014-9894-4. ISSN 0178-4617. S2CID 253977813.

External links

Tree data structures
Search trees
(dynamic sets/associative arrays)
Heaps
Tries
Spatial data partitioning trees
Other trees
Category: