Range mode query

Last updated

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.

Contents

Problem statement

Given an array , we wish to answer queries of the form , where . The mode of any array is an element such that the frequency of is greater than or equal to the frequency of . For example, if , then 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 .

Theorem 1

Let and be any multisets. If is a mode of and , then is a mode of .

Proof

Let be a mode of and be its frequency in . Suppose that is not a mode of . Thus, there exists an element with frequency that is the mode of . Since is the mode of and that , then . Thus, should be the mode of which is a contradiction.

Results

SpaceQuery TimeRestrictionsSource
[1]
is the word size [1]
[2]
[1]
[2]

Lower bound

Any data structure using cells of bits each needs 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 and the mode of , there is no simple way of computing the mode of . Any element of or could be the mode. For example, if and its frequency is , and and its frequency is also , there could be an element with frequency in and frequency in . , but its frequency in is greater than the frequency of and , which makes a better candidate for than or .

Linear space data structure with square root query time

This method by Chan et al. [1] uses space and query time. By setting , we get and bounds for space and query time.

Preprocessing

Let be an array, and be an array that contains the distinct values of A, where is the number of distinct elements. We define to be an array such that, for each , contains the rank (position) of in . Arrays can be created by a linear scan of .

Arrays are also created, such that, for each , . We then create an array , such that, for all , contains the rank of in . Again, a linear scan of suffices to create arrays and .

It is now possible to answer queries of the form "is the frequency of in at least " in constant time, by checking whether .

The array is split B into blocks , each of size . Thus, a block spans over . The mode and the frequency of each block or set of consecutive blocks will be pre-computed in two tables and . is the mode of , or equivalently, the mode of , and stores the corresponding frequency. These two tables can be stored in space, and can be populated in by scanning times, computing a row of each time with the following algorithm:

algorithm computeS_Sprime isinput: Array B = [0:n - 1],          Array D = [0:Delta - 1],          Integer soutput: 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 forfor 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 doif firstOccurence[B[j]] = -1 then                 firstOccurence[B[j]]  j             end ifif atLeastQInstances(firstOccurence[B[j]], block_end, fc + 1) then                 c  B[j]                 fc  fc + 1             end ifif 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 ifend whilefor all j in {0, ..., Delta - 1} do             firstOccurence[j]  -1          end forend for

Query

We will define the query algorithm over array . This can be translated to an answer over , since for any , is a mode for if and only if is a mode for . We can convert an answer for to an answer for in constant time by looking in or at the corresponding index.

Given a query , the query is split in three parts: the prefix, the span and the suffix. Let and . These denote the indices of the first and last block that are completely contained in . The range of these blocks is called the span. The prefix is then (the set of indices before the span), and the suffix is (the set of indices after the span). The prefix, suffix or span can be empty, the latter is if .

For the span, the mode is already stored in . Let be the frequency of the mode, which is stored in . If the span is empty, let . Recall that, by Theorem 1, the mode of 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 , in which case and are updated to the new value. At the end of the scan, contains the mode of and its frequency.

Scanning procedure

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

Let be the index of the current element. There are three cases:

  1. If , then it was present in and its frequency has already been counted. Pass to the next element.
  2. Otherwise, check if the frequency of in is at least (this can be done in constant time since it is the equivalent of checking it for ).
    1. If it is not, then pass to the next element.
    2. If it is, then compute the actual frequency of in by a linear scan (starting at index ) or a binary search in . Set and .

This linear scan (excluding the frequency computations) is bounded by the block size , since neither the prefix or the suffix can be greater than . 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 .

Subquadratic space data structure with constant query time

This method by [2] uses 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 for constant query time if .

Preprocessing

