1

I was learning about quick find and made a very basic implementation in java but my union function was not working properly when I initially implemented it. I made some changes and it started working but I'm not sure whats causing the problem in the first iteration of my function. I can see that for some reason the parent of P is changing even though I'm not making any changes to the argument p but I don't know why. Sorry if this is a stupid question I'm just lost here.

First attempt

public class QuickFindUF {

    private int[] id;

    public QuickFindUF(int N){
        id = new int[N];
        for (int i = 0;i<N;i++){
            id[i] = i;
        }
    }

    public boolean connected(int p, int q){
        if (id[p] == id[q]){
            return true;
        }
        return false;
    }

    public int find(int x){
        return id[x];
    }

    public void union(int p, int q){

        if (connected(p,q)) {
            System.out.println(p+" and "+q + " are already connected");
            return;
        }

        for (int i =0; i < id.length;i++){
            if (id[i] == find(p)){
                System.out.println("p's parent is " + find(p) + " list item is "+id[i]);
                id[i] =find(q);
            }
        }
    }
    public void print(){

        System.out.print("[");
        for (int i =0; i < id.length; i++){
            System.out.print(id[i]);
            System.out.print(", ");
        }
        System.out.println("]");
    }

    public static void main(String args[]){
        QuickFindUF qfobj = new QuickFindUF(10);
        qfobj.print();
        qfobj.union(3,5);
        qfobj.print();
        qfobj.union(3,8);
        qfobj.print();

    }
}

second (working) attempt

public class QuickFindUF {

    private int[] id;

    public QuickFindUF(int N){
        id = new int[N];
        for (int i = 0;i<N;i++){
            id[i] = i;
        }
    }

    public boolean connected(int p, int q){
        if (id[p] == id[q]){
            return true;
        }
        return false;
    }

    public int find(int x){
        return id[x];
    }

    public void union(int p, int q){

        if (connected(p,q)) {
            System.out.println(p+" and "+q + " are already connected");
            return;
        }

        int pid = find(p);
        int qid = find(q);

        for (int i =0; i < id.length;i++){
            if (id[i] == pid){
                System.out.println("p's parent is " + find(p) + " list item is "+id[i]);
                id[i] =qid;
            }
        }
    }
    public void print(){

        System.out.print("[");
        for (int i =0; i < id.length; i++){
            System.out.print(id[i]);
            System.out.print(", ");
        }
        System.out.println("]");
    }

    public static void main(String args[]){
        QuickFindUF qfobj = new QuickFindUF(10);
        qfobj.print();
        qfobj.union(3,5);
        qfobj.print();
        qfobj.union(3,8);
        qfobj.print();

    }
}

Abhi
  • 13
  • 2
  • can you describe the goal of the algorithm? i.e. find what from what? – Lei Yang Mar 16 '22 at 02:45
  • I am unsure what you are trying to achieve with this code, But when you use `find(p)` inside the union loop you get a live result that will reflect changes made to `id` like so `id[i] =find(q);`. However, if you use `int pid = find(p);` outside the union loop then you are storing an old value that is not changed whenever you use `pid` inside the loop, or when you update `id` here `id[i] = pid;`. Do you intend to store the old fixed value like shown in your second attempt `id[i] =qid;`, or should you really be using `id[i] =find(q);` as shown in your first attempt? – sorifiend Mar 16 '22 at 04:05
  • Wouldn't find(p) and pid be the exact same value at every iteration of the loop since its independent? – Abhi Mar 18 '22 at 00:23

0 Answers0