动态连通算法

修改了好久嗯,在随机生成的整数对理想的情况下能较快处理几十万以内随机数对全部连通问题


测试类:

/**
 * Created by 小林未郁 on 2016/7/17.
 */

import java.util.Random;
import java.util.Scanner;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;


public class TestClass {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("please enter a 'N'");
        count(input.nextInt());

    }

    static void count(int N) {


        try {

            System.setOut(new PrintStream(new FileOutputStream("system_out.txt")));

        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
            return;
        }


        PathCompressionUF fuck = new PathCompressionUF(N);
        Random rand = new Random();

        int i = 0;
        int j = 0;
        int flag=0;
        for (; ; ) {

            while (true) {
                i = rand.nextInt(N);
                j = rand.nextInt(N);

                if (i == j) {
                    continue;
                } else {


                    if (fuck.connected(i, j)) {
                        break;
                    } else {
                        fuck.union(i, j);
                        System.out.println(i + " " + j);
                        flag++;
                        break;
                    }
                }
            }
	//0.1,非常保守的一个参数
            if(flag<N*0.1){
                continue;
            }else {
                if (fuck.judge()) {
                    break;
                } else {
                    continue;
                }

            }

        }
        System.out.println("执行完毕!");
    }


}



实现类:

/**
 * Created by 小林未郁 on 2016/8/4.
 */

public class PathCompressionUF {
    private int count;
    private int[] id;
    private int[] sz;

    PathCompressionUF(int N) {
        count = N;
        id = new int[N];
        sz = new int[N];
        for (int i = 0; i < N; i++) {
            id[i] = i;
            sz[i] = 1;
        }
    }

    public int find(int p) {

        int temp = p;

        while (p != id[p]) {
            p = id[p];
        }

        while (temp != id[temp]) {
            int temp1 = id[temp];
            id[temp] = p;
            temp = temp1;

        }


        return p;
    }

    public void union(int p, int q) {
        int i = find(p);
        int j = find(q);

        if (i == j) {
            return;
        }

        if (sz[i] <= sz[j]) {
            id[i] = j;
            sz[j] += sz[i];
        } else {
            id[j] = i;
            sz[i] += sz[j];
        }
        count--;
    }

    public boolean connected(int p, int q) {
        return find(p) == find(q);
    }

    public int count() {
        return count;
    }


    public boolean judge(){
        int bitch=id[0];
        for(int i=0;i<id.length;i++){
            if(connected(bitch,id[i]))
                continue;
            else
                return false;
        }
        return true;
    }

}
 
随机生成的数对全部都写入到一个文件里了,如图是输入为200000时的情形,共生成了一百多万对随机数,处理时间大概用了不到十秒.judge我从平方复杂度优化到了线性的,但是judge应该还能再优化我感觉..但是不知道怎么写了,以后再写
 
 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值