1 Definition
LCA (least common ancestors ):Recent common ancestor. For the two nodes U and V with the root tree T, the recent common ancestor LCA (t, u, v) represents a node X, meet the requirements that X is the ancestor of U and V, and the depth of X is as large as possible (minimum depth of root ). Another way to understand t is to regard T as a undirected acyclic graph, while LCA (t, u, v) is the minimum depth point in the shortest path from u to v. Here is an example of LCA: for t = <V, E>, V = {1, 2, 3, 4, 5}, E = {(1, 2), (1, 3 ), (3, 4), (3, 5)}, then there are: LCA (T, 5, 2) = 1, LCA (T, 3, 4) = 3, LCA (T, 4, 5) = 3.
2 offline algorithm-Tarjan
The algorithm design strategy is based on the basic assumption that the input data is known before the algorithm is executed. That is to say, the algorithm has complete information related to the problem when solving the problem, this kind of algorithm is usually developed on the premise of having complete information about the problemOffline algorithm (Off Line algorithms)(From http://baike.baidu.com/view/2734232.htm? Fr = ala0_1)
The Tarjan algorithm is an offline algorithm used to solve the LCA problem. It requires reading all the LCA queries (finding a lca (t, u, v) is called a LCA query) before solving the problem ). ExploitationQuery setWith superior time-space complexity, we can implement the O (N + q) algorithm for the LCA problem. Here, Q indicates the number of queries. The Tarjan algorithm is based on the framework of Deep-priority search. For a newly searched node, a set composed of this node is created first, and then each subtree of the current node is searched, after each subtree is searched, you can determine that the LCA query (I .e., find the LCA (t, u, v) in the subtree has been solved. The results of other LCA queries must be outside the subtree. In this case, the set formed by the subtree is merged with the set of the current node, and the current node is set as the ancestor of the set. Then, search for the next subtree until all the subtree of the current node is completely searched. At this time, the current node is also set to checked. At the same time, you can handle the LCA inquiry about the current node. If there is a query from the current node to node v, V has been checked, and the recent public ancestor of the current node and V has not been checked because in-depth priority search is performed, the subtree of the recent common ancestor including V must have been searched, so this recent common ancestor must be the ancestor of the V Collection. The pseudocode for this algorithm is as follows:
LCA (u){
Make-set (u)
Ancestor [find-set (u)] = u/* sets the ancestor of the set where u is located */
V {
LCA (V)
Union (v, u)/* merge the subset generated by V into u */
Ancestor [find-set (u)] = u/* prevents the change of the Set root (Representative) of U by means of tree-like heuristic combination */
}
Checked [u] = true
For each (u, v) belongs to P {
If checked [v] = true
The nearest common ancestor of U and V is ancestor [find-set (v)].
}
}
In the above pseudo code about and query set function make-set, find-set, union can refer to the space on and query set article: http://hi.baidu.com/ly01kongjian/blog/item/e09057310ff40df31b4cffcb.html
Because it is based on the deep-priority search algorithm, you only need to call the LCA (root [T]) to answer all the LCA requests. Here root [T] indicates the root of the tree T, assume that all queries (u, v) constitute a set of P.