10.07.2015 Views

Sequential and parallel algorithms for the NCA problem on pure ...

Sequential and parallel algorithms for the NCA problem on pure ...

Sequential and parallel algorithms for the NCA problem on pure ...

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

A. D. Palú et al. / Theoretical Computer Science 352 (2006) 108 – 135 109The <str<strong>on</strong>g>NCA</str<strong>on</strong>g> <str<strong>on</strong>g>problem</str<strong>on</strong>g> has been studied extensively. For <str<strong>on</strong>g>the</str<strong>on</strong>g> static case, <str<strong>on</strong>g>the</str<strong>on</strong>g> original work by Harel <str<strong>on</strong>g>and</str<strong>on</strong>g> Tarjan [15]provides a c<strong>on</strong>stant-time algorithm <str<strong>on</strong>g>for</str<strong>on</strong>g> per<str<strong>on</strong>g>for</str<strong>on</strong>g>ming <str<strong>on</strong>g>the</str<strong>on</strong>g> nca(x, y) operati<strong>on</strong> after a linear-time pre-processing of <str<strong>on</strong>g>the</str<strong>on</strong>g>tree. This result was later simplified <str<strong>on</strong>g>and</str<strong>on</strong>g> <str<strong>on</strong>g>parallel</str<strong>on</strong>g>ized by Schieber <str<strong>on</strong>g>and</str<strong>on</strong>g> Vishkin [22] <str<strong>on</strong>g>and</str<strong>on</strong>g> Gusfield [14]. Bender <str<strong>on</strong>g>and</str<strong>on</strong>g>Farach-Colt<strong>on</strong> [4] provided an effectively implementable algorithm which provides O(1) time executi<strong>on</strong> of nca(x, y)operati<strong>on</strong> with linear-time pre-processing of <str<strong>on</strong>g>the</str<strong>on</strong>g> tree. In all <str<strong>on</strong>g>the</str<strong>on</strong>g>se <str<strong>on</strong>g>algorithms</str<strong>on</strong>g>, complexity analysis is d<strong>on</strong>e assuming <str<strong>on</strong>g>the</str<strong>on</strong>g>RAM model.For <str<strong>on</strong>g>the</str<strong>on</strong>g> dynamic case, Tsakalidis [25] provides <str<strong>on</strong>g>algorithms</str<strong>on</strong>g> with O(lg h) worst-case time <str<strong>on</strong>g>for</str<strong>on</strong>g> <str<strong>on</strong>g>the</str<strong>on</strong>g> nca operati<strong>on</strong> <str<strong>on</strong>g>and</str<strong>on</strong>g>almost amortized O(1) time <str<strong>on</strong>g>for</str<strong>on</strong>g> add-leaf <str<strong>on</strong>g>and</str<strong>on</strong>g> delete in a dynamic tree, where h is <str<strong>on</strong>g>the</str<strong>on</strong>g> height of <str<strong>on</strong>g>the</str<strong>on</strong>g> tree. The algorithmis developed <str<strong>on</strong>g>for</str<strong>on</strong>g> an Arithmetic Pointer Machine (APM) model under <str<strong>on</strong>g>the</str<strong>on</strong>g> uni<str<strong>on</strong>g>for</str<strong>on</strong>g>m cost measure assumpti<strong>on</strong> (c<strong>on</strong>stanttime arithmetic <str<strong>on</strong>g>for</str<strong>on</strong>g> Θ(lg n)-size integers). This result <strong>on</strong> APMs has been recently improved in Alstrup <str<strong>on</strong>g>and</str<strong>on</strong>g> Thorup [1],where it is shown that <str<strong>on</strong>g>the</str<strong>on</strong>g> <str<strong>on</strong>g>NCA</str<strong>on</strong>g> <str<strong>on</strong>g>problem</str<strong>on</strong>g> can be solved in worst-case O(lg lg n) time per operati<strong>on</strong>, <str<strong>on</strong>g>and</str<strong>on</strong>g> that it can besolved in O(n + q lg lg n) time <strong>on</strong> APMs, where n is <str<strong>on</strong>g>the</str<strong>on</strong>g> number of link operati<strong>on</strong>s <str<strong>on</strong>g>and</str<strong>on</strong>g> q is <str<strong>on</strong>g>the</str<strong>on</strong>g> number of <str<strong>on</strong>g>NCA</str<strong>on</strong>g> queries.The work by Cole <str<strong>on</strong>g>and</str<strong>on</strong>g> Hariharan [7] provides <str<strong>on</strong>g>the</str<strong>on</strong>g> ability to insert (leaves <str<strong>on</strong>g>and</str<strong>on</strong>g> internal nodes) <str<strong>on</strong>g>and</str<strong>on</strong>g> delete nodes (leaves<str<strong>on</strong>g>and</str<strong>on</strong>g> internal nodes with <strong>on</strong>e child) in a tree, <str<strong>on</strong>g>and</str<strong>on</strong>g> execute <str<strong>on</strong>g>the</str<strong>on</strong>g> nca(x, y) operati<strong>on</strong> in worst-case c<strong>on</strong>stant time. Bothmethods make use of arithmetic capabilities of <str<strong>on</strong>g>the</str<strong>on</strong>g> respective machine models. For <str<strong>on</strong>g>the</str<strong>on</strong>g> offline versi<strong>on</strong>, a linear-timealgorithm <strong>on</strong> APMs was given by Buchsbaum et al. [6].The best known <str<strong>on</strong>g>parallel</str<strong>on</strong>g> <str<strong>on</strong>g>NCA</str<strong>on</strong>g> <str<strong>on</strong>g>algorithms</str<strong>on</strong>g> <strong>on</strong> PRAMs (e.g., <str<strong>on</strong>g>the</str<strong>on</strong>g> algorithm by Schieber <str<strong>on</strong>g>and</str<strong>on</strong>g> Vishkin, [22]) requireO(lg n) <str<strong>on</strong>g>parallel</str<strong>on</strong>g> time <str<strong>on</strong>g>for</str<strong>on</strong>g> pre-processing using O(n/ lg n) processors, <str<strong>on</strong>g>and</str<strong>on</strong>g> answers <str<strong>on</strong>g>the</str<strong>on</strong>g> <str<strong>on</strong>g>NCA</str<strong>on</strong>g> queries in O(1) time.Berkman <str<strong>on</strong>g>and</str<strong>on</strong>g> Vishkin [5] also show that if an Euler tour of <str<strong>on</strong>g>the</str<strong>on</strong>g> tree <str<strong>on</strong>g>and</str<strong>on</strong>g> <str<strong>on</strong>g>the</str<strong>on</strong>g> levels of all nodes are known in advance,<str<strong>on</strong>g>the</str<strong>on</strong>g>n <str<strong>on</strong>g>the</str<strong>on</strong>g> <str<strong>on</strong>g>NCA</str<strong>on</strong>g> <str<strong>on</strong>g>problem</str<strong>on</strong>g> can be solved with I m (n) <str<strong>on</strong>g>parallel</str<strong>on</strong>g> pre-processing using an optimal number of processors,<str<strong>on</strong>g>and</str<strong>on</strong>g> O(m) query time, where I m (n) = α(m, n) = min{i | A(m, i)n} <str<strong>on</strong>g>and</str<strong>on</strong>g> A is <str<strong>on</strong>g>the</str<strong>on</strong>g> Ackermann’s functi<strong>on</strong>. If thisextra in<str<strong>on</strong>g>for</str<strong>on</strong>g>mati<strong>on</strong> is not known in advance, <str<strong>on</strong>g>the</str<strong>on</strong>g>n <str<strong>on</strong>g>the</str<strong>on</strong>g> <str<strong>on</strong>g>parallel</str<strong>on</strong>g> pre-processing time is increased to O(lg n) <str<strong>on</strong>g>and</str<strong>on</strong>g> eachquery can be processed in O(1) time. We are not aware of any <str<strong>on</strong>g>parallel</str<strong>on</strong>g> pointer machine algorithm <str<strong>on</strong>g>for</str<strong>on</strong>g> <str<strong>on</strong>g>the</str<strong>on</strong>g> <str<strong>on</strong>g>NCA</str<strong>on</strong>g><str<strong>on</strong>g>problem</str<strong>on</strong>g>. It is possible to translate <str<strong>on</strong>g>the</str<strong>on</strong>g> known PRAM <str<strong>on</strong>g>algorithms</str<strong>on</strong>g> to Parallel pointer machine <str<strong>on</strong>g>algorithms</str<strong>on</strong>g>, although itis not clear how <strong>on</strong>e will avoid a penalty of at least of factor of lg n in this translati<strong>on</strong> (recall that PRAMs allowc<strong>on</strong>stant time arithmetic <strong>on</strong> numbers of size up to lg n <str<strong>on</strong>g>and</str<strong>on</strong>g> indexing into arrays using integer addresses, unlike pointermachines).In this work we focus <strong>on</strong> solving <str<strong>on</strong>g>the</str<strong>on</strong>g> <str<strong>on</strong>g>problem</str<strong>on</strong>g> <strong>on</strong> Pure Pointer Machines (PPMs), i.e., pointer machines that do notallow c<strong>on</strong>stant-time arithmetic operati<strong>on</strong>s. We present a simple, arithmetic-free, efficient scheme to compress treesmaintaining <str<strong>on</strong>g>the</str<strong>on</strong>g> <str<strong>on</strong>g>NCA</str<strong>on</strong>g> in<str<strong>on</strong>g>for</str<strong>on</strong>g>mati<strong>on</strong>. This compressi<strong>on</strong> scheme is different from <str<strong>on</strong>g>the</str<strong>on</strong>g> <strong>on</strong>es previously used in literature[15,7]. In particular, it does not make any use of arithmetic <str<strong>on</strong>g>and</str<strong>on</strong>g> it is very local in nature <str<strong>on</strong>g>and</str<strong>on</strong>g> hence seems eminently<str<strong>on</strong>g>parallel</str<strong>on</strong>g>izable. We use this compressi<strong>on</strong> scheme to provide an O(n+q lg lg n) soluti<strong>on</strong> <str<strong>on</strong>g>for</str<strong>on</strong>g> solving <str<strong>on</strong>g>the</str<strong>on</strong>g> <str<strong>on</strong>g>NCA</str<strong>on</strong>g> <str<strong>on</strong>g>problem</str<strong>on</strong>g> <strong>on</strong>PPMs in both <str<strong>on</strong>g>the</str<strong>on</strong>g> static <str<strong>on</strong>g>and</str<strong>on</strong>g> <str<strong>on</strong>g>the</str<strong>on</strong>g> dynamic case, where n is <str<strong>on</strong>g>the</str<strong>on</strong>g> number of add-leaf/delete operati<strong>on</strong>s <str<strong>on</strong>g>and</str<strong>on</strong>g> q is <str<strong>on</strong>g>the</str<strong>on</strong>g> number ofqueries. This soluti<strong>on</strong> is optimal because of a known matching lower bound [15]. Moreover, it has <str<strong>on</strong>g>the</str<strong>on</strong>g> same complexityas that of an optimal soluti<strong>on</strong> <strong>on</strong> APMs. Hence, our result shows that use of arithmetic is not essential <str<strong>on</strong>g>for</str<strong>on</strong>g> doing <str<strong>on</strong>g>NCA</str<strong>on</strong>g>calculati<strong>on</strong>s optimally. This is intellectually satisfying because, intuitively, <str<strong>on</strong>g>NCA</str<strong>on</strong>g> is a structural <str<strong>on</strong>g>and</str<strong>on</strong>g> not an arithmetic<str<strong>on</strong>g>problem</str<strong>on</strong>g>. The result is also interesting because it shows that, <str<strong>on</strong>g>for</str<strong>on</strong>g> <str<strong>on</strong>g>the</str<strong>on</strong>g> <str<strong>on</strong>g>NCA</str<strong>on</strong>g> <str<strong>on</strong>g>problem</str<strong>on</strong>g>, it is possible to totally avoid <str<strong>on</strong>g>the</str<strong>on</strong>g> polylogpenalty that <strong>on</strong>e has to incur in a generic translati<strong>on</strong> of an algorithm designed <str<strong>on</strong>g>for</str<strong>on</strong>g> pointer machines with arithmeticto PPMs.The soluti<strong>on</strong> can be extended to provide an efficient <str<strong>on</strong>g>parallel</str<strong>on</strong>g> pointer machine algorithm <str<strong>on</strong>g>for</str<strong>on</strong>g> <str<strong>on</strong>g>the</str<strong>on</strong>g> <str<strong>on</strong>g>NCA</str<strong>on</strong>g> <str<strong>on</strong>g>problem</str<strong>on</strong>g> <str<strong>on</strong>g>for</str<strong>on</strong>g>trees in <str<strong>on</strong>g>the</str<strong>on</strong>g> static case. The algorithm assumes that <str<strong>on</strong>g>the</str<strong>on</strong>g> tree T is known in advance. It requires O(lg n) <str<strong>on</strong>g>parallel</str<strong>on</strong>g> time<str<strong>on</strong>g>and</str<strong>on</strong>g> O(n) processors <str<strong>on</strong>g>for</str<strong>on</strong>g> pre-processing <str<strong>on</strong>g>the</str<strong>on</strong>g> tree, where n is <str<strong>on</strong>g>the</str<strong>on</strong>g> number of nodes. Thereafter, <str<strong>on</strong>g>the</str<strong>on</strong>g> algorithm can answerany <str<strong>on</strong>g>NCA</str<strong>on</strong>g> query in O(lg lg n) time using a single processor. To our knowledge, this is <str<strong>on</strong>g>the</str<strong>on</strong>g> best known <str<strong>on</strong>g>parallel</str<strong>on</strong>g> pointermachine algorithm <str<strong>on</strong>g>for</str<strong>on</strong>g> <str<strong>on</strong>g>the</str<strong>on</strong>g> <str<strong>on</strong>g>NCA</str<strong>on</strong>g> <str<strong>on</strong>g>problem</str<strong>on</strong>g>. Our <str<strong>on</strong>g>NCA</str<strong>on</strong>g> algorithm requires an efficient <str<strong>on</strong>g>parallel</str<strong>on</strong>g> soluti<strong>on</strong> of <str<strong>on</strong>g>the</str<strong>on</strong>g> temporalprecedence (T P) <str<strong>on</strong>g>problem</str<strong>on</strong>g> [21]. We provide an efficient <str<strong>on</strong>g>parallel</str<strong>on</strong>g> pointer machine algorithm to solve this <str<strong>on</strong>g>problem</str<strong>on</strong>g> aswell.The paper is organized as follows. Secti<strong>on</strong> 2 presents <str<strong>on</strong>g>the</str<strong>on</strong>g> computati<strong>on</strong> model—<str<strong>on</strong>g>the</str<strong>on</strong>g> Pointer Machine—used in <str<strong>on</strong>g>the</str<strong>on</strong>g>c<strong>on</strong>text of this investigati<strong>on</strong>. Secti<strong>on</strong> 3 introduces <str<strong>on</strong>g>the</str<strong>on</strong>g> notati<strong>on</strong>s <str<strong>on</strong>g>and</str<strong>on</strong>g> some basic definiti<strong>on</strong>s used throughout <str<strong>on</strong>g>the</str<strong>on</strong>g> paper.Secti<strong>on</strong> 4 presents <str<strong>on</strong>g>the</str<strong>on</strong>g> basic tree compressi<strong>on</strong> algorithm used to pre-process trees. Secti<strong>on</strong>s 5 <str<strong>on</strong>g>and</str<strong>on</strong>g> 6 present <str<strong>on</strong>g>algorithms</str<strong>on</strong>g><str<strong>on</strong>g>for</str<strong>on</strong>g> answering <str<strong>on</strong>g>NCA</str<strong>on</strong>g> queries in <str<strong>on</strong>g>the</str<strong>on</strong>g> static <str<strong>on</strong>g>and</str<strong>on</strong>g> in <str<strong>on</strong>g>the</str<strong>on</strong>g> dynamic case. Secti<strong>on</strong> 7 illustrates how <str<strong>on</strong>g>the</str<strong>on</strong>g> compressi<strong>on</strong> algorithmcan be modified to provide an optimal <str<strong>on</strong>g>parallel</str<strong>on</strong>g> scheme <str<strong>on</strong>g>for</str<strong>on</strong>g> <str<strong>on</strong>g>NCA</str<strong>on</strong>g> queries. Secti<strong>on</strong> 8 provides some c<strong>on</strong>clusi<strong>on</strong>s <str<strong>on</strong>g>and</str<strong>on</strong>g>indicati<strong>on</strong>s <str<strong>on</strong>g>for</str<strong>on</strong>g> future work.


110 A. D. Palú et al. / Theoretical Computer Science 352 (2006) 108 – 1352. Pointer machinesA PPM is a restricted versi<strong>on</strong> of a Pointer Machine, that does not allow c<strong>on</strong>stant time arithmetic operati<strong>on</strong>s. A PPMc<strong>on</strong>sists of a finite but exp<str<strong>on</strong>g>and</str<strong>on</strong>g>able collecti<strong>on</strong> of records <str<strong>on</strong>g>and</str<strong>on</strong>g> a finite collecti<strong>on</strong> of registers. Each record is uniquelyidentified through an address.A special address nil is used to denote an invalid address. Each record is a finite collecti<strong>on</strong>of fields <str<strong>on</strong>g>and</str<strong>on</strong>g> all <str<strong>on</strong>g>the</str<strong>on</strong>g> records have <str<strong>on</strong>g>the</str<strong>on</strong>g> same structure. Each field may c<strong>on</strong>tain ei<str<strong>on</strong>g>the</str<strong>on</strong>g>r a data or an address. The machine canexecute programs; <str<strong>on</strong>g>the</str<strong>on</strong>g> instructi<strong>on</strong>s in a program allow <strong>on</strong>e to move addresses <str<strong>on</strong>g>and</str<strong>on</strong>g> data between registers <str<strong>on</strong>g>and</str<strong>on</strong>g> betweenregisters <str<strong>on</strong>g>and</str<strong>on</strong>g> records’ fields. Special instructi<strong>on</strong>s are used to create a new record <str<strong>on</strong>g>and</str<strong>on</strong>g> to per<str<strong>on</strong>g>for</str<strong>on</strong>g>m c<strong>on</strong>diti<strong>on</strong>al jumps.The <strong>on</strong>ly c<strong>on</strong>diti<strong>on</strong>s allowed in <str<strong>on</strong>g>the</str<strong>on</strong>g> jumps are equality comparis<strong>on</strong>s between pointer registers. Observe that <str<strong>on</strong>g>the</str<strong>on</strong>g> c<strong>on</strong>tentof <str<strong>on</strong>g>the</str<strong>on</strong>g> data fields will never affect <str<strong>on</strong>g>the</str<strong>on</strong>g> behavior of <str<strong>on</strong>g>the</str<strong>on</strong>g> computati<strong>on</strong>. In terms of analysis of complexity, it is assumedthat each instructi<strong>on</strong> has a unit cost. This model is essentially <str<strong>on</strong>g>the</str<strong>on</strong>g> Linking Automat<strong>on</strong> model proposed by Knuth [16]<str<strong>on</strong>g>and</str<strong>on</strong>g> is a representative of what has been called atomistic pointer machine model in [2].PPMs provide a good base <str<strong>on</strong>g>for</str<strong>on</strong>g> modeling implementati<strong>on</strong> of linked data structures, like trees <str<strong>on</strong>g>and</str<strong>on</strong>g> lists—indeed, asignificant amount of research (e.g., [19,23,20,17]) has recently been proposed in this directi<strong>on</strong>. The PPM model is alsosimpler, w.r.t. RAM <str<strong>on</strong>g>and</str<strong>on</strong>g> o<str<strong>on</strong>g>the</str<strong>on</strong>g>r models, thus making it more suitable <str<strong>on</strong>g>for</str<strong>on</strong>g> analysis of lower bounds of time complexity.Observe that while RAM comm<strong>on</strong>ly hides <str<strong>on</strong>g>the</str<strong>on</strong>g> actual cost of arithmetic operati<strong>on</strong>s, by allowing operati<strong>on</strong>s <strong>on</strong> numbersof size up to lg n (n being <str<strong>on</strong>g>the</str<strong>on</strong>g> size of <str<strong>on</strong>g>the</str<strong>on</strong>g> input) to be treated as c<strong>on</strong>stant time operati<strong>on</strong>s (uni<str<strong>on</strong>g>for</str<strong>on</strong>g>m cost assumpti<strong>on</strong>),PPMs instead make <str<strong>on</strong>g>the</str<strong>on</strong>g>se costs explicit. This model of computati<strong>on</strong> is also close to <str<strong>on</strong>g>the</str<strong>on</strong>g> traditi<strong>on</strong>al method of data accessused in logic programming languages—indeed, observe that a significant part of our research has been motivated by<str<strong>on</strong>g>problem</str<strong>on</strong>g>s deriving from <str<strong>on</strong>g>the</str<strong>on</strong>g> field of logic programming (e.g., [19,20,26]). Note that <str<strong>on</strong>g>the</str<strong>on</strong>g> PPM model is similar to <str<strong>on</strong>g>the</str<strong>on</strong>g>Turing Machine model with respect to <str<strong>on</strong>g>the</str<strong>on</strong>g> fact that <str<strong>on</strong>g>the</str<strong>on</strong>g> complexity of <str<strong>on</strong>g>the</str<strong>on</strong>g> arithmetic operati<strong>on</strong>s has to be accounted <str<strong>on</strong>g>for</str<strong>on</strong>g>while analyzing <str<strong>on</strong>g>the</str<strong>on</strong>g> complexity of an algorithm. It is more powerful than <str<strong>on</strong>g>the</str<strong>on</strong>g> Turing machine model because it allows<str<strong>on</strong>g>for</str<strong>on</strong>g> “jumps” based <strong>on</strong> pointer comparis<strong>on</strong>s in c<strong>on</strong>stant time, that is not possible in <str<strong>on</strong>g>the</str<strong>on</strong>g> Turing machine model. Fur<str<strong>on</strong>g>the</str<strong>on</strong>g>rdetails <strong>on</strong> PPMs <str<strong>on</strong>g>and</str<strong>on</strong>g> <strong>on</strong> <str<strong>on</strong>g>algorithms</str<strong>on</strong>g> developed <str<strong>on</strong>g>for</str<strong>on</strong>g> PPMs can be found in [2,3,16,24].As with sequential pointer machine model, various versi<strong>on</strong>s of <str<strong>on</strong>g>parallel</str<strong>on</strong>g> pointer machines have been proposed. Theyall share <str<strong>on</strong>g>the</str<strong>on</strong>g> comm<strong>on</strong> characteristic that no pointer arithmetic is allowed; <str<strong>on</strong>g>the</str<strong>on</strong>g>se models comm<strong>on</strong>ly differ in <str<strong>on</strong>g>the</str<strong>on</strong>g> wayinterprocessor communicati<strong>on</strong> is realized (see [8] <str<strong>on</strong>g>for</str<strong>on</strong>g> an extensive discussi<strong>on</strong>). All models rely <strong>on</strong> <str<strong>on</strong>g>the</str<strong>on</strong>g> presence of anumber of processors; each processor is essentially a sequential pointer machine, <str<strong>on</strong>g>and</str<strong>on</strong>g> all processors execute <str<strong>on</strong>g>the</str<strong>on</strong>g> sameprogram in a synchr<strong>on</strong>ous fashi<strong>on</strong>. At <strong>on</strong>e end of <str<strong>on</strong>g>the</str<strong>on</strong>g> spectrum we have <str<strong>on</strong>g>the</str<strong>on</strong>g> CRCW Parallel Pointer Machine [13], wherearbitrary (c<strong>on</strong>current) read <str<strong>on</strong>g>and</str<strong>on</strong>g> write operati<strong>on</strong>s <strong>on</strong> a shared memory are allowed (although <str<strong>on</strong>g>the</str<strong>on</strong>g> shared memory cannotbe accessed as an array). At <str<strong>on</strong>g>the</str<strong>on</strong>g> o<str<strong>on</strong>g>the</str<strong>on</strong>g>r end of <str<strong>on</strong>g>the</str<strong>on</strong>g> spectrum, we have <str<strong>on</strong>g>the</str<strong>on</strong>g> Parallel PPM model [8]. The Parallel PPM isdefined by a collecti<strong>on</strong> of finite state synchr<strong>on</strong>ous machines (thus ruling out <str<strong>on</strong>g>the</str<strong>on</strong>g> use of c<strong>on</strong>stant time arithmetic), eachof which can rearrange its communicati<strong>on</strong> links by a bounded amount in <strong>on</strong>e step. Each finite state machine has anordered set of input lines (also called links), that can be viewed as taps <strong>on</strong> o<str<strong>on</strong>g>the</str<strong>on</strong>g>r processors’ outputs. The usual <str<strong>on</strong>g>parallel</str<strong>on</strong>g>PPM model allows <str<strong>on</strong>g>for</str<strong>on</strong>g> unbound fan-out but <strong>on</strong>ly c<strong>on</strong>stant fan-in. Each finite state machine has <str<strong>on</strong>g>the</str<strong>on</strong>g> ability to changeits links in a restricted way: a finite state machine may redirect <strong>on</strong>e of its links to point to ano<str<strong>on</strong>g>the</str<strong>on</strong>g>r unit at a “pointerdistance” no more than two from it. It has been shown that Parallel PPMs are surprisingly powerful. The details of whatexactly c<strong>on</strong>stitutes a <str<strong>on</strong>g>parallel</str<strong>on</strong>g> PPM can be found in Cook <str<strong>on</strong>g>and</str<strong>on</strong>g> Dym<strong>on</strong>d [8].There is a number of models whose computati<strong>on</strong>al power lies between that of <str<strong>on</strong>g>the</str<strong>on</strong>g> two models defined above, e.g.,<str<strong>on</strong>g>the</str<strong>on</strong>g> CREW/EREW Parallel pointer machines, <str<strong>on</strong>g>the</str<strong>on</strong>g> C<strong>on</strong>current-Read Owner-Write model (CROW), <str<strong>on</strong>g>and</str<strong>on</strong>g> <str<strong>on</strong>g>the</str<strong>on</strong>g> SIMDAGmodel with its variants [12]. Several interesting results regarding <str<strong>on</strong>g>the</str<strong>on</strong>g>ir computati<strong>on</strong>al power have been established.In particular, an n-processor CROW PRAM running in time O(lg n) can be simulated by a Parallel PPM in timeO(lg n lg lg n) using polynomially many processors. In additi<strong>on</strong> any step-by-step simulati<strong>on</strong> of an n processors CROWPRAM by a Parallel PPM requires time Ω(lg lg n) per step [9].3. Notati<strong>on</strong> <str<strong>on</strong>g>and</str<strong>on</strong>g> definiti<strong>on</strong>sAll <str<strong>on</strong>g>the</str<strong>on</strong>g> <str<strong>on</strong>g>problem</str<strong>on</strong>g>s c<strong>on</strong>sidered in this paper relate to dynamic binary trees. The following operati<strong>on</strong>s are used tomanipulate <str<strong>on</strong>g>the</str<strong>on</strong>g> structure of a dynamic tree:• create_tree(v) creates a new tree c<strong>on</strong>taining a single vertex (<str<strong>on</strong>g>the</str<strong>on</strong>g> root) labeled v; <str<strong>on</strong>g>the</str<strong>on</strong>g> operati<strong>on</strong> returns as result <str<strong>on</strong>g>the</str<strong>on</strong>g>node representing <str<strong>on</strong>g>the</str<strong>on</strong>g> root of <str<strong>on</strong>g>the</str<strong>on</strong>g> tree;


