social-network-connectivity-with-union-find

开始想的是肯定是要用加权UF,这篇文章详细的说了思路
下面是我的代码

import java.io.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;


public class SocialNetworkConnectivity {
private QuickFindUF uf;
private int checkfull;
private File file;

public SocialNetworkConnectivity(int N, File file) {
    this.uf = new QuickFindUF(N);
    this.checkfull = N;
    this.file = file;
}

public String addConnection() {
    String earilesttime = null;

    try {
        BufferedReader bufferedReader = new BufferedReader(new FileReader(this.file));
        String line = null;

        try {
            while ((line = bufferedReader.readLine()) != null) {
                String[] splitstring = line.split(" ");
                if (!this.uf.connected(Integer.parseInt(splitstring[2]), Integer.parseInt(splitstring[1]))) {
                    this.uf.union(Integer.parseInt(splitstring[2]), Integer.parseInt(splitstring[1]));
                    --this.checkfull;
                    if (this.fullConnected()) {
                        earilesttime = splitstring[0];
                        break;
                    }
                }
            }

            bufferedReader.close();
        } catch (IOException var5) {
            var5.printStackTrace();
        }
    } catch (FileNotFoundException var6) {
        var6.printStackTrace();
    }

    return earilesttime;
}

public boolean fullConnected() {
    return this.checkfull == 1;
}

/*
*file
*2017091801 0 1
*2017091802 2 4
*2017091803 6 9
*2017091804 4 7
*2017091805 5 6
*2017091806 7 8
*2017091807 2 5
*2017091808 6 7
*2017091809 1 2
*2017091810 0 2
*2017091811 1 9
*2017091812 3 7
 */
public static void main(String[] args) {
    File file = new File("C:\\Users\\lh\\Desktop\\friends.txt");
    SocialNetworkConnectivity socialNetworkConnectivity = new SocialNetworkConnectivity(10, file);
    System.out.println(socialNetworkConnectivity.addConnection());
}

}

这是加权UF

public class QuickFindUF {
private int[] id;
private int[] sz;

public QuickFindUF(int N) {
    this.id = new int[N];
    this.sz = new int[N];

    for(int i = 0; i < N; this.sz[i] = i++) {
        this.id[i] = i;
    }

}

private int root(int i) {
    while(i != this.id[i]) {
        this.id[i] = this.id[this.id[i]];
        i = this.id[i];
    }

    return i;
}

public boolean connected(int p, int q) {
    return this.root(p) == this.root(q);
}

public void union(int p, int q) {
    int pRoot = this.root(p);
    int qRoot = this.root(q);
    if(pRoot != qRoot) {
        if(this.sz[pRoot] < this.sz[qRoot]) {
            this.id[pRoot] = qRoot;
            this.sz[qRoot] += this.sz[pRoot];
        } else {
            this.id[qRoot] = pRoot;
            this.sz[pRoot] += this.sz[qRoot];
        }

    }
}

public int findMax1(int p) {
    int[] largest = new int[this.sz.length];
    int pRoot = this.root(p);
    int count = 0;

    int max;
    for(max = 0; max < this.id.length; ++max) {
        if(this.root(max) == pRoot) {
            largest[count] = max;
            ++count;
        }
    }

    max = 0;

    for(int i = 0; i < largest.length; ++i) {
        if(largest[i] >= max) {
            max = largest[i];
        }
    }

    return max;
}

public static void main(String[] args) {
    QuickFindUF uf = new QuickFindUF(10);
    uf.union(0, 2);
    uf.union(8, 4);
    uf.union(0, 4);
    uf.union(0, 6);
    System.out.println(uf.findMax(0));
    System.out.println(uf.findMax(6));
    uf.union(1, 9);
    System.out.println(uf.findMax(1));
    System.out.println(uf.findMax(9));
    uf.union(1, 2);
    System.out.println(uf.findMax(2));
}

}

你可能感兴趣的:(social-network-connectivity-with-union-find)