Union-find sets is a very ingenious and useful data structure, which is mainly used to deal with the merging problems of disjoint sets . Some common uses include the Kruskal algorithm for connecting sub-graphs, the minimum spanning tree, and the nearest common ancestor (Least Common ancestors, LCA).
When using and checking sets, there is a set of disjoint dynamic collections firstS={< Span id= "mathjax-span-11" class= "Msubsup" >s 1, S 2, ? ,Sk} s={s1,s2,?, Sk}, which typically uses an integer to represent an element in the collection.
Each collection may contain one or more elements and select an element in the collection as the representative . Each collection contains exactly what elements are not cared for, and which element is chosen as the representative of the general is not concerned. What we care about is that for a given element, you can quickly find the set where the element resides, and merge the collection of two elements, and the time complexity of these operations is constant .
There are three basic operations for the check set:
- MakeSet (s): establishes a new and checked set that contains the s single-element collection.
- Unionset (x, y): Merges the set of element x and element y, requiring that the set of x and Y do not intersect, if the intersection is not merged.
- Find (x): Finds the representative of the collection where element x is located, which can also be used to determine whether two elements are in the same collection, as long as they compare their respective representatives.
and the realization of the principle of the set is also relatively simple, is to use the tree to represent the collection, each node of the tree represents an element of the collection, the root of the corresponding element is the representative of the collection, 1 is shown.
The node of the tree represents the elements in the collection, the pointer represents a pointer to the parent node, and the pointer to the root node points to itself, indicating that it has no parent node. The parent node of each node continues to look up, and eventually the root node of the tree, the representative element of the collection, is found.
Now, it should be easy to write the code for MakeSet and find, assuming that using a long enough array to store the tree nodes (much like the static linked list mentioned earlier), the MakeSet is to construct 2 of the forest, where each element is a set of cells, that is, the parent node itself:
The
Corresponding code is as follows, with a time complexity of O ( n ) " > O (n ) :
1 const int MAXSIZE = 500 2 Uset[maxsize]; 3 &NBSP; 4 int size) { 5 for (int i = 0 ; i < size;i++) Uset[i] = I; 6 }
Next, is the find operation, if each time along the parent node to look up, the time complexity is the height of the tree, it is impossible to reach the constant level. Here you need to apply a very simple and effective strategy-path compression.
Path compression, which means that each node on the lookup path points directly to the root node at each lookup, as shown in 3.
I prepared two versions of the Find operation implementation, respectively, is recursive version and non-recursive version, but the two version has not found any obvious efficiency gap, so the specific use of which is entirely personal preference.
1 intFindintx) {2if(x = uset[x]) uset[x] =find (Uset[x]);3returnUset[x];4 }5 intFindintx) {6intp =x, T;7 while(uset[p]! = p) p =Uset[p];8 while(x! = p) {t = uset[x]; uset[x] = p; x =t;}9returnx;Ten}
Finally, the merge operation Unionset, and the merge of the check set is also very simple, that is, the tree root of one set points to the root of another collection, 4.
1 void unionset (intint y)2{3 int a=find (x), b=find (y); 4 if (a!=B) 5 uset[a]=uset[b]; 6 }
http://blog.csdn.net/dellaserss/article/details/7724401 incisive
and check Set