HOME

TheInfoList



OR:

In
computer science Computer science is the study of computation, automation, and information. Computer science spans theoretical disciplines (such as algorithms, theory of computation, information theory, and automation) to Applied science, practical discipli ...
, Tarjan's off-line lowest common ancestors algorithm is an
algorithm In mathematics and computer science, an algorithm () is a finite sequence of rigorous instructions, typically used to solve a class of specific Computational problem, problems or to perform a computation. Algorithms are used as specificat ...
for computing
lowest common ancestor In graph theory and computer science, the lowest common ancestor (LCA) (also called least common ancestor) of two nodes and in a Tree (graph theory), tree or directed acyclic graph (DAG) is the lowest (i.e. deepest) node that has both and a ...
s for pairs of nodes in a tree, based on the
union-find In computer science, a disjoint-set data structure, also called a union–find data structure or merge–find set, is a data structure that stores a collection of disjoint (non-overlapping) sets. Equivalently, it stores a partition of a set ...
data structure. The lowest common ancestor of two nodes ''d'' and ''e'' in a
rooted tree In graph theory, a tree is an undirected graph in which any two vertices are connected by ''exactly one'' path, or equivalently a connected acyclic undirected graph. A forest is an undirected graph in which any two vertices are connected by ''a ...
''T'' is the node ''g'' that is an ancestor of both ''d'' and ''e'' and that has the greatest depth in ''T''. It is named after
Robert Tarjan Robert Endre Tarjan (born April 30, 1948) is an American computer scientist and mathematician. He is the discoverer of several graph algorithms, including Tarjan's off-line lowest common ancestors algorithm, and co-inventor of both splay trees a ...
, who discovered the technique in 1979. Tarjan's algorithm is an offline algorithm; that is, unlike other lowest common ancestor algorithms, it requires that all pairs of nodes for which the lowest common ancestor is desired must be specified in advance. The simplest version of the algorithm uses the union-find data structure, which unlike other lowest common ancestor data structures can take more than constant time per operation when the number of pairs of nodes is similar in magnitude to the number of nodes. A later refinement by speeds the algorithm up to
linear time In computer science, the time complexity is the computational complexity that describes the amount of computer time it takes to run an algorithm. Time complexity is commonly estimated by counting the number of elementary operations performed by ...
.


Pseudocode

The pseudocode below determines the lowest common ancestor of each pair in ''P'', given the root ''r'' of a tree in which the children of node ''n'' are in the set ''n.children''. For this offline algorithm, the set ''P'' must be specified in advance. It uses the ''MakeSet'', ''Find'', and ''Union'' functions of a disjoint-set forest. ''MakeSet(u)'' removes ''u'' to a singleton set, ''Find(u)'' returns the standard representative of the set containing ''u'', and ''Union(u,v)'' merges the set containing ''u'' with the set containing ''v''. TarjanOLCA(''r'') is first called on the root ''r''. function TarjanOLCA(u) is MakeSet(u) u.ancestor := u for each v in u.children do TarjanOLCA(v) Union(u, v) Find(u).ancestor := u u.color := black for each v such that in P do if v.color

black then print "Tarjan's Lowest Common Ancestor of " + u + " and " + v + " is " + Find(v).ancestor + "." Each node is initially white, and is colored black after it and all its children have been visited. For each node pair ' to be investigated: * When ''v'' is already black (viz. when ''v'' comes before ''u'' in a post-order traversal of the tree): After ''u'' is colored black, the lowest common ancestor of this pair is available as ''Find(v).ancestor'', but only while the LCA of ''u'' and ''v'' is not colored black. * Otherwise: Once ''v'' is colored black, the LCA will be available as ''Find(u).ancestor'', while the LCA is not colored black. For reference, here are optimized versions of ''MakeSet'', ''Find'', and ''Union'' for a disjoint-set forest: function MakeSet(x) is x.parent := x x.rank := 1 function Union(x, y) is xRoot := Find(x) yRoot := Find(y) if xRoot.rank > yRoot.rank then yRoot.parent := xRoot else if xRoot.rank < yRoot.rank then xRoot.parent := yRoot else if xRoot.rank

yRoot.rank then yRoot.parent := xRoot xRoot.rank := xRoot.rank + 1 function Find(x) is if x.parent != x then x.parent := Find(x.parent) return x.parent


References

*. *{{citation , last = Tarjan , authorlink = Robert Tarjan , title = Applications of path compression on balanced trees , journal =
Journal of the ACM The ''Journal of the ACM'' is a peer-reviewed scientific journal covering computer science in general, especially theoretical aspects. It is an official journal of the Association for Computing Machinery. Its current editor-in-chief is Venkatesan ...
, volume = 26 , issue = 4 , year = 1979 , pages = 690–715 , doi = 10.1145/322154.322161, first =R. E.. Graph algorithms Articles with example pseudocode