Range mode query

From HandWiki

In data structures, the range mode query problem asks to build a data structure on some input data to efficiently answer queries asking for the mode of any consecutive subset of the input.

Problem statement

Given an array [math]\displaystyle{ A[1:n] = [a_1,a_2,...,a_n] }[/math], we wish to answer queries of the form [math]\displaystyle{ mode(A, i:j) }[/math], where [math]\displaystyle{ 1 \leq i \leq j \leq n }[/math]. The mode [math]\displaystyle{ mode(S) }[/math] of any array [math]\displaystyle{ S = [s_1,s_2,...,s_k] }[/math] is an element [math]\displaystyle{ s_i }[/math] such that the frequency of [math]\displaystyle{ s_i }[/math] is greater than or equal to the frequency of [math]\displaystyle{ s_j \; \forall j \in \{1,...,k\} }[/math]. For example, if [math]\displaystyle{ S = [1,2,4,2,3,4,2] }[/math], then [math]\displaystyle{ mode(S) = 2 }[/math] because it occurs three times, while all other values occur fewer times. In this problem, the queries ask for the mode of subarrays of the form [math]\displaystyle{ A[i:j] = [a_i,a_{i+1},...,a_j] }[/math].

Theorem 1

Let [math]\displaystyle{ A }[/math] and [math]\displaystyle{ B }[/math] be any multisets. If [math]\displaystyle{ c }[/math] is a mode of [math]\displaystyle{ A \cup B }[/math] and [math]\displaystyle{ c \notin A }[/math], then [math]\displaystyle{ c }[/math] is a mode of [math]\displaystyle{ B }[/math].

Proof

Let [math]\displaystyle{ c \notin A }[/math] be a mode of [math]\displaystyle{ C = A \cup B }[/math] and [math]\displaystyle{ f_c }[/math] be its frequency in [math]\displaystyle{ C }[/math]. Suppose that [math]\displaystyle{ c }[/math] is not a mode of [math]\displaystyle{ B }[/math]. Thus, there exists an element [math]\displaystyle{ b }[/math] with frequency [math]\displaystyle{ f_b }[/math] that is the mode of [math]\displaystyle{ B }[/math]. Since [math]\displaystyle{ b }[/math] is the mode of [math]\displaystyle{ B }[/math] and that [math]\displaystyle{ c \notin A }[/math], then [math]\displaystyle{ f_b \gt f_c }[/math]. Thus, [math]\displaystyle{ b }[/math] should be the mode of [math]\displaystyle{ C }[/math] which is a contradiction.

Results

Space Query Time Restrictions Source
[math]\displaystyle{ O(n) }[/math] [math]\displaystyle{ O(\sqrt{n}) }[/math] [1]
[math]\displaystyle{ O(n) }[/math] [math]\displaystyle{ O(\sqrt{n/w}) }[/math] [math]\displaystyle{ w }[/math] is the word size [1]
[math]\displaystyle{ O(n^2\log\log n/ \log n) }[/math] [math]\displaystyle{ O(1) }[/math] [2]
[math]\displaystyle{ O(n^{2-2\epsilon}/ \log n) }[/math] [math]\displaystyle{ O(n^\epsilon) }[/math] [math]\displaystyle{ 0\leq \epsilon\leq 1/2 }[/math] [1]

Lower bound

Any data structure using [math]\displaystyle{ S }[/math] cells of [math]\displaystyle{ w }[/math] bits each needs [math]\displaystyle{ \Omega\left(\frac{\log n}{\log (S w/n)}\right) }[/math] time to answer a range mode query.[3]