Let be an array. The preprocessing is done in three steps:

  1. Split the array in blocks , where the size of each block is . Build a table of size where is the mode of . The total space for this step is
  2. For any query , let be the block that contains and be the block that contains . Let the span be the set of blocks completely contained in . The mode of the block can be retrieved from . By Theorem 1, the mode can be either an element of the prefix (indices of before the start of the span), an element of the suffix (indices of after the end of the span), or . The size of the prefix plus the size of the suffix is bounded by , thus the position of the mode isstored as an integer ranging from to , where indicates a position in the prefix/suffix and indicates that the mode is the mode of the span. There are possible queries involving blocks and , so these values are stored in a table of size . Furthermore, there are such tables, so the total space required for this step is . To access those tables, a pointer is added in addition to the mode in the table for each pair of blocks.
  3. To handle queries where and are in the same block, all such solutions are precomputed. There are of them, they are stored in a three dimensional table of this size.

The total space used by this data structure is , which reduces to if we take .

Query

Given a query , check if it is completely contained inside a block, in which case the answer is stored in table . If the query spans exactly one or more blocks, then the answer is found in table . Otherwise, use the pointer stored in table at position , where are the indices of the blocks that contain respectively and , to find the table that contains the positions of the mode for these blocks and use the position to find the mode in . This can be done in constant time.

Related Research Articles

Dynamic programming Problem optimization method.

Dynamic programming is both a mathematical optimization method and a computer programming method. The method was developed by Richard Bellman in the 1950s and has found applications in numerous fields, from aerospace engineering to economics.

In computer science, the Boyer–Moore string-search algorithm is an efficient string-searching algorithm that is the standard benchmark for practical string-search literature. It was developed by Robert S. Boyer and J Strother Moore in 1977. The original paper contained static tables for computing the pattern shifts without an explanation of how to produce them. The algorithm for producing the tables was published in a follow-on paper; this paper contained errors which were later corrected by Wojciech Rytter in 1980. The algorithm preprocesses the string being searched for, but not the string being searched in. It is thus well-suited for applications in which the pattern is much shorter than the text or where it persists across multiple searches. The Boyer–Moore algorithm uses information gathered during the preprocess step to skip sections of the text, resulting in a lower constant factor than many other string search algorithms. In general, the algorithm runs faster as the pattern length increases. The key features of the algorithm are to match on the tail of the pattern rather than the head, and to skip along the text in jumps of multiple characters rather than searching every single character in the text.

Suffix tree Tree containing all suffixes of a given text

In computer science, a suffix tree is a compressed trie containing all the suffixes of the given text as their keys and positions in the text as their values. Suffix trees allow particularly fast implementations of many important string operations.

In computer science, a fusion tree is a type of tree data structure that implements an associative array on w-bit integers. When operating on a collection of n key–value pairs, it uses O(n) space and performs searches in O(logwn) time, which is asymptotically faster than a traditional self-balancing binary search tree, and also better than the van Emde Boas tree for large values of w. It achieves this speed by exploiting certain constant-time operations that can be done on a machine word. Fusion trees were invented in 1990 by Michael Fredman and Dan Willard.

In computer science, a suffix array is a sorted array of all suffixes of a string. It is a data structure used in, among others, full text indices, data compression algorithms, and the field of bibliometrics.

In computer science, the prefix sum, cumulative sum, inclusive scan, or simply scan of a sequence of numbers x0, x1, x2, ... is a second sequence of numbers y0, y1, y2, ..., the sums of prefixes of the input sequence:

In descriptive complexity, a branch of computational complexity, FO is a complexity class of structures that can be recognized by formulas of first-order logic, and also equals the complexity class AC0. Descriptive complexity uses the formalism of logic, but does not use several key notions associated with logic such as proof theory or axiomatization.

In computer science, a succinct data structure is a data structure which uses an amount of space that is "close" to the information-theoretic lower bound, but still allows for efficient query operations. The concept was originally introduced by Jacobson to encode bit vectors, (unlabeled) trees, and planar graphs. Unlike general lossless data compression algorithms, succinct data structures retain the ability to use them in-place, without decompressing them first. A related notion is that of a compressed data structure, in which the size of the data structure depends upon the particular data being represented.

Fréchet distribution

The Fréchet distribution, also known as inverse Weibull distribution, is a special case of the generalized extreme value distribution. It has the cumulative distribution function

