Lowest Common Ancestor(LCA)

Lowest Common Ancestor(LCA)

Lowest Common Ancestor(LCA) Fayssal El Moufatich Technische Universit¨atM¨unchen St. Petersburg JASS 2008 1 Introduction we denote the NCA of two nodes x and y from the tree T as nca(x; y). As we have already LCA problem is one of the most fundamental pointed out, efficiently computing NCAs has algorithmic problems on trees. It is concerned been studied extensively for the last 3 decades with how we can find the Least Common in both online and offline settings. Ancestor of a pair of nodes. Over the last 3 decades, it has been intensively studied mainy because: 3 Example Here follows an example for a tree. Choosing • It is inherently algorithmically beautiful. two arbitrary nodes from the tree, one is inter- • Fast algorithms for the LCA problem can ested in finding their lowest common ancestor. be used to solve other algorithmic prob- lems. 0 2 Definitions 1 4 Before embarking into the technicalities of the different algorithms for LCA, let us first agree on the terminology that we will be using 2 3 5 6 7 throughout this article. Let there be a rooted tree T(E,V). A node x 2 T is called an ancestor of a node y 2 T if the 8 9 path from the root of T to y goes through x. Also, a node v 2 T is called to be a common For example, the nca(2; 5) = 0, nca(7; 5) = 4, ancestor of x and y if it is an ancestor of both and nca(0; 9) = 0 . x and y. Hence, the Nearest/Lowest Common Ancestor, NCA or LCA, of two nodes x, y is 4 Applications the common ancestor of x and y whose distance to x (and to y) smaller than the distance to x of A procedure solving the NCA problem has any common ancestor of x and y. From now on, been widely used by algorithms from a large 1 Fayssal El Moufatich March 23, 2008 spectrum of applications. To point out, LCA x and y. Then, let i = max((1); (2); (3)) where: algorithms have been used in finding the max- imum weighted matching in a graph, finding a minimum spanning tree in a graph, find- 1. index of the leftmost bit in which ing a dominator tree in a graph in a directed inorder(x) and inorder(y) differ. flow-graph, several string algorithms, dynamic planarity testing, network routing,solving vari- ous geometric problems including range search- 2. index of the rightmost 1 in inorder(x). ing, finding evolutionary trees, and in bounded tree-width algorithms as well as in many other 3. index of the rightmost 1 in inorder(y). fields. It turns out that it can be proved by induction 5 Survey of Algorithms that: One of the most fundamental results on com- puting NCAs is that of Harel and Tarjan [5], Lemma 1. [2]the inorder(nca(x; y)) consists [3]. They describe a linear time algorithm to of the leftmost ` − i bits of inorder(x) (or preprocess a tree and build a data structure inorder(y) if the max was (3)) followed by a that allows subsequent NCA queries to be an- 1 and i zeros. swered in constant time!. Several simpler al- gorithms with essentially the same properties but better constant factors were proposed af- An example that illustrates the meaning of terwards. They all use the observation that it the above lemma follows. is rather easy to solve the problem when the input tree is a complete binary tree.We will be having a short view on the properties of the 7 Example Harel and Tarjan algorithm, and then we will be considering a simpler algorithm that was Before getting to the example illustrated in presented later by Farach and Bender. Figure1, it might be worth noting here that in this setting, the basic idea is construct 6 How do we do it? the inorder(nca(x; y)) from inorder(x) and inorder(y) alone and without accessing the We have said in the previous section that original tree or any other global data structure. finding the LCA of arbitrary two nodes This basically means that we are answering the from a completely balanced binary tree is LCA query in constant time! rather simple. We proceed by first label- Now considering the tree depicted in Figure1, ing the nodes by their index in an inorder let us choose to arbitrary nodes in the tree. Let traversal of the complete binary tree. It us say that we choose 6 and 9, i.e. 0110 and follows that if the tree has n nodes, each 1001. The position of the different bit from left such number occupies ` = blog(n)c bits. We is 3, the index of the first 1 in 6 from right is assume that the LSB is the rightmost and 1 and the index of the first 1 in 9 from right that its index is 0. Let denote inorder(x) is 0. Hence max(3,1,0)= 3. Hence 1000 is the and inorder(y) to be the inorder indexes of lowest common ancestor of 6 and 9. 2 Fayssal El Moufatich March 23, 2008 Figure 1: Inorder labeling for a completely balanced binary tree [6] 8 So what if the input tree is sketch is inclusive. For complete details, please not a completely balanced refer to the related paper of Alstrup et al[?]. binary tree? Proof. [4] • Use lexigraphic sorting the sequence of in- In such case, one can simply do a mapping to tergers or binary strings. a completely binary balanced tree. It turns out that different algorithms differ by the way • Use results from Gilbert and Moore on al- they do this mapping. However, all algorithms phabetic coding of sequences of integers have to use some precomputed auxilliary data hbik(jbij < log n − log yi + O(1) for all i). structures and the labels of the nodes to com- • use labeling along HPs, Heavy Paths. pute the NCAS due the usage of the completely balanced binary tree mapping. Unfortunately, most of the algorithms for general trees do not allow to compute a unique identifier of nca(x,y) 9 NCA and Discrete Range from short labels associated with x and y alone. However, one can prove the following interest- Searching (DRS) ing theorem: Gabow, Bentley and Tarjan[9] nicely observed that one-dimensional DRS problem is equiva- Theorem 2. There is a linear time algorithm lent to NCA problem.Hence, DRS is used by that labels the n nodes of a rooted tree T with most of simple NCA algorithms. here follows labels of length O(log n) bits such that from the a definition for the DRS problem: labels of nodes x, y in T alone, one can compute the label of nca(x; y) in constant time. Definition 3. DRS Problem Given a sequence of real numbers x1, x2, ...xn, preprocess the se- here follows a sketch for the proof of the quence so that one can answer efficiently sub- above theorem. However by no means this sequent queries of the form: 3 Fayssal El Moufatich March 23, 2008 given a pair of indices (i; j), what is the maxi- xi and xj representing x and y, and compute mum element among xi,...,xj or max(i; j). the maximum among xi; :::; xj. The node cor- responding to the maximum element is simply DRS problem is a fundamental geometric nca(x; y)! searching problem. It turns out that DRS can be reduced to NCA by constructing a Carte- sian tree for the sequence x1, ..., xn [9]. 12 Euler tour of a tree Figure2 shows an example for an Eulerian tour 10 What is a Cartesian tree? of a tree. Definition 4. Cartesian Tree The Cartesian tree of the sequence x1; :::; xn is a binary tree 13 What is the LCA of given with n nodes each containing a number xi and the following properties: two nodes then? Let x = max(x ; :::; x ) j 1 n After getting the sequence of the Euler tour of 1. The root of the Cartesian tree contains xj. the tree, the LCA of given two nodes would be simply the node of the least depth (i.e. Clos- 2. The left subtree of the root is a Cartesian est to the root) that lies between the nodes in tree for x1; :::; xj−1. the Euler tour. Hence, finding specific node in the tree is equivalent to finding the minimum 3. The right subtree of the root is a Cartesian element in the proper interval in the array of tree for x ; :::; x . j+1 n numbers. The latter problem, it turns out, can The Cartesian tree for x1; :::; xn can be con- be solved by min-range queries. structed in O(n) [Vuillemin, 1980]. The algo- rithm is rather simple and should be left to 14 Range Minimum Query the curious reader to tinker with. As such, the maximum among xi; :::; xj namely corresponds (RMQ) Problem to the NCA of the node containing xi and the This is the same as the DSR problem but out- node containing xj. puts the minimum instead. 11 What about NCA as DRS? Definition 5. RMQ Problem Structure to Preprocess: an array of numbers of length n. As stated above Gabow et al. also show how to Query: for indices i and j and n, query reduce the NCA problem to the DRS problem. RMQ(x,y) returns the index of the smallest Given a tree, we first construct a sequence of element in the subarray A[i...j].

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    9 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us