博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Lowest Common Ancestor
阅读量:4029 次
发布时间:2019-05-24

本文共 10074 字,大约阅读时间需要 33 分钟。

Lowest common ancestor

From Wikipedia, the free encyclopedia
This article is about lowest common ancestors in graph theory and computer science. For the common ancestor of a set of species in evolutionary trees, see  . For a common ancestor of all life forms, see  .
In this tree, the lowest common ancestor of the nodes 
xand 
y is marked in dark green. Other common ancestors are shown in light green.

In  and , the lowest common ancestor (LCA) of two nodes v and w in a  or (DAG) is the lowest (i.e. deepest) node that has both v and w as descendants, where we define each node to be a descendant of itself (so if v has a direct connection from ww is the lowest common ancestor).

The LCA of v and w in T is the shared ancestor of v and w that is located farthest from the root. Computation of lowest common ancestors may be useful, for instance, as part of a procedure for determining the distance between pairs of nodes in a tree: the distance from v to wcan be computed as the distance from the root to v, plus the distance from the root to w, minus twice the distance from the root to their lowest common ancestor (). In , the lowest common ancestor is also known as the least common subsumer.

In a  where each node points to its parent, the lowest common ancestor can be easily determined by finding the first intersection of the paths from v and w to the root. In general, the computational time required for this algorithm is O(h) where h is the height of the tree (length of longest path from a leaf to the root). However, there exist several algorithms for processing trees so that lowest common ancestors may be found more quickly. , for example, preprocesses a tree in linear time to provide constant-time LCA queries. In general DAGs, similar algorithms exist, but with super-linear complexity.

Contents

 
 [] 

History[]

The lowest common ancestor problem was defined by , , and  (), but Dov Harel and  () were the first to develop an optimally efficient lowest common ancestor data structure. Their algorithm processes any tree in linear time, using a , so that subsequent lowest common ancestor queries may be answered in constant time per query. However, their data structure is complex and difficult to implement. Tarjan also found a simpler but less efficient algorithm, based on the  data structure, for .

Baruch Schieber and  () simplified the data structure of Harel and Tarjan, leading to an implementable structure with the same asymptotic preprocessing and query time bounds. Their simplification is based on the principle that, in two special kinds of trees, lowest common ancestors are easy to determine: if the tree is a path, then the lowest common ancestor can be computed simply from the minimum of the levels of the two queried nodes, while if the tree is a , the nodes may be indexed in such a way that lowest common ancestors reduce to simple binary operations on the indices. The structure of Schieber and Vishkin decomposes any tree into a collection of paths, such that the connections between the paths have the structure of a binary tree, and combines both of these two simpler indexing techniques.

Omer Berkman and Uzi Vishkin () discovered a completely new way to answer lowest common ancestor queries, again achieving linear preprocessing time with constant query time. Their method involves forming an  of a graph formed from the input tree by doubling every edge, and using this tour to write a sequence of level numbers of the nodes in the order the tour visits them; a lowest common ancestor query can then be transformed into a query that seeks the minimum value occurring within some subinterval of this sequence of numbers. They then handle this  problem by combining two techniques, one technique based on precomputing the answers to large intervals that have sizes that are powers of two, and the other based on table lookup for small-interval queries. This method was later presented in a simplified form by Michael Bender and  (). As had been previously observed by , the range minimum problem can in turn be transformed back into a lowest common ancestor problem using the technique of .

Further simplifications were made by  and .

A variant of the problem is the dynamic LCA problem in which the data structure should be prepared to handle LCA queries intermixed with operations that change the tree (that is, rearrange the tree by adding and removing edges) This variant can be solved using O(logN) time for all modifications and queries. This is done by maintaining the forest using the dynamic trees data structure with partitioning by size; this then maintains a heavy-;light decomposition of each tree, and allows LCA queries to be carried out in logarithmic time in the size of the tree.

Without preprocessing you can also improve the naïve online algorithm's computation time to O(log h) by storing the paths through the tree using skew-binary random access lists, while still permitting the tree to be extended in constant time (). This allows LCA queries to be carried out in logarithmic time in the height of the tree.

Extension to directed acyclic graphs[]

A DAG with the common ancestors of 
xand 
y in light green, and their lowest common ancestors in dark green.

While originally studied in the context of trees, the notion of lowest common ancestors can be defined for directed acyclic graphs (DAGs), using either of two possible definitions. In both, the edges of the DAG are assumed to point from parents to children.

  • Given G = (VE),  define a  (V, ≤) such that x ≤ y iff x is in the   of y. The lowest common ancestors of x and y are then the minimum elements under ≤ of the common ancestor set {
    z ∈ V | x ≤ z and y ≤ z
    }.
  •  give an equivalent definition, where the lowest common ancestors of x and y are the nodes of  zero in the  of G induced by the set of common ancestors of x and y.

In a tree, the lowest common ancestor is unique; in a DAG of n nodes, each pair of nodes may have as much as n-2 LCAs (), while the existence of an LCA for a pair of nodes is not even guaranteed in arbitrary connected DAGs.