A. D. Palú et al. / Theoretical Computer Science 352 (2006) 108 – 135 111• exp<str<strong>on</strong>g>and</str<strong>on</strong>g>(x, v 1 ,v 2 ) assumes that x is a leaf in <str<strong>on</strong>g>the</str<strong>on</strong>g> tree, <str<strong>on</strong>g>and</str<strong>on</strong>g> it exp<str<strong>on</strong>g>and</str<strong>on</strong>g>s <str<strong>on</strong>g>the</str<strong>on</strong>g> tree by creating two children of x, respectivelylabeled v 1 <str<strong>on</strong>g>and</str<strong>on</strong>g> v 2 ;• remove(x) is used to remove <str<strong>on</strong>g>the</str<strong>on</strong>g> node x, which is assumed to be a leaf.A binary tree induces a natural partial ordering <strong>on</strong> <str<strong>on</strong>g>the</str<strong>on</strong>g> set of nodes in <str<strong>on</strong>g>the</str<strong>on</strong>g> tree. Given two nodes v <str<strong>on</strong>g>and</str<strong>on</strong>g> w, we writev w if v is an ancestor of w. v ≺ w additi<strong>on</strong>ally says that v ̸= w. We will assume to be a reflexive relati<strong>on</strong>. Observethat is a partial but not a total order.The <str<strong>on</strong>g>problem</str<strong>on</strong>g> c<strong>on</strong>sidered in this c<strong>on</strong>text is <str<strong>on</strong>g>the</str<strong>on</strong>g> <str<strong>on</strong>g>problem</str<strong>on</strong>g> of determining nearest comm<strong>on</strong> ancestors in a dynamic tree.This <str<strong>on</strong>g>problem</str<strong>on</strong>g> introduces an additi<strong>on</strong>al operati<strong>on</strong> to manipulate dynamic trees, called nca(x, y), where x <str<strong>on</strong>g>and</str<strong>on</strong>g> y are tw<strong>on</strong>odes in <str<strong>on</strong>g>the</str<strong>on</strong>g> tree. Given two nodes x,y, <str<strong>on</strong>g>the</str<strong>on</strong>g> operati<strong>on</strong> nca(x, y) returns a pointer to <str<strong>on</strong>g>the</str<strong>on</strong>g> nearest comm<strong>on</strong> ancestor of<str<strong>on</strong>g>the</str<strong>on</strong>g> nodes x <str<strong>on</strong>g>and</str<strong>on</strong>g> y. More <str<strong>on</strong>g>for</str<strong>on</strong>g>mally,nca(x, y) = z ⇔ (z x ∧ z y ∧∀w(w x ∧ w y ⇒ w z)).We will indicate with <str<strong>on</strong>g>NCA</str<strong>on</strong>g><str<strong>on</strong>g>the</str<strong>on</strong>g> <str<strong>on</strong>g>problem</str<strong>on</strong>g> of executing an arbitrary, correct, <strong>on</strong>-line sequence of create_tree, exp<str<strong>on</strong>g>and</str<strong>on</strong>g>,remove, <str<strong>on</strong>g>and</str<strong>on</strong>g> nca operati<strong>on</strong>s.4. A compressi<strong>on</strong> scheme <str<strong>on</strong>g>for</str<strong>on</strong>g> treesThe PPM algorithm to solve <str<strong>on</strong>g>the</str<strong>on</strong>g> <str<strong>on</strong>g>NCA</str<strong>on</strong>g> <str<strong>on</strong>g>problem</str<strong>on</strong>g> we propose is based <strong>on</strong> a compressi<strong>on</strong> scheme, aimed at creating anew tree with logarithmic depth that preserves <str<strong>on</strong>g>the</str<strong>on</strong>g> ancestor structure of <str<strong>on</strong>g>the</str<strong>on</strong>g> original tree. The compressi<strong>on</strong> algorithm wepropose starts from <str<strong>on</strong>g>the</str<strong>on</strong>g> initial tree T = T 0 <str<strong>on</strong>g>and</str<strong>on</strong>g> repeatedly per<str<strong>on</strong>g>for</str<strong>on</strong>g>ms two types of compressi<strong>on</strong>s, generating a sequenceof trees: T 0 ,T 1 ,T 2 ,...until a tree T k c<strong>on</strong>taining a single node is obtained. The trees in this sequence are used to builda sec<strong>on</strong>d tree structure (H-tree), that summarizes <str<strong>on</strong>g>the</str<strong>on</strong>g> <str<strong>on</strong>g>NCA</str<strong>on</strong>g> in<str<strong>on</strong>g>for</str<strong>on</strong>g>mati<strong>on</strong> of T. The key property of <str<strong>on</strong>g>the</str<strong>on</strong>g> H-tree is that itsdepth is at most logarithmic in <str<strong>on</strong>g>the</str<strong>on</strong>g> number of nodes T. This allows us to produce fast answers to <str<strong>on</strong>g>NCA</str<strong>on</strong>g> queries.Given T i , Ti+1 L <str<strong>on</strong>g>the</str<strong>on</strong>g> result of leaf-compressi<strong>on</strong> of T i, is obtained by merging each leaf of T i with its parent. If a leafl is merged with its parent parent(l), <str<strong>on</strong>g>the</str<strong>on</strong>g>n parent(l) is said to be <str<strong>on</strong>g>the</str<strong>on</strong>g> direct representative of l.Apath-compressi<strong>on</strong>of a tree Ti+1 L returns a tree T i+1, where each path c<strong>on</strong>taining <strong>on</strong>ly nodes with a single child <str<strong>on</strong>g>and</str<strong>on</strong>g> ending in a leaf ofTi+1 L is replaced by <str<strong>on</strong>g>the</str<strong>on</strong>g> head of such path. If a path c<strong>on</strong>taining nodes v 0,v 1 ,...,v k is compressed to <str<strong>on</strong>g>the</str<strong>on</strong>g> node v 0 , <str<strong>on</strong>g>the</str<strong>on</strong>g>nv 0 is said to be <str<strong>on</strong>g>the</str<strong>on</strong>g> direct representative of v 0 ,...,v k .Acompressi<strong>on</strong> of a tree T i is <str<strong>on</strong>g>the</str<strong>on</strong>g> tree T i+1 , where T i+1 is <str<strong>on</strong>g>the</str<strong>on</strong>g>path-compressi<strong>on</strong> of Ti+1 L , <str<strong>on</strong>g>and</str<strong>on</strong>g> T i+1 L is <str<strong>on</strong>g>the</str<strong>on</strong>g> result of a leaf-compressi<strong>on</strong> <strong>on</strong> T i. In this notati<strong>on</strong> let T = T 0 .Fig. 1 shows an example of repeated compressi<strong>on</strong> of T. Both leaf <str<strong>on</strong>g>and</str<strong>on</strong>g> path-compressi<strong>on</strong>s start at <str<strong>on</strong>g>the</str<strong>on</strong>g> fr<strong>on</strong>tier ofeach tree. Each time a leaf-compressi<strong>on</strong> is applied, all leaves are merged with <str<strong>on</strong>g>the</str<strong>on</strong>g>ir parents. For example, in Fig. 1leaf-compressi<strong>on</strong> removes nodes 10–15 (Fig. 1.1) by merging <str<strong>on</strong>g>the</str<strong>on</strong>g>m with <str<strong>on</strong>g>the</str<strong>on</strong>g>ir parents (Fig. 1.2). A path-compressi<strong>on</strong>merges all paths ending in a leaf into <str<strong>on</strong>g>the</str<strong>on</strong>g>ir heads. For example, in Fig. 1.3 <str<strong>on</strong>g>the</str<strong>on</strong>g> path composed by nodes 4, 6, 9 has beencollapsed to <str<strong>on</strong>g>the</str<strong>on</strong>g> single node 4 (node 4 is <str<strong>on</strong>g>the</str<strong>on</strong>g> direct representative of 4, 6 <str<strong>on</strong>g>and</str<strong>on</strong>g> 9). The tree is compressed starting from<str<strong>on</strong>g>the</str<strong>on</strong>g> leaves <str<strong>on</strong>g>and</str<strong>on</strong>g> moving towards <str<strong>on</strong>g>the</str<strong>on</strong>g> root. In Fig. 1 we have marked <str<strong>on</strong>g>the</str<strong>on</strong>g> representatives of each compressi<strong>on</strong> with darkernodes.4.1. The H-treeIn order to compute <str<strong>on</strong>g>NCA</str<strong>on</strong>g> queries in optimal time, it is useful to collect <str<strong>on</strong>g>the</str<strong>on</strong>g> in<str<strong>on</strong>g>for</str<strong>on</strong>g>mati<strong>on</strong> about representatives in aseparate tree, called Horiz<strong>on</strong>tal Tree (H-tree). The H-tree, H, can be c<strong>on</strong>structed from <str<strong>on</strong>g>the</str<strong>on</strong>g> sequence of trees obtainedduring <str<strong>on</strong>g>the</str<strong>on</strong>g> compressi<strong>on</strong> process (e.g., <str<strong>on</strong>g>the</str<strong>on</strong>g> trees shown in Fig. 1). If a leaf-compressi<strong>on</strong> is applied to node v in tree T i<str<strong>on</strong>g>and</str<strong>on</strong>g> l is <str<strong>on</strong>g>the</str<strong>on</strong>g> direct representative of v in such compressi<strong>on</strong>, <str<strong>on</strong>g>the</str<strong>on</strong>g>n node v is c<strong>on</strong>nected to <str<strong>on</strong>g>the</str<strong>on</strong>g> last occurrence of l in atree T j (i < j), where l appears in T j as a direct representative of a leaf-compressi<strong>on</strong>. If all <str<strong>on</strong>g>the</str<strong>on</strong>g> children of a nodea in T i are leaf-compressed at <str<strong>on</strong>g>the</str<strong>on</strong>g> same time, <str<strong>on</strong>g>the</str<strong>on</strong>g>n <str<strong>on</strong>g>the</str<strong>on</strong>g> representative of such children is node a in Ti+1 L (as <str<strong>on</strong>g>for</str<strong>on</strong>g> leaves10, 11 in Fig. 1). If <str<strong>on</strong>g>the</str<strong>on</strong>g> children of a are leaf-compressed at different points in time (e.g., <str<strong>on</strong>g>the</str<strong>on</strong>g> children of 1 in Fig. 1),<str<strong>on</strong>g>the</str<strong>on</strong>g>n <str<strong>on</strong>g>the</str<strong>on</strong>g> representative of such leaf is <str<strong>on</strong>g>the</str<strong>on</strong>g> last occurrence of its direct representative in a tree as representative in aleaf-compressi<strong>on</strong>. If a path-compressi<strong>on</strong> is applied, <str<strong>on</strong>g>the</str<strong>on</strong>g>n all nodes in <str<strong>on</strong>g>the</str<strong>on</strong>g> path are c<strong>on</strong>nected to <str<strong>on</strong>g>the</str<strong>on</strong>g> head of <str<strong>on</strong>g>the</str<strong>on</strong>g> list in <str<strong>on</strong>g>the</str<strong>on</strong>g>next tree (see Fig. 1). Such node is <str<strong>on</strong>g>the</str<strong>on</strong>g> representative of all nodes in <str<strong>on</strong>g>the</str<strong>on</strong>g> path. H is obtained using <str<strong>on</strong>g>the</str<strong>on</strong>g> single node in <str<strong>on</strong>g>the</str<strong>on</strong>g>last compressed tree (e.g., <str<strong>on</strong>g>the</str<strong>on</strong>g> node in T 2 in Fig. 1) as <str<strong>on</strong>g>the</str<strong>on</strong>g> root <str<strong>on</strong>g>and</str<strong>on</strong>g> using <str<strong>on</strong>g>the</str<strong>on</strong>g> links between nodes <str<strong>on</strong>g>and</str<strong>on</strong>g> representativesas edges (e.g., <str<strong>on</strong>g>the</str<strong>on</strong>g> dark edges in Fig. 1).


112 A. D. Palú et al. / Theoretical Computer Science 352 (2006) 108 – 135Phase 1. Phase 2. Phase 3. Phase 4. Phase 5.12 3 4567 8 910 11 12 13 14 151Path-Compressi<strong>on</strong>5 2 1Leaf-Compressi<strong>on</strong>784Path-Compressi<strong>on</strong>7 89 6 4Leaf-Compressi<strong>on</strong>10 11 12 13 14 15 3Fig. 1. Building <str<strong>on</strong>g>the</str<strong>on</strong>g> H-tree. Phase 1: Original Tree T 0 ; Phase 2: After leaf-compressi<strong>on</strong> TL1; Phase 3: After path-compressi<strong>on</strong> T 1; Phase 4: Afterleaf-compressi<strong>on</strong> TL2; Phase 5: After path-compressi<strong>on</strong> T 2.Observe that <str<strong>on</strong>g>the</str<strong>on</strong>g> leaves of <str<strong>on</strong>g>the</str<strong>on</strong>g> original tree are leaves in H although H might have additi<strong>on</strong>al leaves. Also, eachinternal node in T is present in H, as each internal node is ei<str<strong>on</strong>g>the</str<strong>on</strong>g>r a representative in a leaf compressi<strong>on</strong> or is involved ina path compressi<strong>on</strong>. Observe that H has at most 2n nodes, since each node can appear in H because of (possibly many)leaf-compressi<strong>on</strong>s at most <strong>on</strong>ce <str<strong>on</strong>g>and</str<strong>on</strong>g> can be involved in a path-compressi<strong>on</strong> at most <strong>on</strong>ce. Moreover, if a node v ∈ Tappears twice in H, <str<strong>on</strong>g>the</str<strong>on</strong>g>n it must be <str<strong>on</strong>g>the</str<strong>on</strong>g> case that <strong>on</strong>e occurrence of v in H is due to <str<strong>on</strong>g>the</str<strong>on</strong>g> fact that v was a head in a pathcompressi<strong>on</strong> <str<strong>on</strong>g>and</str<strong>on</strong>g> is a direct representative in leaf compressi<strong>on</strong>s which precede <str<strong>on</strong>g>the</str<strong>on</strong>g> a<str<strong>on</strong>g>for</str<strong>on</strong>g>ementi<strong>on</strong>ed path compressi<strong>on</strong>.Note that <strong>on</strong>e occurrence of v in H must be a child of <str<strong>on</strong>g>the</str<strong>on</strong>g> o<str<strong>on</strong>g>the</str<strong>on</strong>g>r occurrence of v in H. The next lemma provides a resultcritical to <str<strong>on</strong>g>the</str<strong>on</strong>g> efficiency of <str<strong>on</strong>g>the</str<strong>on</strong>g> compressi<strong>on</strong> scheme. Let subtree T (v) be <str<strong>on</strong>g>the</str<strong>on</strong>g> subtree of T rooted at node v.Lemma 1. If a node v of T still exists in T k <str<strong>on</strong>g>the</str<strong>on</strong>g>n <str<strong>on</strong>g>the</str<strong>on</strong>g> subtree T (v) has at least 2 k nodes.Proof. Let us prove this result by inducti<strong>on</strong> <strong>on</strong> k.Base:Fork = 0 <str<strong>on</strong>g>the</str<strong>on</strong>g> result is trivial, since <str<strong>on</strong>g>the</str<strong>on</strong>g> subtree rooted at v c<strong>on</strong>tains at least <strong>on</strong>e (i.e., 2 ◦ ) node. Let us c<strong>on</strong>sider<str<strong>on</strong>g>the</str<strong>on</strong>g> case k = 1. For each node v in T 1 let us call w 1 a leaf of subtree T1 (v). The node w 1 is <str<strong>on</strong>g>the</str<strong>on</strong>g> result of <str<strong>on</strong>g>the</str<strong>on</strong>g> previouspath-compressi<strong>on</strong> <strong>on</strong> T1 L. Let us call w 2 <str<strong>on</strong>g>the</str<strong>on</strong>g> leaf in T1L compressed in w 1. The node w 2 cannot be a leaf in T 0 , o<str<strong>on</strong>g>the</str<strong>on</strong>g>rwiseit would have been compressed with its parent. It follows that w 2 has at least <strong>on</strong>e child, let us call it w 3 . This impliesthat v is different from w 3 . This proves that subtree T (v) c<strong>on</strong>tains at least 2 1 nodes.Inductive step: Let us c<strong>on</strong>sider <str<strong>on</strong>g>the</str<strong>on</strong>g> case k = i, <str<strong>on</strong>g>and</str<strong>on</strong>g> let us assume by inductive hypo<str<strong>on</strong>g>the</str<strong>on</strong>g>sis that <str<strong>on</strong>g>the</str<strong>on</strong>g> results hold <str<strong>on</strong>g>for</str<strong>on</strong>g>i − 1.For each node v in T i let us indicate with w 1 a leaf of subtree Ti (v) (see also Fig. 2). The node w 1 is <str<strong>on</strong>g>the</str<strong>on</strong>g> result of<str<strong>on</strong>g>the</str<strong>on</strong>g> previous path-compressi<strong>on</strong> applied to Ti L . Let us call w 2 <str<strong>on</strong>g>the</str<strong>on</strong>g> leaf in TiL that has been compressed with w 1 . Thenode w 2 cannot have been a leaf of T i−1 , else it would have been compressed with its parent. It follows that w 2 hasat least <strong>on</strong>e child, let us call it w 3 . The node w 3 in T i−1 is <str<strong>on</strong>g>the</str<strong>on</strong>g> result of a path-compressi<strong>on</strong> applied to Ti−1 L .Ifw 3 was<str<strong>on</strong>g>the</str<strong>on</strong>g> <strong>on</strong>ly child of w 2 , <str<strong>on</strong>g>the</str<strong>on</strong>g>n <str<strong>on</strong>g>the</str<strong>on</strong>g> path-compressi<strong>on</strong> would not have ended in w 3 . Thus, w 3 has a sibling in T i−1 —let us


A. D. Palú et al. / Theoretical Computer Science 352 (2006) 108 – 135 113Ti-1 TLi Tivvvw1w1w1w2w2w3w4Fig. 2. Generic compressi<strong>on</strong>.call it w 4 . Using <str<strong>on</strong>g>the</str<strong>on</strong>g> inductive hypo<str<strong>on</strong>g>the</str<strong>on</strong>g>sis applied to nodes w 3 <str<strong>on</strong>g>and</str<strong>on</strong>g> w 4 in T i−1 , we can c<strong>on</strong>clude that subtree T (v) hasat least 1 + 2 i−1 + 2 i−1 nodes. □Corollary 1. Let n be <str<strong>on</strong>g>the</str<strong>on</strong>g> number of nodes in T <str<strong>on</strong>g>and</str<strong>on</strong>g> let k be <str<strong>on</strong>g>the</str<strong>on</strong>g> minimum integer such that T k has a single node. Thenk lg n. In o<str<strong>on</strong>g>the</str<strong>on</strong>g>r words, T gets compressed to a single node within lg n compressi<strong>on</strong>s.4.2. Answering <str<strong>on</strong>g>NCA</str<strong>on</strong>g>s using H-treesGiven <str<strong>on</strong>g>the</str<strong>on</strong>g> query nca(x, y), where x,y ∈ T , it is possible to answer <str<strong>on</strong>g>the</str<strong>on</strong>g> query using H. In particular, <str<strong>on</strong>g>the</str<strong>on</strong>g> <str<strong>on</strong>g>NCA</str<strong>on</strong>g> oftwo nodes in T can be computed by first computing an <str<strong>on</strong>g>NCA</str<strong>on</strong>g> of <str<strong>on</strong>g>the</str<strong>on</strong>g> “entry-points” <str<strong>on</strong>g>for</str<strong>on</strong>g> x <str<strong>on</strong>g>and</str<strong>on</strong>g> y in H. The entry-point inH <str<strong>on</strong>g>for</str<strong>on</strong>g> x is simply <str<strong>on</strong>g>the</str<strong>on</strong>g> lower (or <str<strong>on</strong>g>the</str<strong>on</strong>g> <strong>on</strong>ly) occurrence of x in H. We provide an intuitive descripti<strong>on</strong> of this method—<str<strong>on</strong>g>the</str<strong>on</strong>g>algorithm called nca H is presented in Secti<strong>on</strong> 5. We show now that <str<strong>on</strong>g>the</str<strong>on</strong>g> H-tree preserves enough <str<strong>on</strong>g>NCA</str<strong>on</strong>g> in<str<strong>on</strong>g>for</str<strong>on</strong>g>mati<strong>on</strong> fromT. Let z be <str<strong>on</strong>g>the</str<strong>on</strong>g> nca H (x, y).Ifz is a representative of a leaf-compressi<strong>on</strong>, <str<strong>on</strong>g>the</str<strong>on</strong>g>n z is also <str<strong>on</strong>g>the</str<strong>on</strong>g> nca of x,y ∈ T . O<str<strong>on</strong>g>the</str<strong>on</strong>g>rwiselet z 0 ,z 1 ,...,z k be <str<strong>on</strong>g>the</str<strong>on</strong>g> nodes bel<strong>on</strong>ging to <str<strong>on</strong>g>the</str<strong>on</strong>g> path that has been compressed to z. There are two distinct nodes z i ,z jin this path such that <str<strong>on</strong>g>the</str<strong>on</strong>g> subtree rooted at z i (z j ) c<strong>on</strong>tains x (y) Thus, <str<strong>on</strong>g>the</str<strong>on</strong>g> nca of x <str<strong>on</strong>g>and</str<strong>on</strong>g> y is <str<strong>on</strong>g>the</str<strong>on</strong>g> highest node between z i<str<strong>on</strong>g>and</str<strong>on</strong>g> z j , <str<strong>on</strong>g>and</str<strong>on</strong>g> answering an <str<strong>on</strong>g>NCA</str<strong>on</strong>g> query in T boils down to computing an <str<strong>on</strong>g>NCA</str<strong>on</strong>g> query in H. From Lemma 1, we can inferthat <str<strong>on</strong>g>the</str<strong>on</strong>g> height of H is O(lg n). In <str<strong>on</strong>g>the</str<strong>on</strong>g> next subsecti<strong>on</strong> we illustrate an algorithm that allows <str<strong>on</strong>g>the</str<strong>on</strong>g> computati<strong>on</strong> of <str<strong>on</strong>g>the</str<strong>on</strong>g> ncaof any two nodes of a dynamic tree in worst case time complexity O(lg h) per query, where h is <str<strong>on</strong>g>the</str<strong>on</strong>g> height of <str<strong>on</strong>g>the</str<strong>on</strong>g> tree.Using this result, we can compute <str<strong>on</strong>g>the</str<strong>on</strong>g> nca of x,y in H in worst-case time O(lg lg n). This allows <str<strong>on</strong>g>the</str<strong>on</strong>g> computati<strong>on</strong> of<str<strong>on</strong>g>the</str<strong>on</strong>g> <str<strong>on</strong>g>NCA</str<strong>on</strong>g> in T with worst-case time complexity O(lg lg n).4.3. O(lg h) time computati<strong>on</strong> of <str<strong>on</strong>g>NCA</str<strong>on</strong>g>We provide a worst-case O(lg h) soluti<strong>on</strong> <str<strong>on</strong>g>for</str<strong>on</strong>g> <str<strong>on</strong>g>the</str<strong>on</strong>g> <str<strong>on</strong>g>NCA</str<strong>on</strong>g> Problem <strong>on</strong> PPMs, where h is <str<strong>on</strong>g>the</str<strong>on</strong>g> height of <str<strong>on</strong>g>the</str<strong>on</strong>g> dynamictree. It is worth noting that a worst-case c<strong>on</strong>stant-time soluti<strong>on</strong> is not possible <str<strong>on</strong>g>for</str<strong>on</strong>g> <str<strong>on</strong>g>the</str<strong>on</strong>g> <str<strong>on</strong>g>NCA</str<strong>on</strong>g>Problem <strong>on</strong> <strong>pure</strong> pointermachines, even in <str<strong>on</strong>g>the</str<strong>on</strong>g> static case. A lower bound of Ω(lg lg n) (n being <str<strong>on</strong>g>the</str<strong>on</strong>g> number of nodes in <str<strong>on</strong>g>the</str<strong>on</strong>g> tree) is provided at<str<strong>on</strong>g>the</str<strong>on</strong>g> end of this secti<strong>on</strong> (see also [15]).The basic idea behind our soluti<strong>on</strong> is to maintain <str<strong>on</strong>g>the</str<strong>on</strong>g> depth of <str<strong>on</strong>g>the</str<strong>on</strong>g> tree nodes. For any vertex x in <str<strong>on</strong>g>the</str<strong>on</strong>g> tree, let usdenote with anc(x, d) <str<strong>on</strong>g>the</str<strong>on</strong>g> ancestor of vertex x lying at depth d in <str<strong>on</strong>g>the</str<strong>on</strong>g> tree. Thus, if we have two nodes x <str<strong>on</strong>g>and</str<strong>on</strong>g> y <str<strong>on</strong>g>and</str<strong>on</strong>g>anc(x, d) = anc(y, d), <str<strong>on</strong>g>the</str<strong>on</strong>g>n we can infer that nca(x, y) is at a depth at least d in <str<strong>on</strong>g>the</str<strong>on</strong>g> tree. O<str<strong>on</strong>g>the</str<strong>on</strong>g>rwise: nca(x, y) =nca(anc(x, d), anc(y, d)).In our soluti<strong>on</strong>, with each node in <str<strong>on</strong>g>the</str<strong>on</strong>g> dynamic tree we store <str<strong>on</strong>g>the</str<strong>on</strong>g> depth of <str<strong>on</strong>g>the</str<strong>on</strong>g> node. The depths of <str<strong>on</strong>g>the</str<strong>on</strong>g> tree arerepresented by nodes in ano<str<strong>on</strong>g>the</str<strong>on</strong>g>r binary tree (<str<strong>on</strong>g>the</str<strong>on</strong>g> tree to <str<strong>on</strong>g>the</str<strong>on</strong>g> right in Fig. 3). Each time a new node is inserted in <str<strong>on</strong>g>the</str<strong>on</strong>g>main tree, we calculate its depth. When <str<strong>on</strong>g>the</str<strong>on</strong>g> node x is added to <str<strong>on</strong>g>the</str<strong>on</strong>g> main tree, we can determine its depth by looking at


