publicclassQuickUnionPathComparisonUF{privateint[] id;// set id of each object to itself (N array accesses)publicQuickUnionUF(int N) { id =newint[N];for (int i =0; i < N; i++) id[i] = i; }// chase parent pointers until reach root (depth of i array accesses)privateintroot(int i) {while (i != id[i]) { id[i] = id[id[i]]; // only change with quick union i = id[i]; }return i; }// check if p and q have same root (depth of p and q array accesses)publicbooleanconnected(int p,int q) {returnroot(p)==root(q); }// change root of p to point to root of q (depth of p and q array accesses)publicvoidunion(int p,int q) {int i =root(p);int j =root(q); id[i] = j; }}