Consider an ordinal tree T on n nodes, each of which is assigned a category from an alphabet [σ]={1,2,…,σ}. We preprocess the tree T in order to support categorical path counting queries, which ask for the number of distinct categories occurring on the path in T between two query nodes x and y. For this problem, we propose a linear-space data structure with query time O(nlglgσlgw), where w=Ω(lgn) is the word size in the word-RAM. As shown in our proof, from the assumption that matrix multiplication cannot be solved in time polynomially faster than cubic (with only combinatorial methods), our result is optimal, save for polylogarithmic speed-ups. For a trade-off parameter 1≤t≤n, we propose an O(n+n2t2)-word, O(tlglgσlgw) query time data structure. We also consider c-approximate categorical path counting queries, which must return an approximation to the number of distinct categories occurring on the query path, by counting each such category at least once and at most c times. We describe a linear-space data structure that supports 2-approximate categorical path counting queries in O(lgn/lglgn) time.Next, we generalize the categorical path counting queries to weighted trees. Here, a query specifies two nodes x,y and an orthogonal range Q. The answer to thus formed categorical path range counting query is the number of distinct categories occurring on the path from x to y, if only the nodes with weights falling inside Q are considered. We propose an O(nlglgn+(n/t)4)-word data structure with O(tlglgn) query time, or an O(n+(n/t)4)-word data structure with O(tlgϵn) query time. For an appropriate choice of the trade-off parameter t, this implies a linear-space data structure with O(n3/4lgϵn) query time. We then extend the approach to the trees weighted with vectors from [n]d, where d is a constant integer greater than or equal to 2. We present a data structure with O(nlgd−1+ϵn+(n/t)2d+2) words of space and O(tlgd−1n(lglgn)d−2) query time. For an O(n⋅polylogn)-space solution, one thus has O(n2d+12d+2⋅polylogn) query time.The inherent difficulty revealed by the lower bound we proved motivated us to consider data structures based on sketching. In unweighted trees, we propose a sketching data structure to solve the approximate categorical path counting problem which asks for a (1±ϵ)-approximation (i.e. within 1±ϵ of the true answer) of the number of distinct categories on the given path, with probability 1−δ, where 0<ϵ,δ<1 are constants. The data structure occupies O(n+ntlgn) words of space, for the query time of O(tlgn). For trees weighted with d-dimensional weight vectors (d≥1), we propose a data structure with O((n+ntlgn)lgdn) words of space and O(tlgd+1n) query time.All these problems generalize the corresponding categorical range counting problems in Euclidean space Rd+1, for respective d, by replacing one of the dimensions with a tree topology.
Read full abstract