Pre
Full-text Base setting
-----------------------------
N Total number of sets (i.e., number of individual sets)
M total number of operations
F Find operand
Ackerman (k,n) = {n+1/k==0 | Ackerman.iter (n+1,k=k-1) (k-1,n)}/specific definition can see Wiki
Alpha (n) = Min{k:ackerman (k,1) >=n}
From the introduction of algorithms
The disjoint set has two optimizations: merge by rank (Union by Rank,ur) and path compression (path compression,pc)
which
Time complexity combined by rank only $\text{o} (M \log N) $
Time complexity with path compression only $\theta (n + F \cdot (1 + \log_{2 + f/n} N)) $
Two time Complexity $\text{o} (M \alpha (n)) $
Test Program
and Set Program
#define SIZEX 100000000int f[sizex],rk[sizex];struct ds{inline void init (int size) {for (int i=0;i<size;++i) f[i]=-1;} void find (int t) {return ~f[t]?t:find (f[t]);} inline void join (int a,int b) {A=find (a), b=find (b); if (a==b) return;f[b]=a;}} Djs;struct dsu{inline void init (int size) {for (int i=0;i<size;++i) f[i]=-1,rk[i]=1;} void find (int t) {return ~f[t]?t:find (f[t]);} inline void join (int a,int b) {A=find (a), B=find (b), if (a==b) return;if (Rk[a]>=rk[b]) {f[b]=a;rk[a]+=rk[b];} Else{f[a]=b;rk[b]+=rk[a];}}} Djsu;struct dsp{inline void init (int size) {for (int i=0;i<size;++i) f[i]=-1;} void find (int t) {return ~f[t]?t:f[t]=find (f[t]);} inline void join (int a,int b) {A=find (a), b=find (b); if (a==b) return;f[b]=a;}} Djsp;struct dsup{inline void init (int size) {for (int i=0;i<size;++i) f[i]=-1,rk[i]=1;} void find (int t) {return ~f[t]?t:f[t]=find (f[t]);} inline void join (int a,int b) {A=find (a), B=find (b), if (a==b) return;if (Rk[a]>=rk[b]) {f[b]=a;rk[a]+=rk[b];} Else{f[a]=b;rk[b]+=rk[a];}}} Djsup;
Test program
#define SIZEX 100000000int f[sizex],rk[sizex];struct ds{inline void init (int size) {for (int i=0;i<size;++i) f[i]=-1;} int find (int t) {return ~f[t]?find (f[t]): t;} inline void join (int a,int b) {A=find (a), b=find (b); if (a==b) return;f[b]=a;}} Djs;struct dsu{inline void init (int size) {for (int i=0;i<size;++i) f[i]=-1,rk[i]=1;} int find (int t) {return ~f[t]?find (f[t]): t;} inline void join (int a,int b) {A=find (a), B=find (b), if (a==b) return;if (Rk[a]>=rk[b]) {f[b]=a;rk[a]+=rk[b];} Else{f[a]=b;rk[b]+=rk[a];}}} Djsu;struct dsp{inline void init (int size) {for (int i=0;i<size;++i) f[i]=-1;} int find (int t) {return ~f[t]?f[t]=find (f[t]): t;} inline void join (int a,int b) {A=find (a), b=find (b); if (a==b) return;f[b]=a;}} Djsp;struct dsup{inline void init (int size) {for (int i=0;i<size;++i) f[i]=-1,rk[i]=1;} int find (int t) {return ~f[t]?f[t]=find (f[t]): t;} inline void join (int a,int b) {A=find (a), B=find (b), if (a==b) return;if (Rk[a]>=rk[b]) {f[b]=a;rk[a]+=rk[b];} Else{f[a]=b;rk[b]+=rk[a];}}} Djsup, #include <CSTdio> #include <random> #include <malloc.h> #include <sys/time.h>using namespace Std;struct cmd{ BOOL Type;int A, B;} temp;cmd* Data;long Long mytic () {Long long result = 0.0;struct Timeval tv;gettimeofday (&TV, NULL); result = ((Long lo NG) tv.tv_sec) *1000000 + (long long) Tv.tv_usec;return result;} #define DIC1 () DisA (generator) #define DIC2 () disb (generator) void gendata (int a,int b,int c) {mt19937 generator;uniform_ Int_distribution<int> DisA (0,a-1);uniform_int_distribution<int> disb (0,b+c-1); int k=b+c,i=0,j;for (; i <b;++i) Data[i].type=0,data[i].a=dic1 (), Data[i].b=dic1 (); for (; i<k;++i) Data[i].type=1,data[i].a=dic1 (); for (I=0;i<k;++i) {J=dic2 (); if (i==j) continue;temp=data[i];d ata[i]=data[j];d ata[j]=temp;}} void testn (int a,int b,int c) {int k=b+c,i;printf ("None opt\n"), Long Long start=mytic ();d js.init (a); for (I=0;i<k;++i) {switch (data[i].type) {case 0:djs.join (data[i].a,data[i].b), Case 1:djs.find (DATA[I].A);}} Start=mytic ()-start;printf ("Time Usage:%lld us\n", start);} void Testu (int a,int b,int c) {int k=b+c,i;printf ("union by Rank opt\n"), Long Long start=mytic ();d jsu.init (a); for (i=0;i& Lt;k;++i) {switch (data[i].type) {case 0:djsu.join (data[i].a,data[i].b), Case 1:djsu.find (DATA[I].A);}} Start=mytic ()-start;printf ("Time Usage:%lld us\n", start);} void testp (int a,int b,int c) {int k=b+c,i;printf ("path compression opt\n"), Long Long start=mytic ();d jsp.init (a); for (i=0 ; i<k;++i) {switch (data[i].type) {case 0:djsp.join (data[i].a,data[i].b), Case 1:djsp.find (DATA[I].A);}} Start=mytic ()-start;printf ("Time Usage:%lld us\n", start);} void Testup (int a,int b,int c) {int k=b+c,i;printf ("both opt\n"), Long Long start=mytic ();d jsup.init (a); for (i=0;i<k;+ +i) {switch (data[i].type) {case 0:djsup.join (data[i].a,data[i].b), Case 1:djsup.find (DATA[I].A);}} Start=mytic ()-start;printf ("Time Usage:%lld us\n", start);} int main () {int a,b,c,i,j,k,l,m,n,n,u,p,up;data= (cmd*) malloc (200000000*sizeof (cmd)), while (printf ("0 to Quit>"), scanf ("%d", &a), a) {printf ("N U P up\n"); scanF ("%d%d%d%d", &n,&u,&p,&up);p rintf ("Set Size:"), scanf ("%d", &a);p rintf ("OP join num:") scanf ("% D ", &b);p rintf (" Op find num: "); scanf ("%d ", &c); if (a>100000000| | (b+c) >200000000) continue;i=b+c;printf ("Total%d ops\n", b+c), Gendata (A,b,c), if (n) testn (a,b,c), if (U) Testu (a,b,c if (P) TESTP (a,b,c); if (up) Testup (a,b,c);} Free (data); return 0;}
raw data
Set Size:500join operations:500find operations:500total opsnone opt time usage:383 usunion by Rank opt time usage : Uspath Compression opt time usage:48 usboth opt (s) time usage:39 usset size:10000join operations:7000find Operati Ons:10000total 17000 opsnone opt time usage:1159 usunion by Rank opt time usage:246 uspath Compression opt time usage: 274 Usboth opt (s) time usage:245 usset size:100000join operations:70000find operations:100000total 170000 opsnone opt Time usage:134035 usunion by Rank opt time usage:3096 uspath Compression opt time usage:3291 usboth opt (s) Time usage: 2967 usset size:400000join operations:280000find operations:400000total 680000 opsnone opt time usage:5293524 usUnion By Rank opt time usage:14099 uspath Compression opt time usage:14466 usboth opt (s) time usage:12509 Usset size:1000000 Join Operations:700000find operations:1000000total 1700000 opsunion by Rank opt time usage:48863 uspath Compression opt Time usage:40933 UsbotH opt (s) time usage:41501 usset size:10000000join operations:7000000find operations:10000000total 17000000 opsUnion by Rank opt time usage:882355 uspath Compression opt time usage:1118981 usboth opt (s) time usage:814115 Usset size:10000 0000join operations:70000000find operations:100000000total 170000000 opsunion by Rank opt time usage:13025344 usPath Co Mpression opt time usage:19499277 usboth opt (s) time usage:12493686 US
(-O2)
===================================================set Size:100000000join operations:60000000find Operations: 120000000total 180000000 opsunion by Rank opt time usage:10360505 uspath Compression opt time usage:11255217 usboth opt ( s) Time usage:9452136 US
(-O3)
And check the efficiency of the set test