Query sets to implement the Tarjan algorithm, and set to implement the Tarjan Algorithm

Source: Internet
Author: User

Query sets to implement the Tarjan algorithm, and set to implement the Tarjan Algorithm

This article is a special example of http://noalgo.info/476.html.

Int father [mx]; // father of the node int ancestor [mx]; // ancestor of the accessed Node Set

Interpretation of the functions of these two arrays;

First, it must be clear that the re-built tree of the query set is not the same as the original tree;

Here is an example of the document:

The Tarjan algorithm is based on DFS (depth-first search). The order of tree depth-first traversal in the graph should be:

4->7->5->1->2->6->3->0

But the author said that the processing order of nodes is:

4->7->5->1->0->2->6->3

In fact, the first order here is the order in which we process the query requests. For example, if we traverse to 5, we can obtain all the nodes that have been traversed before 5 and 5 (5, 4) (5, 7) query results;

The second order is the order in which we create and query sets.

The key code is as follows:

1 void Tarjan (int x) // The Tarjan algorithm is used to solve LCA 2 {3 for (int I = 0; I <tree [x]. size (); I ++) 4 {5 Tarjan (tree [x] [I]); // access subtree 6 unionSet (x, tree [x] [I]); // merges the set of the subtree node and the root node x. Here, we query the set processing node x 7 ancestor [findSet (x)] = x; // The ancestor of the merged set is x 8} 9 vs [x] = 1; // marked as accessed, here is the DFS traversal node x10 for (int I = 0; I <query [x]. size (); I ++) // queries related to root node x 11 if (vs [query [x] [I]) // if another node to be queried has been accessed, the nearest common ancestor of output result 12 printf ("% d and % d is: % d \ n", x, 13 query [x] [I], ancestor [findSet (query [x] [I]); 14}

The reason for the different order lies in the following 6th and 9th lines of code. When we have not traversed the parent node, after processing the first subtree, the parent node is already in and in the query set. For example, if we haven't traversed Node 1, after traversing node 4, the merge of node 4 and its parent node 1 will be processed;

 

Next, we will build and query the set tree;

Step 1:

Elements that have been traversed: {4}, set [4] {4}-> 4

Step 2:

The set {4} and parent node {1} are merged by rank. The merged set is {4, 1 },The Set indicates that the element is 4, that is, father [4] = 4, father [1] = 4; The Public ancestor of the set {4} is 1, and ancestor [4] = 1; that is, the representative element of this set is not its common ancestor.

Step 3:

Elements that have been traversed include {} And two sets [4] {} and [7] {7}. The elements in [] represent elements, {} is all elements in the set.) in this case, if the query () is used, 4 has been traversed, And the element representing Access 4 in the set is father [4]. the common ancestor of set 4 is ancestor [father [4].

Step 4:

Elements that have been traversed include {, 5}, set [7] {7}, merged set [5] {5} to [7] {}, ancestor [7] = 5

.....

After traversing the first subtree of the root node, the set [7] {,} and the root node set [0] {0} are merged into [7, 7}, that is, father [0] = 7. At the same time, the Set's public ancestor [7] = 0 is updated;

Elements {,} That have finally been traversed. The set is [7] {,}, ancestor [7] = 0.

For ease of understanding, the final graph is not compressed by path. In fact, the parent node of all elements is set to represent element 7.

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.