The change of the ID array:
0 1 2 3 4 5 6 7 8 9 10 components9 00 1 2 3 4 5 6 7 8 0 9 components3 40 1 244 5 6 7 8 0 8 components5 80 1 2) 4 486 7 8) 0 7 components7 20 1 2 4 4 8 628 0 6 components2 10 114 4 8 618 0 5 components5 70 1 1) 4 416 110 4 components0 341 1 4 4 1 6 1 143 components4 211 11 11 6 1 112 Components
Operation Count Analysis:
The Find () function accesses the array 1 times per Call.
The connected function calls the Find () function every two times, so it accesses the array 2 Times.
The Union function accesses the array in the following number of Times: 2 + N + (1,n-1). Where 2 calls the Find () function for two times, n is the n number of times group, (1,n-1) is the possible array substitution number.
public Static voidmain (string[] Args) {//Initialize N components intN =Stdin.readint (); Ufquickfind uf=NewUfquickfind (N); Stdout.println (uf); while(!Stdin.isempty ()) { intp =Stdin.readint (); intQ =Stdin.readint (); if(uf.connected (p, Q)) {//Ignore if connectedStdout.println (p + "" + q + "is Connected")); Continue; } uf.union (p, q);//Connect p and QStdout.println (p + "" +q); Stdout.println (uf); }
For this client, the connected function and the union function are called once for each data pair.
The following log group accesses are analyzed:
9 0:2 + 2 + 10 + 1
3 4:2 + 2 + 10 + 1
5 8:2 + 2 + 10 + 1
7 2:2 + 2 + 10 + 1
2 1:2 + 2 + 10 + 2
5 7:2 + 2 + 10 + 2
0 3:2 + 2 + 10 + 2
4 2:2 + 2 + 10 + 4
Algorithm (algorithms) version 4th practice 1.5.1