Editing Lowest common ancestor

Jump to: navigation, search

Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.

The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then save the changes below to finish undoing the edit.
Latest revision Your text
Line 5: Line 5:
 
# <math>\operatorname{LCA}(\{u\}) = u</math>.
 
# <math>\operatorname{LCA}(\{u\}) = u</math>.
 
# <math>u</math> is an ancestor of <math>v</math> if and only if <math>\operatorname{LCA}(u,v) = u</math>.
 
# <math>u</math> is an ancestor of <math>v</math> if and only if <math>\operatorname{LCA}(u,v) = u</math>.
# If neither <math>u</math> nor <math>v</math> is an ancestor of the other, than <math>u</math> and <math>v</math> lie in different immediate subtrees of <math>\operatorname{LCA}(u,v)</math>. (That is, the child of the <math>\operatorname{LCA}(u,v)</math> of which <math>u</math> is a descendant is not the same as the child of the <math>\operatorname{LCA}(u,v)</math> of which <math>v</math> is a descendant.) Furthermore, the <math>\operatorname{LCA}(u,v)</math> is the only node in the tree for which this is true.
+
# If neither <math>u</math> nor <math>v</math> is an ancestor of the other, than <math>u</math> and <math>v</math> lie in different immediate subtrees of <math>\operatorname{LCA}(u,v)</math>. (That is, the child of the LCA of which <math>u</math> is a descendant is not the same as the child of the LCA of which <math>v</math> is a descendant.) Furthermore, the LCA is the only node in the tree for which this is true.
 
# The entire set of common ancestors of <math>S</math> is given by <math>\operatorname{LCA}(S)</math> and all of its ancestors (all the way up to the root of the tree). In particular, every common ancestor of <math>S</math> is an ancestor of <math>\operatorname{LCA}(S)</math>.
 
# The entire set of common ancestors of <math>S</math> is given by <math>\operatorname{LCA}(S)</math> and all of its ancestors (all the way up to the root of the tree). In particular, every common ancestor of <math>S</math> is an ancestor of <math>\operatorname{LCA}(S)</math>.
 
# <math>\operatorname{LCA}(S)</math> precedes all nodes in <math>S</math> in the tree's preordering, and follows all nodes in <math>S</math> in the tree's postordering.
 
# <math>\operatorname{LCA}(S)</math> precedes all nodes in <math>S</math> in the tree's preordering, and follows all nodes in <math>S</math> in the tree's postordering.
Line 33: Line 33:
 
         print u
 
         print u
 
</pre>
 
</pre>
In other words, a node with <math>k</math> children is visited <math>k+1</math> times; once when we first encounter it by recursing down from its parent, and once again after each of its child subtrees has been fully explored. In the example tree shown to the right, if we assume that we visit children in left-to-right order as shown on the diagram, we obtain the ordering ABDBEBFBACGCHCA. Observe that this is the same order that we would obtain if, on the diagram, we started out on the left side of the circle containing the letter "A", and started walking down along the edge between A and B, and walked "all the way around" the tree, writing down the label of each node we touched along the way. This traversal also corresponds to an [[Eulerian circuit]] of the tree, assuming that we replace each undirected edge with a pair of directed edges. Since a tree with <math>N</math> nodes has <math>N-1</math> edges, and each edge is traversed once in each direction, this Eulerian circuit has length <math>2N-2</math>; and hence it contains <math>2N-1</math> vertices.
+
In other words, a node with <math>k</math> children is visited <math>k+1</math> times; once when we first encounter it by recursing down from its parent, and once again after each of its child subtrees has been fully explored. In the example tree shown to the right, if we assume that we visit children in left-to-right order as shown on the diagram, we obtain the ordering ABDBEBFBACGCHCA. Observe that the is the same order that we would obtain if, on the diagram, we started out on the left side of the circle containing the letter "A", and started walking down along the edge between A and B, and walked "all the way around" the tree, writing down the label of each node we touched along the way. This traversal also corresponds to an [[Eulerian circuit]] of the tree, assuming that we replace each undirected edge with a pair of directed edges. Since a tree with <math>N</math> nodes has <math>N-1</math> edges, and each edge is traversed once in each direction, this Eulerian circuit has length <math>2N-2</math>; and hence it contains <math>2N-1</math> vertices.
  
 
This traversal has the very useful property that between (inclusive) any occurrence of node <math>u</math> in it and any occurrence of node <math>v</math> in it, <math>\operatorname{LCA}(u,v)</math> is guaranteed to appear at least once, and no other node with depth less than or equal to that of <math>\operatorname{LCA}(u,v)</math> will appear at all. For example, consider the positions of D and F in the traversal ABDBEBFBACGCHCA; we see that their LCA, that is, B, occurs between them, and that the nodes C and A do not occur at all.
 