This contrasts with other range query problems, such as the range minimum query which have solutions offering constant time query time and linear space. This is due to the hardness of the mode problem, since even if we know the mode of [math]\displaystyle{ A[i:j] }[/math] and the mode of [math]\displaystyle{ A[j+1:k] }[/math], there is no simple way of computing the mode of [math]\displaystyle{ A[i:k] }[/math]. Any element of [math]\displaystyle{ A[i:j] }[/math] or [math]\displaystyle{ A[j+1:k] }[/math] could be the mode. For example, if [math]\displaystyle{ mode(A[i:j]) = a }[/math] and its frequency is [math]\displaystyle{ f_a }[/math], and [math]\displaystyle{ mode(A[j+1:k]) = b }[/math] and its frequency is also [math]\displaystyle{ f_a }[/math], there could be an element [math]\displaystyle{ c }[/math] with frequency [math]\displaystyle{ f_a-1 }[/math] in [math]\displaystyle{ A[i:j] }[/math] and frequency [math]\displaystyle{ f_a-1 }[/math] in [math]\displaystyle{ A[j+1:k] }[/math]. [math]\displaystyle{ a \not= c \not= b }[/math], but its frequency in [math]\displaystyle{ A[i:k] }[/math] is greater than the frequency of [math]\displaystyle{ a }[/math] and [math]\displaystyle{ b }[/math], which makes [math]\displaystyle{ c }[/math] a better candidate for [math]\displaystyle{ mode(A[i:k]) }[/math] than [math]\displaystyle{ a }[/math] or [math]\displaystyle{ b }[/math].

Linear space data structure with square root query time

This method by Chan et al.[1] uses [math]\displaystyle{ O(n + s^2) }[/math] space and [math]\displaystyle{ O(n/s) }[/math] query time. By setting [math]\displaystyle{ s=\sqrt{n} }[/math], we get [math]\displaystyle{ O(n) }[/math] and [math]\displaystyle{ O(\sqrt{n}) }[/math] bounds for space and query time.

Preprocessing

Let [math]\displaystyle{ A[1:n] }[/math] be an array, and [math]\displaystyle{ D[1:\Delta] }[/math] be an array that contains the distinct values of A, where [math]\displaystyle{ \Delta }[/math] is the number of distinct elements. We define [math]\displaystyle{ B[1:n] }[/math] to be an array such that, for each [math]\displaystyle{ i }[/math], [math]\displaystyle{ B[i] }[/math] contains the rank (position) of [math]\displaystyle{ A[i] }[/math] in [math]\displaystyle{ D }[/math]. Arrays [math]\displaystyle{ B,D }[/math] can be created by a linear scan of [math]\displaystyle{ A }[/math].

