"Data structure and algorithm" and the basis of the check set

Source: Internet
Author: User

1. Introduction

The and check set is a tree-type data structure, which is used to deal with the merging problems of disjoint sets.

And the main operation of the check set are:

(1) merging two disjoint sets;

(2) Determine whether two elements belong to the same set;

(3) path compression;

2. Common operation

Use Father[i] to represent the parent node of element I, for example:

Uses the root node of the tree in which an element is located to represent the set of elements;

When judging whether two elements belong to the same set, it is only possible to determine whether the root node of their tree is the same;

In other words, when we merge two sets, we only need to connect the edges between the two root nodes.

Get the root node code:
1 int findfather (int  x) {2     if(father[x] = = x)3         return  x; 4     Else 5         return Findfather (father[x]); 6 }
Determine if you belong to the same collection code:
1 bool judge (int x,int  y) {2     int  fx,fy; 3     FX = Findfather (x); 4     FY = findfather (y); 5     return fx==fy; 6 }

Merge different elements into the same collection code:
1 void unionset (int x,int  y) {2     x = findfather (x); 3     y = findfather (y); 4     Father[x] = y; 5 }

3. Optimize the idea of path compression

Each time the search, if the path is longer, then modify the information so that the next time you find faster;

Steps

(1) Find the root node;

(2) Modify all nodes on the lookup path and point them to the root node;

For example, find the following and check the set of "20", "9,10,20" are on the lookup path, then the path compression

Lookup algorithm code with path compression
1 intFindfather (intx) {2     intR =x;3     //get the root of x4      while(Father[r]! =R)5R =Father[r];6     intI=x;7     //update the nodes in searching path8      while(I! =R) {9j =Father[i];TenFather[i] =R; Onei =J; A     } -     returnR; -}
4. Optimization 2-Merging ideas

Two sets merge, that is, 2 trees merge, in order to reduce the depth of the merged tree, generally take the tree roots of the depth of small trees as the root of the tree of the Children node.

Strategy

Increases the depth of the secondary spatial record tree.

Merge code:

1 voidUnionset (intXinty) {2x =findfather (x);3y =Findfather (y);4     if(x = =y)5         return ;6     if(Rank[x] >Rank[y]) {7Father[y] =x;8}Else{9         if(Rank[x] = =Rank[y])Tenrank[y]++; OneFATHER[X] =y; A     } -}
5, and find examples of 5.1, HDOJ1232 (unblocked project)

Http://www.cnblogs.com/CheeseZH/archive/2012/05/13/2498073.html

5.2, HDOJ1272 (Xiaoxi's Maze)

Http://www.cnblogs.com/CheeseZH/archive/2012/05/25/2518639.html

6, and check set exercises

(1) Legend of the Galactic Heroes (NOI2002)

(2) Food chain (NOI2001)

(3) Parity (ceoi99)

"Data structure and algorithm" and the basis of the check set

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.