Range minimum query

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

In computer science, streaming algorithms are algorithms for processing data streams in which the input is presented as a sequence of items and can be examined in only a few passes. In most models, these algorithms have access to limited memory. They may also have limited processing time per item.

Samplesort is a sorting algorithm that is a divide and conquer algorithm often used in parallel processing systems. Conventional divide and conquer sorting algorithms partitions the array into sub-intervals or buckets. The buckets are then sorted individually and then concatenated together. However, if the array is non-uniformly distributed, the performance of these sorting algorithms can be significantly throttled. Samplesort addresses this issue by selecting a sample of size s from the n-element sequence, and determining the range of the buckets by sorting the sample and choosing p−1 < s elements from the result. These elements then divide the array into p approximately equal-sized buckets. Samplesort is described in the 1970 paper, "Samplesort: A Sampling Approach to Minimal Storage Tree Sorting", by W. D. Frazer and A. C. McKellar.

In computational complexity the decision tree model is the model of computation in which an algorithm is considered to be basically a decision tree, i.e., a sequence of queries or tests that are done adaptively, so the outcome of the previous tests can influence the test is performed next.

Fenwick tree

A Fenwick tree or binary indexed tree is a data structure that can efficiently update elements and calculate prefix sums in a table of numbers.

In machine learning, a Ranking SVM is a variant of the support vector machine algorithm, which is used to solve certain ranking problems. The ranking SVM algorithm was published by Thorsten Joachims in 2002. The original purpose of the algorithm was to improve the performance of an internet search engine. However, it was found that Ranking SVM also can be used to solve other problems such as Rank SIFT.

In data structures, a range query consists of preprocessing some input data into a data structure to efficiently answer any number of queries on any subset of the input. Particularly, there is a group of problems that have been extensively studied where the input is an array of unsorted numbers and a query consists of computing some function, such as the minimum, on a specific range of the array.

In computer science, the longest common prefix array is an auxiliary data structure to the suffix array. It stores the lengths of the longest common prefixes (LCPs) between all pairs of consecutive suffixes in a sorted suffix array.

Suffix automaton Deterministic finite automaton accepting set of all suffixes of particular string

In computer science, a suffix automaton is an efficient data structure for representing the substring index of a given string which allows the storage, processing, and retrieval of compressed information about all its substrings. The suffix automaton of a string is the smallest directed acyclic graph with a dedicated initial vertex and a set of "final" vertices, such that paths from the initial vertex to final vertices represent the suffixes of the string. Formally speaking, a suffix automaton is defined by the following set of properties:

  1. Its arcs are tagged with letters;
  2. none of its nodes have two outgoing arcs tagged with the same letter;
  3. for every suffix of there exists a path from initial vertex to some final vertex such that the concatenation of letters on the path forms this suffix;
  4. it has the fewest vertices among all graphs defined by the properties above.
Parallel external memory

In computer science, a parallel external memory (PEM) model is a cache-aware, external-memory abstract machine. It is the parallel-computing analogy to the single-processor external memory (EM) model. In a similar way, it is the cache-aware analogy to the parallel random-access machine (PRAM). The PEM model consists of a number of processors, together with their respective private caches and a shared main memory.

LSH is a cryptographic hash function designed in 2014 by South Korea to provide integrity in general-purpose software environments such as PCs and smart devices. LSH is one of the cryptographic algorithms approved by the Korean Cryptographic Module Validation Program (KCMVP). And it is the national standard of South Korea.

References

  1. 1 2 3 4 5 6 Chan, Timothy M.; Durocher, Stephane; Larsen, Kasper Green; Morrison, Jason; Wilkinson, Bryan T. (2013). "Linear-Space Data Structures for Range Mode Query in Arrays" (PDF). Theory of Computing Systems. Springer: 1–23.
  2. 1 2 3 Krizanc, Danny; Morin, Pat; Smid, Michiel H. M. (2003). "Range Mode and Range Median Queries on Lists and Trees" (PDF). ISAAC: 517–526.
  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.