本文共 10074 字,大约阅读时间需要 33 分钟。
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 w, w 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.
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.
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.
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.
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.
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 ().
转载地址:http://mshbi.baihongyu.com/