In another faster implementation in a disjoint collection, a root tree is used to represent the collection. Each member of the tree points to its parent node, and the root of each tree contains the delegate (representative) and is his own parent node. A disjoint forest is made up of trees such as the following:
[Note: (b) is (a) the result of UNION (E,G)]
Using the above representation is no more intuitive than using a linked list representation algorithm, but it can be improved by "combining by rank" and "path compression".
Merge by rank (Union by rank):
For each node, a rank is used to represent an upper bound of the node height. In a merge by rank, a lower rank is followed in a union operation to point to a root with a large rank.
Path compression (compression):
In the find-set operation, this heuristic strategy is used to point each node on the lookup path directly to the root node. (path compression does not change the rank of the node)
The pseudo-code for the relevant operation is as follows:
It should be noted that the above Find-set process takes a two-way approach (Two-pass method): A trip is up along the lookup path up to the root, and the second trip is down along the lookup path to update each node so that it points directly to the root.
The complete code for the disjoint forest to implement the connected sub-graph is as follows:
#include <iostream> #include <string> #include <cstdlib> #include <vector>using namespace std; typedef struct SETNODE{CHAR key;int rank;setnode *parent;setnode (char k): Key (k), rank (0), parent (NULL) {}}setnode; typedef struct SET{SETNODE *root;} set;typedef struct Edge{char U;char v;} Edge;setnode *make_set (char k) {Setnode *x=new setnode (k); x->parent = X;return x;} Setnode *find_set (Setnode *x) {if (x! = x->parent) X->parent=find_set (x->parent); return x->parent;} void Link (Setnode *x,setnode *y) {if (X->rank > Y->rank) y->parent = x;else{x->parent = y;if (X->rank = = y >rank) Y->rank = Y->rank + 1;}} void Set_union (Setnode *x,setnode *y) {Link (Find_set (x), Find_set (y)); Setnode *z=find_set (x);} void Forestset_create (Set forestset[],char vertex[],int vnum) {for (int i=0;i<vnum;i++) {int index= (int) vertex[i]; eg.a->97,b->98,... forestset[index].root = Make_set (Vertex[i]);}} void Compute_concomponents (Set forestset[],edge EDGEarray[],int eNum) {//compute the component forestfor (int i=0;i<enum;i++) {setnode *set_u=forestset[(int) edgearray[ I].u].root;setnode *set_v=forestset[(int) edgearray[i].v].root;if (Find_set (set_u)! = Find_set (set_v)) Set_Union (set _U,SET_V);} }void print_concomponents (Set forestset[],char vertex[],int vnum) {//classify The forest and Print the Connect components a nd the representativestring representative;for (int i=0;i<vnum;i++) {Setnode *t;t=find_set (forestSet[(int) vertex[i ]].root); if (Representative.find (t->key) = =-1)//the char T is not in representative representative + = T->key;} cout<< "The representative of the forest:" <<representative<<endl;int replen=representative.length ( );vector<char> *componentsvec = new vector<char>[replen]; for (int i=0; i<vnum; i++) {Setnode *temp;temp=find_set (forestset[(int) vertex[i]].root); int index= Representative.find (Temp->key); Componentsvec[index].push_back (Vertex[i]);} for (int i=0; i<replen;i++) {cout<< "The Connect component" <<i+1<< "is:"; for (int j=0; J<componentsvec[i].size (); J + +) cout<<componentsvec[i].at (j) << "; Cout<<endl;}} int main () {char vertex[]={' a ', ' B ', ' C ', ' d ', ' e ', ' f ', ' g ', ' h ', ' I ', ' j '};edge edgearray[]={{' B ', ' d '},{' e ', ' g '},{' a ', ' C ' },{' h ', ' I '},{' a ', ' B '},{' e ', ' F '},{' B ', ' C '}};int vnum=sizeof (vertex)/sizeof (char); int enum=sizeof (Edgearray)/ sizeof (EDGE); Set forestset[256]={null};forestset_create (Forestset,vertex,vnum); Create Forest setcompute_concomponents (forestset,edgearray,enum); Computing the Component forestprint_concomponents (forestset,vertex,vnum); return 0;}Operation Result:
"Note: If there is an error, please correct me ~ ~ ~"
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Introduction to Algorithms Chapter 21st: Disjoint collection Forest