114 A. D. Palú et al. / Theoretical Computer Science 352 (2006) 108 – 13510011p1230145p2Fig. 3. Maintaining depths.<str<strong>on</strong>g>the</str<strong>on</strong>g> depth pointer stored in <str<strong>on</strong>g>the</str<strong>on</strong>g> parent node of x. We can assume that each node in <str<strong>on</strong>g>the</str<strong>on</strong>g> depths tree c<strong>on</strong>tains a pointer to<str<strong>on</strong>g>the</str<strong>on</strong>g> logical successor node in such tree (see <str<strong>on</strong>g>the</str<strong>on</strong>g> dark links in Fig. 3). If <str<strong>on</strong>g>the</str<strong>on</strong>g> depth node associated to <str<strong>on</strong>g>the</str<strong>on</strong>g> parent of x in<str<strong>on</strong>g>the</str<strong>on</strong>g> main tree does not have a successor, <str<strong>on</strong>g>the</str<strong>on</strong>g>n this means that <str<strong>on</strong>g>the</str<strong>on</strong>g> new node x lies in a new level of <str<strong>on</strong>g>the</str<strong>on</strong>g> main tree, whichwill lead to <str<strong>on</strong>g>the</str<strong>on</strong>g> creati<strong>on</strong> of a new node also in <str<strong>on</strong>g>the</str<strong>on</strong>g> depth tree. The pointers p1 <str<strong>on</strong>g>and</str<strong>on</strong>g> p2 can be used to determine when anew level has to be created in <str<strong>on</strong>g>the</str<strong>on</strong>g> depths tree.Observe that <str<strong>on</strong>g>the</str<strong>on</strong>g> creati<strong>on</strong> of <str<strong>on</strong>g>the</str<strong>on</strong>g> depth structure can be per<str<strong>on</strong>g>for</str<strong>on</strong>g>med in O(1) time each time an exp<str<strong>on</strong>g>and</str<strong>on</strong>g> operati<strong>on</strong> isper<str<strong>on</strong>g>for</str<strong>on</strong>g>med.In additi<strong>on</strong>, we maintain <str<strong>on</strong>g>for</str<strong>on</strong>g> each node in <str<strong>on</strong>g>the</str<strong>on</strong>g> tree a list of pointers to selected ancestor nodes (<str<strong>on</strong>g>the</str<strong>on</strong>g> predecessors listdescribed in <str<strong>on</strong>g>the</str<strong>on</strong>g> next subsecti<strong>on</strong>). These pointers are used to per<str<strong>on</strong>g>for</str<strong>on</strong>g>m a binary search leading to <str<strong>on</strong>g>the</str<strong>on</strong>g> identificati<strong>on</strong> of<str<strong>on</strong>g>the</str<strong>on</strong>g> nearest comm<strong>on</strong> ancestor. The resulting data structure, discovered independently, resembles <str<strong>on</strong>g>the</str<strong>on</strong>g> <strong>on</strong>e used in [25],but does not assume c<strong>on</strong>stant time arithmetic capabilities.The rest of this secti<strong>on</strong> describes how to realize <str<strong>on</strong>g>the</str<strong>on</strong>g>se ideas. We start by describing <str<strong>on</strong>g>the</str<strong>on</strong>g> data structures needed toefficiently solve <str<strong>on</strong>g>the</str<strong>on</strong>g> <str<strong>on</strong>g>NCA</str<strong>on</strong>g><str<strong>on</strong>g>problem</str<strong>on</strong>g>.4.3.1. Predecessors list4.3.1.1. General definiti<strong>on</strong>s. To support <str<strong>on</strong>g>the</str<strong>on</strong>g> executi<strong>on</strong> of <str<strong>on</strong>g>the</str<strong>on</strong>g> nca operati<strong>on</strong> we need an additi<strong>on</strong>al data structure superimposed<strong>on</strong> <str<strong>on</strong>g>the</str<strong>on</strong>g> dynamic tree: a predecessors list (p-list) attached to each node of <str<strong>on</strong>g>the</str<strong>on</strong>g> tree. Each element of <str<strong>on</strong>g>the</str<strong>on</strong>g> p-list isa record with four fields, called respectively data link, right-up link, left-up link, <str<strong>on</strong>g>and</str<strong>on</strong>g> middle-up link (in additi<strong>on</strong> to <str<strong>on</strong>g>the</str<strong>on</strong>g>st<str<strong>on</strong>g>and</str<strong>on</strong>g>ard fields required to maintain <str<strong>on</strong>g>the</str<strong>on</strong>g> linked list).In <str<strong>on</strong>g>the</str<strong>on</strong>g> rest of this paper we will make use of <str<strong>on</strong>g>the</str<strong>on</strong>g> following notati<strong>on</strong>: given a node x in <str<strong>on</strong>g>the</str<strong>on</strong>g> tree, last(x) (first(x)) denotes<str<strong>on</strong>g>the</str<strong>on</strong>g> last (first) record in <str<strong>on</strong>g>the</str<strong>on</strong>g> p-list associated to <str<strong>on</strong>g>the</str<strong>on</strong>g> node x. In additi<strong>on</strong>, pred(x, i) indicates <str<strong>on</strong>g>the</str<strong>on</strong>g> ith element of <str<strong>on</strong>g>the</str<strong>on</strong>g> p-listof x.Ifs is an element of a p-list, <str<strong>on</strong>g>the</str<strong>on</strong>g>n data(s), right_link(s), left_link(s), <str<strong>on</strong>g>and</str<strong>on</strong>g> middle_link(s) represent <str<strong>on</strong>g>the</str<strong>on</strong>g> c<strong>on</strong>tent of<str<strong>on</strong>g>the</str<strong>on</strong>g> data link, right-up link, left-up link, <str<strong>on</strong>g>and</str<strong>on</strong>g> middle-up link stored in <str<strong>on</strong>g>the</str<strong>on</strong>g> element s. The successor (predecessor) of anelement s of a p-list is indicated with next(s) (prev(s))—i.e.,first(x) = pred(x, 0),next(pred(x, i)) = pred(x, i + 1),prev(pred(x, i + 1)) = pred(x, i).The p-list of node x c<strong>on</strong>tains ⌊lg h⌋ +1 elements, where h is <str<strong>on</strong>g>the</str<strong>on</strong>g> depth of node x in <str<strong>on</strong>g>the</str<strong>on</strong>g> tree. The p-list of x isdesigned to c<strong>on</strong>tain pointers to ancestor nodes of x. In particular <str<strong>on</strong>g>the</str<strong>on</strong>g> p-list of x points to <str<strong>on</strong>g>the</str<strong>on</strong>g> ancestors of x which havedistance 1, 2, 4, 8,...,2 ⌊lg h⌋ from node x. Let jump(x, k) denote <str<strong>on</strong>g>the</str<strong>on</strong>g> ancestor of node x which is at a distance k fromx in <str<strong>on</strong>g>the</str<strong>on</strong>g> tree. The data link field of <str<strong>on</strong>g>the</str<strong>on</strong>g> ith element of <str<strong>on</strong>g>the</str<strong>on</strong>g> p-list of x c<strong>on</strong>tains a pointer to <str<strong>on</strong>g>the</str<strong>on</strong>g> node jump(x, 2 i ), <str<strong>on</strong>g>for</str<strong>on</strong>g>i = 0, 1,...,⌊lg depth(x)⌋. More precisely, if x is a node in <str<strong>on</strong>g>the</str<strong>on</strong>g> tree, <str<strong>on</strong>g>the</str<strong>on</strong>g>ndata(pred(x, i)) = jump(x, 2 i ).The o<str<strong>on</strong>g>the</str<strong>on</strong>g>r three pointers maintained in each element of <str<strong>on</strong>g>the</str<strong>on</strong>g> p-list, <str<strong>on</strong>g>the</str<strong>on</strong>g> right-up link, <str<strong>on</strong>g>the</str<strong>on</strong>g> left-up link, <str<strong>on</strong>g>and</str<strong>on</strong>g> <str<strong>on</strong>g>the</str<strong>on</strong>g> middle-uplink, point to elements in <str<strong>on</strong>g>the</str<strong>on</strong>g> p-lists of o<str<strong>on</strong>g>the</str<strong>on</strong>g>r nodes in <str<strong>on</strong>g>the</str<strong>on</strong>g> tree. As we will show in <str<strong>on</strong>g>the</str<strong>on</strong>g> rest of this secti<strong>on</strong>, <str<strong>on</strong>g>the</str<strong>on</strong>g> right-uplinks are employed to help in <str<strong>on</strong>g>the</str<strong>on</strong>g> creati<strong>on</strong> of <str<strong>on</strong>g>the</str<strong>on</strong>g> p-list, while <str<strong>on</strong>g>the</str<strong>on</strong>g> left-up <str<strong>on</strong>g>and</str<strong>on</strong>g> <str<strong>on</strong>g>the</str<strong>on</strong>g> middle-up links are employed to allow<str<strong>on</strong>g>the</str<strong>on</strong>g> efficient computati<strong>on</strong> of <str<strong>on</strong>g>the</str<strong>on</strong>g> nca.


A. D. Palú et al. / Theoretical Computer Science 352 (2006) 108 – 135 115data right-upFig. 4. Right-up links between nodes of <str<strong>on</strong>g>the</str<strong>on</strong>g> p-lists.The meaning of <str<strong>on</strong>g>the</str<strong>on</strong>g>se links can be described as follows. If x is a node in <str<strong>on</strong>g>the</str<strong>on</strong>g> tree, <str<strong>on</strong>g>the</str<strong>on</strong>g>n <str<strong>on</strong>g>the</str<strong>on</strong>g> right-up link of pred(x, i)is a pointer to <str<strong>on</strong>g>the</str<strong>on</strong>g> (i + 1)th element of <str<strong>on</strong>g>the</str<strong>on</strong>g> p-list of <str<strong>on</strong>g>the</str<strong>on</strong>g> node jump(x, 2 i ), if it exists (it is nil o<str<strong>on</strong>g>the</str<strong>on</strong>g>rwise). More precisely,right_link(pred(x, i)) = pred(jump(x, 2 i ), i + 1).This is illustrated in Fig. 4. Similarly, <str<strong>on</strong>g>the</str<strong>on</strong>g> middle-up <str<strong>on</strong>g>and</str<strong>on</strong>g> <str<strong>on</strong>g>the</str<strong>on</strong>g> left-up links of <str<strong>on</strong>g>the</str<strong>on</strong>g> ith element of <str<strong>on</strong>g>the</str<strong>on</strong>g> p-list of x c<strong>on</strong>tainpointers to <str<strong>on</strong>g>the</str<strong>on</strong>g> ith <str<strong>on</strong>g>and</str<strong>on</strong>g> <str<strong>on</strong>g>the</str<strong>on</strong>g> (i − 1)th elements of <str<strong>on</strong>g>the</str<strong>on</strong>g> p-list of jump(x, 2 i )—i.e.,middle_link(pred(x, i)) = pred(jump(x, 2 i ), i),left_link(pred(x, i + 1)) = pred(jump(x, 2 i+1 ), i).Observe that <strong>on</strong>ly <strong>on</strong>e of <str<strong>on</strong>g>the</str<strong>on</strong>g> three link pointers is actually required—e.g., <str<strong>on</strong>g>the</str<strong>on</strong>g> right-up link can be directly computedfrom <str<strong>on</strong>g>the</str<strong>on</strong>g> middle-up link. We maintain <str<strong>on</strong>g>the</str<strong>on</strong>g> three links <str<strong>on</strong>g>for</str<strong>on</strong>g> <str<strong>on</strong>g>the</str<strong>on</strong>g> sake of clarity of presentati<strong>on</strong>.4.3.1.2. Management of <str<strong>on</strong>g>the</str<strong>on</strong>g> p-list. The management of <str<strong>on</strong>g>the</str<strong>on</strong>g> p-lists is somewhat involved. Nothing special needs to bed<strong>on</strong>e whenever create_tree <str<strong>on</strong>g>and</str<strong>on</strong>g> remove are per<str<strong>on</strong>g>for</str<strong>on</strong>g>med—observe that by definiti<strong>on</strong> <str<strong>on</strong>g>the</str<strong>on</strong>g> remove operati<strong>on</strong> is applied<strong>on</strong>ly to leaves of <str<strong>on</strong>g>the</str<strong>on</strong>g> tree, <str<strong>on</strong>g>and</str<strong>on</strong>g> it will not affect <str<strong>on</strong>g>the</str<strong>on</strong>g> p-lists of o<str<strong>on</strong>g>the</str<strong>on</strong>g>r nodes. Let us show how we can create <str<strong>on</strong>g>the</str<strong>on</strong>g> p-list <str<strong>on</strong>g>for</str<strong>on</strong>g>each new node created when an exp<str<strong>on</strong>g>and</str<strong>on</strong>g> operati<strong>on</strong> is per<str<strong>on</strong>g>for</str<strong>on</strong>g>med.Let us c<strong>on</strong>sider <str<strong>on</strong>g>the</str<strong>on</strong>g> executi<strong>on</strong> of exp<str<strong>on</strong>g>and</str<strong>on</strong>g>(x,y,z). Let us focus <strong>on</strong> <str<strong>on</strong>g>the</str<strong>on</strong>g> creati<strong>on</strong> of y as a child of node x (<str<strong>on</strong>g>the</str<strong>on</strong>g> creati<strong>on</strong>of z is basically identical). The c<strong>on</strong>structi<strong>on</strong> can be accomplished by taking advantage of <str<strong>on</strong>g>the</str<strong>on</strong>g> properties of <str<strong>on</strong>g>the</str<strong>on</strong>g> right-uplinks. The creati<strong>on</strong> of a new p-list can be obtained by simply following <str<strong>on</strong>g>the</str<strong>on</strong>g> appropriate right-up links <str<strong>on</strong>g>and</str<strong>on</strong>g> copying <str<strong>on</strong>g>the</str<strong>on</strong>g>p-list elements encountered. In particular, <str<strong>on</strong>g>the</str<strong>on</strong>g> creati<strong>on</strong> of <str<strong>on</strong>g>the</str<strong>on</strong>g> p-list <str<strong>on</strong>g>for</str<strong>on</strong>g> y can be per<str<strong>on</strong>g>for</str<strong>on</strong>g>med as follows:1. The first element of <str<strong>on</strong>g>the</str<strong>on</strong>g> p-list <str<strong>on</strong>g>for</str<strong>on</strong>g> y c<strong>on</strong>tains(a) A pointer to <str<strong>on</strong>g>the</str<strong>on</strong>g> node x in <str<strong>on</strong>g>the</str<strong>on</strong>g> data field, i.e.,data(first(y)) = x.(b) A pointer to <str<strong>on</strong>g>the</str<strong>on</strong>g> sec<strong>on</strong>d node in <str<strong>on</strong>g>the</str<strong>on</strong>g> p-list of x in <str<strong>on</strong>g>the</str<strong>on</strong>g> right-up link field, i.e.,right_link(first(y)) = pred(x, 1).(c) A pointer to <str<strong>on</strong>g>the</str<strong>on</strong>g> first element of <str<strong>on</strong>g>the</str<strong>on</strong>g> p-list of x in <str<strong>on</strong>g>the</str<strong>on</strong>g> middle-up link field, i.e.,middle_link(first(y)) = first(x).(d) A nil pointer in <str<strong>on</strong>g>the</str<strong>on</strong>g> left-up link field.2. All <str<strong>on</strong>g>the</str<strong>on</strong>g> o<str<strong>on</strong>g>the</str<strong>on</strong>g>r elements of <str<strong>on</strong>g>the</str<strong>on</strong>g> p-list <str<strong>on</strong>g>for</str<strong>on</strong>g> y can be obtained essentially by following <str<strong>on</strong>g>the</str<strong>on</strong>g> right-up link list of p-listelements, starting with <str<strong>on</strong>g>the</str<strong>on</strong>g> first element of <str<strong>on</strong>g>the</str<strong>on</strong>g> p-list of x <str<strong>on</strong>g>and</str<strong>on</strong>g> copying each <strong>on</strong>e of <str<strong>on</strong>g>the</str<strong>on</strong>g>m. The appropriate right-up,middle-up, <str<strong>on</strong>g>and</str<strong>on</strong>g> left-up links <str<strong>on</strong>g>for</str<strong>on</strong>g> this new list can be calculated simultaneously, as illustrated in <str<strong>on</strong>g>the</str<strong>on</strong>g> algorithm inFig. 5.


116 A. D. Palú et al. / Theoretical Computer Science 352 (2006) 108 – 135Fig. 5. Computati<strong>on</strong> of right-up links.Observe that this c<strong>on</strong>structi<strong>on</strong> can be improved; if <str<strong>on</strong>g>the</str<strong>on</strong>g> newly created node y already has a sibling z, <str<strong>on</strong>g>the</str<strong>on</strong>g>n <str<strong>on</strong>g>the</str<strong>on</strong>g> p-listsof y <str<strong>on</strong>g>and</str<strong>on</strong>g> z are identical <str<strong>on</strong>g>and</str<strong>on</strong>g> can be shared. Similarly, if two nodes y <str<strong>on</strong>g>and</str<strong>on</strong>g> z are at <str<strong>on</strong>g>the</str<strong>on</strong>g> same level <str<strong>on</strong>g>and</str<strong>on</strong>g> jump(y, 2 i ) =jump(z, 2 i ), <str<strong>on</strong>g>the</str<strong>on</strong>g>npred(y, j) = pred(z, j)<str<strong>on</strong>g>for</str<strong>on</strong>g> j i. Never<str<strong>on</strong>g>the</str<strong>on</strong>g>less, it is unclear whe<str<strong>on</strong>g>the</str<strong>on</strong>g>r such optimizati<strong>on</strong>s may lead to an improved asymptotic space complexity.Example 1. C<strong>on</strong>sider <str<strong>on</strong>g>the</str<strong>on</strong>g> tree in Fig. 6 1 <str<strong>on</strong>g>and</str<strong>on</strong>g> let us assume that a new node (node 10) is inserted as right child of 9.The figure shows <str<strong>on</strong>g>the</str<strong>on</strong>g> p-lists of <str<strong>on</strong>g>the</str<strong>on</strong>g> existing nodes as well as <str<strong>on</strong>g>the</str<strong>on</strong>g> p-list of <str<strong>on</strong>g>the</str<strong>on</strong>g> new node. This has been created applying<str<strong>on</strong>g>the</str<strong>on</strong>g> algorithm described earlier. The first element of <str<strong>on</strong>g>the</str<strong>on</strong>g> p-list c<strong>on</strong>tains a pointer to parent 9; <str<strong>on</strong>g>the</str<strong>on</strong>g> rest of <str<strong>on</strong>g>the</str<strong>on</strong>g> p-list is acopy of <str<strong>on</strong>g>the</str<strong>on</strong>g> list made by right-up links <str<strong>on</strong>g>and</str<strong>on</strong>g> starting from <str<strong>on</strong>g>the</str<strong>on</strong>g> first element of <str<strong>on</strong>g>the</str<strong>on</strong>g> p-list of 9 (such list is shown usingsolid lines in <str<strong>on</strong>g>the</str<strong>on</strong>g> figure). The right-up links of <str<strong>on</strong>g>the</str<strong>on</strong>g> new elements are shown as dashed lines.Note that <str<strong>on</strong>g>the</str<strong>on</strong>g> creati<strong>on</strong> of <str<strong>on</strong>g>the</str<strong>on</strong>g> new p-list requires Θ(lg h) time, where h is <str<strong>on</strong>g>the</str<strong>on</strong>g> depth of <str<strong>on</strong>g>the</str<strong>on</strong>g> new node. Hence, <str<strong>on</strong>g>the</str<strong>on</strong>g>exp<str<strong>on</strong>g>and</str<strong>on</strong>g> operati<strong>on</strong> takes time Θ(lg h). The correctness of <str<strong>on</strong>g>the</str<strong>on</strong>g> algorithm follows by observing that <str<strong>on</strong>g>the</str<strong>on</strong>g> data field ofpred(y, i + 1) is copied from <str<strong>on</strong>g>the</str<strong>on</strong>g> ith element of <str<strong>on</strong>g>the</str<strong>on</strong>g> p-list of <str<strong>on</strong>g>the</str<strong>on</strong>g> node pointed to by <str<strong>on</strong>g>the</str<strong>on</strong>g> data field of <str<strong>on</strong>g>the</str<strong>on</strong>g> ith element of<str<strong>on</strong>g>the</str<strong>on</strong>g> p-list of y, i.e.,data(pred(y, i + 1)) = data(pred(data(pred(y, i)), i)).Noting that jump(jump(y, 2 i ), 2 i ) = jump(y, 2 i+1 ), it is easy to establish inductively that <str<strong>on</strong>g>the</str<strong>on</strong>g> data fields of <str<strong>on</strong>g>the</str<strong>on</strong>g> p-listof y are correctly computed. The fact that <str<strong>on</strong>g>the</str<strong>on</strong>g> right-up, middle-up, <str<strong>on</strong>g>and</str<strong>on</strong>g> left-up links are correctly computed is alsostraight<str<strong>on</strong>g>for</str<strong>on</strong>g>ward to establish.4.3.2. Computing <str<strong>on</strong>g>the</str<strong>on</strong>g> ncaWe subdivide <str<strong>on</strong>g>the</str<strong>on</strong>g> process of computing nca(x, y) into two sub<str<strong>on</strong>g>problem</str<strong>on</strong>g>s: (i) determine <str<strong>on</strong>g>the</str<strong>on</strong>g> nearest comm<strong>on</strong> ancestorunder <str<strong>on</strong>g>the</str<strong>on</strong>g> assumpti<strong>on</strong> that x <str<strong>on</strong>g>and</str<strong>on</strong>g> y are at <str<strong>on</strong>g>the</str<strong>on</strong>g> same depth; (ii) given x <str<strong>on</strong>g>and</str<strong>on</strong>g> y such that depth(x) < depth(y) determine <str<strong>on</strong>g>the</str<strong>on</strong>g>ancestor y ′ of y such that depth(y ′ ) = depth(x). It is clear that <str<strong>on</strong>g>the</str<strong>on</strong>g> ability to solve <str<strong>on</strong>g>the</str<strong>on</strong>g>se two sub<str<strong>on</strong>g>problem</str<strong>on</strong>g>s will providea general soluti<strong>on</strong> to <str<strong>on</strong>g>the</str<strong>on</strong>g> task of determining nca(x, y) <str<strong>on</strong>g>for</str<strong>on</strong>g> arbitrary nodes x,y. Below we provide a O(lg h) soluti<strong>on</strong><str<strong>on</strong>g>for</str<strong>on</strong>g> both <str<strong>on</strong>g>the</str<strong>on</strong>g>se sub<str<strong>on</strong>g>problem</str<strong>on</strong>g>s. This gives us a O(lg h) soluti<strong>on</strong> <str<strong>on</strong>g>for</str<strong>on</strong>g> <str<strong>on</strong>g>the</str<strong>on</strong>g> <str<strong>on</strong>g>NCA</str<strong>on</strong>g><str<strong>on</strong>g>problem</str<strong>on</strong>g>.4.3.2.1. Determining nca <str<strong>on</strong>g>for</str<strong>on</strong>g> same depth nodes. We will make use of <str<strong>on</strong>g>the</str<strong>on</strong>g> elements of <str<strong>on</strong>g>the</str<strong>on</strong>g> p-list. Let us assume x <str<strong>on</strong>g>and</str<strong>on</strong>g> yto be two nodes that are at <str<strong>on</strong>g>the</str<strong>on</strong>g> same depth (h) in <str<strong>on</strong>g>the</str<strong>on</strong>g> tree. Then, <str<strong>on</strong>g>the</str<strong>on</strong>g> p-lists of x <str<strong>on</strong>g>and</str<strong>on</strong>g> y each c<strong>on</strong>tain ⌊lg h⌋+1 elements.The computati<strong>on</strong> of nca(x, y) is per<str<strong>on</strong>g>for</str<strong>on</strong>g>med through <str<strong>on</strong>g>the</str<strong>on</strong>g> algorithm in Fig. 7.1 The figure shows <strong>on</strong>ly <str<strong>on</strong>g>the</str<strong>on</strong>g> nodes <strong>on</strong> <str<strong>on</strong>g>the</str<strong>on</strong>g> path from <str<strong>on</strong>g>the</str<strong>on</strong>g> root to <str<strong>on</strong>g>the</str<strong>on</strong>g> new node.


A. D. Palú et al. / Theoretical Computer Science 352 (2006) 108 – 135 11712132 143 254 3 165 4 27 6 5 38 7 6 498107 5 19 8 6 2Fig. 6. Creati<strong>on</strong> of p-list: example.Fig. 7. Computati<strong>on</strong> of <str<strong>on</strong>g>the</str<strong>on</strong>g> nca.The idea behind <str<strong>on</strong>g>the</str<strong>on</strong>g> algorithm is to locate <str<strong>on</strong>g>the</str<strong>on</strong>g> nearest comm<strong>on</strong> ancestor of two nodes by per<str<strong>on</strong>g>for</str<strong>on</strong>g>ming successive“jumps” in <str<strong>on</strong>g>the</str<strong>on</strong>g> tree, making use of <str<strong>on</strong>g>the</str<strong>on</strong>g> pointers stored in <str<strong>on</strong>g>the</str<strong>on</strong>g> p-list of <str<strong>on</strong>g>the</str<strong>on</strong>g> two nodes. The first loop (lines 1–8) is usedto deal with <str<strong>on</strong>g>the</str<strong>on</strong>g> special case where <str<strong>on</strong>g>the</str<strong>on</strong>g> nca lies in <str<strong>on</strong>g>the</str<strong>on</strong>g> highest part of <str<strong>on</strong>g>the</str<strong>on</strong>g> tree (above <str<strong>on</strong>g>the</str<strong>on</strong>g> highest node pointed by <str<strong>on</strong>g>the</str<strong>on</strong>g>p-list). The loop in lines 10–13 compares ancestors of <str<strong>on</strong>g>the</str<strong>on</strong>g> two nodes, starting from <str<strong>on</strong>g>the</str<strong>on</strong>g> ancestor in <str<strong>on</strong>g>the</str<strong>on</strong>g> p-list which isfar<str<strong>on</strong>g>the</str<strong>on</strong>g>r away from <str<strong>on</strong>g>the</str<strong>on</strong>g> nodes x <str<strong>on</strong>g>and</str<strong>on</strong>g> y. During <str<strong>on</strong>g>the</str<strong>on</strong>g> successive iterati<strong>on</strong> of this first loop we compare <str<strong>on</strong>g>the</str<strong>on</strong>g> nodes jump(x, 2 i )


