and check Set

Source: Internet
Author: User

<title>and check Set</title> and check Set

A simple and check-set implementation:

void Init(void) { for(int I= 0; I < 10;    i++) {Par[i] = i; }}int Find(int I) {if(I! = Par[i]) {returnPar[i] = find (Par[i]); }Else{returnI }}void Union_connect(int I,int J) {int P= Find (i);int Q= Find (j);if(p = = q) {return; }Else{Par[q] = p; }}

With path compression and check set

int find0(int I) {// Recursive Implementation    if(I! = Par[i]) {Par[i] = find0 (Par[i]);// the compression path when backtracking}// The nodes from the I-node search to the ancestor nodes point to the ancestor node .    returnPar[i];}int Main() {Union_connect (1, 3);    Union_connect (3, 4);    Union_connect (4, 8);    Union_connect (8, 9); Union_connect (5, 8);// here par[1] and Par[8] become 5, but 3 and 4 are unchanged .    return0;}int Find1(int I) {// Iterative implementations    int x= i;// Save the original node     while(I! = Par[i]) {// Find and Nodei = par[i];// Find the node and use I to record}int k= x;// return to the original node     while(k! = i) {// non-recursive path compression operations        int J= Par[k];// The parent node of the J staging Par[k]Par[k] = i;// Par[i] Point and NodeK = J;// k Move to parent node}returnI// returns the value of the root node}

and 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.