Arrays [math]\displaystyle{ Q_1, Q_2, ..., Q_\Delta }[/math] are also created, such that, for each [math]\displaystyle{ a \in \{1,...,\Delta\} }[/math], [math]\displaystyle{ Q_a = \{b\; |\; B[b] = a\} }[/math]. We then create an array [math]\displaystyle{ B'[1:n] }[/math], such that, for all [math]\displaystyle{ b \in \{1,...,n\} }[/math], [math]\displaystyle{ B'[b] }[/math] contains the rank of [math]\displaystyle{ b }[/math] in [math]\displaystyle{ Q_{B[b]} }[/math]. Again, a linear scan of [math]\displaystyle{ B }[/math] suffices to create arrays [math]\displaystyle{ Q_1,Q_2,...,Q_\Delta }[/math] and [math]\displaystyle{ B' }[/math].

It is now possible to answer queries of the form "is the frequency of [math]\displaystyle{ B[i] }[/math] in [math]\displaystyle{ B[i:j] }[/math] at least [math]\displaystyle{ q }[/math]" in constant time, by checking whether [math]\displaystyle{ Q_{B[i]}[B'[i]+q-1] \leq j }[/math].

The array is split B into [math]\displaystyle{ s }[/math] blocks [math]\displaystyle{ b_1,b_2,...,b_s }[/math], each of size [math]\displaystyle{ t=\lceil n/s\rceil }[/math]. Thus, a block [math]\displaystyle{ b_i }[/math] spans over [math]\displaystyle{ B[i\cdot t+1 : (i+1)t] }[/math]. The mode and the frequency of each block or set of consecutive blocks will be pre-computed in two tables [math]\displaystyle{ S }[/math] and [math]\displaystyle{ S' }[/math]. [math]\displaystyle{ S[b_i,b_j] }[/math] is the mode of [math]\displaystyle{ b_i \cup b_{i+1} \cup ... \cup b_j }[/math], or equivalently, the mode of [math]\displaystyle{ B[b_it+1 : (b_j+1) t] }[/math], and [math]\displaystyle{ S' }[/math] stores the corresponding frequency. These two tables can be stored in [math]\displaystyle{ O(s^2) }[/math] space, and can be populated in [math]\displaystyle{ O(s\cdot n) }[/math] by scanning [math]\displaystyle{ B }[/math] [math]\displaystyle{ s }[/math] times, computing a row of [math]\displaystyle{ S,S' }[/math] each time with the following algorithm:

algorithm computeS_Sprime is
    input: Array B = [0:n - 1], 
        Array D = [0:Delta - 1], 
        Integer s
    output: Tables S and Sprime
    let S ← Table(0:n - 1, 0:n - 1)
    let Sprime ← Table(0:n - 1, 0:n - 1)
    let firstOccurence ← Array(0:Delta - 1)
    for all i in {0, ..., Delta - 1} do
        firstOccurence[i] ← -1 
    end for
    for i ← 0:s - 1 do    
        let j ← i × t
        let c ← 0
        let fc ← 0
        let noBlock ← i
        let block_start ← j
        let block_end ← min{(i + 1) × t - 1, n - 1}
        while j < n do    
            if firstOccurence[B[j]] = -1 then
                firstOccurence[B[j]] ← j
            end if		
            if atLeastQInstances(firstOccurence[B[j]], block_end, fc + 1) then
                c ← B[j]
                fc ← fc + 1
            end if		
            if j = block_end then
                S[i * s + noBlock] ← c
                Sprime[i × s + noBlock] ← fc			
                noBlock ← noBlock + 1
                block_end ← min{block_end + t, n - 1}
            end if
        end while
        for all j in {0, ..., Delta - 1} do
            firstOccurence[j] ← -1 
        end for
    end for

Query

We will define the query algorithm over array [math]\displaystyle{ B }[/math]. This can be translated to an answer over [math]\displaystyle{ A }[/math], since for any [math]\displaystyle{ a,i,j }[/math], [math]\displaystyle{ B[a] }[/math] is a mode for [math]\displaystyle{ B[i:j] }[/math] if and only if [math]\displaystyle{ A[a] }[/math] is a mode for [math]\displaystyle{ A[i:j] }[/math]. We can convert an answer for [math]\displaystyle{ B }[/math] to an answer for [math]\displaystyle{ A }[/math] in constant time by looking in [math]\displaystyle{ A }[/math] or [math]\displaystyle{ B }[/math] at the corresponding index.

Given a query [math]\displaystyle{ mode(B,i,j) }[/math], the query is split in three parts: the prefix, the span and the suffix. Let [math]\displaystyle{ b_i=\lceil (i-1)/t\rceil }[/math] and [math]\displaystyle{ b_j = \lfloor j/t \rfloor -1 }[/math]. These denote the indices of the first and last block that are completely contained in [math]\displaystyle{ B }[/math]. The range of these blocks is called the span. The prefix is then [math]\displaystyle{ B[i:min\{b_i t,j\}] }[/math] (the set of indices before the span), and the suffix is [math]\displaystyle{ B[max\{(b_j+1)t+1,i\}:j] }[/math] (the set of indices after the span). The prefix, suffix or span can be empty, the latter is if [math]\displaystyle{ b_j \lt b_i }[/math].

For the span, the mode [math]\displaystyle{ c }[/math] is already stored in [math]\displaystyle{ S[b_i,b_j] }[/math]. Let [math]\displaystyle{ f_c }[/math] be the frequency of the mode, which is stored in [math]\displaystyle{ S'[b_i,b_j] }[/math]. If the span is empty, let [math]\displaystyle{ f_c=0 }[/math]. Recall that, by Theorem 1, the mode of [math]\displaystyle{ B[i:j] }[/math] is either an element of the prefix, span or suffix. A linear scan is performed over each element in the prefix and in the suffix to check if its frequency is greater than the current candidate [math]\displaystyle{ c }[/math], in which case [math]\displaystyle{ c }[/math] and [math]\displaystyle{ f_c }[/math] are updated to the new value. At the end of the scan, [math]\displaystyle{ c }[/math] contains the mode of [math]\displaystyle{ B[i:j] }[/math] and [math]\displaystyle{ f_c }[/math] its frequency.

Scanning procedure

The procedure is similar for both prefix and suffix, so it suffice to run this procedure for both:

Let [math]\displaystyle{ x }[/math] be the index of the current element. There are three cases:

  1. If [math]\displaystyle{ Q_{B[x]}[B'[x]-1] \geq i }[/math], then it was present in [math]\displaystyle{ B[i:x-1] }[/math] and its frequency has already been counted. Pass to the next element.
  2. Otherwise, check if the frequency of [math]\displaystyle{ B[x] }[/math] in [math]\displaystyle{ B[i:j] }[/math] is at least [math]\displaystyle{ f_c }[/math] (this can be done in constant time since it is the equivalent of checking it for [math]\displaystyle{ B[x:j] }[/math]).
    1. If it is not, then pass to the next element.
    2. If it is, then compute the actual frequency [math]\displaystyle{ f_x }[/math] of [math]\displaystyle{ B[x] }[/math] in [math]\displaystyle{ B[i:j] }[/math] by a linear scan (starting at index [math]\displaystyle{ B'[x]+f_c-1 }[/math]) or a binary search in [math]\displaystyle{ Q_{B[x]} }[/math]. Set [math]\displaystyle{ c:= B[x] }[/math] and [math]\displaystyle{ f_c := f_x }[/math].

This linear scan (excluding the frequency computations) is bounded by the block size [math]\displaystyle{ t }[/math], since neither the prefix or the suffix can be greater than [math]\displaystyle{ t }[/math]. A further analysis of the linear scans done for frequency computations shows that it is also bounded by the block size.[1] Thus, the query time is [math]\displaystyle{ O(t) = O(n/s) }[/math].

Subquadratic space data structure with constant query time

This method by [2] uses [math]\displaystyle{ O\left(\frac{n^2 \log{\log{n}}}{\log{n}}\right) }[/math] space for a constant time query. We can observe that, if a constant query time is desired, this is a better solution than the one proposed by Chan et al.,[1] as the latter gives a space of [math]\displaystyle{ O(n^2) }[/math] for constant query time if [math]\displaystyle{ s=n }[/math].

Preprocessing

Let [math]\displaystyle{ A[1:n] }[/math] be an array. The preprocessing is done in three steps:

  1. Split the array [math]\displaystyle{ A }[/math] in [math]\displaystyle{ s }[/math] blocks [math]\displaystyle{ b_1,b_2,...,b_s }[/math], where the size of each block is [math]\displaystyle{ t=\lceil n/s \rceil }[/math]. Build a table [math]\displaystyle{ S }[/math] of size [math]\displaystyle{ s \times s }[/math] where [math]\displaystyle{ S[i,j] }[/math] is the mode of [math]\displaystyle{ b_i \cup b_{i+1} \cup ... \cup b_j }[/math]. The total space for this step is [math]\displaystyle{ O(s^2) }[/math]
  2. For any query [math]\displaystyle{ mode(A,i,j) }[/math], let [math]\displaystyle{ b_{i'} }[/math] be the block that contains [math]\displaystyle{ i }[/math] and [math]\displaystyle{ b_{j'} }[/math] be the block that contains [math]\displaystyle{ j }[/math]. Let the span be the set of blocks completely contained in [math]\displaystyle{ A[i:j] }[/math]. The mode [math]\displaystyle{ c }[/math] of the block can be retrieved from [math]\displaystyle{ S }[/math]. By Theorem 1, the mode can be either an element of the prefix (indices of [math]\displaystyle{ A[i:j] }[/math] before the start of the span), an element of the suffix (indices of [math]\displaystyle{ A[i:j] }[/math] after the end of the span), or [math]\displaystyle{ c }[/math]. The size of the prefix plus the size of the suffix is bounded by [math]\displaystyle{ 2t }[/math], thus the position of the mode isstored as an integer ranging from [math]\displaystyle{ 0 }[/math] to [math]\displaystyle{ 2t }[/math], where [math]\displaystyle{ [0:2t-1] }[/math]indicates a position in the prefix/suffix and [math]\displaystyle{ 2t }[/math] indicates that the mode is the mode of the span. There are [math]\displaystyle{ \binom{t}{2} }[/math] possible queries involving blocks [math]\displaystyle{ b_{i'} }[/math] and [math]\displaystyle{ b_{j'} }[/math], so these values are stored in a table of size [math]\displaystyle{ t^2 }[/math]. Furthermore, there are [math]\displaystyle{ (2t+1)^{t^2} }[/math] such tables, so the total space required for this step is [math]\displaystyle{ O(t^2 (2t+1)^{t^2}) }[/math]. To access those tables, a pointer is added in addition to the mode in the table [math]\displaystyle{ S }[/math] for each pair of blocks.
  3. To handle queries [math]\displaystyle{ mode(A,i,j) }[/math] where [math]\displaystyle{ i }[/math] and [math]\displaystyle{ j }[/math] are in the same block, all such solutions are precomputed. There are [math]\displaystyle{ O(st^2) }[/math] of them, they are stored in a three dimensional table [math]\displaystyle{ T }[/math] of this size.

The total space used by this data structure is [math]\displaystyle{ O(s^2 + t^2(2t+1)^{t^2} + st^2) }[/math], which reduces to [math]\displaystyle{ O\left(\frac{n^2 \log{\log{n}}}{\log{n}}\right) }[/math] if we take [math]\displaystyle{ t=\sqrt{\log{n}/\log{\log{n}}} }[/math].

Query

Given a query [math]\displaystyle{ mode(A,i,j) }[/math], check if it is completely contained inside a block, in which case the answer is stored in table [math]\displaystyle{ T }[/math]. If the query spans exactly one or more blocks, then the answer is found in table [math]\displaystyle{ S }[/math]. Otherwise, use the pointer stored in table [math]\displaystyle{ S }[/math] at position [math]\displaystyle{ S[b_{i'},b_{j'}] }[/math], where [math]\displaystyle{ b_{i'},b_{j'} }[/math] are the indices of the blocks that contain respectively [math]\displaystyle{ i }[/math] and [math]\displaystyle{ j }[/math], to find the table [math]\displaystyle{ U_{b_{i'},b_{j'}} }[/math] that contains the positions of the mode for these blocks and use the position to find the mode in [math]\displaystyle{ A }[/math]. This can be done in constant time.

References

  1. 1.0 1.1 1.2 1.3 1.4 1.5 Chan, Timothy M.; Durocher, Stephane; Larsen, Kasper Green; Morrison, Jason; Wilkinson, Bryan T. (2013). "Linear-Space Data Structures for Range Mode Query in Arrays". Theory of Computing Systems (Springer): 1–23. http://cs.au.dk/~larsen/papers/linear_mode.pdf. 
  2. 2.0 2.1 Cite error: Invalid <ref> tag; no text was provided for refs named morin
  3. Greve, M; Jørgensen, A.; Larsen, K.; Truelsen, J. (2010). "Cell probe lower bounds and approximations for range mode". Automata, Languages and Programming: 605–616.