118 A. D. Palú et al. / Theoretical Computer Science 352 (2006) 108 – 135nca(x,y)nca(x,y)xyi+1 i i i+1newxnewyi+1 ii i+1Fig. 8. Searching <str<strong>on</strong>g>for</str<strong>on</strong>g> nearest comm<strong>on</strong> ancestor.Fig. 9. Determining equal height ancestor.<str<strong>on</strong>g>and</str<strong>on</strong>g> jump(y, 2 i ) <str<strong>on</strong>g>for</str<strong>on</strong>g> successively decreasing values of i (to avoid <str<strong>on</strong>g>the</str<strong>on</strong>g> use of arithmetic, we are simply moving pointersin <str<strong>on</strong>g>the</str<strong>on</strong>g> p-lists of x <str<strong>on</strong>g>and</str<strong>on</strong>g> y). The loop c<strong>on</strong>tinues as l<strong>on</strong>g as <str<strong>on</strong>g>the</str<strong>on</strong>g> nodes reached are equal. All <str<strong>on</strong>g>the</str<strong>on</strong>g>se are comm<strong>on</strong> ancestors<str<strong>on</strong>g>for</str<strong>on</strong>g> x <str<strong>on</strong>g>and</str<strong>on</strong>g> y. As so<strong>on</strong> as we reach two ancestors jump(x, 2 i ) <str<strong>on</strong>g>and</str<strong>on</strong>g> jump(y, 2 i ) that are different (see Fig. 8 <strong>on</strong> <str<strong>on</strong>g>the</str<strong>on</strong>g> left)we can verify whe<str<strong>on</strong>g>the</str<strong>on</strong>g>r <str<strong>on</strong>g>the</str<strong>on</strong>g> nearest comm<strong>on</strong> ancestor has been reached or not at <str<strong>on</strong>g>the</str<strong>on</strong>g> previous step (jump(x, 2 i+1 )). Thistest can be easily per<str<strong>on</strong>g>for</str<strong>on</strong>g>med by checking if <str<strong>on</strong>g>the</str<strong>on</strong>g> two nodes reached at jump i have <str<strong>on</strong>g>the</str<strong>on</strong>g> same parent (line 14).If <str<strong>on</strong>g>the</str<strong>on</strong>g> ancestor jump(x, 2 i+1 ) is not <str<strong>on</strong>g>the</str<strong>on</strong>g> nearest comm<strong>on</strong> ancestor (see Fig. 8 <strong>on</strong> <str<strong>on</strong>g>the</str<strong>on</strong>g> right), <str<strong>on</strong>g>the</str<strong>on</strong>g>n <str<strong>on</strong>g>the</str<strong>on</strong>g> algorithm repeats<str<strong>on</strong>g>the</str<strong>on</strong>g> computati<strong>on</strong> by replacing <str<strong>on</strong>g>the</str<strong>on</strong>g> nodes x <str<strong>on</strong>g>and</str<strong>on</strong>g> y, respectively with jump(x, 2 i ) <str<strong>on</strong>g>and</str<strong>on</strong>g> jump(y, 2 i ). From <str<strong>on</strong>g>the</str<strong>on</strong>g> tests madeearlier, we can limit <str<strong>on</strong>g>the</str<strong>on</strong>g> search to <str<strong>on</strong>g>the</str<strong>on</strong>g> ancestors of distance up to 2 i —<str<strong>on</strong>g>and</str<strong>on</strong>g> this is accomplished by starting a new iterati<strong>on</strong>of <str<strong>on</strong>g>the</str<strong>on</strong>g> loop in line 9 not by taking <str<strong>on</strong>g>the</str<strong>on</strong>g> last element of <str<strong>on</strong>g>the</str<strong>on</strong>g> p-lists of <str<strong>on</strong>g>the</str<strong>on</strong>g> new x <str<strong>on</strong>g>and</str<strong>on</strong>g> y but by taking <str<strong>on</strong>g>the</str<strong>on</strong>g> p-list elementspointed by <str<strong>on</strong>g>the</str<strong>on</strong>g> left-up links (i.e., maintain <str<strong>on</strong>g>the</str<strong>on</strong>g> same jump distance). C<strong>on</strong>sidering that <str<strong>on</strong>g>the</str<strong>on</strong>g> number of iterati<strong>on</strong>s per<str<strong>on</strong>g>for</str<strong>on</strong>g>medis limited by <str<strong>on</strong>g>the</str<strong>on</strong>g> length of <str<strong>on</strong>g>the</str<strong>on</strong>g> l<strong>on</strong>gest p-list in <str<strong>on</strong>g>the</str<strong>on</strong>g> tree (that is O(lg h) <str<strong>on</strong>g>for</str<strong>on</strong>g> a tree of height h), <str<strong>on</strong>g>the</str<strong>on</strong>g>n we can c<strong>on</strong>clude that<str<strong>on</strong>g>the</str<strong>on</strong>g> algorithm has a complexity of O(lg h).4.3.2.2. Determining equal height ancestor. Fig. 9 provides a procedure that, given nodes x,y such that depth(x)


A. D. Palú et al. / Theoretical Computer Science 352 (2006) 108 – 135 119has a pointer to this p-list. Of course each of a 1 ,a 2 ,...,a r has a parent pointer to x. The time <str<strong>on</strong>g>for</str<strong>on</strong>g> this operati<strong>on</strong> <str<strong>on</strong>g>the</str<strong>on</strong>g>nclearly is O(r + lg h). Note that since <str<strong>on</strong>g>the</str<strong>on</strong>g> p-list is created <strong>on</strong>ly <strong>on</strong>ce this avoids <str<strong>on</strong>g>the</str<strong>on</strong>g> Ω(r lg h) cost that <str<strong>on</strong>g>the</str<strong>on</strong>g> naive methodwill have to incur. If we think of exp<str<strong>on</strong>g>and</str<strong>on</strong>g> operati<strong>on</strong> as adding leaves each add-leaf operati<strong>on</strong> still takes <strong>on</strong>ly O(lg h)time. In fact, <str<strong>on</strong>g>the</str<strong>on</strong>g> Θ(lg h) time is spent <strong>on</strong>ly <str<strong>on</strong>g>for</str<strong>on</strong>g> <str<strong>on</strong>g>the</str<strong>on</strong>g> first add-leaf operati<strong>on</strong> at any node, <str<strong>on</strong>g>the</str<strong>on</strong>g>reafter each add-leaf takes<strong>on</strong>ly O(1).The nca(x,y) calculati<strong>on</strong> will work essentially as be<str<strong>on</strong>g>for</str<strong>on</strong>g>e expect that <str<strong>on</strong>g>the</str<strong>on</strong>g> procedure will terminate <strong>on</strong>ly when ancestorx ′ of x <str<strong>on</strong>g>and</str<strong>on</strong>g> y ′ of y are found such that parent(x ′ ) = parent(y ′ ). The running time still remains O(lg h).Observe also that this soluti<strong>on</strong> has a space requirement of O(n lg h) <str<strong>on</strong>g>for</str<strong>on</strong>g> a tree with n nodes <str<strong>on</strong>g>and</str<strong>on</strong>g> height h.4.3.3. Lower bound time complexityWe can prove a lower bound time complexity <str<strong>on</strong>g>for</str<strong>on</strong>g> this <str<strong>on</strong>g>problem</str<strong>on</strong>g> <strong>on</strong> PPMs as follows. Let us assume that we havea soluti<strong>on</strong> to <str<strong>on</strong>g>the</str<strong>on</strong>g> <str<strong>on</strong>g>NCA</str<strong>on</strong>g> <str<strong>on</strong>g>problem</str<strong>on</strong>g> <str<strong>on</strong>g>and</str<strong>on</strong>g> let us show how we can use it to build a soluti<strong>on</strong> <str<strong>on</strong>g>for</str<strong>on</strong>g> <str<strong>on</strong>g>the</str<strong>on</strong>g> TP <str<strong>on</strong>g>problem</str<strong>on</strong>g>. TheTP <str<strong>on</strong>g>problem</str<strong>on</strong>g> is <str<strong>on</strong>g>the</str<strong>on</strong>g> <str<strong>on</strong>g>problem</str<strong>on</strong>g> of per<str<strong>on</strong>g>for</str<strong>on</strong>g>ming an <strong>on</strong>-line sequence of operati<strong>on</strong>s, <str<strong>on</strong>g>and</str<strong>on</strong>g> <str<strong>on</strong>g>the</str<strong>on</strong>g> <strong>on</strong>ly two allowed operati<strong>on</strong>sare insert(x)—that adds <str<strong>on</strong>g>the</str<strong>on</strong>g> element x to a collecti<strong>on</strong> of elements—<str<strong>on</strong>g>and</str<strong>on</strong>g> precedes(x, y)—that verifies whe<str<strong>on</strong>g>the</str<strong>on</strong>g>r <str<strong>on</strong>g>the</str<strong>on</strong>g>element x has been inserted be<str<strong>on</strong>g>for</str<strong>on</strong>g>e <str<strong>on</strong>g>the</str<strong>on</strong>g> element y.In[21] we have proved that <str<strong>on</strong>g>the</str<strong>on</strong>g> TP<str<strong>on</strong>g>problem</str<strong>on</strong>g> has a lower bound timecomplexity of Ω(lg lg n) per operati<strong>on</strong>, where n is <str<strong>on</strong>g>the</str<strong>on</strong>g> number of elements inserted. This allows us to c<strong>on</strong>clude <str<strong>on</strong>g>the</str<strong>on</strong>g>following result:Theorem 2. The <str<strong>on</strong>g>NCA</str<strong>on</strong>g> <str<strong>on</strong>g>problem</str<strong>on</strong>g> has a lower bound time complexity of Ω(lg lg n), where n is <str<strong>on</strong>g>the</str<strong>on</strong>g> number of nodes in<str<strong>on</strong>g>the</str<strong>on</strong>g> tree.Proof. Let us implement <str<strong>on</strong>g>the</str<strong>on</strong>g> two operati<strong>on</strong>s of <str<strong>on</strong>g>the</str<strong>on</strong>g> TP<str<strong>on</strong>g>problem</str<strong>on</strong>g> using <str<strong>on</strong>g>the</str<strong>on</strong>g> operati<strong>on</strong>s available in <str<strong>on</strong>g>the</str<strong>on</strong>g> <str<strong>on</strong>g>NCA</str<strong>on</strong>g><str<strong>on</strong>g>problem</str<strong>on</strong>g>.The overall idea is to insert elements in a tree which is composed <strong>on</strong>ly by a right spine; each element inserted isa new leaf created down <str<strong>on</strong>g>the</str<strong>on</strong>g> right spine. Each precedes operati<strong>on</strong> can be realized as follows: (precedes(x, y) ⇔nca(x, y) = x). This allows us to c<strong>on</strong>clude that <str<strong>on</strong>g>the</str<strong>on</strong>g> <str<strong>on</strong>g>NCA</str<strong>on</strong>g><str<strong>on</strong>g>problem</str<strong>on</strong>g> has a lower bound time complexity of Ω(lg lg n)as well (where n is <str<strong>on</strong>g>the</str<strong>on</strong>g> number of nodes in <str<strong>on</strong>g>the</str<strong>on</strong>g> tree). □5. A sequential algorithm <str<strong>on</strong>g>for</str<strong>on</strong>g> <str<strong>on</strong>g>NCA</str<strong>on</strong>g> queries in <str<strong>on</strong>g>the</str<strong>on</strong>g> static caseIn <str<strong>on</strong>g>the</str<strong>on</strong>g> next secti<strong>on</strong>s, as in [1], we use a more general definiti<strong>on</strong> of nca: nca(x, y) = (z, z x ,z y ), where z is <str<strong>on</strong>g>the</str<strong>on</strong>g> ncaof x <str<strong>on</strong>g>and</str<strong>on</strong>g> y, <str<strong>on</strong>g>and</str<strong>on</strong>g> if x = z (y = z) <str<strong>on</strong>g>the</str<strong>on</strong>g>n z x = z (z y = z) else z x (z y ) is <str<strong>on</strong>g>the</str<strong>on</strong>g> ancestor of x (y) such that z x (z y ) is a child of z.In <str<strong>on</strong>g>the</str<strong>on</strong>g> static case T is pre-processed be<str<strong>on</strong>g>for</str<strong>on</strong>g>e any query is executed. C<strong>on</strong>ceptually, <str<strong>on</strong>g>the</str<strong>on</strong>g> pre-processing creates 2k + 1trees, named T 0 ,T1 L,T 1,T2 L,...,TLk ,T k. The T 0 tree is equal to T <str<strong>on</strong>g>and</str<strong>on</strong>g> each o<str<strong>on</strong>g>the</str<strong>on</strong>g>r tree is <str<strong>on</strong>g>the</str<strong>on</strong>g> result of <str<strong>on</strong>g>the</str<strong>on</strong>g> corresp<strong>on</strong>dingcompressi<strong>on</strong>. To improve <str<strong>on</strong>g>the</str<strong>on</strong>g> time <str<strong>on</strong>g>and</str<strong>on</strong>g> space required, T i <str<strong>on</strong>g>and</str<strong>on</strong>g> TiL trees are not explicitly created. Each time <strong>on</strong>ly <str<strong>on</strong>g>the</str<strong>on</strong>g>nodes being encountered <str<strong>on</strong>g>for</str<strong>on</strong>g> <str<strong>on</strong>g>the</str<strong>on</strong>g> first time are created anew, except that during a path compressi<strong>on</strong> a new node is created<str<strong>on</strong>g>for</str<strong>on</strong>g> <str<strong>on</strong>g>the</str<strong>on</strong>g> head of <str<strong>on</strong>g>the</str<strong>on</strong>g> path. H is composed of <str<strong>on</strong>g>the</str<strong>on</strong>g> uni<strong>on</strong> of all nodes created during <str<strong>on</strong>g>the</str<strong>on</strong>g> various compressi<strong>on</strong> phases.5.1. Data structuresFor each w in T, let entry(w) be <str<strong>on</strong>g>the</str<strong>on</strong>g> entry point of w in H. Note that each node in a tree T i or TiL is a copy of a nodeexisting in T; if <str<strong>on</strong>g>the</str<strong>on</strong>g> node v ∈ H is a copy of node u ∈ T , <str<strong>on</strong>g>the</str<strong>on</strong>g>n node(v) is a pointer to u. Let children(v) be a pointer toa list of nodes c<strong>on</strong>taining <str<strong>on</strong>g>the</str<strong>on</strong>g> children of node v in H, <str<strong>on</strong>g>and</str<strong>on</strong>g> let parent(v) denote <str<strong>on</strong>g>the</str<strong>on</strong>g> parent of node v in H. During <str<strong>on</strong>g>the</str<strong>on</strong>g>pre-processing phase, we will also make use of two flags associated to each node of H:(1) A flag leaf-compr(v) that indicates whe<str<strong>on</strong>g>the</str<strong>on</strong>g>r <str<strong>on</strong>g>the</str<strong>on</strong>g> node v is <str<strong>on</strong>g>the</str<strong>on</strong>g> result of a leaf compressi<strong>on</strong>.(2) A flag is-leaf (v) that indicates if <str<strong>on</strong>g>the</str<strong>on</strong>g> representative produced by a leaf compressi<strong>on</strong> is a leaf in <str<strong>on</strong>g>the</str<strong>on</strong>g> new tree.5.2. C<strong>on</strong>structi<strong>on</strong> of <str<strong>on</strong>g>the</str<strong>on</strong>g> H-treeTo answer <str<strong>on</strong>g>the</str<strong>on</strong>g> <str<strong>on</strong>g>NCA</str<strong>on</strong>g> queries, we require <str<strong>on</strong>g>the</str<strong>on</strong>g> ability to efficiently compare <str<strong>on</strong>g>the</str<strong>on</strong>g> depth of nodes that appear <strong>on</strong> <str<strong>on</strong>g>the</str<strong>on</strong>g> samebranch. This can be accomplished by making use of <str<strong>on</strong>g>the</str<strong>on</strong>g> data structures developed to solve <str<strong>on</strong>g>the</str<strong>on</strong>g> TP<str<strong>on</strong>g>problem</str<strong>on</strong>g> [21,17].


120 A. D. Palú et al. / Theoretical Computer Science 352 (2006) 108 – 135Using <str<strong>on</strong>g>the</str<strong>on</strong>g> optimal scheme from P<strong>on</strong>telli <str<strong>on</strong>g>and</str<strong>on</strong>g> Ranjan [17], we can build a data structure in O(n) time that allows us tocompare depths in O(lg lg n) time.The pre-processing algorithm used to c<strong>on</strong>struct H is described in Fig. 10. With <strong>on</strong>e visit of T, <strong>on</strong>e can create <str<strong>on</strong>g>the</str<strong>on</strong>g>leaves of T 0 , by simply copying each leaf v of T to a new node u <str<strong>on</strong>g>and</str<strong>on</strong>g> updating both entry(v) = u <str<strong>on</strong>g>and</str<strong>on</strong>g> node(u) = v(lines 1–4 of Fig. 10). After this, <str<strong>on</strong>g>the</str<strong>on</strong>g> process proceeds by repeatedly applying leaf-compressi<strong>on</strong> (lines 7–19) <str<strong>on</strong>g>and</str<strong>on</strong>g>path-compressi<strong>on</strong>. (lines 21–42). The process stops as so<strong>on</strong> as we are left with a tree c<strong>on</strong>taining a single node. Thelast T k ,k lg(n), has <strong>on</strong>ly <strong>on</strong>e node.Lemma 2. The time required to c<strong>on</strong>struct H is O(n), where n is <str<strong>on</strong>g>the</str<strong>on</strong>g> number of nodes in T.Proof. Let us start by observing that each node v in T appears at most 2 times in H (<strong>on</strong>ce as representative of aleaf-compressi<strong>on</strong> <str<strong>on</strong>g>and</str<strong>on</strong>g> <strong>on</strong>ce as a representative of a path-compressi<strong>on</strong>). Let us call c(v) <str<strong>on</strong>g>the</str<strong>on</strong>g> number of children of v inT. Each node v can be involved in:• at most 1 leaf compressi<strong>on</strong> where v is a leaf;• at most 1 path compressi<strong>on</strong>.As we will show in <str<strong>on</strong>g>the</str<strong>on</strong>g> successive two subsecti<strong>on</strong>s, each individual leaf- <str<strong>on</strong>g>and</str<strong>on</strong>g> path-compressi<strong>on</strong> can be per<str<strong>on</strong>g>for</str<strong>on</strong>g>med inworst-case time complexity O(1) per node.Observe that during a leaf-compressi<strong>on</strong>, each node v can ei<str<strong>on</strong>g>the</str<strong>on</strong>g>r• be compressed to its parent;• be <str<strong>on</strong>g>the</str<strong>on</strong>g> direct representative of any of its (c(v)) children.By distributing <str<strong>on</strong>g>the</str<strong>on</strong>g> cost of leaf-compressi<strong>on</strong>s to <str<strong>on</strong>g>the</str<strong>on</strong>g> leaf nodes, <str<strong>on</strong>g>the</str<strong>on</strong>g> amortized cost of leaf compressi<strong>on</strong>s is O(1) pernode.Observe now what happens during a path compressi<strong>on</strong>; let us assume that node v gets path-compressed to a representativew:• if v ̸= w, <str<strong>on</strong>g>the</str<strong>on</strong>g>n v will not be present in <str<strong>on</strong>g>the</str<strong>on</strong>g> T i tree obtained from <str<strong>on</strong>g>the</str<strong>on</strong>g> path compressi<strong>on</strong>;• if v = w, <str<strong>on</strong>g>the</str<strong>on</strong>g>n v will appear as a leaf in <str<strong>on</strong>g>the</str<strong>on</strong>g> T i tree resulting from <str<strong>on</strong>g>the</str<strong>on</strong>g> path-compressi<strong>on</strong>; this also means that v willbe removed at <str<strong>on</strong>g>the</str<strong>on</strong>g> next step (when v is leaf-compressed).As be<str<strong>on</strong>g>for</str<strong>on</strong>g>e, by distributing <str<strong>on</strong>g>the</str<strong>on</strong>g> cost of path compressi<strong>on</strong> to <str<strong>on</strong>g>the</str<strong>on</strong>g> nodes involved, <str<strong>on</strong>g>the</str<strong>on</strong>g> amortized cost is O(1) per node.Hence, <str<strong>on</strong>g>the</str<strong>on</strong>g> total cost over all compressi<strong>on</strong>s is at most ∑ v∈T (1 + 1) = 2n. □5.3. Leaf compressi<strong>on</strong>For each leaf v in T i , a leaf-compressi<strong>on</strong> is applied.A new representative w <str<strong>on</strong>g>for</str<strong>on</strong>g> <str<strong>on</strong>g>the</str<strong>on</strong>g> parent of v in T (entry(parent(v)) =w) is added to <str<strong>on</strong>g>the</str<strong>on</strong>g> tree Ti+1 L , if this is <str<strong>on</strong>g>the</str<strong>on</strong>g> first time a node is compressed to w (lines 8–12). After this check, <str<strong>on</strong>g>the</str<strong>on</strong>g> nodev is compressed to its parent w (lines 13,14). Once all <str<strong>on</strong>g>the</str<strong>on</strong>g> leaves of T i are processed, <str<strong>on</strong>g>for</str<strong>on</strong>g> each node w in Ti+1 L weset <str<strong>on</strong>g>the</str<strong>on</strong>g> flag is-leaf (w) = true if w is a leaf in Ti+1 L (lines 15–19). The is-leaf flag indicates <str<strong>on</strong>g>the</str<strong>on</strong>g> nodes where <str<strong>on</strong>g>the</str<strong>on</strong>g> nextpath-compressi<strong>on</strong> will start.Observe that determining <str<strong>on</strong>g>the</str<strong>on</strong>g> leaves of <str<strong>on</strong>g>the</str<strong>on</strong>g> tree can be efficiently per<str<strong>on</strong>g>for</str<strong>on</strong>g>med, e.g., by keeping a copy of T <str<strong>on</strong>g>and</str<strong>on</strong>g>removing nodes from it as <str<strong>on</strong>g>the</str<strong>on</strong>g>y get compressed. In <str<strong>on</strong>g>the</str<strong>on</strong>g> copy of <str<strong>on</strong>g>the</str<strong>on</strong>g> tree we can maintain <str<strong>on</strong>g>the</str<strong>on</strong>g> fr<strong>on</strong>tier as a linked list—intime O(1) <strong>on</strong> a PPM [20].5.4. Path compressi<strong>on</strong>Path-compressi<strong>on</strong> is initiated from each node v in T Li, such that is-leaf (v) = true. We have already menti<strong>on</strong>ed in <str<strong>on</strong>g>the</str<strong>on</strong>g>previous subsecti<strong>on</strong> how this test can be per<str<strong>on</strong>g>for</str<strong>on</strong>g>med in O(1) time. Executi<strong>on</strong> of path-compressi<strong>on</strong> starting from v leadsto <str<strong>on</strong>g>the</str<strong>on</strong>g> additi<strong>on</strong> of a node w to T i+1 . w will be <str<strong>on</strong>g>the</str<strong>on</strong>g> representative of <str<strong>on</strong>g>the</str<strong>on</strong>g> compressed path starting from v (lines 20–24).When <str<strong>on</strong>g>the</str<strong>on</strong>g> path compressi<strong>on</strong> stops, <str<strong>on</strong>g>the</str<strong>on</strong>g> node w will be assigned to <str<strong>on</strong>g>the</str<strong>on</strong>g> correct node in T—i.e., <str<strong>on</strong>g>the</str<strong>on</strong>g> representative of <str<strong>on</strong>g>the</str<strong>on</strong>g>path.


A. D. Palú et al. / Theoretical Computer Science 352 (2006) 108 – 135 121Fig. 10. C<strong>on</strong>structi<strong>on</strong> of <str<strong>on</strong>g>the</str<strong>on</strong>g> H-tree.The following iterati<strong>on</strong> (lines 25–27) assigns to each v in <str<strong>on</strong>g>the</str<strong>on</strong>g> current path <str<strong>on</strong>g>the</str<strong>on</strong>g> direct representative w, <str<strong>on</strong>g>and</str<strong>on</strong>g> triesto extend <str<strong>on</strong>g>the</str<strong>on</strong>g> path leading to <str<strong>on</strong>g>the</str<strong>on</strong>g> root with parent(v), ifparent(v) is still part of <str<strong>on</strong>g>the</str<strong>on</strong>g> path (lines 28–39). When <str<strong>on</strong>g>the</str<strong>on</strong>g>iterati<strong>on</strong> stops, <str<strong>on</strong>g>the</str<strong>on</strong>g> current node v is <str<strong>on</strong>g>the</str<strong>on</strong>g> head of <str<strong>on</strong>g>the</str<strong>on</strong>g> path, <str<strong>on</strong>g>and</str<strong>on</strong>g> its representative w in T i+1 is a copy of <str<strong>on</strong>g>the</str<strong>on</strong>g> node v (lines40–42). The check that verifies whe<str<strong>on</strong>g>the</str<strong>on</strong>g>r a node is part of a path can be implemented in c<strong>on</strong>stant time without <str<strong>on</strong>g>the</str<strong>on</strong>g> useof arithmetic (as described earlier).


