Optimal Preprocessing For Answering Online Product Queries
Optimal Preprocessing For Answering Online Product Queries
Department of Mathematics
School of Mathematical Sciences
Tel Aviv University
Tel Aviv, Israel 69978
Baruch Schieber ‡
ABSTRACT
We examine the amount of preprocessing needed for answering certain on-lin e
queries as fast as possible. We start with the following basic problem. Suppose we are
given a semigroup (S , °). Let s 1, . . . , sn be elements of S . We want to answer on-line
queries of the form, "What is the product si °si +1° . . . °s j −1°s j ?" for any given
1 ≤ i ≤ j ≤ n . We show that a preprocessing of Θ(n λ(k ,n )) time and space is both
necessary and sufficient to answer each such query in at most k steps, for any fixed k .
The function λ(k ,.) is the inverse of a certain function at the Qk /2 P−th level of the prim-
itive recursive hierarchy. In case linear preprocessing is desired, we show that one can
answer each such query in at most O (α(n )) steps and that this is best possible. The
function α(n ) is the inverse Ackermann function.
We also consider the following extended problem. Let T be a tree with an element
of S associated with each of its vertices. We want to answer on-line queries of the form,
"What is the product of the elements associated with the vertices along the path from u
to v ?" for any pair of vertices u and v in T . We derive results, which are similar t o
the above, for the preprocessing needed for answering such queries.
All our sequential preprocessing algorithms can be parallelized efficiently to giv e
optimal parallel algorithms which run in O (logn ) time on a CREW PRAM. These
parallel algorithms are optimal in both running time and total number of operations.
hhhhhhhhhhhhhhhh
† The research of this author was supported in part by Allon Fellowship, Bat Sheva de Rothschild Foundation and the Fund for
Basic Research administered by the Israel Academy of Sciences.
‡ The research of this author was supported by the Applied Mathematical Sciences subprogram of the Office of Energy Research,
U.S. Department of Energy under contract number DE-AC02-76ER03077.
1. INTRODUCTION
We examine the amount of preprocessing needed for answering certain on-line queries as fast as pos-
sible. Suppose we are given a semigroup (S , °). We consider the following queries.
The Linear Product Query. Let s 1, . . . , sn be elements of S . We want to answer on-line queries of the
form, "What is the product si °si +1° . . . °s j −1°s j ?" for any given 1 ≤ i ≤ j ≤ n .
The Tree Product Query. Let T be an unrooted tree with an element of S associated with each of its ver-
tices. We want to answer on-line queries of the form, "What is the product of the elements associated with
the vertices along the path from u to v ?" for any pair of vertices u and v in T .
We present very efficient preprocessing algorithms for the above queries and show that under reason -
able assumptions these algorithms are best possible. Our assumptions are as follows. (1) Given any two
elements a and b in the semigroup S we can compute a °b in constant time. (2) The only available opera-
We say that we answer a query in k steps if we have to multiply k available precomputed semigroup ele-
The Linear Product Query. We show that in order to answer each Linear Product query in at most k steps,
for any fixed k , a preprocessing of Θ(n λ(k ,n )) time and space is both necessary and sufficient. The func-
tion λ(k ,.) is the inverse of a certain function at the Qk /2 P-th level of the primitive recursive hierarchy. We
also present a linear preprocessing algorithm that enables us to answer each query in at most O (α(n ))
steps, where α(n ) is the inverse Ackermann function (cf. [Ac-28]). It is further shown that no linear
The Tree Product Query. We show that in order to answer each Tree Product query in at most 2k steps, for
any fixed k , a preprocessing of O (n λ(k ,n )) time and space is sufficient, where n is the number of vertices.
(From the previous result we have that a preprocessing of Ω(n λ(2k ,n )) time and space is necessary for
answering Tree Product queries in certain trees.) Here also the best linear preprocessing algorithm enables
All our sequential preprocessing algorithms can be parallelized efficiently. The models of parallel
computation are the well-known concurrent-read exclusive-write (CREW) parallel random access machine
-2-
(PRAM) and concurrent-read concurrent-write (CREW) PRAM. (See, e.g., [Vi-83].) The resulting parallel
preprocessing algorithms for answering Linear Product queries in at most k steps and for answering Tree
Product queries in at most 2k steps run in O (logn ) time using n λ(k ,n )/logn processors on a CREW
PRAM. These algorithms are the fastest algorithms achievable on a CREW PRAM. This is, since Ω(logn )
is a lower bound on the running time of any parallel preprocessing algorithm for answering product queries
in o (logn ) steps on a CREW PRAM. This lower bound is easily derived from the lower bound of [CD-82].
For the case where the semigroup operation is the maximum operation (or similar) we can achieve a paral-
lel preprocessing algorithm for the Linear Product problem that runs in O (loglogn ) time using
n λ(k ,n )/loglogn processors on a CRCW PRAM. This algorithm is the fastest algorithm achievable with a
linear number of processors on a CRCW PRAM, as proved in [Va-75]. Notice that all these parallel algo-
rithms are also optimal in their total number of operations (i.e., the product of their running time and
number of processors). This is, since their total number of operations is equal to the time complexity of the
The parallel preprocessing algorithms use the same ideas as the sequential algorithms together with the
parallel preprocessing algorithm for answering lowest common ancestor (LCA) queries of [ScV-87] and th e
parallel Accelerated Centroid Decomposition of [CV-86]. Since the parallelization is mostly of technical
We establish a trade-off between the time needed to answer a query and the preprocessing time. This
trade-off is very strict. For example, if we want each Linear Product query to be answered in at most two
steps we have to preprocess the input Θ(n logn ) time, but if we allow four computation steps per each
query we may preprocess the input only O (n log* n ) time. On the other hand, if we want the preprocessing
to be in linear time, we must allow Θ(α(n )) computation steps for each query. Similar trade-off appears in
[DDPW-83] as the trade-off between the the depth and the size of superconcentrators and in [CFL-83a],
[CFL-83b] as the trade-off between the depth and the size of certain unbounded fanin circuits.
[Ta-79] considers an off-line version of the Tree Product problem. He gives an of f −line sequential
algorithm based on path compression for commutative semigroups which runs in time
O ((m +n )αT (m +n ,n )), where m is the number of off-line queries, n is the number of vertices and αT (.,.)
is a function closely related to the inverse Ackermann function. Our algorithm, which is completely
-3-
different, has several advantages relative to Tarjan’s algorithm: (1) It is on-line. (2) It works for any associ -
ative semigroup. (3) We do not consider the amortized complexity. That is, the total complexity is deter-
mined by the preprocessing time and the number of queries multiplied by the maximum answering time o f
a query. (4) We give an algorithm scheme which enables us to design a preprocessing algorithm for any
desired answering time. Note that we can design an algorithm which achieves the same time complexity as
[Ta-79]. For this we preprocess the input O (m +n ) time and then answer each of the m queries in at most
O (αT (m +n ,n )) steps.
Our algorithms use the divide-and-conquer technique. Specifically, we decompose the size n problem
into subproblems of smaller size and show that after investing linear time and space we may consider eac h
subproblem independently. This framework gives us, in fact, running time which converges to O (n α(n )).
To get some intuition of the queries we give an example. Let the semigroup S be the set of real
numbers and let the semigroup operation be the minimum operation. In this interpretation the Linear Pro-
duct problem gets the following meaning. Suppose we are given a vector of n real numbers a 1, . . . , an .
We want to find, as fast as possible, the minimum number in any sub-vector. That is, to find
MIN { ai ,ai +1, . . . , a j −1,a j }, for any given 1 ≤ i ≤ j ≤ n . Similarly, in the Tree Product problem, we are
given a tree with a real number associated with each of its vertices. We want to find, as fast as possible,
The defined queries have many applications. For example, consider a communication network, where
the nodes are connected using a spanning tree. Assume that each link of the network has a specified capa-
city. Each time we want to communicate from one point to another we have to know the maximum mes-
sage size we can send. This size equals the minimum capacity along the path connecting the two points
and so it can be found by answering a suitable Tree Product query in the given tree.
[Ta-79] gives three applications for his off-line algorithm. (1) Finding maximum flow values in a
multiterminal network. (2) Verifying minimum spanning trees. That is, given a graph and a spanning tree,
verify whether the spanning tree is minimal. (3) Updating a minimum spanning tree after increasing the cost
of one of its edges. Naturally, we can use our algorithm for these applications also.
The rest of the paper is organized as follows. In Section 2 we give upper and lower bounds for th e
Linear Product problem. In Section 3 we present preprocessing algorithms for the Tree Product problem.
Suppose we are given a semigroup (S , °). Let s 1, . . . , sn be elements of S . In this section we exam-
ine the amount of sequential preprocessing needed for answering queries of the form, "What is the produc t
At first we describe preprocessing algorithms for answering Linear Product queries in at most one and
two steps. Afterwards, we describe a preprocessing algorithm for any k > 2 steps. This algorithm uses the
The preprocessing algorithm for one step. Observe that the best preprocessing algorithm for one step is the
naive algorithm which precomputes all the necessary products in advance. Clearly, this takes O (n 2) time
and space.
The preprocessing algorithm for two steps. Let l be Qn /2 P. We precompute all the products si ° . . . °sl , for
1 ≤ i < l , and sl +1° . . . °s j , for l +1 < j ≤ n . This can be done in linear time and space. Suppose we are
given the query si °si +1° . . . °s j −1°s j . If j = l or i = l +1 then we can answer the query in one step using the
precomputed products. If i ≤ l and j > l then we can answer the query in two steps by computing the pro-
duct of the precomputed si ° . . . °sl and sl +1° . . . °s j . Thus, the rest of the preprocessing should be aimed
for answering queries of the form si °si +1° . . . °s j −1°s j , where either 1 ≤ i ≤ j < l , or l +1 < i ≤ j ≤ n . This is
done recursively using the same method. The total preprocessing time and space is given by the
recurrence: T 2(n ) ≤ 2T 2(n /2) + n , whose solution is n logn . Hence, the preprocessing takes O (n logn ) time
and space.
Notice that when we answer a query we have to be able to retrieve each of the precomputed products
whose multiplication gives the result with no overhead. To simplify the retrieval we modify the preprocess -
ing algorithm (without changing its complexity bounds). We start the algorithm with l = 2 Qlogn P
instead of
l = Qn /2 P and continue in the same manner when we recur. One can easily see that after this modification
we can retrieve the precomputed products required for answering a query si °si +1° . . . °s j −1°s j by performing
logn -bit operations on the indices i and j . (In case these operations are not part of our machine’s reper -
toire, look-up tables for each missing operation are prepared in linear time and linear space as part of the
preprocessing. These tables will be used to perform the missing operations in constant time.)
-5-
To describe our algorithm for k > 2 steps, we shall need to define some very rapidly growing and very
Similarly, we define:
B (0,j ) = j 2, for j ≥ 1
B (i ,0) = 2, for i ≥ 1
B (i ,j ) = B (i −1,B (i ,j −1)), for i ,j ≥ 1.
λ(4,x ) = log* x . Observe that λ(i ,x ) = MIN { j e λ(j )(i −2,x ) ≤ 1 }, for i ≥ 2, where λ(1)(i ,x ) = λ(i ,x ) and
Remark: The bound of Θ(n λ(k ,n )) on the time and space of the preprocessing algorithm for k steps is
valid for k > 1. Notice that in order to answer Linear Product queries in one step we have to invest Θ(n 2)
The preprocessing algorithm for k > 2 steps. The preprocessing algorithm for k > 2 steps runs in
O (n λ(k ,n )) time and space. Let l be λ(k −2,n ) and let m be n /l . (To simplify the presentation we assume
that m is an integer.) For each x = 1,...,m we precompute all the products si ° . . . °slx , for l (x −1) < i < lx .
For each x = 1,...,m −1 we precompute all the products slx +1° . . . °s j , for lx +1 < j < l (x +1). This can be
done in time and space which are proportional to 2n . Let sdx be sl (x −1)+1° . . . °slx , for x = 1,...,m . We
preprocess the m elements sd1, . . . , sdm , using the preprocessing algorithm for k −2 steps. This is done in
time and space which are proportional to (k −2)m λ(k −2,m )) ≤ (k −2)n . (Note that this holds also for k =3.)
Suppose we are given the query si °si +1° . . . °s j −1°s j (for i < j ). Let x be Ri /l H and let y be Q j /l P. If x = y
then we can compute the product in at most two steps as in the algorithm for two steps. If x < y then we
(i) We compute sdx +1° . . . °sdy in k −2 steps. (ii) We multiply the products: si ° . . . °slx , sdx +1° . . . °sdy and
sly +1° . . . °s j . Thus, the rest of the preprocessing should be aimed for answering queries of the form
si °si +1° . . . °s j −1°s j , where l (x −1) < i < j < lx , for some 1 ≤ x ≤ m . This is done recursively using the same
-6-
method. The total preprocessing time and space is given by the recurrence:
n
Tk (n ) ≤ hhhhhhhhh Tk (λ(k −2,n )) + kn . It is not difficult to verify that the solution of this recurrence is
λ(k −2,n )
kn λ(k ,n ). Since k is constant we have that the total preprocessing time and space is O (n λ(k ,n )).
We show how to retrieve the precomputed products required for answering a query si °si +1° . . . °s j −1°s j . We
start by retrieving the first and last required products. Again, let l = λ(k −2,n ) and m = n /l . We distinguish
between two cases. (Case A) x = Ri /l H ≤ y = Q j /l P. In this case the first product is si ° . . . °slx and the last is
sly +1° . . . °s j . (Case B) Not Case A. That is, l (x −1) < i < j <lx , for some 1 ≤ x ≤ m . Or, in words, i and j
belong to the same block of size l . In this case we retrieve the required products using a look-up table .
Observe that these products are defined (within their block) by i mod l and j mod l . Hence, the needed look-up
table is of size l xl . This table can be computed in linear time and space during the preprocessing stage .
The rest of the required products are retrieved recursively in the same method. Notice that we have Rk /2 H
recursion levels. Hence, the retrieval is done with no time or space overhead.
We conclude the description of the upper bounds by presenting the best linear preprocessing algo-
rithm. This linear time and space preprocessing algorithm enables us to answer Linear Product queries in a t
We start by describing a simple linear preprocessing algorithm (which is not the best). We use a bal-
anced binary tree whose leaves are s 1, . . . , sn . For each internal vertex we compute the product of its des -
cendent leaves. Clearly, this can be done in linear time and space. It can be easily verified that using these
precomputed products we can compute si °si +1° . . . °s j −1°s j for any given 1 ≤ i ≤ j ≤ n in at most 2 Rlogn H
steps. This is done, simply, by "climbing" from the leaf (which represents) si and the leaf (which
We combine this simple preprocessing algorithm with the preprocessing algorithm described above to
get the best linear preprocessing algorithm. Again, we use the divide-and-conquer technique. Let l = 2α2(n ).
We partition s 1, . . . , sn into m = n /l blocks of size l each. We preprocess each block using the above sim-
ple algorithm. This takes linear time and space. It enables us to answer intra-block queries in at mos t
2 Rlogl H = O (logα(n )) steps. We aim to preprocess the input so that we will be able to answer inter-block
queries in O (α(n )) steps. For this, we define sd1, . . . , sdm as before (that is, sdx = sl (x −1)+1° . . . °slx ) and
preprocess these m elements using the preprocessing algorithm for 2α(n ) steps described above. In addition
-7-
we precompute for each x = 1,...,m all the products si ° . . . °slx , for l (x −1) < i < lx , and for each
x = 1,...,m −1 all the products slx +1° . . . °s j , for lx +1 < j < l (x +1). This enables us to answer inter-block
queries in at most 2α(n )+2 steps. Observe that the preprocessing takes
2α(n )m λ(2α(n ),m ) = 2α(n )(n /2α2(n ))α(n ) = n time and space. (Remark: The preprocessing algorithm for
k steps requires kn λ(k ,n ) time and space. Since in our case k = 2α(n )+2 is not a constant we had to take
it into consideration.) This implies that we can answer queries in at most O (α(n )) steps after linear prepro-
cessing. Note that this is best possible since, by our lower bound proven below, Ω(α(n )) steps is the best
number of steps achievable even in case we allow a preprocessing of n α(n ) time and space.
We show that a preprocessing of Ω(n λ(k ,n )) time and space is needed for answering Linear Product
queries in at most k steps. We have the following assumptions. (1) Given two elements a and b in the
semigroup S we can compute a °b in constant time. (2) The only available operation on the semigroup ele-
ments is the semigroup operation. (Remark: Assumption (2) is crucial for the lower bound. To see this, note
that if the given semigroup is a group we can perform n prefix computations on the input in linear time and
then answer Linear Product queries in constant time using the inverse operation of the group.)
For proving the lower bound we prove a stronger result, which is of independent interest. For two
integers i ≤ j , we denote the set of integers { i ,i +1,..., j −1, j } by [ i ,j ]. (We shall refer to it as the integer
interval [ i ,j ].) A set IP of subsets of [1,n ] is said to be a k −covering set of [ 1,n ] if each integer interval
contained in [1,n ] (i.e., each integer interval [i ,j ], for 1 ≤ i ≤ j ≤ n ) is the union of at most k subsets in IP .
We want to find a lower bound for the minimum possible cardinality of a k −covering set of [ 1,n ], denoted
Pk (n ). We prove that Pk (n ) = Ω(n λ(k ,n )), for k ≥ 2. (Note that the size of any 1−covering set is Ω(n 2).)
We claim that Pk (n ) is a lower bound on the preprocessing needed for answering Linear Product queries in
at most k steps. To prove this claim associate each product precomputed in the preprocessing algorithm
with the set of indices of the elements consisting it. Notice that this gives a k covering set as each integer
interval [i ,j ] must be the union of at most k of these sets (corresponding to the precomputed products
whose product is si °si +1° . . . °s j −1°s j ). Moreover, our lower bound is stronger than the lower bound for the
Linear Product problem in two aspects: (i) We consider coverings by subsets , while for the Linear Product
problem we may consider only coverings by integer intervals . (ii) We consider any coverings, while for
-8-
the Linear Product problem we may consider only exact coverings. That is, only coverings for which each
integer subinterval is the union of at most k pairwise disjoint subsets. This stronger result, implies that
we can not improve our algorithm even if the semigroup S is commutative and/or consists only of idempo-
tent elements. (That is, a °a = a , for every element a ∈ S .) An example of a semigroup which is both com-
mutative and consists only of idempotent elements is any set of numbers with the operation maximum or
minimum.
The lower bound is proven inductively. We start by proving the lower bound for k = 2 and then prove
The lower bound for k = 2. We show that P 2(n ) satisfies the recurrence
P 2(n ) ≥ P 2( Rn /2 H−1) + P 2( Qn /2 P) + Qn /2 P. The lower bound Ω(n logn ) follows. Let l be Qn /2 P+1. Partition
[ 1,n ] into two subintervals I 1 = [ 1,l −1 ] and I 2 = [ l +1,n ]. Clearly, a 2−covering set of [ 1,n ] must contain
2−covering sets of I 1 and I 2. Moreover, these 2−covering sets are disjoint as the 2−covering set of I 1 (resp.
I 2) consists only of subsets of I 1 (resp. I 2). We show that any 2−covering set of [ 1,n ] must contain Qn /2 P
additional subsets. Each one of these additional subsets contains either the element l or elements from I 1
and from I 2. Let IP be a 2−covering set of [ 1,n ]. We distinguish between two cases:
(Case A) Each element x in I 1 belongs to a subset Q ∈ IP such that: (i) x is the minimal element in Q . (ii)
Q contains elements which are ≥ l . Clearly, in this case we have Qn /2 P different subsets containing ele-
(Case B) Not Case A. That is, there exists an element x in I 1 such that each subset Q ∈ IP which contains
x as its minimal element does not contain elements which are ≥ l . Consider the intervals [ x ,i ], for i ≥ l .
Each such interval must be the union of at most two subsets in IP . Since no subset Q ∈ IP satisfies condi-
tions (i) and (ii) of Case A, each interval [x ,i ] must be the union of exactly two subsets in IP . One subset,
say Q 1, must contain x as its minimal element, and the other subset, say Q 2, must contain l . Also, note
that the maximum element in Q 2 must be i . Hence, we have Rn /2 H different subsets containing l .
The lower bound for k > 2. We show that Pk (n ) satisfies the recurrence
n
Pk (n ) ≥ hhhhhhhhh Pk (λ(k −2,n )−1) + Ω(n ).
λ(k −2,n )
The lower bound Ω(n λ(k ,n )) follows. Let l be λ(k −2,n ) and let m be n /l . (Again, we assume that m is
an integer.) Partition [ 1,n ] into m subintervals: I j = [ l (j −1)+1,lj −1 ], for j =1,...,m . Clearly, a k −covering
-9-
set of [ 1,n ] must contain k −covering sets of each I j . Moreover, these k -covering sets are disjoint as the
k −covering set of I j consists only of subsets of I j . We show that any k −covering set of [ 1,n ] must contain
Let IP be a k −covering set of [ 1,n ]. A subset Q ∈ IP is global if it is not contained in any subinter-
val. An element x ∈ I j is global if it is an extremal (minimal or maximal) element in some global subset.
Finally, a subinterval is global if all of its elements are global. We show that IP contains Ω(n ) global sub-
(Case A) There are Qm /2 P global subintervals. Each global subinterval contains l −1 global elements. Note
that each global element corresponds to at least one global subset, and that each global subset may
correspond to at most two global elements. Hence, there must be at least 1⁄4 m (l −1) = Ω(n ) global subsets.
(Case B) Not Case A. That is, there are Qm /2 P subintervals which are not global. Note that each nonglobal
subinterval contains at least one nonglobal element. That is, an element which is not extremal in any global
subset. Let x ∈ I j 1 and y ∈ I j 2 be nonglobal elements, where j 1 < j 2. Consider the interval [ x ,y ]. It must
be the union of at most k subsets of IP . Since no global subset contains x (resp. y ) as its minimal (resp.
maximal) element, at least one of these subsets must be contained in I j 1 (resp. I j 2). Hence the union of the
rest of the subsets contains the interval [ lj 1,l (j 2−1) ]. This implies that if we omit from the subsets in IP all
the elements which are not of the form lx , for some x = 1,...,m −1 such that Ix is nonglobal, we are left with
a (k −2)−covering set for the set { lx e Ix is nonglobal}. By the inductive hypothesis this set must contain
Ω((m /2) λ(k −2,m /2)) = Ω(n ) nonempty subsets. Note that each such subset corresponds to at least one glo-
Let T be an unrooted tree with an element of S associated with each of its vertices. We want to
answer on-line queries of the form, "What is the product of the elements associated with the vertices alon g
the path from u to v ?" for any pair of vertices u and v in T . (We denote such a query
Tree −Product (u ,v ).) In this section we present an O (n λ(k ,n )) time and space preprocessing algorithm fo r
answering Tree Product queries in at most 2k steps, where n is the number of vertices and k ≥ 2 is a fixed
parameter. We also show a linear time and linear space preprocessing algorithm for answering Tree Produc t
We start by showing that it is sufficient to preprocess T only in order to answer queries of the form
Tree −Product (u ,v ), where either u is the ancestor of v or vice versa, for an arbitrarily chosen root r of T .
Suppose we are given a query Tree −Product (u ,v ) such that neither u is an ancestor of v nor v is an
ancestor of u . We answer it in three stages. (1) We find the lowest common ancestor of u and v (denoted
LCA (u ,v )). (2) We compute Tree −Product (u ,LCA (u ,v )). (3) We compute Tree −Product (LCA (u ,v ),v ).
Clearly, Tree −Product (u ,v ) = Tree −Product (u ,LCA (u ,v ))°Tree −Product (LCA (u ,v ),v )). For computing
LCA (u ,v ) we preprocess T using the linear time and space preprocessing algorithm of [HT-84] or the
simplified preprocessing algorithm of [ScV-87]. These preprocessing algorithms enable us to answer querie s
of the form, "Which vertex is the lowest common ancestor (LCA) of u and v ?" for any pair of u and v in
T , in constant time. Below, we present a preprocessing algorithm for answering queries of the form
Tree −Product (u ,v ), where u is the ancestor of v in at most k steps. Our preprocessing algorithm takes
O (n λ(k ,n )) time and space. The preprocessing algorithm for answering queries of the form
Tree −Product (u ,v ), where v is the ancestor of u is similar. (Note that when the semigroup is not commu-
tative it is possible that Tree −Product (u ,v ) ≠ Tree −Product (v ,u ).) Combining both algorithms results in an
O (n λ(k ,n )) time and space preprocessing algorithm for answering a general Tree Product query in at most
2k steps.
Our preprocessing algorithm uses a divide-and-conquer technique as the preprocessing algorithm for
answering Linear Product queries. That is, we partition the tree into O(n /λ(k −2,n )) connected components
of size O (λ(k −2,n )) each and show that after investing linear time and space work we may consider each
connected component independently. We decompose the size n problem into problems of size λ(k −2,n ) in
four stages.
Stage 1. We binarize the tree T using a well-known transformation as follows. For each vertex v in T of
outdegree d > 2, where w 1, . . . , wd are the sons of v , we replace v with the new vertices v 1, . . . , vd −1.
We make vi the father of vi +1 and wi , for i = 1,...,d −2 and vd −1 the father of wd −1 and wd . Let B be the
resulting rooted binary tree. Note that the number of vertices in B is at most twice the number of vertices
in T . Finally, we associate the unit element of S with each new vertex. (Note that in case S lacks a unit
element we can simply add such an element to S .) Clearly, this can be done in linear time and space.
- 11 -
Stage 2. We partition B into O (n /λ(k −2,n )) connected components of size O (λ(k −2,n )) each by remov-
ing O (n /λ(k −2,n )) edges. (For k = 2 we partition B into exactly two components of size between n /3 and
2n /3 each.) The existence of such a partitioning is guaranteed by the separator theorem of [LT-79] for th e
family of trees with a maximum degree three. The partitioning can be done in linear time and space using
Let C be one of the resulting connected components of B . Note that C is also a rooted binary tree.
Stage 3.1. For each vertex x in C we compute Tree −Product (rC ,x ), where rC is the root of C . This can
be done in linear time and space using, e.g., Breadth First Search.
Stage 3.2. For each vertex x in C such that at least one son of x (in B ) does not belong to C and for each
ancestor y of x in C , we compute Tree −Product (y ,x ). The products are computed in constant time per
product by "climbing" from each such vertex x to the root of its component rC . Note that the total number
of such vertices x is O (n /λ(k −2,n )), also, the number of ancestors of each such vertex in its component is
O (λ(k −2,n )). Hence, the total number of products computed in this stage is O (n ). Thus, this stage takes
Let v be a vertex in T . Denote by C (v ) the connected component which contains v and by FB (v ) the
father of v in B . We define a new rooted tree Bd as follows. The vertices of Bd correspond to the con-
nected components of B . The root of Bd is C (r ), where r is the root of B . For every edge (v ,FB (v )) in B
such that C (v ) ≠ C (FB (v )) we make C (FB (v )) the father of C (v ). We associate an element from S with
each vertex of Bd as follows. (i) The unit element of S is associated with the root of Bd . (ii) To each other
vertex C in Bd we associate Tree −Product (rD ,FB (rC )), where rD is the root of the connected component
Stage 4. We perform the preprocessing algorithm for answering Tree Product queries in at most k −2 steps
on Bd . (This is done only if k > 2.) This takes O (λ(k −2,n )(n /λ(k −2,n ))) = O (n ) time and space.
The validity of the decomposition algorithm. We show how to answer a query Tree −Product (u ,v ) in T ,
such that u and v belong to different components in at most k steps. Recall that u is an ancestor of v . If
Tree −Product (u ,FB (rC (v ))) and Tree −Product (rC (v ),v ). Suppose k > 2. Let x be the last vertex of C (u )
which appears along the path from u to v in B and let C be the grandson of C (u ) which appears along
the path from C (u ) to C (v ) in Bd . Tree −Product (u ,v ) is the product of (i) Tree −Product (u ,x ) in B ,
- 12 -
precomputed in Stage 3.2 (ii) Tree −Product (C ,C (v )) in Bd . Using the preprocessing of Stage 4, this pro-
duct can be computed in at most k −2 steps. (iii) Tree −Product (rC (v ),v ) in B , precomputed in Stage 3.1.
In order to be able to retrieve these precomputed products within the stated complexity bounds we must b e
able to find x and C in constant time. Using the ideas of the algorithms of [HT-84] and [ScV-87] we can
preprocess Bd in linear time and space such that we would be able to find the ancestor of w whose distance
from the root is d in constant time, for any vertex w in Bd and any distance d . This enables us to find C
whose distance from C (v ) is given in constant time. Given C , we can also find x = FB (rD ), where rD is
All the above discussion applies to k ≥ 2, i.e., to answering time of 2k ≥ 4 per query. It is worth not-
ing that using similar ideas we can design an O (n logn ) time and space preprocessing algorithm for answer -
ing Tree Product queries in at most two steps per query and an O (n loglogn ) time and space preprocessing
algorithm for answering Tree Product queries in at most three steps per query.
We conclude this section by describing a linear time and space preprocessing algorithm for answering
As in the description of the best linear preprocessing algorithm for answering Linear Product queries,
we start by describing a preliminary linear preprocessing algorithm which is not the best.
The preliminary linear preprocessing algorithm. Suppose we are given a tree T , rooted at r . We present a
linear time and space preprocessing algorithm which enables us to answer queries of the form
Tree −Product (u ,v ), for any pair of vertices u and v , such that u is an ancestor of v , in O (logn ) steps.
Following [AHU-74], [Ta-75] and [HT-84], we partition T into a collection of disjoint paths, as follows.
For each vertex v in T , let SIZE (v ) be the number of its descendants (including itself). Define an edge
(v ,u ) (where u is the father of v ) to be heavy if 2SIZE (v ) ≥ SIZE (u ) and light otherwise. Since the size of
a vertex is one greater than the sum of the sizes of its children, at most one heavy edge exits from each
vertex. Thus, the heavy edges partition the vertices of T into a collection of heavy paths . (A vertex with
no entering or exiting heavy edge is a single-vertex heavy path.) Define the head of a heavy path to be the
Let u and v be two vertices in T , such that u is an ancestor of v . One can easily verify the following two
facts. (1) The vertices along the (unique) path in T between u and v are partitioned by the heavy edges
- 13 -
into at most Rlogn H heavy sub-paths. (2) Each such heavy sub-path, except possibly the first, starts with the
We are ready now to describe the preliminary preprocessing algorithm. It has three stages each taking
linear time and linear space. (1) We partition the input tree T into heavy paths as described above. (2) We
preprocess each heavy path using the simple linear preprocessing algorithm for answering Linear Product
queries described in Section 2. (3) For each head u of a heavy path and for each vertex v in its heavy path
Suppose we are given a query Tree −Product (u ,v ) we show how to answer it in O (logn ) steps. We
(Possibility A) u and v are in the same heavy path. In this case we can answer the query using the prepro-
(Possibility B) u and v are not in the same heavy path. Recall that the path from u to v is partitioned into
at most Rlogn H heavy sub-paths and that each such heavy sub-path, except possibly the first, starts with the
head of its corresponding heavy path. Thus, to compute Tree −Product (u ,v ) we multiply (i) the O (logn )
precomputed products, computed in Stage 2, which give the product of the vertices along the first sub-path .
(ii) the O (logn ) precomputed products, computed in Stage 3, which give the product of the vertices along
the rest of the sub-paths. (One precomputed product per each such sub-path.)
The linear preprocessing algorithm for answering Tree Product queries has five stages. (1) We binarize
the input tree T . Let B be the resulting binary tree. (2) We decompose B into connected components of
size O (α2(n )) each. As in the decomposition algorithm, described in the start of this section, we compute
the following for each connected component C of B . (3.1) For each vertex x in C we compute
Tree −Product (rC ,x ), where rC is the root of C . (3.2) For each vertex x in C such that at least one son of
x (in B ) does not belong to C and for each ancestor y of x in C , we compute Tree −Product (y ,x ). We
define the tree Bd as in the decomposition algorithm above. Note that Bd has O (n /α2(n )) vertices. (4) We
preprocess Bd in O (n ) time and space using our preprocessing algorithm for answering Tree Product querie s
in at most O (α(n )) steps. As shown before, this preprocessing together with the computation done in
Stage 3 enable us to answer inter-component queries in at most O (α(n )) steps. (4) We preprocess each
component in linear time and linear space using the preliminary linear preprocessing algorithm. This
preprocessing enables us to answer intra-component queries in at most O (logα(n )) steps. Thus, we can
- 14 -
4. OPEN PROBLEMS
We presented efficient preprocessing algorithms for answering product queries. Under reasonable
assumptions these algorithms are optimal. Note that they apply only to static input. The most important
open problem is what can be done when the input is not static. In the Linear Product case, we see three
kinds of dynamic operations: (i) changing the value of an element. (ii) adding a new element (iii) deleting
an element. The simple linear preprocessing algorithm, described in Section 2, can be easily adapted to the
dynamic case. It gives a linear preprocessing algorithm which enables us to perform each of the above thre e
dynamic operations and also to answer Linear Product queries in O (logn ) time. We do not know whether
this is best possible and we are also unable to prove any nontrivial lower bound or trade off between
preprocessing time and processing time. For the Tree Product case, the possible dynamic operations are: (i)
changing the value of an element (ii) linking two trees by adding a new edge. (iii) cutting a tree by deleting
an edge. Using the ideas of [HT-84] and [ST-83] we can design a linear preprocessing algorithm for this
case which will also enable us to perform each dynamic operation and to answer Tree Product queries in
Another direction for future work is to find more applications where the described preprocessing algo -
Acknowledgement. We are grateful to Zvi Galil, Yael Maon and Uzi Vishkin for stimulating discussions
REFERENCES
[AHU-74] A.V. Aho, J.E. Hopcroft and J.D. Ullman, "On finding lowest common ancestors in trees",
SIAM J. Comput., 5 (1976), pp. 115-132.
[Ac-28] W. Ackermann,"Zum Hilbertschen Aufbauder reellen Zahlen", Math. Ann. 99 (1928), pp.
118-133.
[CD-82] S. Cook and C. Dwork., "Bounds on the time for parallel RAMs to compute simple func-
tions", Proc. of the 14th ACM Symp. on Theory of Computing, 1982, pp. 231-233.
[CFL-83a] A.K. Chandra, S. Fortune and R.J. Lipton, "Unbounded fanin circuits and associative func-
tions", Proc. of the 15th ACM Symp. on Theory of Computing, 1983, pp. 52-60.
[CFL-83b] A.K. Chandra, S. Fortune and R.J. Lipton, "Lower bounds for constant depth circuits for
prefix problems", Proc. of the 10th Int. Colloquium on Automata Languages and
- 15 -
Programming, Lecture Notes in Computer Science, Springer-Verlag (1983), 154, pp. 109-
117.
[CV-86] R. Cole and U. Vishkin, "The accelerated centroid decomposition technique for optimal
parallel tree evaluation in logarithmic time", TR 52/86, The Moise and Frida Eskenasy Insti-
tute of Computer Science, Tel Aviv University (1986).
[DDPW-83] D. Dolev, C. Dwork, N. Pippenger and A. Wigderson, "Superconcentrators, generalizers an d
generalized connectors with limited depth", Proc. of the 15th ACM Symp. on Theory of Com-
puting, 1983, pp. 42-51.
[Fr-85] G.N. Frederickson, "Data structures for on-line updating of minimum spanning trees, with
applications", SIAM J. Comput., 14 (1985), pp. 781-798.
[HT-84] D. Harel and R.E. Tarjan, "Fast Algorithms for finding nearest common ancestors", SIAM J.
Comput., 13 (1984), pp. 338-355.
[LT-79] R.J. Lipton and R.E. Tarjan, "A separator theorem for planar graphs", SIAM J. Appl. Math.,
3 (1979), pp. 177-189.
[ST-83] D. Sleator and R.E. Tarjan, "A data structure for dynamic trees", J. of Comput. System Sci.,
26 (1983), pp. 362-391.
[ScV-87] B. Schieber and U. Vishkin, "On finding lowest common ancestors: simplification and paral-
lelization", TR 63/87, The Moise and Frida Eskenasy Institute of Computer Science, Tel
Aviv University (1987).
[Ta-75] R.E. Tarjan, "Efficiency of a good but not linear set union algorithm", J. Assoc. Comput.
Mach., 22 (1975), pp. 215-225.
[Ta-79] R.E. Tarjan, "Applications of path compression on balanced trees", J. Assoc. Comput. Mach.,
26 (1979), pp. 690-715.
[Va-75] L.G. Valiant, "Parallelism in comparison problems", SIAM J. Comput., 4 (1975), pp. 348-
355.
[Vi-83] U. Vishkin, "Implementation of simultaneous memory address access in models that forbid
it", J. of Algorithms, 4 (1983), pp. 45-50.