Recent public ancestor (LCA): Offline & online Algorithms

Source: Internet
Author: User

Problem: For the nearest common ancestor of two nodes (that is, the lowest ancestor in the common ancestor in the tree), here are two algorithms for different scenarios.

HIHO15: Offline Tarjan algorithmBasic Ideas

The Tarjan algorithm is suitable for offline batch processing of multiple query requests. The basic idea is to visit the tree in the order of the depth-first search, to dye the node of the tree, and at first all the nodes are white, and when it passes through a node for the time being, it is dyed gray, and when it passes through this node the second time-that is, when it leaves the subtrees tree, it is dyed black.


The significance of doing this, for example, when we search for a node in depth, we find that A and B nodes are a set of queries that we need to deal with. At this point the tree nodes are dyed as:


found that the color of the B-node is gray, so the recent public ancestor must be a B node (gray represents the first time to enter the node of the subtree, a node in the Shari of B);

For a set of queries at this point A and p, found that the P node color is white, it is left to access to the P node processing;

There is a group of questions A and C, found that C is black, then the LCA is the first gray node of the C node upward.

    • Summary: I first calculate each node involved in the query, and then in the process of depth-first search for the node staining, if found that the current access to the node is related to a query, then I will look at another node in the query color, if it is white, then left to deal with, if it is gray, So recently the public ancestor must be this gray node, if it is black, then the recent public ancestor is this black node upward the first gray node.

Find the nearest gray node of a black node using and looking up a set .

Another problem is how to quickly find the first gray node up to the Black node: use and check for maintenance.

Think about it, when we get to a junction C, the node is gray, and the subtree back to that node, the subtree nodes are all black, and the first gray node of the black nodes upward is the current node C.

And this node leaves the time-varying black, his own upward of the first gray node is its parent, d, then this process, is actually the C node represents the collection into the D node represents the collection, such as:

    • Summary: Each node is initially a separate set, and whenever a node is turned black, it merges its collection into the set of its father's node. At any time, the representation element of the set of any black node is the first gray node up the node!
    • Description: The req array represents each node's representative element, and the Gray junction O is characterized by req[o]==o

So the process of finding can be written as follows:

int tree::find (int no) {   if (no = = Req[no])      return no;   else{      Req[no] = find (Req[no]);      return req[no];}   }


When the algorithm is implemented to solve the HIHO15 problem, in order to quickly find a node in the tree mapping, the string is mapped to the index value of the tree node in the array, so it is convenient to use and check the set.

HIHO17: Online algorithmBasic Ideas

Is that every time you ask to give a direct result, here is an online algorithm, the basic idea is to search from the root node depth of the tree, every time through a certain point-whether it is from its father nodes into this point, or every time from its son node return this point, are recorded in order. This transforms a tree into an array, and finding the nearest public ancestor of the two nodes in the tree is the only way to find the smallest point in the range of the last occurrence of the two nodes in the array, with the minimum value algorithm (RMQ).

Example of converting a tree to an array (picture from Baidu):


Array: 5 2 8 6 2 7 2 1 3 1 4 1

Note: Here I make a slight adjustment, that is, the non-leaf node and so on each access to a child node after the record, and the leaf nodes recorded once.

The code appendix is in the Leetcode warehouse of my GitHub account, please go to view if necessary.


Recent public ancestor (LCA): Offline & online Algorithms

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.