122 A. D. Palú et al. / Theoretical Computer Science 352 (2006) 108 – 1355.5. Final pre-processing stepOnce H has been c<strong>on</strong>structed, it is fur<str<strong>on</strong>g>the</str<strong>on</strong>g>r processed to enrich it with <str<strong>on</strong>g>the</str<strong>on</strong>g> data structures required to support <str<strong>on</strong>g>the</str<strong>on</strong>g>computati<strong>on</strong> of <str<strong>on</strong>g>NCA</str<strong>on</strong>g>s in O(lg h) time (h being <str<strong>on</strong>g>the</str<strong>on</strong>g> height of H). The details of <str<strong>on</strong>g>the</str<strong>on</strong>g>se data structures have been describedin [19] <str<strong>on</strong>g>and</str<strong>on</strong>g> in Secti<strong>on</strong> 4.3. The process requires <str<strong>on</strong>g>the</str<strong>on</strong>g> creati<strong>on</strong> of <str<strong>on</strong>g>the</str<strong>on</strong>g> p-list data structure <str<strong>on</strong>g>for</str<strong>on</strong>g> each node in H. Since <str<strong>on</strong>g>the</str<strong>on</strong>g>height of H is at most O(lg n), <str<strong>on</strong>g>the</str<strong>on</strong>g> p-list of each node v c<strong>on</strong>tains at most lg depth(v) elements, <str<strong>on</strong>g>and</str<strong>on</strong>g> each element of a p-listcan be built in O(1) time, <str<strong>on</strong>g>the</str<strong>on</strong>g> process of creating <str<strong>on</strong>g>the</str<strong>on</strong>g>se additi<strong>on</strong>al data structures requires O(n lg lg n) time. Observealso that <str<strong>on</strong>g>the</str<strong>on</strong>g> space requirement <str<strong>on</strong>g>for</str<strong>on</strong>g> this soluti<strong>on</strong> becomes O(n lg lg n). It is possible to improve this pre-processing time,reducing it to O(n), through <str<strong>on</strong>g>the</str<strong>on</strong>g> use of <str<strong>on</strong>g>the</str<strong>on</strong>g> MicroMacroUniverse approach described in [1] <str<strong>on</strong>g>and</str<strong>on</strong>g> in Secti<strong>on</strong> 6: in thiscase H is partiti<strong>on</strong>ed in μTrees (Micro-Trees) with depth at most lg lg n.5.6. Answering <str<strong>on</strong>g>NCA</str<strong>on</strong>g> queriesTo compute <str<strong>on</strong>g>the</str<strong>on</strong>g> nca of x,y ∈ T , <str<strong>on</strong>g>the</str<strong>on</strong>g> algorithm nca H (x, y) works as follows:1. Compute <str<strong>on</strong>g>the</str<strong>on</strong>g> nca(entry(x), entry(y)) = (z, z x ,z y ), z ∈ H . The computati<strong>on</strong> can be per<str<strong>on</strong>g>for</str<strong>on</strong>g>med using <str<strong>on</strong>g>the</str<strong>on</strong>g> algorithmin Secti<strong>on</strong> 4.3.2. If leaf-compr(z) = true <str<strong>on</strong>g>the</str<strong>on</strong>g>n return (node(z), node(z x )node(z y )).3. O<str<strong>on</strong>g>the</str<strong>on</strong>g>rwise z is <str<strong>on</strong>g>the</str<strong>on</strong>g> result of a path compressi<strong>on</strong>. In this case if node(z x ) is higher than node(z y ) in T, <str<strong>on</strong>g>the</str<strong>on</strong>g> nca is(node(z x ), w 1 , w 2 ), where w 1 is <str<strong>on</strong>g>the</str<strong>on</strong>g> node corresp<strong>on</strong>ding to <str<strong>on</strong>g>the</str<strong>on</strong>g> child of z x that is ancestor of entry(x) in H <str<strong>on</strong>g>and</str<strong>on</strong>g> w 2is <str<strong>on</strong>g>the</str<strong>on</strong>g> node in T corresp<strong>on</strong>ding to <str<strong>on</strong>g>the</str<strong>on</strong>g> left sibling of z x in H. w 1 can be obtained by using p-lists in time O(lg lg n)<str<strong>on</strong>g>and</str<strong>on</strong>g> w 2 can be found in c<strong>on</strong>stant time. The case where node(z y ) is higher than node(z x ) is symmetric. The test tocheck if node(z x ) is higher than node(z y ) can be per<str<strong>on</strong>g>for</str<strong>on</strong>g>med in time O(lg lg n) using <str<strong>on</strong>g>the</str<strong>on</strong>g> previously menti<strong>on</strong>ed depthin<str<strong>on</strong>g>for</str<strong>on</strong>g>mati<strong>on</strong>.6. A sequential algorithm <str<strong>on</strong>g>for</str<strong>on</strong>g> <str<strong>on</strong>g>NCA</str<strong>on</strong>g> queries in <str<strong>on</strong>g>the</str<strong>on</strong>g> dynamic caseIn this secti<strong>on</strong>, we present a sequential algorithm to address <str<strong>on</strong>g>the</str<strong>on</strong>g> <str<strong>on</strong>g>NCA</str<strong>on</strong>g> <str<strong>on</strong>g>problem</str<strong>on</strong>g> in <str<strong>on</strong>g>the</str<strong>on</strong>g> case where modificati<strong>on</strong>s of<str<strong>on</strong>g>the</str<strong>on</strong>g> tree structures (as additi<strong>on</strong> <str<strong>on</strong>g>and</str<strong>on</strong>g> deleti<strong>on</strong> of leaves). The method will guarantee <str<strong>on</strong>g>the</str<strong>on</strong>g> following result:Theorem 3. The method offers a O(n+q lg lg n) soluti<strong>on</strong> to <str<strong>on</strong>g>the</str<strong>on</strong>g> <str<strong>on</strong>g>NCA</str<strong>on</strong>g> <str<strong>on</strong>g>problem</str<strong>on</strong>g>, where n is <str<strong>on</strong>g>the</str<strong>on</strong>g> number of add-leaf/deleteoperati<strong>on</strong>s <str<strong>on</strong>g>and</str<strong>on</strong>g> q is <str<strong>on</strong>g>the</str<strong>on</strong>g> number of <str<strong>on</strong>g>NCA</str<strong>on</strong>g> queries.The algorithm follows an approach similar to <str<strong>on</strong>g>the</str<strong>on</strong>g> MicroMacroUniverse described in [1,11] in c<strong>on</strong>juncti<strong>on</strong> withrepeated use of <str<strong>on</strong>g>the</str<strong>on</strong>g> static algorithm described in Secti<strong>on</strong> 5. Let n be <str<strong>on</strong>g>the</str<strong>on</strong>g> number of nodes in T. We c<strong>on</strong>sider two typesof trees (Fig. 11):• The μTrees (Micro-Trees) are trees of a <str<strong>on</strong>g>for</str<strong>on</strong>g>est S of disjoint subtrees partiti<strong>on</strong>ing T.• The MTree (Macro-Tree) is a tree collecting <str<strong>on</strong>g>the</str<strong>on</strong>g> roots of <str<strong>on</strong>g>the</str<strong>on</strong>g> μTrees.The MTree essentially compresses <str<strong>on</strong>g>the</str<strong>on</strong>g> nodes of each μTree into its root node <str<strong>on</strong>g>and</str<strong>on</strong>g> preserves <str<strong>on</strong>g>the</str<strong>on</strong>g> structure of T <strong>on</strong> <str<strong>on</strong>g>the</str<strong>on</strong>g>seresulting root nodes. The height of each μTree is restricted to be at most c T (n). When a node v is added to T, if<str<strong>on</strong>g>the</str<strong>on</strong>g>μTree c<strong>on</strong>taining parent(v) has a depth greater than c T (n), <str<strong>on</strong>g>the</str<strong>on</strong>g>nanewμTree rooted in v is created else <str<strong>on</strong>g>the</str<strong>on</strong>g> node issimply added to <str<strong>on</strong>g>the</str<strong>on</strong>g> appropriate μTree. To obtain <str<strong>on</strong>g>the</str<strong>on</strong>g> optimal result, <str<strong>on</strong>g>the</str<strong>on</strong>g> MicroMacroUniverse approach is applied againto <str<strong>on</strong>g>the</str<strong>on</strong>g> μTrees. For <str<strong>on</strong>g>the</str<strong>on</strong>g> MTree <str<strong>on</strong>g>the</str<strong>on</strong>g> scheme used is based again <strong>on</strong> partiti<strong>on</strong>ing <str<strong>on</strong>g>the</str<strong>on</strong>g> tree into disjoint subtrees. Howeverthis partiti<strong>on</strong>ing is more dynamic in nature, since <str<strong>on</strong>g>the</str<strong>on</strong>g> subtree to which a node bel<strong>on</strong>gs can change.In order to answer an <str<strong>on</strong>g>NCA</str<strong>on</strong>g> query, our algorithm first solves <str<strong>on</strong>g>the</str<strong>on</strong>g> <str<strong>on</strong>g>problem</str<strong>on</strong>g> in <str<strong>on</strong>g>the</str<strong>on</strong>g> MTree <str<strong>on</strong>g>and</str<strong>on</strong>g> <str<strong>on</strong>g>the</str<strong>on</strong>g>n refines <str<strong>on</strong>g>the</str<strong>on</strong>g> soluti<strong>on</strong>by working in <str<strong>on</strong>g>the</str<strong>on</strong>g> appropriate μTree. We denote with nca μ <str<strong>on</strong>g>the</str<strong>on</strong>g> <str<strong>on</strong>g>NCA</str<strong>on</strong>g> algorithm used <str<strong>on</strong>g>for</str<strong>on</strong>g> <str<strong>on</strong>g>the</str<strong>on</strong>g> μTrees in S <str<strong>on</strong>g>and</str<strong>on</strong>g> nca M<str<strong>on</strong>g>the</str<strong>on</strong>g> <str<strong>on</strong>g>NCA</str<strong>on</strong>g> algorithm used <str<strong>on</strong>g>for</str<strong>on</strong>g> <str<strong>on</strong>g>the</str<strong>on</strong>g> MTree. Each time a node v is inserted in T, v is also inserted in a data structure thatcollects <str<strong>on</strong>g>the</str<strong>on</strong>g> relative height in<str<strong>on</strong>g>for</str<strong>on</strong>g>mati<strong>on</strong> of <str<strong>on</strong>g>the</str<strong>on</strong>g> nodes, using a Temporal Precedence list. As described in Secti<strong>on</strong> 5.6, thisin<str<strong>on</strong>g>for</str<strong>on</strong>g>mati<strong>on</strong> will be required to per<str<strong>on</strong>g>for</str<strong>on</strong>g>m an <str<strong>on</strong>g>NCA</str<strong>on</strong>g> query using p-lists.


A. D. Palú et al. / Theoretical Computer Science 352 (2006) 108 – 135 123Tree T <str<strong>on</strong>g>and</str<strong>on</strong>g> µTrees MTree A µTree <str<strong>on</strong>g>and</str<strong>on</strong>g> µµTrees MµTreeCT(n)CS(n)CT(n)CT(n)CS(n) CS(n)MTree <str<strong>on</strong>g>and</str<strong>on</strong>g> µMTreesMMTreemacro(.)micro(.)size(.)root(m)m1 2 k= nodes in mFig. 11. Tree structures involved in <str<strong>on</strong>g>the</str<strong>on</strong>g> dynamic algorithm.6.1. The nca M algorithmIn this secti<strong>on</strong> we present an algorithm to compute <str<strong>on</strong>g>the</str<strong>on</strong>g> nca with a cost of O(N + Q lg lg N), with N add-leaf/deleteoperati<strong>on</strong>s <str<strong>on</strong>g>and</str<strong>on</strong>g> Q <str<strong>on</strong>g>NCA</str<strong>on</strong>g> queries in <str<strong>on</strong>g>the</str<strong>on</strong>g> MTree. The <str<strong>on</strong>g>problem</str<strong>on</strong>g> is solved by using ano<str<strong>on</strong>g>the</str<strong>on</strong>g>r MicroMacroUniverse approachapplied to <str<strong>on</strong>g>the</str<strong>on</strong>g> MTree. The intuitive idea is to dynamically maintain a set of trees, pre-processed with <str<strong>on</strong>g>the</str<strong>on</strong>g> static algorithmpresented in Secti<strong>on</strong> 5. As in <str<strong>on</strong>g>the</str<strong>on</strong>g> st<str<strong>on</strong>g>and</str<strong>on</strong>g>ard micro–macro schemes <str<strong>on</strong>g>the</str<strong>on</strong>g> MTree is partiti<strong>on</strong>ed into micro-trees whose rootsare maintained in a macro-tree. However, <str<strong>on</strong>g>the</str<strong>on</strong>g> partiti<strong>on</strong>ing is dynamic in nature. The sizes of <str<strong>on</strong>g>the</str<strong>on</strong>g> micro-trees can be verydifferent as well as <str<strong>on</strong>g>the</str<strong>on</strong>g> micro-tree to which a node bel<strong>on</strong>gs changes dynamically. Let us call each of <str<strong>on</strong>g>the</str<strong>on</strong>g>se trees dμTree(dynamic-micro-Tree). The pre-process of a dμTree allows us to efficiently solve each <str<strong>on</strong>g>NCA</str<strong>on</strong>g> query <strong>on</strong> that tree, using<str<strong>on</strong>g>the</str<strong>on</strong>g> nca H algorithm presented earlier. The root of each dμTree is represented by a node in ano<str<strong>on</strong>g>the</str<strong>on</strong>g>r tree, called dMTree(dynamic-Macro-Tree). We will show that <str<strong>on</strong>g>the</str<strong>on</strong>g> dMTree has a “small” depth—thus, simpler <str<strong>on</strong>g>NCA</str<strong>on</strong>g> <str<strong>on</strong>g>algorithms</str<strong>on</strong>g> (e.g., <str<strong>on</strong>g>the</str<strong>on</strong>g><strong>on</strong>e based <strong>on</strong> p-lists from Secti<strong>on</strong> 4.3) can be used here to provide efficient <str<strong>on</strong>g>NCA</str<strong>on</strong>g> computati<strong>on</strong>.


124 A. D. Palú et al. / Theoretical Computer Science 352 (2006) 108 – 135Let <str<strong>on</strong>g>the</str<strong>on</strong>g> pre-process of a dμTree T m be <str<strong>on</strong>g>the</str<strong>on</strong>g> static pre-process described in Secti<strong>on</strong> 5 applied to <str<strong>on</strong>g>the</str<strong>on</strong>g> tree subtree MTree(root(T m )). Thus, when T m is pre-processed, all o<str<strong>on</strong>g>the</str<strong>on</strong>g>r dμTrees hanging <strong>on</strong> T m are merged in a single new dμTree. Thebasic idea is to wait to re-pre-process a dμTree T m until <str<strong>on</strong>g>the</str<strong>on</strong>g> number of nodes in subtree MTree (root(T m )) has doubledsince <str<strong>on</strong>g>the</str<strong>on</strong>g> last pre-process of T m . To answer an <str<strong>on</strong>g>NCA</str<strong>on</strong>g> query we first solve <str<strong>on</strong>g>the</str<strong>on</strong>g> <str<strong>on</strong>g>problem</str<strong>on</strong>g> with <str<strong>on</strong>g>the</str<strong>on</strong>g> p-list <str<strong>on</strong>g>NCA</str<strong>on</strong>g> algorithmfrom Secti<strong>on</strong> 4.3 <strong>on</strong> <str<strong>on</strong>g>the</str<strong>on</strong>g> dMTree <str<strong>on</strong>g>and</str<strong>on</strong>g> <str<strong>on</strong>g>the</str<strong>on</strong>g>n we “refine” that soluti<strong>on</strong> using <str<strong>on</strong>g>the</str<strong>on</strong>g> nca H <strong>on</strong> <str<strong>on</strong>g>the</str<strong>on</strong>g> dμTree associated to <str<strong>on</strong>g>the</str<strong>on</strong>g>result obtained from <str<strong>on</strong>g>the</str<strong>on</strong>g> dMTree.Dynamic inserti<strong>on</strong>s: Let T m be a dμTree <str<strong>on</strong>g>and</str<strong>on</strong>g> v m <str<strong>on</strong>g>the</str<strong>on</strong>g> node representative of T m in <str<strong>on</strong>g>the</str<strong>on</strong>g> dMTree. Let us define root(T m )to be <str<strong>on</strong>g>the</str<strong>on</strong>g> root of T m in <str<strong>on</strong>g>the</str<strong>on</strong>g> MTree, micro(v m ) a pointer to root(T m ), macro(T m ) a pointer to v m . We also maintain <str<strong>on</strong>g>the</str<strong>on</strong>g>size of T m by keeping a pointer size(T m ) that points to a list that has length |T m |. size(T m ) is created <str<strong>on</strong>g>and</str<strong>on</strong>g> inserted with anumber of nodes equal to <str<strong>on</strong>g>the</str<strong>on</strong>g> number of nodes in <str<strong>on</strong>g>the</str<strong>on</strong>g> subtree MTree (root(T m )), when T m is pre-processed. The size(T m )list is used as a decrementing counter to decide when to do ano<str<strong>on</strong>g>the</str<strong>on</strong>g>r pre-process.Each time a node v is inserted in <str<strong>on</strong>g>the</str<strong>on</strong>g> MTree as child of w, anewdμTree T m is created, <str<strong>on</strong>g>and</str<strong>on</strong>g> <str<strong>on</strong>g>the</str<strong>on</strong>g> representative ofroot(T m ) = v is added in <str<strong>on</strong>g>the</str<strong>on</strong>g> dMTree as child of macro(T w ), where T w is <str<strong>on</strong>g>the</str<strong>on</strong>g> dμTree c<strong>on</strong>taining w. For each node <strong>on</strong><str<strong>on</strong>g>the</str<strong>on</strong>g> path P from macro(T m ) to <str<strong>on</strong>g>the</str<strong>on</strong>g> root of <str<strong>on</strong>g>the</str<strong>on</strong>g> dMTree (<str<strong>on</strong>g>the</str<strong>on</strong>g>se corresp<strong>on</strong>d to some dμTree T m ′ ), we update <str<strong>on</strong>g>the</str<strong>on</strong>g> number ofnew nodes added in <str<strong>on</strong>g>the</str<strong>on</strong>g> subtree MTree (root(T m ′ )) by 1. This can be d<strong>on</strong>e decrementing <str<strong>on</strong>g>the</str<strong>on</strong>g> “counter” size(T m ′ ) by <strong>on</strong>e,that is, shifting <str<strong>on</strong>g>the</str<strong>on</strong>g> pointer size(T m ′ ) to <str<strong>on</strong>g>the</str<strong>on</strong>g> next node in that list. If a dμTree T m has c<strong>on</strong>sumed all nodes in size(T m ),<str<strong>on</strong>g>the</str<strong>on</strong>g>n T m has to be pre-processed again. Let us call v h <str<strong>on</strong>g>the</str<strong>on</strong>g> highest node in <str<strong>on</strong>g>the</str<strong>on</strong>g> path P c<strong>on</strong>sidered, such that micro(v h )has to be pre-processed. The pre-process is applied to subtree MTree (v h ), which becomes <str<strong>on</strong>g>the</str<strong>on</strong>g> new dμTree. All nodes insubtree MTree (v h ) are deleted <str<strong>on</strong>g>and</str<strong>on</strong>g> replaced by <str<strong>on</strong>g>the</str<strong>on</strong>g> node v h . The size(micro(v h )) list is initialized with <str<strong>on</strong>g>the</str<strong>on</strong>g> inserti<strong>on</strong> of anumber of nodes equal to <str<strong>on</strong>g>the</str<strong>on</strong>g> number of nodes c<strong>on</strong>tained in subtree MTree (v h ).Notice that each time a node v is involved in a pre-process resulting in a tree T ′ , <str<strong>on</strong>g>the</str<strong>on</strong>g> size of T ′ is at least twice <str<strong>on</strong>g>the</str<strong>on</strong>g>size of <str<strong>on</strong>g>the</str<strong>on</strong>g> tree T ′′ which c<strong>on</strong>tained v be<str<strong>on</strong>g>for</str<strong>on</strong>g>e <str<strong>on</strong>g>the</str<strong>on</strong>g> pre-process. The following result can be derived:Propositi<strong>on</strong> 4. A node v in <str<strong>on</strong>g>the</str<strong>on</strong>g> MTree of size t is involved in at most lg t distinct pre-process operati<strong>on</strong>s.As we will show in <str<strong>on</strong>g>the</str<strong>on</strong>g> next lemma, <str<strong>on</strong>g>the</str<strong>on</strong>g> dMTree has depth less or equal to O(lg N), where N is <str<strong>on</strong>g>the</str<strong>on</strong>g> number of nodesin MTree. Thus <str<strong>on</strong>g>the</str<strong>on</strong>g> update of counters may be per<str<strong>on</strong>g>for</str<strong>on</strong>g>med O(lg N) time <str<strong>on</strong>g>for</str<strong>on</strong>g> each inserti<strong>on</strong> of a node in <str<strong>on</strong>g>the</str<strong>on</strong>g> MTree.Lemma 3. Immediately after a pre-process, if a path P starting from a node in dMTree <str<strong>on</strong>g>and</str<strong>on</strong>g> ending <strong>on</strong> a leaf has knodes, <str<strong>on</strong>g>the</str<strong>on</strong>g>n <str<strong>on</strong>g>the</str<strong>on</strong>g> total number of nodes in dμTrees represented by nodes in P is at least 2 k−1 .Proof. Let us prove this result by inducti<strong>on</strong> <strong>on</strong> k.Base: Fork = 1 <str<strong>on</strong>g>the</str<strong>on</strong>g> result is trivial, since <str<strong>on</strong>g>the</str<strong>on</strong>g> dμTree represented c<strong>on</strong>tains at least <strong>on</strong>e (i.e., 2 ◦ ) node.Inductive step: Let us c<strong>on</strong>sider <str<strong>on</strong>g>the</str<strong>on</strong>g> case k = i>1, <str<strong>on</strong>g>and</str<strong>on</strong>g> let us assume by inductive hypo<str<strong>on</strong>g>the</str<strong>on</strong>g>sis that <str<strong>on</strong>g>the</str<strong>on</strong>g> results hold<str<strong>on</strong>g>for</str<strong>on</strong>g> i − 1.Let v l be <str<strong>on</strong>g>the</str<strong>on</strong>g> first node in <str<strong>on</strong>g>the</str<strong>on</strong>g> path P <str<strong>on</strong>g>and</str<strong>on</strong>g> R <str<strong>on</strong>g>the</str<strong>on</strong>g> rest of <str<strong>on</strong>g>the</str<strong>on</strong>g> path from v l to <str<strong>on</strong>g>the</str<strong>on</strong>g> appropriate leaf. Let T m be <str<strong>on</strong>g>the</str<strong>on</strong>g> dμTreerepresented by v l in <str<strong>on</strong>g>the</str<strong>on</strong>g> dMTree. Using <str<strong>on</strong>g>the</str<strong>on</strong>g> inductive hypo<str<strong>on</strong>g>the</str<strong>on</strong>g>sis applied to R we can infer that <str<strong>on</strong>g>the</str<strong>on</strong>g> nodes in R representat least 2 i−2 nodes in <str<strong>on</strong>g>the</str<strong>on</strong>g> MTree. The tree T m has at least 1 + 2 i−2 nodes, as o<str<strong>on</strong>g>the</str<strong>on</strong>g>rwise, according to <str<strong>on</strong>g>the</str<strong>on</strong>g> algorithm,it (or <strong>on</strong>e of its ancestors) should be pre-processed again (<str<strong>on</strong>g>and</str<strong>on</strong>g> will <str<strong>on</strong>g>the</str<strong>on</strong>g>re<str<strong>on</strong>g>for</str<strong>on</strong>g>e be a leaf in <str<strong>on</strong>g>the</str<strong>on</strong>g> resulting dMtree) <str<strong>on</strong>g>and</str<strong>on</strong>g>by hypo<str<strong>on</strong>g>the</str<strong>on</strong>g>sis <str<strong>on</strong>g>the</str<strong>on</strong>g> MTree was just pre-processed. Thus <str<strong>on</strong>g>the</str<strong>on</strong>g> total number of nodes represented by <str<strong>on</strong>g>the</str<strong>on</strong>g> path P is at least1 + 2 i−2 + 2 i−2 2 i−1 . □It follows that <str<strong>on</strong>g>the</str<strong>on</strong>g> dMTree has depth at most lg N, where N is <str<strong>on</strong>g>the</str<strong>on</strong>g> number of nodes in <str<strong>on</strong>g>the</str<strong>on</strong>g> MTree. The MTree hasat most N n/c T (n) nodes. Choosing c T (n) = lg n lg lg n, <str<strong>on</strong>g>the</str<strong>on</strong>g> MTree has at most O(n/(lg n lg lg n)) nodes. ApplyingLemma 3, we c<strong>on</strong>clude that <str<strong>on</strong>g>the</str<strong>on</strong>g> dMtree has depth at most lg n. Thus, <str<strong>on</strong>g>the</str<strong>on</strong>g> <str<strong>on</strong>g>NCA</str<strong>on</strong>g> in <str<strong>on</strong>g>the</str<strong>on</strong>g> dMtree can be computed in timeO(lg lg n) using p-lists.We now show that n inserti<strong>on</strong>s in T will cost O(n) to maintain <str<strong>on</strong>g>the</str<strong>on</strong>g> MTree structure. We showed that a pre-process ofa tree with t nodes in <str<strong>on</strong>g>the</str<strong>on</strong>g> static case costs O(t lg lg t). Let N be <str<strong>on</strong>g>the</str<strong>on</strong>g> number of nodes in <str<strong>on</strong>g>the</str<strong>on</strong>g> MTree. From Propositi<strong>on</strong>4, we know that a node v in <str<strong>on</strong>g>the</str<strong>on</strong>g> MTree is involved in at most lg N pre-processes. Each of <str<strong>on</strong>g>the</str<strong>on</strong>g>m will cost l lg l, wherel is <str<strong>on</strong>g>the</str<strong>on</strong>g> size of <str<strong>on</strong>g>the</str<strong>on</strong>g> tree pre-processed. Thus, <str<strong>on</strong>g>for</str<strong>on</strong>g> each v <str<strong>on</strong>g>the</str<strong>on</strong>g> amortized cost per process is lg lg l lg lg N <str<strong>on</strong>g>and</str<strong>on</strong>g> <str<strong>on</strong>g>the</str<strong>on</strong>g> costper node is lg N lg lg N. Recalling that N = n/c T (n) <str<strong>on</strong>g>and</str<strong>on</strong>g> c T (n) = lg n lg lg n, <str<strong>on</strong>g>the</str<strong>on</strong>g> total amortized cost is O(1) perinserti<strong>on</strong> in T.