A brute-force algorithm for finding lowest common ancestors is given by : find all ancestors of x and y, then return the maximum element of the intersection of the two sets. Better algorithms exist that, analogous to the LCA algorithms on trees, preprocess a graph to enable constant-time LCA queries. The problem of LCA existence can be solved optimally for sparse DAGs by means of an O(|V||E|) algorithm due to .

A technique for answering LCA lookups in constant time using DAG pre-processing that is sensitive to the structure of the DAG was proposed by (). This technique provides the best possible runtimes for pre-processing the DAG depending on the structure of the DAG. Effectively, it is possible to achieve near linear pre-processing for sparse graphs and constant LCA look-up times using their technique.

Implementation[]

The implementation of the techniques proposed in () is available for public use at. This library contains tests for measuring the pre-processing performance for DAGs with varying structural complexity such as trees, random DAGs and powerset lattices. The library shows itself to be seamlessly adaptive and achieves the fastest possible theoretical runtimes in all three structural classes.

Applications[]

The problem of computing lowest common ancestors of classes in an  arises in the implementation of  systems (). The LCA problem also finds applications in models of  found in  ().

See also[]

References[]

  1.  Dash, Santanu Kumar; Christianson, Bruce. .
  • ; ;  (1973), "On finding lowest common ancestors in trees", , pp. 253–265, :.
  • Aït-Kaci, H.; Boyer, R.; Lincoln, P.; Nasr, R. (1989),  (PDF) 11 (1): 115–146, :.
  • Alstrup, Stephen; Gavoille, Cyril; Kaplan, Haim; Rauhe, Theis (2004), , Theory of Computing Systems 37 (3): 441–456, :. A preliminary version appeared in SPAA 2002.
  • Bender, Michael A.;  (2000), "The LCA problem revisited", ,  1776, Springer-Verlag, pp. 88–94, :.
  • Bender, Michael A.; ; Pemmasani, Giridhar; ; Sumazin, Pavel (2005),  (PDF)Journal of Algorithms 57 (2): 75–94, :.
  • Berkman, Omer;  (1993), "Recursive Star-Tree Parallel Data Structure", SIAM Journal on Computing 22 (2): 221–242, :.
  • Djidjev, Hristo N.; Pantziou, Grammati E.; Zaroliagis, Christos D. (1991), "Computing shortest paths and distances in planar graphs", Automata, Languages and Programming: 18th International Colloquium, Madrid, Spain, July 8–12, 1991, Proceedings, Lecture Notes in Computer Science 510, Springer, pp. 327–338, :.
  • Fischer, Johannes; Heun, Volker (2006), "Theoretical and Practical Improvements on the RMQ-Problem, with Applications to LCA and LCE", Proceedings of the 17th Annual Symposium on Combinatorial Pattern Matching, Lecture Notes in Computer Science 4009, Springer-Verlag, pp. 36–48, :.
  • Gabow, Harold N.; ;  (1984), "Scaling and related techniques for geometry problems", , New York, NY, USA: ACM, pp. 135–143, :.
  • Harel, Dov;  (1984), "Fast algorithms for finding nearest common ancestors", SIAM Journal on Computing 13 (2): 338–355, :.
  • Kowaluk, Miroslaw; Lingas, Andrzej (2005), "LCA queries in directed acyclic graphs", in Caires, Luís; Italiano, Giuseppe F.; Monteiro, Luís; Palamidessi, Catuscia; Yung, Moti, Automata, Languages and Programming, 32nd International Colloquium, ICALP 2005, Lisbon, Portugal, July 11-15, 2005, Proceedings, Lecture Notes in Computer Science 3580, Springer, pp. 241–248, :
  • Dash, Santanu Kumar; Scholz, Sven-Bodo; Herhut, Stephan; Christianson, Bruce (2013), , Theoretical Computer Science 513: 25–37
  • Schieber, Baruch;  (1988), "On finding lowest common ancestors: simplification and parallelization", SIAM Journal on Computing 17 (6): 1253–1262, :.

External links[]

  • , by Kamal Rawat
  • , by 
  • . Course by , notes written by Loizos Michael and Christos Kapoutsis. , written by Alison Cichowlas.
  •  in . A simplified version of the Schieber–Vishkin technique that works only for balanced binary trees.
  •  of  explaining the Schieber–Vishkin technique
  •  by Edward Kmett, which includes the skew-binary random access list algorithm.  slides for the same package.

转载地址:http://mshbi.baihongyu.com/

你可能感兴趣的文章
机器学习中样本数据预处理
查看>>
机器学习中样本缺失值的处理方法
查看>>
机器学习中样本比例不平衡的处理方法
查看>>
机器学习中的文本处理
查看>>
K近邻分类
查看>>
Java集合
查看>>
Java泛型、反射、注解、Lambda表达式
查看>>
Spring框架入门
查看>>
Linear Regression及各种线型回归在正则化中的应用
查看>>
朴素贝叶斯算法
查看>>
逻辑回归
查看>>
感知机 - 支持向量机
查看>>
决策树算法(ID3、C4.5、CART)
查看>>
集成学习(Bagging、Boosting、Stacking)
查看>>
无监督学习
查看>>
K均值算法(K-means)
查看>>
机器学习中的损失函数
查看>>
机器学习中的性能度量
查看>>
机器学习中的优化问题
查看>>
机器学习中的参数估计方法
查看>>