1.5.11实现加权quick-find算法,其中我们总是将较小的分量重命名为较大的分量的标识符。这种改变会对性能产生怎样的影响?
答:每次union需要多访问N次id数组进行判断,同时还需要对两个分量的触点个数的sz赋新值,也会增加数组的访问,性能将会降低。public class E1d5d11{ private int[] id; private int[] sz; private int count; public E1d5d11(int N) { count=N; id=new int[N]; for (int i=0;i<N;i++) id[i]=i; // sz=new int[N]; for (int i=0;i<N;i++) sz[i]=1; } public int count() {return count;} boolean connected(int p,int q) {return find(p)==find(q);} public int find(int p) {return id[p];} public void union(int p,int q) { int pID=find(p); int qID=find(q); if (pID==qID) return; int totalSize=sz[p]+sz[q]; int oldID; int newID; if(sz[p]<sz[q]) { oldID=pID; newID=qID; } else { oldID=qID; newID=pID; } for (int i=0;i<id.length;i++) { if(id[i]==oldID) id[i]=newID; if(id[i]==newID) sz[i]=totalSize; } count--; } // public static void main(String[] qrgs) { int N=StdIn.readInt(); E1d5d11 uf=new E1d5d11(N); while (!StdIn.isEmpty()) { int p=StdIn.readInt(); int q=StdIn.readInt(); if(uf.connected(p,q)) continue; StdOut.println(p+ " " +q); uf.union(p,q); }//end while }//end main}//end class