Equivalence relation and Equivalence class
For each pair of elements (A, a, a,b∈s,a R B is either true or false, it is said that the relationship R is defined on the set S. If a R B is true, then we say A is related to B.
The equivalence relationship (equivalence relation) is a relationship that satisfies the following three properties R:
(1) Reflexivity: For all a∈s,a R A
(2) Symmetry: If a R B is and only if B R a
(3) Transitivity: If a R B and B r C is a R C
The relationship "≤" is not an equivalence relationship. Although it is reflexive (that is, a≤a), transitive (that is, a≤b and b≤c derive a≤c), it is not symmetrical because a≤b cannot derive b≤a.
If two cities are located in the same country, then they are defined by the relationship. It is easy to verify that this is an equivalence relationship. If you can travel by road from A to B, then set A and B have a relationship. If all roads are driven in two directions, then this relationship is also an equivalence relationship.
And check the set:
In computer science, and the collection is a tree-shaped data structure, which maintains to deal with some do not want to cross the collection (disjoint sets) merger and query problems. Often used in the forest to express, to be quickly structured.
And check the set to keep a group of not want to cross the Dynamic Collection S={s1,s2,...,sk}. Each collection is identified by a delegate, which represents a member of the collection. In some applications, it doesn't matter which member is chosen as a delegate. In some applications, how to select a representative may have pre-stated rules, such as selecting the smallest element in a collection.
And check the set to provide the operation:
1. Find: Determines which collection the element belongs to. It can be used to determine whether two elements are part of the same subset. Returns the name of the collection containing the given element
2. Union: Merging two subsets into the same set
3. MakeSet: Used to create a single-element collection.
And the method of representation of the set:
1. Single-linked list representation
A simple way to implement and look up a data structure is that each collection is represented by a list of links. The first object of each list is represented by the collection in which it resides. Each object in the list contains a collection member, a pointer to an object that contains the members of the next collection, and a pointer to the delegate. Each linked list contains a head pointer and a tail pointer, and head points to the list's representative, tail to the last object in the list.
2. And check the forest
Another faster implementation of the check set is to use a root tree to represent each node in the collection tree that contains a member, each tree representing a collection. In a disjoint forest, each member points only to the parent node that points to it. The root of each tree contains the representation of the collection and is its own parent node. As we will see, although the direct algorithm using this representation is not faster than the algorithm represented by the linked list, by introducing two heuristic strategies ("Merge by rank" and "path compression"), we can get a progressive optimal disjoint collection data structure.
In this representation, the make-set operation simply creates a tree with only one node, and the find operation locates the root of the tree along the pointer to the parent node. This makes the lookup path a node that is accessed through a simple path on the root node. The union operation causes a tree to point to the root of another tree. As shown in the Union operation:
At the beginning, each collection contains one element. These books don't have to be two forks, but it's easy to use a binary tree, because the only information we need is the parent link. The name of the collection is given by the node at the root. Since only the name of the parent node is required, it can be assumed that the tree is not explicitly stored in the array (similar to the two fork heap): Each member of the array S[i] represents the father of element I. If I is the root, then s[i]=-1.
Two kinds of improvement strategies for forest are also found:
1. Merge by rank (Union by rank). Methods for improving Union operations. The idea of this approach is to point the root of a tree with fewer nodes to the root of a tree with more nodes in the Union operation. In order to implement this method, we need to remember the size of each tree. Because an array is actually used, you can let the array of each root contain a negative value for the size of its tree. In this way, the initial array representation is 1. When a union is executed, the size of the tree is checked, and the new size is the original size. This is not difficult to achieve, and does not require additional space, and its average speed is fast.
2. route compression (path compression). methods to improve the find operation. It points each node directly to the root during the lookup process. As shown below
Implementation code:
1 classDisjsets {2 Public:3 ExplicitDisjsets (intnumelements);4 5 intFind (intx);6 7 voidUnion (intElement1,intELEMENT2);//Union is key word in C + +, use Union8 9 Private:Tenvector<int>s; One }; A -Disjsets::D isjsets (intnumelements): s (numelements) { - for(vector<int>::iterator iter =S.begin (); theIter! = S.end (); ++ITER) { -*iter =-1; - } - } + - //Path Compression + intDisjsets::find (intx) { A if(S[x] <0) at returnx; - Else - returnS[X] =Find (s[x]); - } - - voidDisjsets::union (intElement1,intElement2) { in intROOT1 = Find (element1);//find root of Element1 - intRoot2 = Find (Element2);//find root of Element2 to + if(S[root2] < S[ROOT1]) {//Root2 is deeper -S[ROOT1] = Root2;//Make root2 new root the}Else { * if(S[ROOT1] = =S[root2]) $s[root1]--;//Update Height ID samePanax NotoginsengS[root2] = ROOT1;//Make root1 new root - } the}
"Classic data Structure" and check set