Tree chain splitting (light and heavy chain) algorithm notes [go]

Source: Internet
Author: User

Think about it for the first time I've heard about this data structure for about 2.5 of the time, but never.

But it's not too much trouble to look back and see.

First of all, it is best to learn the Lcalca tree-shaped DPDP and the dfsdfs sequence of the three knowledge points before learning the tree chain.

If these three knowledge points are not mastered, the tree chain is difficult to understand, and of course.

-------------------------------------------------------------------------------------------

Tree links are often used to deal with tree patterns, but point weights/Benquan need to modify query topics

We'll work on this tree after we've selected a point as the root.

The first step

A traversal ((usually DFS) DFS) starts from the root, and this traversal requires the following information to be counted

Father node Fafa current node depth dd subtree Size Szsz These are obviously already used more skillfully when learning the pre-knowledge.

And then in this traversal we need to find the heavy son of the current node Sonson

The definition of a heavy son is the one with the largest number of child nodes of the current node (if there are multiple whichever)

The rest is the light son.

All the other nodes with their heavy/light edges are called heavy/light edge

To define a continuous heavy edge as a chain

We will find such a nature

The number of heavy chains and light edges that walk from either node to the root node is within the Loglog level

The proof is as follows:

Because the subtree size of any light son is smaller than half the size of the subtree corresponding to the parent node

Therefore, the subtree size is at least twice times greater than that of a light son walking up to the parent node along the light edge.

The number of light-edged bars that pass is naturally no more than log2nlog2n.

Then, because the heavy chains are intermittent (one can be synthesized in succession)

So the number of heavy chains passed is not more than the number of light-edged bars +1+1

So the number of bars after the heavy chain is also loglog level.

A comprehensive proof of the original proposition

Go up from the light, and obviously every lightweight can do it. O (1) O (1) up

And go up from the heavy chain to do each heavy chain only with O (1) O (1) must do some extra processing

Step Two

Using the information we've traversed for the first time, we'll go through it again (Dfs Dfs is needed)

For each heavy son asked to go up along the heavy chain up to the top of the point position toptop

This toptop is obviously the same as the parent node.

For each light son because the heavy chain does not exist, we can make toptop for itself.

Now it's only O (1) O (1) to go up from the heavy chain.

But the complexity of the changes is still not reliable.

For modifications and inquiries between two points light side we can be violent directly modify the query heavy chain must be combined with some data structures to ensure efficiency

At this point, we may recall that when we learned the Dfsdfs sequence, we mapped the points in the tree to a one-dimensional array based on the DFS sequence.

Thus, we can make use of data structures such as segment tree to modify and inquire the successive intervals in the Dfsdfs sequence.

So in order to be able to maintain data structures such as segment trees, we must map the points on the same heavy chain to a continuous interval.

This is not a complicated operation. We just need to traverse to the point where each point is dfsdfs to its heavy son. The points on the last heavy chain are mapped to a one-dimensional array, which is continuous.

After these two steps, the core part of the tree chain is over.

But watch out for two points, and you can't walk more than these two points in a lcalca.

The specific operation here is best to think independently of the contrast multiplier Lcalca will be better understood

See here Everyone probably can also reflect the complexity of the tree chain is O (nlogn+qlog2n)

Transfer from http://www.cnblogs.com/sagitta/p/5660749.html

Tree chain splitting (light and heavy chain) algorithm notes [go]

Related Article

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.