Range Quantile Queries: Another Virtue of Wavelet Trees
Range Quantile Queries: Another Virtue of Wavelet Trees
Range Quantile Queries: Another Virtue of Wavelet Trees
?
Another Virtue of Wavelet Trees
2
School of Computer Science and Information Technology,
Royal Melbourne Institute of Technology, Australia
{simon.puglisi,andrew.turpin}@rmit.edu.au
1 Introduction
If we are given a list of the closing prices of a stock for the past n days and asked
to find the kth lowest price, then we can do so in O(n) time [2]. We can also
preprocess the list in O(n log n) time and store it in O(n) words such that, given
k later, we can find the answer in O(1) time: we simply sort the list. However, we
might also later face range quantile queries, which have the form “what was the
kth lowest price in the interval between the `th and the rth days?”. Of course, we
could precompute the answers to all such queries, but storing them would take
Ω(n3 log n) bits of space. In this paper we show how to use a balanced wavelet
tree to store the list in O(n) words such that we can answer range quantile
queries in O(log σ) time, where σ is the number of distinct items in the entire
list. We can generalize our result to any constant number of dimensions but,
currently, only by using slightly super-linear space.
We know of no previous work on quantile queries3 , but several authors have
written about range median queries, the special case in which k is half the
length of the interval between ` and r. Krizanc, Morin and Smid [12] introduced
?
This work was supported by the Sofja Kovalevskaja Award from the Alexander von
Humboldt Foundation and the German Federal Ministry of Education and Research
and by the Australian Research Council.
??
Corresponding Author.
3
Henceforth, for brevity, we will use “quantile query” to mean “range quantile query”,
and similarly with other types of range queries.
Table 1. Bounds for range median queries.
the problem of preprocessing for median queries and gave four solutions, three
of which have worse bounds than using a balanced wavelet tree; their fourth
solution involves storing O n2 log log n/ log n words to answer queries in O(1)
time. Bose, Kranakis, Morin and Tang [3] then considered approximate queries,
and Har-Peled and Muthukrishnan [10] and Gfeller and Sanders [8] considered
batched queries. Recently, Krizanc et al.’s fourth solution was superseded by
one due to Petersen and Grabowski [16, 17], who reduced the space bound to
O n2 (log log n)2 / log2 n words. Table 1 shows the bounds for Krizanc et al.’s
first three solutions, for Petersen and Grabowski’s solution, and for using a
balanced wavelet tree.
Har-Peled and Muthukrishnan [10] describe applications of median queries
to the analysis of Web advertising logs. In the final section of this paper we
show that our solution for quantile queries can be used to support coloured
range reporting, that is, to enumerate the distinct items in a sublist. This result
immediately improves Välimäki and Mäkinen’s recent space-efficient solution to
the document listing problem [14, 19].
In the full version of this paper we will also discuss how to use a wavelet tree
to answer range counting queries (see [13]), coloured range counting queries (re-
turning the number of distinct elements in a range without enumerating them),
and how to support updates at the cost of slowing queries down to take time
proportional to the logarithm of the largest number allowed.
2 Wavelet Trees
Grossi, Gupta and Vitter [9] introduced wavelet trees for use in data compression,
and Ferragina, Giancarlo and Manzini [6] showed they have myriad virtues in this
respect. Wavelet trees are also important for compressed full-text indexing [15].
As we shall see, there is yet more to this intriguing data structure.
A wavelet tree T for a sequence s of length n is an ordered, strictly binary
tree whose leaves are labelled with the distinct elements in s in order from left
to right and whose internal nodes store binary strings. The binary string at
the root contains n bits and each is set to 0 or 1 depending on whether the
corresponding character of s is the label of a leaf in T ’s left or right subtree.
For each internal node v of T , the subtree Tv rooted at v is itself a wavelet
tree for the subsequence of s consisting of the occurrences of its leaves’ labels.
For example, if s = a, b, r, a, c, a, d, a, b, r, a and the leaves in T ’s left subtree are
labelled a, b and c, then the root stores 00100010010, the left subtree is a wavelet
tree for abacaaba and the right subtree is a wavelet tree for rdr. The important
properties of the wavelet tree for our purposes are summarized in the following
lemma.
Theorem 1 (Grossi et al. [9]) The wavelet tree T for a list of n elements
on alphabet σ requires n log σ(1 + o(1)) bits of space, and can be constructed in
O(n log σ) time.
To see why the space bound is true, consider that the binary strings’ total
length is the sum over the distinct elements of their frequencies times their
depths, which is O(n log σ) bits. The construction time bound is easy to see
from the recursive description of the wavelet tree given above.
We note as an aside that, while investigating data structures that support
rank and select queries, Mäkinen and Navarro [13] pointed out a connection be-
tween wavelet trees and a data structure due to Chazelle [4] for two-dimensional
range searching on sets of points.
2 0 3 1 4 6 7 9 8 5 k=2
0 0 1 0 1 0 0 1 1 0 `=2
r=5
k=2
2 0 1 3 4 6 7 5 9 8 `=2
1 0 0 0 1 0 1 0 1 0 r=3
k=1
0 1 2 3 4 6 5 7 8 9 `=1
0 1 1 0 r=1
0 1 5 6
Fig. 1. A wavelet tree T (left) for s = 6, 2, 0, 7, 9, 3, 1, 8, 5, 4, and the values (right) the
variables k, ` and r take on as we search for the 5th smallest element in s[3..9]. The
dashed boxes in T show the ranges from which we recursively select.
start at the root of the tree and use a range counting query to find the numbers
of 0s and 1s in the same range of the binary array stored there. If there are more
than k copies of 0 in the range, then we recurse on the left subtree; otherwise,
we subtract the number of 0s from k and recurse on the right subtree. Since we
use a single range counting query at each node as we descend, we use a total of
O(log σ 0 ) time.
Theorem 1. For any constants d and > 0, there exists a data structure of
size O N 1+ log σ 0 bits that answers d-dimensional range quantile queries on A
in O(log σ 0 ) time.
Acknowledgements
Our thanks go to the three anonymous reveiwers whose helpful comments ma-
terially improved the paper, and to Meg Gagie for righting our grammar.
References
1. : Space efficient multi-dimensional range reporting. In: Proceedings of the 15th
Conference on Computing and Combinatorics. (2009) 215–224
2. Blum, M., Floyd, R.W., Pratt, V.R., Rivest, R.L., Tarjan, R.E.: Time bounds for
selection. Journal of Computer and System Sciences 7 (1973) 448–461
3. Bose, P., Kranakis, E., Morin, P., Tang, Y.: Approximate range mode and range
median queries. In: Proceedings of the 22nd Symposium on Theoretical Aspects of
Computer Science. (2005) 377–388
4. Chazelle, B.: A functional approach to data structures and its use in multidimen-
sional searching. SIAM Journal on Computing 17 (1988) 427–462
5. Clark, D.: Compact PAT trees. PhD thesis, Waterloo University, Canada (1996)
6. Ferragina, P., Giancarlo, R., Manzini, G.: The myriad virtues of wavelet trees.
In: Proceedings of the 33rd International Colloquium on Automata, Languages and
Programming. (2006) 560–571
7. Fischer, J.: Efficient Data Structures for String Algorithms. PhD thesis, LMU,
München (2007)
8. Gfeller, B., Sanders, P.: Towards optimal range medians. arXiv:0901.1761 (2009)
9. Grossi, R., Gupta, A., Vitter, J.S.: High-order entropy-compressed text indexes.
In: Proceedings of the 14th Symposium on Discrete Algorithms. (2003) 841–850
10. Har-Peled, S., Muthukrishnan, S.: Range medians. In: Proceedings of the 16th
European Symposium on Algorithms. (2008) 503–514
11. Jacobson, G.: Space-efficient static trees and graphs. In: Proceedings of the 30th
Symposium on Foundations of Computer Science. (1989) 549–554
12. Krizanc, D., Morin, P., Smid, M.H.M.: Range mode and range median queries on
lists and trees. Nordic Journal of Computing 12 (2005) 1–17
13. Mäkinen, V., Navarro, G.: Rank and select revisited and extended. Theoretical
Computer Science 387 (2007) 332–347
14. Muthukrishnan, S.: Efficient algorithms for document retrieval problems. In:
Proceedings of the 13th Annual ACM-SIAM Symposium on Discrete Algorithms
(SODA). (2002) 657–666
15. Navarro, G., Mäkinen, V.: Compressed full text indexes. ACM Computing Surveys
39 (2007) Article 2
16. Petersen, H.: Improved bounds for range mode and range median queries. In:
Proceedings of the 34th Conference on Current Trends in Theory and Practice of
Computer Science. (2008) 418–423
17. Petersen, H., Grabowski, S.: Range mode and range median queries in constant
time and sub-quadratic space. Information Processing Letters 109 (2009) 225–228
18. Raman, R., Raman, V., Rao, S.S.: Succinct indexable dictionaries with applications
to encoding k-ary trees and multisets. In: Proceedings of the 13th Annual ACM-
SIAM Symposium on Discrete Algorithms (SODA). (2002) 233–242
19. Välimäki, N., Mäkinen, V.: Space-efficient algorithms for document retrieval. In:
Proceedings of the 18th Annual Symposium on Combinatorial Pattern Matching
(CPM). (2007) 205–215