"Algorithmic" RMQ LCA Lecture Notes

Source: Internet
Author: User

April 4, should learn the younger brother asked to go to the school to tell a class, in fact, very content is quite miscellaneous, but the purpose is to elicit the LCA algorithm.

Now tidy up the main contents of the day's lectures:

The beginning does not directly elicit the LCA problem, but rather the RMQ (Range minimum/maximum Query) problem.

RMQ is a classic question for a given array, each time asking for the minimum or maximum number of numbers in an interval [l,r].

At first no students knew how to solve the problem. I will weaken the problem, each time asked [1,r] The number of the most value, there is a student out of a Min/max array can be, this is a positive solution.

This is a fairly correct approach, there are many students who learn the line-segment tree in the face of this weakening problem will also be without a brain line tree, the simple problem complicated. Students can directly answer the Min/max array, I think also because they now have too few algorithms, basically no interference.

I am going to introduce the St table (Sparse tables) solution RMQ.

How to solve RMQ if we now have a two-dimensional array f[i][j] that includes the maximum value from the number of 2^i that I have in the total number of backwards?

The students at the bottom gave me a good idea: to find out the range D of L and R, D for binary decomposition, then the complexity of log (d), through the values in several F-arrays to solve the maximum value of the number between L and R.

The idea was pretty nice, but I said it was not good enough, because in fact, as long as the maximum number of 2 numbers can be obtained.

If D is a power of 2, then only F[L][LOG2 (d)] is directly the answer, otherwise the answer is Min/max (f[l][(int) log2 (d)][r-2^ ((int) log2 (d)) +1][(int) log2 (d)]), which looks somewhat complex, In fact, it is convenient to write in the code, meaning is to find the largest k to make 2^k less than D, and then use the left and right 2 section F value can be fully covered to the entire range.

In fact, both of these situations can be normalized to the second method of calculation.

The next question is how to solve the F-array. In fact, the St table is essentially a dynamic planning idea, first initializing all the f[i][0] to A[i], that is, the original value, and then starting from 1 to loop upward until the log (n). F[i][j]=min/max (f[i][j-1],f[i+2^ (j-1)][j-1])

The ST table algorithm has been taught. Another point to mention is that many people will equate RMQ with the St table, which is a misunderstanding. RMQ is not an algorithm, it is an interval-maximum problem, which is essentially a kind of problem. RMQ, of course, can also be solved with a segment tree. Even brute force can also be called an algorithm for RMQ.

Next, I talked about the four-class edges in the graph depth-first traversal, this article does not expand.

Finally, I elicited the LCA problem. None of the students at the bottom knew how to do it without violence. But some students of the idea of violence is still somewhat interesting, such as walking through the tree first, when the first point is found, when backtracking, the mark traversed the value of the point +1,. Traversing the tree again, when the second point is found, when backtracking, marks the value of the point traversed by +1, the first time a point value of 2 is found, it is two points of the nearest public ancestor.

I think this idea still has a bit of meaning, because it is more counter-conventional.

I mainly talk about three kinds of algorithms to solve the LCA problem.

The first is the question of its statute to RMQ. What about the statute? When traversing to a node and backtracking to a node, the timestamp is played and the node corresponding to the timestamp is recorded. Then the LCA problem can be regulated to the minimum of the depth of the node that corresponds to the timestamp of all timestamps between the timestamps for the first time that the corresponding 2 points are traversed.

It sounds a little complicated, but in fact, that's what it's about.

When asked how to apply the LCA to RMQ, one of the students accidentally thought of the second method, but he did not come up with it completely.

The second algorithm is called multiplication on the tree, similar to the St table, F[i][j] represents the 2^j ancestor of node I.

The algorithm flow for the LCA is roughly the same, with two points u and V, it is advisable to assume that the depth of U is greater than or equal to V, then let U go up to two points at the same depth. If two points are the same, the answer is returned directly. Otherwise from the large to the small enumeration of the power of 2, to see if the two points upward so many ancestors are the same, if the same, then thought traversed the head, continue off. Otherwise, it is placed as a new ancestor point. The last 2 points go up again, that is, their father node is the LCA. This allows you to go up to the same height as V, using the method with the RMQ problem mentioned in the second binary decomposition is a train of thought, the depth of the difference as a binary decomposition, jump up log (d) times.

The third algorithm is an off-line algorithm based on Tarjan.

The so-called offline is the first time to save all the queries, the tree to do a depth-first traversal, after each traversal, the corresponding collection of sub-tree is merged into the current node. After all the child nodes have been recursively passed, all queries related to the current point are traversed, and if another point has been accessed, the answer to this query is the representative of the collection at the other point.

At the end of the day, I mentioned that, without in-depth discussion, the problem of LCA was solved by tree-chain splitting. A little kill the chicken with slaughter sledgehammer feeling.

"Algorithmic" RMQ LCA Lecture Notes

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.