Disjoint set adt--array implementation

Source: Internet
Author: User

The disjoint set is an effective data structure to solve the equivalence problem, because the data structure is simple (a few lines of code, a simple array can be done), fast (each operation can basically be done within the constant average time).

First we need to understand what is called equivalence, and before that, there is a definition of a relationship (relation).

Relation: The relationship defined on the dataset S r means that for each pair of elements (a, b) that are part of the DataSet S, a r is either True or false. If a R B is true, it says a related B, that is, a is related to B.

The equivalence relationship is also a relationship (Relation), but it is to satisfy some constraints

A) a R a, for all a that belongs to S

b) a r B when and only if B r a

c) a R B and B r A means a r c

Dynamic equivalence issues:

Defines the relationship R on the non-empty set S, and determines whether a R B is true for each pair of elements (a, a, or a) in the dataset S, i.e. whether A and B are related.

For whether A and B are related, we only need to prove whether A and B are in the same equivalence class set.

The code for the array implementation is as follows:

#include <iostream>using namespace std; #define numsets 8typedef int Disjset [numsets + 1];typedef int Settype;typede f int elementtype;void initiaize (Disjset S) {for   (int i = numsets; i > 0; i.)   s[i] = 0;} SetType Find (ElementType x, Disjset S) {   if (S[x] <= 0)   return x;   else   return Find (s[x],s);} void Setunion (Disjset S, SetType Root1, SetType Root2)    //by height and {   if (S[root2] < S[ROOT1])  //root2 height a bit higher   s[root1] = Root2;   else   {      if (s[root1] = = S[root2])   //As high as  s[root1]--;  S[root2] = Root1;   }} int main () {Disjset s1;initiaize (S1); Setunion (S1, 5,6); Setunion (S1, 7,8); Setunion (S1, 5,7);  Setunion (S1, 5,4); for (int i = 1; I <= numsets; ++i) cout << s1[i] << ' \ t '; cout << endl;cout << Find (3, S1) << Endl;   return 0;}

Portal: http://blog.csdn.net/changyuanchn/article/details/16810535 This blog is written more clearly, can refer to, my blog is mainly code integrity.

Late at night,,,

Disjoint set adt--array implementation

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.