[Pin to the top] Implementation of tree link separation in LCA

Source: Internet
Author: User

This article was intended to beTree link splittingBut I think this is only a derivative of tree link partitioning, so I opened another article. If you are not familiar with the part of tree link partitioning, please refer to the above link.

The recent common ancestor of the two nodes in the computing tree. We generally use the climbing method and the Tarjan is offline.AlgorithmOr convert the LCA to rmq. Here we will talk about a new algorithm for finding the LCA, which is based on the tree chain.

Let's first review the information maintained by each node in the tree link Splitting:

1: siz [v] indicates the total number of nodes in the subtree rooted in v.

 

2: DEP [v] indicates the depth of v.

3: Son [v] indicates the son node of V on the same heavy chain as v.

4: Fa [v] indicates V's Father's Day.

5: Top [v] indicates the top node of the chain where v is located.

(W [v] is actually available, but we don't need it here)

Let's take a look at the tree link segmentation to findCodeThen, let's look at the algorithm principles.

 

 
Int LCA (int A, int B) {While (1) {If (top [a] = top [B]) return Dep [a] <= Dep [B]? A: B; else if (DEP [top [a]> = Dep [top [B]) A = Fa [top [a]; else B = Fa [top [B];}

Is it very short. Next we will analyze this algorithm.

 

 

 

 

1: If top [a] = top [B], it means that a and B are on the same heavy chain. Obviously, the point with a smaller depth is their recent common ancestor.

2: If top [a]! = Top [B], (indicating that A and B are on different heavy chains) and the depth of A is large, then the LCA of A and B cannot be on the heavy chain of.

Because if the LCA of A and B is on the heavy chain where A is located, the top [a] is obviously also the common ancestor of A and B, if Dep [up [a]> Dep [B], it is obviously impossible. If Dep [up [a] <= Dep [B], set Dep [up [a] to D, because D> = Dep [B], so I search up along B, in depth D, we can also find an ancestor where point C is B, and because A and B are not on the same heavy chain, top [a]! = C, which means that in the same depth, B has two different ancestor, which is impossible (because it is a tree). Therefore, LCA cannot be on the heavy chain where A is located. So we can raise a to the parent node of up [A]. At this time, the LCA of A and B remains unchanged.

3: If top [a]! = Top [B], and B has a large depth. Similarly, we can raise B to the parent node of up [B.

4: A and B keep rising, so we can find the LCA of A and B.

Because we know that the number of heavy chains from any point in the tree to the root path will not exceed (logn), so we can change, b.

Because we can find that all the LCA operations are performed on the heavy chain, we can maintain the path information between any two points in the tree, such as the maximum edge and minimum edge between two points, edge and so on. Here we will not give an example one by one.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.