This traversal has the very useful property that between (inclusive) any occurrence of node <math>u</math> in it and any occurrence of node <math>v</math> in it, <math>\operatorname{LCA}(u,v)</math> is guaranteed to appear at least once, and no other node with depth less than or equal to that of <math>\operatorname{LCA}(u,v)</math> will appear at all. For example, consider the positions of D and F in the traversal ABDBEBFBACGCHCA; we see that their LCA, that is, B, occurs between them, and that the nodes C and A do not occur at all.
Line 45: Line 45:
 
Since there is an algorithm that solves RMQ with linear preprocessing time and constant query time, the LCA problem can also be solved in linear preprocessing time and constant query time.
 
Since there is an algorithm that solves RMQ with linear preprocessing time and constant query time, the LCA problem can also be solved in linear preprocessing time and constant query time.
  
===With heavy-light decomposition===
+
===With heavy&ndash;light decomposition===
The [[heavy-light decomposition]] provides an easy way to ascend a tree quickly, which allows an adaptation of the naive algorithm to run in <math>O(\log N)</math> time after linear preprocessing time. This is not asymptotically optimal, as the LCA-to-RMQ reduction discussed in the previous section allows queries to be performed in constant time. However, if the decomposition is being used for other purposes than simply answering LCA queries, then the LCA query time might not dominate the overall runtime, anyway.
+
The [[heavy&ndash;light decomposition]] provides an easy way to ascend a tree quickly, which allows an adaptation of the naive algorithm to run in <math>O(\log N)</math> time after linear preprocessing time. This is not asymptotically optimal, as the LCA-to-RMQ reduction discussed in the previous section allows queries to be performed in constant time. However, if the decomposition is being used for other purposes than simply answering LCA queries, then the LCA query time might not dominate the overall runtime, anyway.
  
 
===Dynamic===
 
===Dynamic===
 
In the fully dynamic variant of the LCA problem, we must be prepared to handle LCA queries intermixed with operations that change the tree (that is, rearrange the tree by adding and removing edges). In general, we suppose that we have a forest of <math>N</math> nodes in which we can arbitrarily link together two nodes from different trees, cut an edge (thus dividing a tree into two trees), or change the root of a tree on a whim, and the answer to an LCA query is either a node or the finding that the two nodes given are in different trees.
 
In the fully dynamic variant of the LCA problem, we must be prepared to handle LCA queries intermixed with operations that change the tree (that is, rearrange the tree by adding and removing edges). In general, we suppose that we have a forest of <math>N</math> nodes in which we can arbitrarily link together two nodes from different trees, cut an edge (thus dividing a tree into two trees), or change the root of a tree on a whim, and the answer to an LCA query is either a node or the finding that the two nodes given are in different trees.
  
This variant can be solved using <math>O(\log N)</math> 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 as discussed in the previous section.
+
This variant can be solved using <math>O(\log N)</math> 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&ndash;light decomposition of each tree, and allows LCA queries to be carried out in logarithmic time as discussed in the previous section.
  
 
==References==
 
==References==
 
<references/>
 
<references/>

Please note that all contributions to PEGWiki are considered to be released under the Attribution 3.0 Unported (see PEGWiki:Copyrights for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource. Do not submit copyrighted work without permission!

Cancel | Editing help (opens in new window)