A. D. Palú et al. / Theoretical Computer Science 352 (2006) 108 – 135 125<str<strong>on</strong>g>NCA</str<strong>on</strong>g> queries: Let us now show how to compute <str<strong>on</strong>g>the</str<strong>on</strong>g> nca M (x, y) with x <str<strong>on</strong>g>and</str<strong>on</strong>g> y in MTree. It is possible to find x ′ <str<strong>on</strong>g>and</str<strong>on</strong>g>y ′ dμTrees c<strong>on</strong>taining x <str<strong>on</strong>g>and</str<strong>on</strong>g> y, respectively in c<strong>on</strong>stant time—e.g., <strong>on</strong>ce a node is pre-processed, we can directly set in<str<strong>on</strong>g>the</str<strong>on</strong>g> node a pointer to <str<strong>on</strong>g>the</str<strong>on</strong>g> corresp<strong>on</strong>ding dμTree.If x <str<strong>on</strong>g>and</str<strong>on</strong>g> y are in <str<strong>on</strong>g>the</str<strong>on</strong>g> same dμTree T m return nca H (x, y) using <str<strong>on</strong>g>the</str<strong>on</strong>g> previously pre-processed H tree <str<strong>on</strong>g>for</str<strong>on</strong>g> T m . O<str<strong>on</strong>g>the</str<strong>on</strong>g>rwise,we can compute <str<strong>on</strong>g>the</str<strong>on</strong>g> nca(root(x ′ ), root(y ′ )) = (z, z x ,z y ) <strong>on</strong> <str<strong>on</strong>g>the</str<strong>on</strong>g> dMTree, using p-lists. If z x = z <str<strong>on</strong>g>the</str<strong>on</strong>g>n <str<strong>on</strong>g>the</str<strong>on</strong>g> result isgiven by nca H (x, parent(micro(z y ))). 2 O<str<strong>on</strong>g>the</str<strong>on</strong>g>rwise, <str<strong>on</strong>g>the</str<strong>on</strong>g> result isnca H (parent(micro(z x ), parent(micro(z y )).The algorithm nca H requires O(lg lg n) time <str<strong>on</strong>g>and</str<strong>on</strong>g> <str<strong>on</strong>g>the</str<strong>on</strong>g> p-list algorithm used <str<strong>on</strong>g>for</str<strong>on</strong>g> <str<strong>on</strong>g>the</str<strong>on</strong>g> dMtree requires O(lg t), wheret lg(n/c T (n)) <str<strong>on</strong>g>and</str<strong>on</strong>g> c T (n) = lg n lg lg n. This allows us to c<strong>on</strong>clude that <str<strong>on</strong>g>the</str<strong>on</strong>g> total time is O(lg lg n).6.2. The nca μ algorithmIn this secti<strong>on</strong> we provide an algorithm with an amortized time complexity of O(1) per inserti<strong>on</strong> <str<strong>on</strong>g>and</str<strong>on</strong>g> worst-casecomplexity of O(lg lg n) per query <str<strong>on</strong>g>for</str<strong>on</strong>g> <str<strong>on</strong>g>the</str<strong>on</strong>g> μTrees.The scheme uses <str<strong>on</strong>g>the</str<strong>on</strong>g> st<str<strong>on</strong>g>and</str<strong>on</strong>g>ard MicroMacroUniverse approach <strong>on</strong> <str<strong>on</strong>g>the</str<strong>on</strong>g> μTrees. The optimal soluti<strong>on</strong> is computedcombining an optimal soluti<strong>on</strong> <strong>on</strong> MμTree <str<strong>on</strong>g>and</str<strong>on</strong>g> an optimal soluti<strong>on</strong> <strong>on</strong> μμTree. Choosing c S (n) = lg lg n <str<strong>on</strong>g>and</str<strong>on</strong>g> recallingthat c T (n) = lg n lg lg n, all <str<strong>on</strong>g>the</str<strong>on</strong>g> μTrees can be processed in O(n) time. To find <str<strong>on</strong>g>the</str<strong>on</strong>g> nca μ of two nodes x <str<strong>on</strong>g>and</str<strong>on</strong>g> y in a μTree,we combine a p-list search <strong>on</strong> MμTree <str<strong>on</strong>g>and</str<strong>on</strong>g> a brute <str<strong>on</strong>g>for</str<strong>on</strong>g>ce search applied <strong>on</strong> <str<strong>on</strong>g>the</str<strong>on</strong>g> resulting μμTree. Clearly this requiresO(lg lg n) time.6.3. H<str<strong>on</strong>g>and</str<strong>on</strong>g>ling deleti<strong>on</strong>sObserve that <str<strong>on</strong>g>the</str<strong>on</strong>g> deleti<strong>on</strong>s are not per<str<strong>on</strong>g>for</str<strong>on</strong>g>med explicitly, instead <str<strong>on</strong>g>the</str<strong>on</strong>g> deleted nodes are just marked as such. The markednodes are deleted at <str<strong>on</strong>g>the</str<strong>on</strong>g> time when <str<strong>on</strong>g>the</str<strong>on</strong>g>y are involved in <str<strong>on</strong>g>the</str<strong>on</strong>g> next pre-processing. We do not update <str<strong>on</strong>g>the</str<strong>on</strong>g> counters whennodes are deleted. This does not affect our analysis, because <str<strong>on</strong>g>the</str<strong>on</strong>g> number of operati<strong>on</strong>s is greater than <str<strong>on</strong>g>the</str<strong>on</strong>g> number ofnodes in T.6.4. Discussi<strong>on</strong>6.4.1. Some implementati<strong>on</strong> detailsThe previously described <str<strong>on</strong>g>algorithms</str<strong>on</strong>g> require <str<strong>on</strong>g>the</str<strong>on</strong>g> comparis<strong>on</strong> between depths of nodes <str<strong>on</strong>g>and</str<strong>on</strong>g> <str<strong>on</strong>g>the</str<strong>on</strong>g> values of <str<strong>on</strong>g>the</str<strong>on</strong>g> functi<strong>on</strong>sc T (n) <str<strong>on</strong>g>and</str<strong>on</strong>g> c S (n).Let us start by assuming n to be known (see later about this assumpti<strong>on</strong>). In this case, be<str<strong>on</strong>g>for</str<strong>on</strong>g>e starting <str<strong>on</strong>g>the</str<strong>on</strong>g> maincomputati<strong>on</strong>, we can c<strong>on</strong>struct lists of length c T (n) <str<strong>on</strong>g>and</str<strong>on</strong>g> c S (n)—this can be achieved in time O(n), by building first alist of length n,lgn, <str<strong>on</strong>g>and</str<strong>on</strong>g> lg lg n (using a complete binary tree to determine lg n from <str<strong>on</strong>g>the</str<strong>on</strong>g> list n, <str<strong>on</strong>g>and</str<strong>on</strong>g> <str<strong>on</strong>g>the</str<strong>on</strong>g>n using <str<strong>on</strong>g>the</str<strong>on</strong>g> l<strong>on</strong>gestbranch in such tree to build ano<str<strong>on</strong>g>the</str<strong>on</strong>g>r binary tree whose height is lg lg n). Once <str<strong>on</strong>g>the</str<strong>on</strong>g>se data structures are available, we canreadily c<strong>on</strong>struct a list representing c S (n)—i.e., <str<strong>on</strong>g>the</str<strong>on</strong>g> list of length lg lg n—<str<strong>on</strong>g>and</str<strong>on</strong>g> a list representing c T (n)—by scanning<str<strong>on</strong>g>the</str<strong>on</strong>g> list of length lg lg n as many times as <str<strong>on</strong>g>the</str<strong>on</strong>g> number of elements in <str<strong>on</strong>g>the</str<strong>on</strong>g> list of length lg n.The menti<strong>on</strong>ed comparis<strong>on</strong>s can be realized by storing in each node of <str<strong>on</strong>g>the</str<strong>on</strong>g> tree points to <str<strong>on</strong>g>the</str<strong>on</strong>g> elements of <str<strong>on</strong>g>the</str<strong>on</strong>g> listsc S (n) <str<strong>on</strong>g>and</str<strong>on</strong>g> c T (n) indicating <str<strong>on</strong>g>the</str<strong>on</strong>g> depth of <str<strong>on</strong>g>the</str<strong>on</strong>g> nodes—this can be realized in time O(1) (as <str<strong>on</strong>g>the</str<strong>on</strong>g> depths of a node can bedetermined from <str<strong>on</strong>g>the</str<strong>on</strong>g> depths of <str<strong>on</strong>g>the</str<strong>on</strong>g> parent of <str<strong>on</strong>g>the</str<strong>on</strong>g> node in <str<strong>on</strong>g>the</str<strong>on</strong>g> tree). The number comparis<strong>on</strong> is simply obtained lookingat <str<strong>on</strong>g>the</str<strong>on</strong>g> pointer from a depth tree node to <str<strong>on</strong>g>the</str<strong>on</strong>g> list, <str<strong>on</strong>g>and</str<strong>on</strong>g> testing whe<str<strong>on</strong>g>the</str<strong>on</strong>g>r <str<strong>on</strong>g>the</str<strong>on</strong>g> node pointed is <str<strong>on</strong>g>the</str<strong>on</strong>g> last element of <str<strong>on</strong>g>the</str<strong>on</strong>g> list.Observe that <str<strong>on</strong>g>the</str<strong>on</strong>g> knowledge of <str<strong>on</strong>g>the</str<strong>on</strong>g> value of n at <str<strong>on</strong>g>the</str<strong>on</strong>g> beginning is not a severe restricti<strong>on</strong>. In [1] it is observed that“If <str<strong>on</strong>g>the</str<strong>on</strong>g> total number of nodes is not known in advance, <str<strong>on</strong>g>the</str<strong>on</strong>g>n n is guessed to be a c<strong>on</strong>stant <str<strong>on</strong>g>and</str<strong>on</strong>g> each time <str<strong>on</strong>g>the</str<strong>on</strong>g> number ofnodes extends <str<strong>on</strong>g>the</str<strong>on</strong>g> guess we double <str<strong>on</strong>g>the</str<strong>on</strong>g> guess <str<strong>on</strong>g>and</str<strong>on</strong>g> rec<strong>on</strong>struct <str<strong>on</strong>g>the</str<strong>on</strong>g> structure”, <str<strong>on</strong>g>and</str<strong>on</strong>g> <str<strong>on</strong>g>the</str<strong>on</strong>g> observati<strong>on</strong> easily applies to our2 The case z = z y is symmetrical.


126 A. D. Palú et al. / Theoretical Computer Science 352 (2006) 108 – 135case as well. Alternatively, <strong>on</strong>e can observe <str<strong>on</strong>g>the</str<strong>on</strong>g> following:• We can use as value <str<strong>on</strong>g>for</str<strong>on</strong>g> n <str<strong>on</strong>g>the</str<strong>on</strong>g> number of nodes existing at that particular moment in <str<strong>on</strong>g>the</str<strong>on</strong>g> tree; this number will changeduring <str<strong>on</strong>g>the</str<strong>on</strong>g> computati<strong>on</strong>, thus growing <str<strong>on</strong>g>the</str<strong>on</strong>g> structures <str<strong>on</strong>g>for</str<strong>on</strong>g> <str<strong>on</strong>g>the</str<strong>on</strong>g> representati<strong>on</strong> of c S (n) <str<strong>on</strong>g>and</str<strong>on</strong>g> c T (n) as nodes are insertedin <str<strong>on</strong>g>the</str<strong>on</strong>g> tree.• The structures need to be rec<strong>on</strong>structed each time <str<strong>on</strong>g>the</str<strong>on</strong>g> dynamically changing value of n reaches a power of 2. If nis <str<strong>on</strong>g>the</str<strong>on</strong>g> final size of <str<strong>on</strong>g>the</str<strong>on</strong>g> tree, <str<strong>on</strong>g>the</str<strong>on</strong>g>n <str<strong>on</strong>g>the</str<strong>on</strong>g> total cost deriving from <str<strong>on</strong>g>the</str<strong>on</strong>g> additi<strong>on</strong>al reorganizati<strong>on</strong>s of <str<strong>on</strong>g>the</str<strong>on</strong>g> data structuresrequired islg ∑ni=1(2 i i lg i) = Θ(n lg n lg lg n).These additi<strong>on</strong>al re-processing do not modify <str<strong>on</strong>g>the</str<strong>on</strong>g> overall complexity of <str<strong>on</strong>g>the</str<strong>on</strong>g> soluti<strong>on</strong>.Observe that <str<strong>on</strong>g>the</str<strong>on</strong>g> overall space requirement <str<strong>on</strong>g>for</str<strong>on</strong>g> this soluti<strong>on</strong> is O(n lg lg n).6.4.2. APMs vs PPMsThe comm<strong>on</strong>ly used APM model allows c<strong>on</strong>stant time arithmetic <strong>on</strong> Θ(lg n) sized integers. The PPM does not allowsuch arithmetic, <str<strong>on</strong>g>and</str<strong>on</strong>g> <strong>on</strong>e has to account <str<strong>on</strong>g>for</str<strong>on</strong>g> simulating any arithmetic needed, when analyzing <str<strong>on</strong>g>the</str<strong>on</strong>g> running time. Thearithmetic can be simulated in PPMs by explicitly representing <str<strong>on</strong>g>the</str<strong>on</strong>g> integers via Θ(lg n) sized lists. This entails that ageneric translati<strong>on</strong> (that just simulates <str<strong>on</strong>g>the</str<strong>on</strong>g> arithmetic) of APM <str<strong>on</strong>g>algorithms</str<strong>on</strong>g> to PPMs will incur a polylog penalty. Moreprecisely an algorithm A that runs in time t(n) <strong>on</strong> an APM <str<strong>on</strong>g>and</str<strong>on</strong>g> uses any arithmetic at all, will take time t(n) lg k n <str<strong>on</strong>g>for</str<strong>on</strong>g>some k>0 <strong>on</strong> a PPM. We present an interesting result about <str<strong>on</strong>g>the</str<strong>on</strong>g> <str<strong>on</strong>g>NCA</str<strong>on</strong>g> <str<strong>on</strong>g>problem</str<strong>on</strong>g>. We show that any optimal APMalgorithm <str<strong>on</strong>g>for</str<strong>on</strong>g> <str<strong>on</strong>g>the</str<strong>on</strong>g> <str<strong>on</strong>g>NCA</str<strong>on</strong>g><str<strong>on</strong>g>problem</str<strong>on</strong>g> can be c<strong>on</strong>verted into a PPM algorithm without incurring any penalty.Propositi<strong>on</strong> 5. An APM algorithm A solving <str<strong>on</strong>g>the</str<strong>on</strong>g> <str<strong>on</strong>g>NCA</str<strong>on</strong>g> <str<strong>on</strong>g>problem</str<strong>on</strong>g> with amortized cost of O(lg k n) per inserti<strong>on</strong> <str<strong>on</strong>g>and</str<strong>on</strong>g>worst-case cost O(lg lg n) per query, can be translated into a PPM algorithm with an amortized cost of O(1) perinserti<strong>on</strong> <str<strong>on</strong>g>and</str<strong>on</strong>g> worst-case cost O(lg lg n) per query.Proof. The tree T can be partiti<strong>on</strong>ed in μTrees with maximal depth of lg k n. The partiti<strong>on</strong> is accomplished with <str<strong>on</strong>g>the</str<strong>on</strong>g>sequential dynamic algorithm presented above, inserting nodes of T with an order of inserti<strong>on</strong> defined by a breadthfirst visit <str<strong>on</strong>g>and</str<strong>on</strong>g> setting c T (n) = lg k n. Then <str<strong>on</strong>g>the</str<strong>on</strong>g> MTree collecting <str<strong>on</strong>g>the</str<strong>on</strong>g> μTrees has n/(lg k n) nodes <str<strong>on</strong>g>and</str<strong>on</strong>g> <str<strong>on</strong>g>the</str<strong>on</strong>g> algorithm Arequires a total of O(n) time <str<strong>on</strong>g>for</str<strong>on</strong>g> inserti<strong>on</strong>s <str<strong>on</strong>g>and</str<strong>on</strong>g> O(lg lg n) <str<strong>on</strong>g>for</str<strong>on</strong>g> <str<strong>on</strong>g>the</str<strong>on</strong>g> <str<strong>on</strong>g>NCA</str<strong>on</strong>g> queries. The MicroMacroUniverse approachis applied again to <str<strong>on</strong>g>the</str<strong>on</strong>g> μTrees. Each μTree T m is partiti<strong>on</strong>ed in μμTrees of depth at most lg lg n. The corresp<strong>on</strong>dingMμTree c<strong>on</strong>tains lg k n/(lg lg n) nodes. The pre-process <str<strong>on</strong>g>for</str<strong>on</strong>g> a MμTree c<strong>on</strong>sists of attaching an O(lg lg n) sized p-listto each node <str<strong>on</strong>g>and</str<strong>on</strong>g> can be accomplished in time O(lg k n/(lg lg n) lg lg n) = O(lg k n). The total cost of pre-processingall <str<strong>on</strong>g>the</str<strong>on</strong>g> MμTrees, <str<strong>on</strong>g>the</str<strong>on</strong>g>re<str<strong>on</strong>g>for</str<strong>on</strong>g>e, is O(n/ lg k n) ∗ O(lg k n) = O(n). The cost of an <str<strong>on</strong>g>NCA</str<strong>on</strong>g> query <strong>on</strong> a μμTree is O(lg lg n)(brute <str<strong>on</strong>g>for</str<strong>on</strong>g>ce), <strong>on</strong> a MμTree is O(lg lg n), because <str<strong>on</strong>g>the</str<strong>on</strong>g> depth of <str<strong>on</strong>g>the</str<strong>on</strong>g> tree is O(lg k n), <str<strong>on</strong>g>and</str<strong>on</strong>g> <strong>on</strong> <str<strong>on</strong>g>the</str<strong>on</strong>g> MTree is O(lg lg n) byhypo<str<strong>on</strong>g>the</str<strong>on</strong>g>sis. In Secti<strong>on</strong> 6 we showed how to collect <str<strong>on</strong>g>the</str<strong>on</strong>g> optimal <str<strong>on</strong>g>NCA</str<strong>on</strong>g> queries <str<strong>on</strong>g>for</str<strong>on</strong>g> each tree with a c<strong>on</strong>stant overhead, toachieve a total cost of an <str<strong>on</strong>g>NCA</str<strong>on</strong>g> query of O(lg lg n). □Corollary 2. The <str<strong>on</strong>g>the</str<strong>on</strong>g>orem remains true even if A was a Pointer Machine algorithm that made use of c<strong>on</strong>stant-timearithmetic <str<strong>on</strong>g>for</str<strong>on</strong>g> lg n size integers.Proof. A Pointer Machine algorithm with running time O(t(n)) can be naively c<strong>on</strong>verted to a PPM algorithm withrunning time O(t(n) lg k n) <str<strong>on</strong>g>for</str<strong>on</strong>g> some k. □7. A <str<strong>on</strong>g>parallel</str<strong>on</strong>g> compressi<strong>on</strong> scheme <str<strong>on</strong>g>for</str<strong>on</strong>g> treesThe compressi<strong>on</strong> algorithm is a sequential iterati<strong>on</strong> of <str<strong>on</strong>g>parallel</str<strong>on</strong>g> phases. Each <str<strong>on</strong>g>parallel</str<strong>on</strong>g> phase is composed of two <str<strong>on</strong>g>parallel</str<strong>on</strong>g>steps. The first step is compressi<strong>on</strong> of leaves (leaf compressi<strong>on</strong>) in <str<strong>on</strong>g>the</str<strong>on</strong>g> current tree <str<strong>on</strong>g>and</str<strong>on</strong>g> <str<strong>on</strong>g>the</str<strong>on</strong>g> sec<strong>on</strong>d step c<strong>on</strong>tributes to<str<strong>on</strong>g>the</str<strong>on</strong>g> compressi<strong>on</strong> of paths (path compressi<strong>on</strong>) in <str<strong>on</strong>g>the</str<strong>on</strong>g> current tree using a step of pointer doubling [10].


A. D. Palú et al. / Theoretical Computer Science 352 (2006) 108 – 135 127ll111lk-1k-1k-1kkkOriginal Tree1headtail243head 5 67 tail8 98795path45H-Tree122path63Fig. 12. An example of slow computati<strong>on</strong>.Fig. 13. An example of bad H-tree.Additi<strong>on</strong>ally, our efficient <str<strong>on</strong>g>parallel</str<strong>on</strong>g> soluti<strong>on</strong> <str<strong>on</strong>g>for</str<strong>on</strong>g> <str<strong>on</strong>g>the</str<strong>on</strong>g> <str<strong>on</strong>g>NCA</str<strong>on</strong>g> <str<strong>on</strong>g>problem</str<strong>on</strong>g> requires <str<strong>on</strong>g>the</str<strong>on</strong>g> ability to efficiently solve <str<strong>on</strong>g>the</str<strong>on</strong>g>TP <str<strong>on</strong>g>problem</str<strong>on</strong>g> [21] in <str<strong>on</strong>g>parallel</str<strong>on</strong>g>. A <str<strong>on</strong>g>parallel</str<strong>on</strong>g> versi<strong>on</strong> of <str<strong>on</strong>g>the</str<strong>on</strong>g> <str<strong>on</strong>g>problem</str<strong>on</strong>g> <str<strong>on</strong>g>and</str<strong>on</strong>g> an efficient <str<strong>on</strong>g>parallel</str<strong>on</strong>g> soluti<strong>on</strong> are presented inSecti<strong>on</strong> 7.6.7.1. From sequential to <str<strong>on</strong>g>parallel</str<strong>on</strong>g>The direct simulati<strong>on</strong> of <str<strong>on</strong>g>the</str<strong>on</strong>g> sequential algorithm requires O(lg 2 n) <str<strong>on</strong>g>parallel</str<strong>on</strong>g> time. Un<str<strong>on</strong>g>for</str<strong>on</strong>g>tunately, this direct simulati<strong>on</strong>may also require Ω(lg 2 n) time. C<strong>on</strong>sider, <str<strong>on</strong>g>for</str<strong>on</strong>g> example, <str<strong>on</strong>g>the</str<strong>on</strong>g> situati<strong>on</strong> in Fig. 12. The tree is composed of a main path, witha number of complete trees (of depth k, k−1,...,1) hanging from it. In this situati<strong>on</strong>, at every leaf compressi<strong>on</strong>, a pathof length l is created in <str<strong>on</strong>g>the</str<strong>on</strong>g> main branch, allowing <str<strong>on</strong>g>for</str<strong>on</strong>g> <str<strong>on</strong>g>the</str<strong>on</strong>g> next path compressi<strong>on</strong> to take place; this path compressi<strong>on</strong>will require lg l time. The process is repeated k times, hence <str<strong>on</strong>g>the</str<strong>on</strong>g> total <str<strong>on</strong>g>parallel</str<strong>on</strong>g> time is k lg l. Ifl is chosen equal to 2 k ,<str<strong>on</strong>g>the</str<strong>on</strong>g>n <str<strong>on</strong>g>the</str<strong>on</strong>g> total number of nodes n = Θ(2 2k ), thus k = Θ(lg n) <str<strong>on</strong>g>and</str<strong>on</strong>g> <str<strong>on</strong>g>the</str<strong>on</strong>g> <str<strong>on</strong>g>parallel</str<strong>on</strong>g> time is k 2 = Θ(lg 2 n).We could attempt to improve this running time by allowing path compressi<strong>on</strong>s to occur also in <str<strong>on</strong>g>the</str<strong>on</strong>g> internal paths (i.e.,paths that do not end in a leaf); similarly we could allow leaf compressi<strong>on</strong> to be per<str<strong>on</strong>g>for</str<strong>on</strong>g>med at all <str<strong>on</strong>g>the</str<strong>on</strong>g> leaves <str<strong>on</strong>g>and</str<strong>on</strong>g> headsof paths detected at each <str<strong>on</strong>g>parallel</str<strong>on</strong>g> step. Un<str<strong>on</strong>g>for</str<strong>on</strong>g>tunately this will not help our case ei<str<strong>on</strong>g>the</str<strong>on</strong>g>r. As illustrated by <str<strong>on</strong>g>the</str<strong>on</strong>g> example inFig. 13, <str<strong>on</strong>g>the</str<strong>on</strong>g> H-tree resulting from <str<strong>on</strong>g>the</str<strong>on</strong>g>se compressi<strong>on</strong>s can have linear depth, thus preventing us from using <str<strong>on</strong>g>the</str<strong>on</strong>g> H-treeto per<str<strong>on</strong>g>for</str<strong>on</strong>g>m fast computati<strong>on</strong> of <str<strong>on</strong>g>NCA</str<strong>on</strong>g> queries.However, <str<strong>on</strong>g>the</str<strong>on</strong>g>se c<strong>on</strong>siderati<strong>on</strong>s do suggest a possible way to improve <str<strong>on</strong>g>parallel</str<strong>on</strong>g> running time without loosing <str<strong>on</strong>g>the</str<strong>on</strong>g>efficient computati<strong>on</strong> of <str<strong>on</strong>g>NCA</str<strong>on</strong>g> queries. The idea is that <str<strong>on</strong>g>the</str<strong>on</strong>g> scheme should compress all paths present in <str<strong>on</strong>g>the</str<strong>on</strong>g> tree (even<str<strong>on</strong>g>the</str<strong>on</strong>g> internal <strong>on</strong>es), but leaf compressi<strong>on</strong>s should not be per<str<strong>on</strong>g>for</str<strong>on</strong>g>med <strong>on</strong> nodes that are currently not leaves. This idea istranslated into a c<strong>on</strong>crete <str<strong>on</strong>g>parallel</str<strong>on</strong>g> algorithm in <str<strong>on</strong>g>the</str<strong>on</strong>g> next secti<strong>on</strong>.7.2. The algorithmWe start by introducing some notati<strong>on</strong>. For a node v in T, T v denotes <str<strong>on</strong>g>the</str<strong>on</strong>g> subtree of T rooted at node v. A <str<strong>on</strong>g>parallel</str<strong>on</strong>g>phase i of <str<strong>on</strong>g>the</str<strong>on</strong>g> algorithm is <str<strong>on</strong>g>the</str<strong>on</strong>g> sequence of two <str<strong>on</strong>g>parallel</str<strong>on</strong>g> steps called a <str<strong>on</strong>g>and</str<strong>on</strong>g> b, which are executed at <str<strong>on</strong>g>parallel</str<strong>on</strong>g> time i(a)<str<strong>on</strong>g>and</str<strong>on</strong>g> i(b), respectively. For an integer i, T i denotes <str<strong>on</strong>g>the</str<strong>on</strong>g> tree after <str<strong>on</strong>g>the</str<strong>on</strong>g> ith <str<strong>on</strong>g>parallel</str<strong>on</strong>g> phase. Given a tree T i , <str<strong>on</strong>g>the</str<strong>on</strong>g> result of stepa applied to T i is <str<strong>on</strong>g>the</str<strong>on</strong>g> tree Ti+1 a <str<strong>on</strong>g>and</str<strong>on</strong>g> <str<strong>on</strong>g>the</str<strong>on</strong>g> result of step b applied to Taiis T i .During <str<strong>on</strong>g>the</str<strong>on</strong>g> processing, nodes in <str<strong>on</strong>g>the</str<strong>on</strong>g> tree may get marked with <str<strong>on</strong>g>the</str<strong>on</strong>g> symbol L; if node v in T is marked L at <str<strong>on</strong>g>parallel</str<strong>on</strong>g>time i(a) (i(b)), <str<strong>on</strong>g>the</str<strong>on</strong>g>n we denote this with m a i (v) = L (m i(v) = L). We will often refer to this marking as m(v) when<str<strong>on</strong>g>the</str<strong>on</strong>g> time is clear from <str<strong>on</strong>g>the</str<strong>on</strong>g> c<strong>on</strong>text. If v is not marked <str<strong>on</strong>g>the</str<strong>on</strong>g>n m(v) =?. Every node v in T has a pointer π to an ancestor ofv at <str<strong>on</strong>g>parallel</str<strong>on</strong>g> time i(a) (i(b)) <str<strong>on</strong>g>and</str<strong>on</strong>g> we denote it with π a i (v) (π i(v)).A leaf compressi<strong>on</strong> of a tree T i is executed in step i(a) <str<strong>on</strong>g>and</str<strong>on</strong>g> returns a tree T ai+1 such that <str<strong>on</strong>g>for</str<strong>on</strong>g> each node v in T i (seeFig. 14):


128 A. D. Palú et al. / Theoretical Computer Science 352 (2006) 108 – 135π k-1π k-1parent(v)π kaaπ kπ kLzLvaπ kLvLvFig. 14. Example of leaf-compressi<strong>on</strong> <str<strong>on</strong>g>for</str<strong>on</strong>g> node v. Fig. 15. Example of path-compressi<strong>on</strong> <str<strong>on</strong>g>for</str<strong>on</strong>g> node v.(i) if (m i (v) = L <str<strong>on</strong>g>and</str<strong>on</strong>g> v currently has no sibling) <str<strong>on</strong>g>the</str<strong>on</strong>g>nπ a i+1 (v) ← π i(parent(v)) <str<strong>on</strong>g>and</str<strong>on</strong>g> m a i+1 (πa i+1 (v)) ← L;(ii) if (m i (v) = L <str<strong>on</strong>g>and</str<strong>on</strong>g> v has a sibling z <str<strong>on</strong>g>and</str<strong>on</strong>g> m i (z) =?) <str<strong>on</strong>g>the</str<strong>on</strong>g>nv is merged with its parent π a i+1 (v) ← NULL;(iii) if (m i (v) =? <str<strong>on</strong>g>and</str<strong>on</strong>g> ((v has a sibling z <str<strong>on</strong>g>and</str<strong>on</strong>g> m i (z) = L) or(v currently has no sibling))) <str<strong>on</strong>g>the</str<strong>on</strong>g>nπ a i+1 (v) ← π i(parent(v));(iv) if (m i (v) = L <str<strong>on</strong>g>and</str<strong>on</strong>g> v has a left sibling z <str<strong>on</strong>g>and</str<strong>on</strong>g> m i (z) = L) <str<strong>on</strong>g>the</str<strong>on</strong>g>nv is merged with its parent <str<strong>on</strong>g>and</str<strong>on</strong>g> π a i+1 (v) ← NULL;(v) if (m i (v) = L, v has a right sibling z <str<strong>on</strong>g>and</str<strong>on</strong>g> m i (z) = L) <str<strong>on</strong>g>the</str<strong>on</strong>g>nπ a i+1 (v) ← π i(parent(v)) <str<strong>on</strong>g>and</str<strong>on</strong>g> m a i+1 (πa i+1 (v)) ← L.A path compressi<strong>on</strong> of a tree Tia is executed in step i(b) <str<strong>on</strong>g>and</str<strong>on</strong>g> returns a tree T i , such that <str<strong>on</strong>g>for</str<strong>on</strong>g> each v in Ti a,π i(v) ←π a i (πa i (v)) <str<strong>on</strong>g>and</str<strong>on</strong>g> if ma i (v) = L <str<strong>on</strong>g>the</str<strong>on</strong>g>n m i(π i (v)) ← L (see Fig. 15).If T is <str<strong>on</strong>g>the</str<strong>on</strong>g> initial tree, <str<strong>on</strong>g>the</str<strong>on</strong>g>n <str<strong>on</strong>g>the</str<strong>on</strong>g> tree T 0 is a copy of T, such that <str<strong>on</strong>g>for</str<strong>on</strong>g> each v in T 0 , π 0 (v) = v if v has a sibling, elseπ 0 (v) = parent(v); in additi<strong>on</strong>, <str<strong>on</strong>g>for</str<strong>on</strong>g> each leaf l of T 0 , m 0 (l) = L. The root is <str<strong>on</strong>g>the</str<strong>on</strong>g> <strong>on</strong>ly excepti<strong>on</strong>: π 0 (root) = root.Fig. 16 provides an example of a compressi<strong>on</strong>. The nodes marked represent <str<strong>on</strong>g>the</str<strong>on</strong>g> nodes labeled L <str<strong>on</strong>g>and</str<strong>on</strong>g> <str<strong>on</strong>g>the</str<strong>on</strong>g> dashedpointers are <str<strong>on</strong>g>the</str<strong>on</strong>g> π pointers. The pointers π pointing to NULL are not shown. The figure also shows <str<strong>on</strong>g>the</str<strong>on</strong>g> <str<strong>on</strong>g>parallel</str<strong>on</strong>g> H-tree(discussed in Secti<strong>on</strong> 7.3).Definiti<strong>on</strong> 6. A node x is finished after step k if <strong>on</strong>e of <str<strong>on</strong>g>the</str<strong>on</strong>g> following holds:(1) x is root <str<strong>on</strong>g>and</str<strong>on</strong>g> m k (x) = L;(2) ∃y y is a proper ancestor of x <str<strong>on</strong>g>and</str<strong>on</strong>g> m k (y) = L;(3) π k (x) = NULL.The <str<strong>on</strong>g>the</str<strong>on</strong>g>orem below provides a result that is critical <str<strong>on</strong>g>for</str<strong>on</strong>g> establishing <str<strong>on</strong>g>the</str<strong>on</strong>g> efficiency of <str<strong>on</strong>g>the</str<strong>on</strong>g> compressi<strong>on</strong> scheme.Theorem 7. For each <str<strong>on</strong>g>parallel</str<strong>on</strong>g> time step k <str<strong>on</strong>g>and</str<strong>on</strong>g> <str<strong>on</strong>g>for</str<strong>on</strong>g> each node x in T <strong>on</strong>e of <str<strong>on</strong>g>the</str<strong>on</strong>g> following holds:(1) x is finished be<str<strong>on</strong>g>for</str<strong>on</strong>g>e or at <str<strong>on</strong>g>the</str<strong>on</strong>g> end of <str<strong>on</strong>g>parallel</str<strong>on</strong>g> step k;(2) x is marked L during <str<strong>on</strong>g>parallel</str<strong>on</strong>g> step k, it is unfinished after <str<strong>on</strong>g>parallel</str<strong>on</strong>g> step k, |T x |2 k−1 <str<strong>on</strong>g>and</str<strong>on</strong>g> |T parent(x) |2 k + 1;(3) x is unmarked <str<strong>on</strong>g>and</str<strong>on</strong>g> unfinished after <str<strong>on</strong>g>parallel</str<strong>on</strong>g> step k, <str<strong>on</strong>g>and</str<strong>on</strong>g> ei<str<strong>on</strong>g>the</str<strong>on</strong>g>r π k (x) = root or (|T parent(πk (x))| −|T x |2 k−1 <str<strong>on</strong>g>and</str<strong>on</strong>g>|T x |2 k + 1).


A. D. Palú et al. / Theoretical Computer Science 352 (2006) 108 – 135 1291.Tree T0 2. Tree T1 a3. Tree T11112 34 56 7 89 10 11 12 13 142 34 56 7 89 10 11 12 13 142 34 56 7 89 10 11 12 13 14a4. Tree T215. Tree T21The H-Tree1’2 32 39 6 4 2 17’3’4 54 511 7 138536 7 86 7 812149 10 11 12 13 149 10 11 12 13 14Fig. 16. An example of <str<strong>on</strong>g>parallel</str<strong>on</strong>g> compressi<strong>on</strong>.Proof.Base case: k = 0. If x is finished after step 0, <str<strong>on</strong>g>the</str<strong>on</strong>g>n x can <strong>on</strong>ly be <str<strong>on</strong>g>the</str<strong>on</strong>g> root <str<strong>on</strong>g>and</str<strong>on</strong>g> T = x, since <strong>on</strong>ly leaves are marked withL <str<strong>on</strong>g>and</str<strong>on</strong>g> <str<strong>on</strong>g>for</str<strong>on</strong>g> each v in T, π(v) = v.Ifm 0 (x) = L <str<strong>on</strong>g>the</str<strong>on</strong>g>n x is a leaf <str<strong>on</strong>g>and</str<strong>on</strong>g> trivially |T x |2 0−1 = 2 −1 <str<strong>on</strong>g>and</str<strong>on</strong>g> |T parent(x) |2 0 +1 = 2.If x is unmarked <str<strong>on</strong>g>and</str<strong>on</strong>g> unfinished, x is an internal node in T.In this case if x is <str<strong>on</strong>g>the</str<strong>on</strong>g> root <str<strong>on</strong>g>the</str<strong>on</strong>g>n π 0 (x) = root else |T parent(x) |−|T x |2 0−1 = 2 −1 <str<strong>on</strong>g>and</str<strong>on</strong>g> |T x |2 0 + 1 = 2.Inductive step: Let us c<strong>on</strong>sider <str<strong>on</strong>g>the</str<strong>on</strong>g> case k, <str<strong>on</strong>g>and</str<strong>on</strong>g> let us assume by inductive hypo<str<strong>on</strong>g>the</str<strong>on</strong>g>sis that <str<strong>on</strong>g>the</str<strong>on</strong>g> results hold <str<strong>on</strong>g>for</str<strong>on</strong>g> allintegers less than k. For every node x in T, <strong>on</strong>e of <str<strong>on</strong>g>the</str<strong>on</strong>g> following holds:(1) x is finished <str<strong>on</strong>g>and</str<strong>on</strong>g> unmarked after phase k. In this case, <str<strong>on</strong>g>the</str<strong>on</strong>g> first claim of <str<strong>on</strong>g>the</str<strong>on</strong>g> <str<strong>on</strong>g>the</str<strong>on</strong>g>orem is trivially satisfied.(2) x is marked L after phase k ′ k − 1. In this case, <strong>on</strong>e of <str<strong>on</strong>g>the</str<strong>on</strong>g> following is true:(i) if x is <str<strong>on</strong>g>the</str<strong>on</strong>g> root, <str<strong>on</strong>g>the</str<strong>on</strong>g>n x is also marked L after phase k;(ii) if x is not <str<strong>on</strong>g>the</str<strong>on</strong>g> root, <str<strong>on</strong>g>the</str<strong>on</strong>g>n during step k ′ + 1(a) x can be leaf compressed to its parent <str<strong>on</strong>g>and</str<strong>on</strong>g> π k ′(x) = NULL;(iii) o<str<strong>on</strong>g>the</str<strong>on</strong>g>rwise, if x is not leaf compressed this implies that x had a right sibling y marked L after phase k ′ − 1.Thus, π k ′(x) is a proper ancestor of x <str<strong>on</strong>g>and</str<strong>on</strong>g> it is marked L after phase k ′ .Hence x is finished after phase k ′ + 1 <str<strong>on</strong>g>and</str<strong>on</strong>g> it is also finished after phase k.(3) x is marked L during phase k.There are two possibilities:Case 1: x is marked L during step k(a).Ifx is <str<strong>on</strong>g>the</str<strong>on</strong>g> root, <str<strong>on</strong>g>the</str<strong>on</strong>g>n x is finished after phase k <str<strong>on</strong>g>and</str<strong>on</strong>g> <str<strong>on</strong>g>the</str<strong>on</strong>g> claim holds. O<str<strong>on</strong>g>the</str<strong>on</strong>g>rwiselet x ′ = parent(x). Suppose x does not have any siblings after step k(a), <str<strong>on</strong>g>the</str<strong>on</strong>g>n an ancestor of x ′ is marked L afterstep k(b) (because of pointer doubling) <str<strong>on</strong>g>and</str<strong>on</strong>g> x is finished after step k. Ifx has a sibling z after step k(a), <str<strong>on</strong>g>the</str<strong>on</strong>g>nz could not have been marked L after step k − 1, o<str<strong>on</strong>g>the</str<strong>on</strong>g>rwise it would have been leaf compressed. There<str<strong>on</strong>g>for</str<strong>on</strong>g>e byinductive hypo<str<strong>on</strong>g>the</str<strong>on</strong>g>sis (case (3)) |T z |2 k−1 . Also, since x is unmarked after step k − 1, |T x |2 k−1 . Moreover,|T parent(x) |=|T x ′|=|T z |+|T x |+12 k + 1.Case 2: x is marked L during step k(b).Ifx is <str<strong>on</strong>g>the</str<strong>on</strong>g> root, <str<strong>on</strong>g>the</str<strong>on</strong>g>n it is finished after step k <str<strong>on</strong>g>and</str<strong>on</strong>g> <str<strong>on</strong>g>the</str<strong>on</strong>g> claim holds. O<str<strong>on</strong>g>the</str<strong>on</strong>g>rwiselet x ′ = parent(x).


130 A. D. Palú et al. / Theoretical Computer Science 352 (2006) 108 – 135x′ = p(x)π k-1vv′ = p(v)xzπ kau′ = p(u)u’π k-1uzπ kauxFig. 17. x marked during step k(b). Fig. 18. x unmarked <str<strong>on</strong>g>and</str<strong>on</strong>g> unfinished after phase k.Suppose that x does not have any sibling after step k(a). It follows that π a k (x) is an ancestor of x′ because π k−1 (x)was pointing at least to x ′ . Since x is unmarked <str<strong>on</strong>g>and</str<strong>on</strong>g> unfinished after step k −1, <str<strong>on</strong>g>the</str<strong>on</strong>g>re exists a descendent u of x suchthat m k−1 (u) = L <str<strong>on</strong>g>and</str<strong>on</strong>g> π a k (πa k (u)) = x. Let us denote with u′ <str<strong>on</strong>g>the</str<strong>on</strong>g> node π a k (u). Clearly u′ ̸= x, o<str<strong>on</strong>g>the</str<strong>on</strong>g>rwise x wouldbe marked in step k(a). Note that u ′ will be marked L after step k(a). It follows that π k (u ′ ) = π a k (πa k (u′ )) = π a k (x)which is a proper ancestor of x—since x does not have siblings after step k(a). π k (u ′ ) is marked L during step k(b).There<str<strong>on</strong>g>for</str<strong>on</strong>g>e x is finished after phase k.Suppose instead that x has a sibling z after step k(a) (see Fig. 17). Then m k−1 (z) ̸= L o<str<strong>on</strong>g>the</str<strong>on</strong>g>rwise z would havebeen leaf compressed at step k(a). There<str<strong>on</strong>g>for</str<strong>on</strong>g>e by inductive hypo<str<strong>on</strong>g>the</str<strong>on</strong>g>sis (case (3)) wehave|T z |2 k−1 . Moreoverm k−1 (x) ̸= L <str<strong>on</strong>g>and</str<strong>on</strong>g>, using <str<strong>on</strong>g>the</str<strong>on</strong>g> same inductive hypo<str<strong>on</strong>g>the</str<strong>on</strong>g>sis, |T x |2 k−1 . Finally |T parent(x) |=|T x ′|=|T x |+|T z |+12 k + 1.(4) x unmarked <str<strong>on</strong>g>and</str<strong>on</strong>g> unfinished after phase k.If x is <str<strong>on</strong>g>the</str<strong>on</strong>g> root, <str<strong>on</strong>g>the</str<strong>on</strong>g>n π k (x) = root <str<strong>on</strong>g>and</str<strong>on</strong>g> <str<strong>on</strong>g>the</str<strong>on</strong>g> claim holds. If a proper ancestor of x is marked L after phase k,<str<strong>on</strong>g>the</str<strong>on</strong>g>n it follows that x is finished after phase k <str<strong>on</strong>g>and</str<strong>on</strong>g> <str<strong>on</strong>g>the</str<strong>on</strong>g> claim holds. O<str<strong>on</strong>g>the</str<strong>on</strong>g>rwise, we have that x is unfinished<str<strong>on</strong>g>and</str<strong>on</strong>g> x has no ancestors marked L after step k. Let u be equal to π k−1 (x) (see Fig. 18). If u is <str<strong>on</strong>g>the</str<strong>on</strong>g> root, <str<strong>on</strong>g>the</str<strong>on</strong>g>π k (x) = root <str<strong>on</strong>g>and</str<strong>on</strong>g> <str<strong>on</strong>g>the</str<strong>on</strong>g> claim holds. O<str<strong>on</strong>g>the</str<strong>on</strong>g>rwise, let u ′ be <str<strong>on</strong>g>the</str<strong>on</strong>g> parent of u, <str<strong>on</strong>g>and</str<strong>on</strong>g> v = π k−1 (u ′ ).Ifv is <str<strong>on</strong>g>the</str<strong>on</strong>g> root,<str<strong>on</strong>g>the</str<strong>on</strong>g>n π k (x) = v = root <str<strong>on</strong>g>and</str<strong>on</strong>g> <str<strong>on</strong>g>the</str<strong>on</strong>g> claim holds. O<str<strong>on</strong>g>the</str<strong>on</strong>g>rwise let v ′ = parent(v). By inductive hypo<str<strong>on</strong>g>the</str<strong>on</strong>g>sis (case (3))|T parent(πk−1 (x))|−|T x |2 k−2 <str<strong>on</strong>g>and</str<strong>on</strong>g> |T parent(πk−1 (u ′ ))|−|T u ′|2 k−2 .Ifu has no siblings or has a sibling marked L afterphase k − 1, <str<strong>on</strong>g>the</str<strong>on</strong>g>n π a k (u) = π k−1(u ′ ) = v (because of <str<strong>on</strong>g>the</str<strong>on</strong>g> leaf compressi<strong>on</strong>) <str<strong>on</strong>g>and</str<strong>on</strong>g> π k (x) is at least v or above. Then|T parent(πk (x))||T parent(v) |=|T parent(πk−1 (u ′ ))||T u ′|+2 k−2 =|T parent(πk−1 (x))|+2 k−2 |T x |+2 k−1 . There<str<strong>on</strong>g>for</str<strong>on</strong>g>e|T parent(πk (x))|−|T x |2 k−1 .Ifu has an unmarked sibling z after phase k − 1, <str<strong>on</strong>g>the</str<strong>on</strong>g>n by inductive hypo<str<strong>on</strong>g>the</str<strong>on</strong>g>sis (case(3)) |T z |2 k−1 . Also, π k (x) = π k−1 (x) = u, <str<strong>on</strong>g>the</str<strong>on</strong>g>n |T parent(πk (x)|−|T x |=|T u ′|−|T x ||T z |2 k−1 .InT x <str<strong>on</strong>g>the</str<strong>on</strong>g>reexists a node y such that m k (y) = L <str<strong>on</strong>g>and</str<strong>on</strong>g> it is unfinished after phase k. y ̸= x because x is not marked L after phasek. Thus, x is a proper ancestor of y <str<strong>on</strong>g>and</str<strong>on</strong>g> from <str<strong>on</strong>g>the</str<strong>on</strong>g> above proof of Case 2 we have that |T x ||T parent(y) |2 k + 1. □Corollary 3. Let n be <str<strong>on</strong>g>the</str<strong>on</strong>g> number of nodes in T <str<strong>on</strong>g>and</str<strong>on</strong>g> let k be <str<strong>on</strong>g>the</str<strong>on</strong>g> smallest integer such that <str<strong>on</strong>g>the</str<strong>on</strong>g> root is finished afterphase k. Then n2 k−1 + 1. In o<str<strong>on</strong>g>the</str<strong>on</strong>g>r words, <str<strong>on</strong>g>the</str<strong>on</strong>g> algorithm requires at most lg(n − 1) + 1 phases.Proof. The root r of T is unfinished (hence unmarked) after phase k −1, o<str<strong>on</strong>g>the</str<strong>on</strong>g>rwise k would not be <str<strong>on</strong>g>the</str<strong>on</strong>g> minimum integer.Let us c<strong>on</strong>sider a node x that is marked L <str<strong>on</strong>g>and</str<strong>on</strong>g> is unfinished after phase k − 1. The node x must be a proper descendantof r. From Theorem 7 it follows that |T ||T parent(x) |2 k−1 + 1. □If we have n processors that have been assigned to <str<strong>on</strong>g>the</str<strong>on</strong>g> n different nodes of T, <str<strong>on</strong>g>the</str<strong>on</strong>g>n both leaf <str<strong>on</strong>g>and</str<strong>on</strong>g> path compressi<strong>on</strong>swill be per<str<strong>on</strong>g>for</str<strong>on</strong>g>med in c<strong>on</strong>stant <str<strong>on</strong>g>parallel</str<strong>on</strong>g> time. From Corollary 3, <str<strong>on</strong>g>the</str<strong>on</strong>g> total number of compressi<strong>on</strong>s is at most lg n, thus<str<strong>on</strong>g>the</str<strong>on</strong>g> total <str<strong>on</strong>g>parallel</str<strong>on</strong>g> time required by <str<strong>on</strong>g>the</str<strong>on</strong>g> algorithm is O(lg n).


A. D. Palú et al. / Theoretical Computer Science 352 (2006) 108 – 135 131Even though <str<strong>on</strong>g>the</str<strong>on</strong>g> algorithm has been presented <str<strong>on</strong>g>for</str<strong>on</strong>g> binary trees, <str<strong>on</strong>g>the</str<strong>on</strong>g> above results remain true even if we c<strong>on</strong>sidertrees with arbitrary arity. In this case <str<strong>on</strong>g>the</str<strong>on</strong>g> leaf compressi<strong>on</strong> will change as follows: <str<strong>on</strong>g>the</str<strong>on</strong>g> last node marked L am<strong>on</strong>g a setof siblings is <str<strong>on</strong>g>the</str<strong>on</strong>g> <strong>on</strong>ly <strong>on</strong>e that is not leaf compressed. If all unfinished siblings are marked L at <str<strong>on</strong>g>the</str<strong>on</strong>g> same time, <str<strong>on</strong>g>the</str<strong>on</strong>g>n <str<strong>on</strong>g>the</str<strong>on</strong>g>node not leaf compressed will be <str<strong>on</strong>g>the</str<strong>on</strong>g> leftmost <strong>on</strong>e. The proofs in this case remain identical.7.3. The H-treeThe H-Tree built in <str<strong>on</strong>g>the</str<strong>on</strong>g> <str<strong>on</strong>g>parallel</str<strong>on</strong>g> scheme is similar to <str<strong>on</strong>g>the</str<strong>on</strong>g> <strong>on</strong>e described in Secti<strong>on</strong> 4. The difference is that, here,each path-compressed branch is extended to <str<strong>on</strong>g>the</str<strong>on</strong>g> leftmost leaf, instead of being split into more levels by alternati<strong>on</strong> ofsequential leaf <str<strong>on</strong>g>and</str<strong>on</strong>g> path compressi<strong>on</strong>s. The topological relati<strong>on</strong> <str<strong>on</strong>g>for</str<strong>on</strong>g> <str<strong>on</strong>g>NCA</str<strong>on</strong>g> queries are never<str<strong>on</strong>g>the</str<strong>on</strong>g>less preserved, <str<strong>on</strong>g>and</str<strong>on</strong>g> thus<str<strong>on</strong>g>the</str<strong>on</strong>g> same <str<strong>on</strong>g>NCA</str<strong>on</strong>g> query algorithm can be employed.It is possible to reuse <str<strong>on</strong>g>the</str<strong>on</strong>g> π pointers to build H in c<strong>on</strong>stant <str<strong>on</strong>g>parallel</str<strong>on</strong>g> time. Once a node v is leaf compressed into itsparent, π(v) is set to NULL. At that time <str<strong>on</strong>g>for</str<strong>on</strong>g> every node w in <str<strong>on</strong>g>the</str<strong>on</strong>g> path having v as head, we have π(w) = v. The goalis to maintain this in<str<strong>on</strong>g>for</str<strong>on</strong>g>mati<strong>on</strong> in <str<strong>on</strong>g>the</str<strong>on</strong>g> successive phases, avoiding pointer doubling if a node is finished (line 26 in Fig.19). Once <str<strong>on</strong>g>the</str<strong>on</strong>g> compressi<strong>on</strong> is completed, every head of a path x has π(x) = NULL <str<strong>on</strong>g>and</str<strong>on</strong>g> <str<strong>on</strong>g>the</str<strong>on</strong>g> π pointer <str<strong>on</strong>g>for</str<strong>on</strong>g> every o<str<strong>on</strong>g>the</str<strong>on</strong>g>rnode y points to <str<strong>on</strong>g>the</str<strong>on</strong>g> head of <str<strong>on</strong>g>the</str<strong>on</strong>g> path c<strong>on</strong>taining y.Let us introduce ano<str<strong>on</strong>g>the</str<strong>on</strong>g>r pointer p H , that will be used as <str<strong>on</strong>g>the</str<strong>on</strong>g> parent pointer in H. During a leaf compressi<strong>on</strong> <str<strong>on</strong>g>the</str<strong>on</strong>g>pointer p H (v) is set to point to parent(v). Since <str<strong>on</strong>g>the</str<strong>on</strong>g> root is not leaf compressed, p H (root) is set to point to NULL.Foreach head x of a path l in T, a new node x ′ is created in H, with π(x ′ ) = x ′ , p H (x ′ ) = p H (x) <str<strong>on</strong>g>and</str<strong>on</strong>g> π(x) = x ′ . After eachstep of pointer doubling (applied to <str<strong>on</strong>g>the</str<strong>on</strong>g> π pointers), every node in a path points to a newly created copy of <str<strong>on</strong>g>the</str<strong>on</strong>g> head.For every node x in a path p H (x) = π(x), <str<strong>on</strong>g>and</str<strong>on</strong>g> this completes <str<strong>on</strong>g>the</str<strong>on</strong>g> building of H.Finally, <str<strong>on</strong>g>for</str<strong>on</strong>g> each path <str<strong>on</strong>g>the</str<strong>on</strong>g> auxiliary data structure <str<strong>on</strong>g>for</str<strong>on</strong>g> <str<strong>on</strong>g>the</str<strong>on</strong>g> TPProblem is set up. It is possible to identify <str<strong>on</strong>g>the</str<strong>on</strong>g> tails ofpath in O(1) time as a node v is a tail iff <str<strong>on</strong>g>for</str<strong>on</strong>g> all children w of v π(v) ̸= π(w). Given a tail t, <str<strong>on</strong>g>the</str<strong>on</strong>g> corresp<strong>on</strong>ding list isprocessed as described in Secti<strong>on</strong> 7.6. The complete algorithm is presented in Fig. 19. The lines marked with * are <str<strong>on</strong>g>the</str<strong>on</strong>g><strong>on</strong>es necessary to set up <str<strong>on</strong>g>the</str<strong>on</strong>g> H tree.7.4. Answering <str<strong>on</strong>g>NCA</str<strong>on</strong>g> queries using H-treesThe H-tree can be used to answer <str<strong>on</strong>g>NCA</str<strong>on</strong>g> queries in <str<strong>on</strong>g>the</str<strong>on</strong>g> same way as in <str<strong>on</strong>g>the</str<strong>on</strong>g> sequential case. In Secti<strong>on</strong> 4.3 we showedthat <str<strong>on</strong>g>the</str<strong>on</strong>g>re is a PPM algorithm that, given a tree with height h, pre-processes <str<strong>on</strong>g>the</str<strong>on</strong>g> tree in time O(n lg h) <str<strong>on</strong>g>and</str<strong>on</strong>g> <str<strong>on</strong>g>the</str<strong>on</strong>g>n cancompute <str<strong>on</strong>g>the</str<strong>on</strong>g> <str<strong>on</strong>g>NCA</str<strong>on</strong>g> of any two given nodes in <str<strong>on</strong>g>the</str<strong>on</strong>g> tree in worst case time complexity O(lg h) per query. The sequentialscheme presented in Secti<strong>on</strong> 4.3 can be easily translated into a <str<strong>on</strong>g>parallel</str<strong>on</strong>g> scheme that uses n processors <str<strong>on</strong>g>and</str<strong>on</strong>g> O(lg h)<str<strong>on</strong>g>parallel</str<strong>on</strong>g> time <str<strong>on</strong>g>for</str<strong>on</strong>g> pre-processing. Using this result, we can pre-process <str<strong>on</strong>g>the</str<strong>on</strong>g> H-tree in <str<strong>on</strong>g>parallel</str<strong>on</strong>g> time O(lg lg n) using nprocessors. Then, <str<strong>on</strong>g>the</str<strong>on</strong>g> <str<strong>on</strong>g>NCA</str<strong>on</strong>g> in H, <str<strong>on</strong>g>and</str<strong>on</strong>g> hence in T, can be computed in time O(lg lg n) using a single processor.7.5. Discussi<strong>on</strong>The algorithm described above clearly can be directly implemented <strong>on</strong> a CRCW Parallel PPM. A <str<strong>on</strong>g>problem</str<strong>on</strong>g> arises ifwe were not allowed c<strong>on</strong>current writes, because too many processors may attempt to update <str<strong>on</strong>g>the</str<strong>on</strong>g> L mark of <str<strong>on</strong>g>the</str<strong>on</strong>g> samenode in <str<strong>on</strong>g>the</str<strong>on</strong>g> tree at <str<strong>on</strong>g>the</str<strong>on</strong>g> same time (e.g., line 10 in Fig. 19). This will not be allowed in <str<strong>on</strong>g>the</str<strong>on</strong>g> CREW/EREW/CROW <str<strong>on</strong>g>parallel</str<strong>on</strong>g>pointer machines. This is also not allowed in <str<strong>on</strong>g>the</str<strong>on</strong>g> Parallel PPM (as described in Secti<strong>on</strong> 2) because it would corresp<strong>on</strong>dto an unbounded fan-in. However, it is possible to modify <str<strong>on</strong>g>the</str<strong>on</strong>g> algorithm to overcome this <str<strong>on</strong>g>problem</str<strong>on</strong>g>. This is essentiallyobtained by c<strong>on</strong>currently per<str<strong>on</strong>g>for</str<strong>on</strong>g>ming a pointer doubling in <str<strong>on</strong>g>the</str<strong>on</strong>g> reverse directi<strong>on</strong> al<strong>on</strong>g <str<strong>on</strong>g>the</str<strong>on</strong>g> branches of <str<strong>on</strong>g>the</str<strong>on</strong>g> tree. Moreprecisely, each node u of <str<strong>on</strong>g>the</str<strong>on</strong>g> tree maintains a pointer π down (u) which is updated to point to π down (v) whenever <str<strong>on</strong>g>the</str<strong>on</strong>g>node has <strong>on</strong>ly <strong>on</strong>e child v. In additi<strong>on</strong>, if π down (v) is marked L, <str<strong>on</strong>g>the</str<strong>on</strong>g>n u will mark itself L as well. With this additi<strong>on</strong>, <str<strong>on</strong>g>the</str<strong>on</strong>g>fan-in of each unit is restricted to be finite. Moreover, <str<strong>on</strong>g>the</str<strong>on</strong>g> algorithm still requires <strong>on</strong>ly O(lg n) <str<strong>on</strong>g>parallel</str<strong>on</strong>g> phases. Hence,<str<strong>on</strong>g>the</str<strong>on</strong>g> algorithm can be modified to correctly work <strong>on</strong> Parallel PPMs.The algorithm requires n processors to per<str<strong>on</strong>g>for</str<strong>on</strong>g>m <str<strong>on</strong>g>the</str<strong>on</strong>g> O(lg n) <str<strong>on</strong>g>parallel</str<strong>on</strong>g> time pre-processing. After pre-processing, asingle processor can answer an <str<strong>on</strong>g>NCA</str<strong>on</strong>g> query in time O(lg lg n). It is interesting to compare this result with <str<strong>on</strong>g>the</str<strong>on</strong>g> o<str<strong>on</strong>g>the</str<strong>on</strong>g>r<str<strong>on</strong>g>parallel</str<strong>on</strong>g> <str<strong>on</strong>g>algorithms</str<strong>on</strong>g> proposed <str<strong>on</strong>g>for</str<strong>on</strong>g> <str<strong>on</strong>g>the</str<strong>on</strong>g> <str<strong>on</strong>g>NCA</str<strong>on</strong>g> <str<strong>on</strong>g>problem</str<strong>on</strong>g>. The best known PRAM <str<strong>on</strong>g>algorithms</str<strong>on</strong>g> require O(n/ lg n) processors<str<strong>on</strong>g>and</str<strong>on</strong>g> work in O(lg n) <str<strong>on</strong>g>parallel</str<strong>on</strong>g> time <str<strong>on</strong>g>for</str<strong>on</strong>g> pre-processing. After pre-processing, a single processor can answer an <str<strong>on</strong>g>NCA</str<strong>on</strong>g> queryin time O(1). Hence, going from a PRAM to a Parallel PPM we incur a penalty of O(lg n) in number of processors


132 A. D. Palú et al. / Theoretical Computer Science 352 (2006) 108 – 135Fig. 19. Pre-processing algorithm.<str<strong>on</strong>g>and</str<strong>on</strong>g> total time taken, <str<strong>on</strong>g>and</str<strong>on</strong>g> a penalty of O(lg lg n) time to answer a query. Observe that we do not incur any penalty in<str<strong>on</strong>g>parallel</str<strong>on</strong>g> time <str<strong>on</strong>g>for</str<strong>on</strong>g> pre-processing.It is also important to observe that if we have any CROW PRAM <str<strong>on</strong>g>NCA</str<strong>on</strong>g> algorithm which solves <str<strong>on</strong>g>the</str<strong>on</strong>g> <str<strong>on</strong>g>problem</str<strong>on</strong>g> in <str<strong>on</strong>g>parallel</str<strong>on</strong>g>time O(lg n) with f (n) processors <str<strong>on</strong>g>and</str<strong>on</strong>g> answers a query in O(1) time, <str<strong>on</strong>g>the</str<strong>on</strong>g>n <str<strong>on</strong>g>for</str<strong>on</strong>g> a generic translati<strong>on</strong> of this algorithmto a Parallel PPM algorithm (as illustrated in [9]) <strong>on</strong>e can <strong>on</strong>ly claim that it requires <str<strong>on</strong>g>parallel</str<strong>on</strong>g> time O(lg n lg lg n) withpolynomially many processors, <str<strong>on</strong>g>and</str<strong>on</strong>g> answers an <str<strong>on</strong>g>NCA</str<strong>on</strong>g> query in time O(lg lg n). Hence, <str<strong>on</strong>g>the</str<strong>on</strong>g> algorithm presented hereis substantially better than a generic translati<strong>on</strong> of any PRAM <str<strong>on</strong>g>NCA</str<strong>on</strong>g> algorithm presented in <str<strong>on</strong>g>the</str<strong>on</strong>g> literature to date to aParallel PPM algorithm.It is interesting to note that if we have simple arithmetic capabilities (actually <strong>on</strong>ly c<strong>on</strong>stant-time additi<strong>on</strong> is needed),<str<strong>on</strong>g>the</str<strong>on</strong>g>n we can compute <str<strong>on</strong>g>the</str<strong>on</strong>g> centroid path <str<strong>on</strong>g>and</str<strong>on</strong>g> <str<strong>on</strong>g>the</str<strong>on</strong>g> H-tree based <strong>on</strong> it in O(lg n) <str<strong>on</strong>g>parallel</str<strong>on</strong>g> time. This is obtained by keepinga count of <str<strong>on</strong>g>the</str<strong>on</strong>g> number of nodes in <str<strong>on</strong>g>the</str<strong>on</strong>g> subtree rooted in each node during <str<strong>on</strong>g>the</str<strong>on</strong>g> algorithm executi<strong>on</strong>. Each time wehave a leaf compressi<strong>on</strong> phase where both children of a node are marked L, instead of leaf compressing <str<strong>on</strong>g>the</str<strong>on</strong>g> rightchild, we leaf compress always <str<strong>on</strong>g>the</str<strong>on</strong>g> child with a smaller count. It is easy to show that this will build <str<strong>on</strong>g>the</str<strong>on</strong>g> centroidpath tree.


A. D. Palú et al. / Theoretical Computer Science 352 (2006) 108 – 135 133All horiz<strong>on</strong>talpointers in <str<strong>on</strong>g>the</str<strong>on</strong>g> treeare sibl pointersAdding <str<strong>on</strong>g>the</str<strong>on</strong>g>sec<strong>on</strong>d mapspointerPointer doubling<str<strong>on</strong>g>and</str<strong>on</strong>g> activati<strong>on</strong> ofnew nodesAll horiz<strong>on</strong>tal pointers in Lare previous pointersAdding newmaps pointersFig. 20. BT tree <str<strong>on</strong>g>and</str<strong>on</strong>g> list L.Fig. 21. Example of mapping.Note that if we are allowed <strong>on</strong>ly <strong>on</strong>e processor to answer an <str<strong>on</strong>g>NCA</str<strong>on</strong>g> query, <str<strong>on</strong>g>the</str<strong>on</strong>g>n <str<strong>on</strong>g>the</str<strong>on</strong>g> time required must be at leastΩ(lg lg n) [21]. Hence our algorithm is optimal in that regard. Observe also that <str<strong>on</strong>g>the</str<strong>on</strong>g> <str<strong>on</strong>g>parallel</str<strong>on</strong>g> time O(lg n) used toper<str<strong>on</strong>g>for</str<strong>on</strong>g>m pre-processing is <str<strong>on</strong>g>the</str<strong>on</strong>g> best known <str<strong>on</strong>g>for</str<strong>on</strong>g> any <str<strong>on</strong>g>parallel</str<strong>on</strong>g> <str<strong>on</strong>g>NCA</str<strong>on</strong>g> algorithm (including PRAM <str<strong>on</strong>g>algorithms</str<strong>on</strong>g>). If <strong>on</strong>e wereallowed arbitrary (e.g., n 3 ) number of processors, <str<strong>on</strong>g>the</str<strong>on</strong>g>n it is possible to devise a Parallel PPM algorithm that requiresO(lg n) <str<strong>on</strong>g>parallel</str<strong>on</strong>g> time <str<strong>on</strong>g>for</str<strong>on</strong>g> pre-processing <str<strong>on</strong>g>and</str<strong>on</strong>g> answers <str<strong>on</strong>g>NCA</str<strong>on</strong>g> queries in time O(1) [9]. This can be simply accomplishedby pre-computing all <str<strong>on</strong>g>the</str<strong>on</strong>g> answers in <str<strong>on</strong>g>parallel</str<strong>on</strong>g> (in time O(lg lg n)) <str<strong>on</strong>g>and</str<strong>on</strong>g> making a different processor resp<strong>on</strong>sible <str<strong>on</strong>g>for</str<strong>on</strong>g> eachdifferent possible query.7.6. A <str<strong>on</strong>g>parallel</str<strong>on</strong>g> algorithm <str<strong>on</strong>g>for</str<strong>on</strong>g> <str<strong>on</strong>g>the</str<strong>on</strong>g> TP<str<strong>on</strong>g>problem</str<strong>on</strong>g>The TPProblem, first defined in [21], can be re<str<strong>on</strong>g>for</str<strong>on</strong>g>mulated in <str<strong>on</strong>g>the</str<strong>on</strong>g> c<strong>on</strong>text of <str<strong>on</strong>g>parallel</str<strong>on</strong>g> computati<strong>on</strong>s as follows: givena list L with l nodes representing an ordered sequence of objects, we want to answer <str<strong>on</strong>g>the</str<strong>on</strong>g> query precedes(x, y), wherex,y are pointers to nodes in that list. We present a soluti<strong>on</strong> to this <str<strong>on</strong>g>problem</str<strong>on</strong>g> <strong>on</strong> Parallel PPMs that requires l processors,O(lg l) <str<strong>on</strong>g>parallel</str<strong>on</strong>g> pre-processing time, <str<strong>on</strong>g>and</str<strong>on</strong>g> O(lg lg l) time to answer each query using a single processor <str<strong>on</strong>g>the</str<strong>on</strong>g>reafter.The basic idea is to create an auxiliary complete binary tree BT, such that each leaf is assigned to an element of L.If BT maintains a left to right ordering in each level, <str<strong>on</strong>g>the</str<strong>on</strong>g>n <str<strong>on</strong>g>the</str<strong>on</strong>g> precedes(x, y) query can be answered comparing <str<strong>on</strong>g>the</str<strong>on</strong>g>children of nca(x, y) in BT. We maintain this order in each level of BT using sibl pointers. BT is c<strong>on</strong>structed via a<str<strong>on</strong>g>parallel</str<strong>on</strong>g> level-by-level c<strong>on</strong>structi<strong>on</strong>. During <str<strong>on</strong>g>the</str<strong>on</strong>g> c<strong>on</strong>structi<strong>on</strong>, each node of BT has <strong>on</strong>e processor associated to it. Theroot of BT is created in <str<strong>on</strong>g>the</str<strong>on</strong>g> first step. Then, in <str<strong>on</strong>g>parallel</str<strong>on</strong>g> (<str<strong>on</strong>g>for</str<strong>on</strong>g> lg l steps), each new processor p associated to a node v inBT executes <str<strong>on</strong>g>the</str<strong>on</strong>g> following operati<strong>on</strong>s: create two new nodes (v l <str<strong>on</strong>g>and</str<strong>on</strong>g> v r ) with new processors associated to <str<strong>on</strong>g>the</str<strong>on</strong>g>m, setsibl pointer of v l to v r <str<strong>on</strong>g>and</str<strong>on</strong>g> set sibl(v r ) to left child of sibl(v).The last level of BT c<strong>on</strong>tains a list of nodes S. Since L is <str<strong>on</strong>g>the</str<strong>on</strong>g> input, from <str<strong>on</strong>g>the</str<strong>on</strong>g> way inputs are presented in <str<strong>on</strong>g>the</str<strong>on</strong>g> ParallelPPM model, we can assume that active processors can be assigned to each element of L in time O(lg l). We can alsoassume that <str<strong>on</strong>g>the</str<strong>on</strong>g>se processors have pointers that point to <str<strong>on</strong>g>the</str<strong>on</strong>g> previous element in <str<strong>on</strong>g>the</str<strong>on</strong>g> list (see Fig. 20). The elements of<str<strong>on</strong>g>the</str<strong>on</strong>g> original list L are mapped to <str<strong>on</strong>g>the</str<strong>on</strong>g> elements of S in O(lg l) <str<strong>on</strong>g>parallel</str<strong>on</strong>g> time with O(l) processors using a pointer doublingscheme, which modifies <str<strong>on</strong>g>the</str<strong>on</strong>g> sibling list of S <str<strong>on</strong>g>and</str<strong>on</strong>g> previous pointers of L. Each node of L c<strong>on</strong>tains a pointer map that isused to point to <str<strong>on</strong>g>the</str<strong>on</strong>g> corresp<strong>on</strong>ding node in S. Initially n<strong>on</strong>e of <str<strong>on</strong>g>the</str<strong>on</strong>g> map pointers is set. In <str<strong>on</strong>g>the</str<strong>on</strong>g> first step processor assignedto <str<strong>on</strong>g>the</str<strong>on</strong>g> head of L sets its map pointer to <str<strong>on</strong>g>the</str<strong>on</strong>g> head of S. At <str<strong>on</strong>g>the</str<strong>on</strong>g> same time, <str<strong>on</strong>g>the</str<strong>on</strong>g> sec<strong>on</strong>d element of L sets its map pointerto <str<strong>on</strong>g>the</str<strong>on</strong>g> sec<strong>on</strong>d element of S (see Fig. 21). This is followed by a step of pointer doubling in both S <str<strong>on</strong>g>and</str<strong>on</strong>g> L. InS pointerdoubling is accomplished using <str<strong>on</strong>g>the</str<strong>on</strong>g> sibl pointers, while in L it is per<str<strong>on</strong>g>for</str<strong>on</strong>g>med using <str<strong>on</strong>g>the</str<strong>on</strong>g> previous pointers. After a step ofpointer doubling, if <str<strong>on</strong>g>the</str<strong>on</strong>g> previous pointer of a node v in L whose map pointer is not set points to a node u whose mappointer is set, <str<strong>on</strong>g>the</str<strong>on</strong>g>n map(v) is set to sibl(map(u)). Note that <str<strong>on</strong>g>the</str<strong>on</strong>g> map pointer of a node in L is set <strong>on</strong>ly <strong>on</strong>ce. The processc<strong>on</strong>tinues until all <str<strong>on</strong>g>the</str<strong>on</strong>g> nodes in L have <str<strong>on</strong>g>the</str<strong>on</strong>g>ir map pointers set. The whole process requires O(lg l) <str<strong>on</strong>g>parallel</str<strong>on</strong>g> time.The last step of <str<strong>on</strong>g>the</str<strong>on</strong>g> pre-processing c<strong>on</strong>structs in O(lg l) <str<strong>on</strong>g>parallel</str<strong>on</strong>g> time <str<strong>on</strong>g>the</str<strong>on</strong>g> auxiliary data structures (called p-lists) usinga straight<str<strong>on</strong>g>for</str<strong>on</strong>g>ward <str<strong>on</strong>g>parallel</str<strong>on</strong>g>izati<strong>on</strong> of <str<strong>on</strong>g>the</str<strong>on</strong>g> algorithm presented in Secti<strong>on</strong> 4.3. A precedes(x,y) query is <str<strong>on</strong>g>the</str<strong>on</strong>g>n answeredby a single processor in O(lg lg l) time using <str<strong>on</strong>g>the</str<strong>on</strong>g> algorithm presented in Secti<strong>on</strong> 4.3.


134 A. D. Palú et al. / Theoretical Computer Science 352 (2006) 108 – 1358. C<strong>on</strong>clusi<strong>on</strong>s <str<strong>on</strong>g>and</str<strong>on</strong>g> future workWe have defined a novel compressi<strong>on</strong> scheme <str<strong>on</strong>g>and</str<strong>on</strong>g> used it <str<strong>on</strong>g>for</str<strong>on</strong>g> solving <str<strong>on</strong>g>the</str<strong>on</strong>g> <str<strong>on</strong>g>NCA</str<strong>on</strong>g> <str<strong>on</strong>g>problem</str<strong>on</strong>g> optimally <strong>on</strong> PPMs bothin <str<strong>on</strong>g>the</str<strong>on</strong>g> static <str<strong>on</strong>g>and</str<strong>on</strong>g> <str<strong>on</strong>g>the</str<strong>on</strong>g> dynamic case. The compressi<strong>on</strong> scheme is interesting due to its simplicity, locality properties,efficiency <str<strong>on</strong>g>and</str<strong>on</strong>g> arithmetic-free nature. However, it is not essential <str<strong>on</strong>g>for</str<strong>on</strong>g> obtaining <str<strong>on</strong>g>the</str<strong>on</strong>g> optimal <str<strong>on</strong>g>NCA</str<strong>on</strong>g> algorithm <str<strong>on</strong>g>for</str<strong>on</strong>g><str<strong>on</strong>g>the</str<strong>on</strong>g> PPMs due to <str<strong>on</strong>g>the</str<strong>on</strong>g> following remarkable <str<strong>on</strong>g>the</str<strong>on</strong>g>orem making use of <str<strong>on</strong>g>the</str<strong>on</strong>g> MicroMacroUniverse scheme presented inSecti<strong>on</strong> 6. We have also shown that <str<strong>on</strong>g>for</str<strong>on</strong>g> <str<strong>on</strong>g>the</str<strong>on</strong>g> <str<strong>on</strong>g>NCA</str<strong>on</strong>g> <str<strong>on</strong>g>problem</str<strong>on</strong>g>, it is possible to totally avoid <str<strong>on</strong>g>the</str<strong>on</strong>g> polylog penalty that <strong>on</strong>ehas to incur in a generic translati<strong>on</strong> of an algorithm designed <str<strong>on</strong>g>for</str<strong>on</strong>g> APMs to PPMs. This gives rise to <str<strong>on</strong>g>the</str<strong>on</strong>g> questi<strong>on</strong>: Is<str<strong>on</strong>g>the</str<strong>on</strong>g>re any natural <str<strong>on</strong>g>problem</str<strong>on</strong>g> <str<strong>on</strong>g>for</str<strong>on</strong>g> which <str<strong>on</strong>g>the</str<strong>on</strong>g> optimal soluti<strong>on</strong> <strong>on</strong> PPMs is provably logarithmically worse as compared to<str<strong>on</strong>g>the</str<strong>on</strong>g> optimal soluti<strong>on</strong> <strong>on</strong> APMs. As of now, we believe that <str<strong>on</strong>g>the</str<strong>on</strong>g> worst such known penalty incurred is O(lg lg n) [21]. Itwill be especially interesting if <str<strong>on</strong>g>the</str<strong>on</strong>g>re is no <str<strong>on</strong>g>problem</str<strong>on</strong>g> at all where <str<strong>on</strong>g>the</str<strong>on</strong>g> logarithmic penalty has to be incurred because thatwill show that <str<strong>on</strong>g>the</str<strong>on</strong>g> generic translati<strong>on</strong> is n<strong>on</strong>-optimal.We also presented an efficient <str<strong>on</strong>g>parallel</str<strong>on</strong>g> pointer machine algorithm <str<strong>on</strong>g>for</str<strong>on</strong>g> <str<strong>on</strong>g>the</str<strong>on</strong>g> <str<strong>on</strong>g>NCA</str<strong>on</strong>g> <str<strong>on</strong>g>problem</str<strong>on</strong>g> <str<strong>on</strong>g>for</str<strong>on</strong>g> trees in <str<strong>on</strong>g>the</str<strong>on</strong>g> static case.Our algorithm requires O(lg n) <str<strong>on</strong>g>parallel</str<strong>on</strong>g> time <str<strong>on</strong>g>and</str<strong>on</strong>g> O(n) processors <str<strong>on</strong>g>for</str<strong>on</strong>g> pre-processing where n is <str<strong>on</strong>g>the</str<strong>on</strong>g> number of nodes in<str<strong>on</strong>g>the</str<strong>on</strong>g> tree. Thereafter, it can answer any <str<strong>on</strong>g>NCA</str<strong>on</strong>g> query in O(lg lg n) time. Our <str<strong>on</strong>g>NCA</str<strong>on</strong>g> algorithm required an efficient <str<strong>on</strong>g>parallel</str<strong>on</strong>g>soluti<strong>on</strong> of a <str<strong>on</strong>g>parallel</str<strong>on</strong>g> versi<strong>on</strong> of <str<strong>on</strong>g>the</str<strong>on</strong>g> Temporal Precedence <str<strong>on</strong>g>problem</str<strong>on</strong>g> [21]. We provided an efficient <str<strong>on</strong>g>parallel</str<strong>on</strong>g> pointermachine algorithm to solve this <str<strong>on</strong>g>problem</str<strong>on</strong>g>.AcknowledgmentsWe would like to thank <str<strong>on</strong>g>the</str<strong>on</strong>g> an<strong>on</strong>ymous referees <str<strong>on</strong>g>for</str<strong>on</strong>g> <str<strong>on</strong>g>the</str<strong>on</strong>g>ir comments. The research has been partially supported byNSF Grants EIA-0220590 <str<strong>on</strong>g>and</str<strong>on</strong>g> HRD-0420407.References[1] S. Alstrup, M. Thorup, Optimal pointer <str<strong>on</strong>g>algorithms</str<strong>on</strong>g> <str<strong>on</strong>g>for</str<strong>on</strong>g> finding nearest comm<strong>on</strong> ancestors in dynamic trees, J. Algorithms 35 (2000) 169–188.[2] A. Ben-Amram, What is a pointer machine?, ACM SIGACT News 26 (2) (1995) 88–95.[3] A. Ben-Amram, Z. Galil, On pointers vs. addresses, J. ACM 39 (3) (1992) 617–648.[4] M. Bender, M. Farach-Colt<strong>on</strong>, The LCA <str<strong>on</strong>g>problem</str<strong>on</strong>g> revisited, in: Proc. LATIN 2000: Theoretical In<str<strong>on</strong>g>for</str<strong>on</strong>g>matics, Latin American Symposium,Springer, Berlin, 2000, pp. 88–94.[5] O. Berkman, U. Vishkin, Recursive ∗-tree <str<strong>on</strong>g>parallel</str<strong>on</strong>g> data-structure, in: Proc. Symp. <strong>on</strong> Foundati<strong>on</strong>s of Computer Science, IEEE Computer Society,Silver Spring, MD, 1989, pp. 196–202.[6] A. Buchsbaum, H. Kaplan, A. Rogers, J. Westbrook, Linear-time pointer-machine <str<strong>on</strong>g>algorithms</str<strong>on</strong>g> <str<strong>on</strong>g>for</str<strong>on</strong>g> least comm<strong>on</strong> ancestors, MST verificati<strong>on</strong>,<str<strong>on</strong>g>and</str<strong>on</strong>g> dominators, in: Symp. <strong>on</strong> Theory of Computing (STOC), ACM, New York, 1998, pp. 279–288.[7] R. Cole, R. Hariharan, Dynamic LCA queries <strong>on</strong> trees, in: Proc. Symp. <strong>on</strong> Discrete Algorithms (SODA), ACM/SIAM, New York/Philadelphia,PA, 1999, pp. 235–244.[8] S. Cook, P. Dym<strong>on</strong>d, Parallel pointer machines, Comput. Complexity 3 (1993) 19–30.[9] P. Dym<strong>on</strong>d, F. Fich, N. Nishimura, P. Ragde, W. Ruzzo, Pointers versus arithmetic in PRAM, in: Structures in Complexity Theory C<strong>on</strong>ference,IEEE, New York, 1993, pp. 239–252.[10] S. Fortune, J. Wyllie, Parallelism in RAMs, in: Symp. <strong>on</strong> Theory of Computing (STOC), ACM Press, New York, 1978, pp. 114–118.[11] H. Gabow, R. Tarjan, A linear-time algorithm <str<strong>on</strong>g>for</str<strong>on</strong>g> a special case of disjoint set uni<strong>on</strong>, J. Comput. System Sci. 30 (1985) 209–221.[12] L. Goldschlager, A universal interc<strong>on</strong>necti<strong>on</strong> pattern <str<strong>on</strong>g>for</str<strong>on</strong>g> <str<strong>on</strong>g>parallel</str<strong>on</strong>g> computers, J. ACM 29 (4) (1982) 1073–1086.[13] M. Goodrich, S. Kosaraju, Sorting <strong>on</strong> a <str<strong>on</strong>g>parallel</str<strong>on</strong>g> pointer machine with applicati<strong>on</strong>s to set expressi<strong>on</strong> evaluati<strong>on</strong>, in: Symp. <strong>on</strong> Foundati<strong>on</strong>s ofComputer Science (FOCS), IEEE, New York, 1989, pp. 190–195.[14] D. Gusfield, Algorithms <strong>on</strong> Strings, Trees, <str<strong>on</strong>g>and</str<strong>on</strong>g> Sequences, Cambridge University Press, Cambridge, 1999.[15] D. Harel, R. Tarjan, Fast <str<strong>on</strong>g>algorithms</str<strong>on</strong>g> <str<strong>on</strong>g>for</str<strong>on</strong>g> finding nearest comm<strong>on</strong> ancestor, SIAM J. Comput. 13 (2) (1984) 338–355.[16] D. Knuth, The Art of Computer Programming, Vol. 1, Addis<strong>on</strong>-Wesley, Reading, MA, 1968.[17] E. P<strong>on</strong>telli, D. Ranjan, A simple optimal soluti<strong>on</strong> <str<strong>on</strong>g>for</str<strong>on</strong>g> <str<strong>on</strong>g>the</str<strong>on</strong>g> temporal precedence <str<strong>on</strong>g>problem</str<strong>on</strong>g> <strong>on</strong> <strong>pure</strong> pointer machines, Theory Comput. Systems 38(1) (2005) 115–130.[18] D. Ranjan, E. P<strong>on</strong>telli, Ancestor <str<strong>on</strong>g>problem</str<strong>on</strong>g>s <strong>on</strong> <strong>pure</strong> pointer machines, in: Latin Amer. Symp. <strong>on</strong> Theoretical In<str<strong>on</strong>g>for</str<strong>on</strong>g>matics (LATIN 2002),Vol. 2286, Springer, Berlin, 2002, pp. 263–277.[19] D. Ranjan, E. P<strong>on</strong>telli, G. Gupta, On <str<strong>on</strong>g>the</str<strong>on</strong>g> complexity of or-<str<strong>on</strong>g>parallel</str<strong>on</strong>g>ism, New Generati<strong>on</strong> Comput. 17 (3) (1999) 285–307.[20] D. Ranjan, E. P<strong>on</strong>telli, G. Gupta, Data structures <str<strong>on</strong>g>for</str<strong>on</strong>g> order-sensitive predicates in <str<strong>on</strong>g>parallel</str<strong>on</strong>g> n<strong>on</strong>-deterministics systems, ACTA In<str<strong>on</strong>g>for</str<strong>on</strong>g>m. 37 (1)(2001) 21–43.[21] D. Ranjan, L. P<strong>on</strong>telli, E. L<strong>on</strong>gpre, G. Gupta, The temporal precedence <str<strong>on</strong>g>problem</str<strong>on</strong>g>, Algorithmica 28 (2000) 288–306.[22] B. Schieber, U. Vishkin, On finding lowest comm<strong>on</strong> ancestors, SIAM J. Comput. 17 (1988) 1253–1262.


A. D. Palú et al. / Theoretical Computer Science 352 (2006) 108 – 135 135[23] G. Stolting Brodal, C. Makris, S. Sioutas, A.K. Tsakalidis, K. Tsichlas, Optimal soluti<strong>on</strong>s <str<strong>on</strong>g>for</str<strong>on</strong>g> <str<strong>on</strong>g>the</str<strong>on</strong>g> temporal precedence <str<strong>on</strong>g>problem</str<strong>on</strong>g>, Algorithmica33 (4) (2002) 494–510.[24] R.E. Tarjan, A class of <str<strong>on</strong>g>algorithms</str<strong>on</strong>g> which require n<strong>on</strong>linear time to maintain disjoint sets, J. Comput. Systems Sci. 18 (1979) 100–127.[25] A. Tsakalidis, The nearest comm<strong>on</strong> ancestor in a dynamic tree, ACTA In<str<strong>on</strong>g>for</str<strong>on</strong>g>m. 25 (1988) 37–54.[26] Y. Wu, E. P<strong>on</strong>telli, D. Ranjan, On <str<strong>on</strong>g>the</str<strong>on</strong>g> complexity of dependent <str<strong>on</strong>g>and</str<strong>on</strong>g>-<str<strong>on</strong>g>parallel</str<strong>on</strong>g>ism in logic programming, in: Int. C<strong>on</strong>f. <strong>on</strong> Logic Programming,Springer, Berlin, 2003, pp. 361–376.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!