"C + +" recent public ancestor LCA (pre-knowledge)

Source: Internet
Author: User

1. Preface

The recent public ancestor (Least Common ancestors), referred to as LCA, is a common ancestor problem raised by Professor Tarjan (yes and he) in a root tree to find the nearest two nodes U and v.

2. What is the recent public ancestor?

In a tree, each node has his father and ancestors, and the recent public ancestor is the two nodes in this tree the depth of the largest public ancestor node .

In other words, it is the nearest common ancestor node of two points on this tree. The combination and text should be a good interpretation of the recent public ancestor:

PS: In LCA, the node itself can also be regarded as its own ancestor

In this tree with node 1 root, the nearest public ancestor of 4 and 5 is the nearest public ancestor of 2,4 and 3 is the nearest public ancestor of 1,4 and 2 is the nearest public ancestor of 1,4 and 1 is 1.

3. What is offline/online algorithm

For the LCA problem, common methods are Tarjan (dfs+ and set) algorithm, multiplication algorithm, ST (dfs+st table) algorithm, the latter two are online algorithms, and the former is offline algorithm (the subsequent LCA algorithm will be updated according to the order given now).

So what is the In/offline algorithm? Here quoted a section of the Baidu Encyclopedia saying:

Online algorithm:"online algorithm means that it can be serialized in a way of processing input, that is, at the beginning do not need to know all the input." "

  Offline algorithm:"refers to the basic assumptions that are based on the known input data before the algorithm is executed, that is, for an offline algorithm, you need to know all the input data of the problem at the beginning, and output the result immediately after solving a problem." "

  Here for example, the online algorithm is what we often say "input a set of data, output a set of data", while the offline algorithm is "input all data in the output answer."

4.LCA Problem Method Introduction:

1.Tarjan algorithm:

2. Multiplication algorithm:

3.ST Table +dfs algorithm:

"C + +" recent public ancestor LCA (pre-knowledge)

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.