并查集的底层实现

 

定义UF接口:


public interface UF {
	int getSize();

	boolean isConnected(int p, int q);

	void unionElements(int p, int q);
}

下面为6个实现类:从UnionFind1到UnionFind6逐渐优化并查集的底层查询效率,减少时间复杂度.


public class UnionFind1 implements UF {
	private int[] id;

	public UnionFind1(int size) {
		id = new int[size];

		for (int i = 0; i < id.length; i++)
			id[i] = i;
	}

	public int getSize() {
		return id.length;
	}

	// 查找元素p所对应的集合编号
	public int find(int p) {
		if (p < 0 && p >= id.length)
			throw new IllegalArgumentException("P is out of bound.");
		return id[p];
	}

	// 查看元素p和元素q是否所属一个集合
	public boolean isConnected(int p, int q) {
		return find(p) == find(q);
	}

	// 合并元素p和元素q所属的集合
	public void unionElements(int p, int q) {
		int pID = find(p);
		int qID = find(q);
		if (pID == qID)
			return;
		for (int i = 0; i < id.length; i++)
			if (id[i] == pID)
				id[i] = qID;
	}

}

public class UnionFind2 implements UF {
	private int[] parent;

	public UnionFind2(int size) {
		parent = new int[size];
		for (int i = 0; i < size; i++)
			parent[i] = i;
	}

	public int getSize() {
		return parent.length;
	}

	// 查找过程,查找元素p所对应的集合编号
	// o(h)复杂度,h为树的高度
	private int find(int p) {
		if (p < 0 && p >= parent.length)
			throw new IllegalArgumentException("P is out of bound.");

		while (p != parent[p])
			p = parent[p];
		return p;
	}

	// 查看元素p和元素q是否所属一个集合
	// o(h)复杂度,h为树的高度
	public boolean isConnected(int p, int q) {
		return find(p) == find(q);
	}

	// 合并元素p和元素q所属的集合
	// o(h)复杂度,h为树的高度
	public void unionElements(int p, int q) {
		int pRoot = find(p);
		int qRoot = find(q);
		if (pRoot == qRoot)
			return;
		parent[pRoot] = qRoot;
	}

}

public class UnionFind3 implements UF {
	private int[] parent;
	private int[] sz;// sz[i]表示以i为根的集合中元素个数

	public UnionFind3(int size) {
		parent = new int[size];
		sz = new int[size];
		for (int i = 0; i < size; i++) {
			parent[i] = i;
			sz[i] = 1;
		}
	}

	public int getSize() {
		return parent.length;
	}

	// 查找过程,查找元素p所对应的集合编号
	// o(h)复杂度,h为树的高度
	private int find(int p) {
		if (p < 0 && p >= parent.length)
			throw new IllegalArgumentException("P is out of bound.");

		while (p != parent[p])
			p = parent[p];
		return p;
	}

	// 查看元素p和元素q是否所属一个集合
	// o(h)复杂度,h为树的高度
	public boolean isConnected(int p, int q) {
		return find(p) == find(q);
	}

	// 合并元素p和元素q所属的集合
	// o(h)复杂度,h为树的高度
	public void unionElements(int p, int q) {
		int pRoot = find(p);
		int qRoot = find(q);
		if (pRoot == qRoot)
			return;
		if(sz[pRoot]<sz[qRoot]) {
		parent[pRoot] = qRoot;
		sz[qRoot]+=sz[pRoot];
		
	}else {
		parent[qRoot]=pRoot;
		sz[pRoot]+=sz[qRoot];
	
	}
	
	  }
}

public class UnionFind4 implements UF {
	private int[] parent;
	private int[] rank;// rank[i]表示以i为根的集合中元素个数

	public UnionFind4(int size) {
		parent = new int[size];
		rank = new int[size];
		for (int i = 0; i < size; i++) {
			parent[i] = i;
			rank[i] = 1;
		}
	}

	public int getSize() {
		return parent.length;
	}

	// 查找过程,查找元素p所对应的集合编号
	// o(h)复杂度,h为树的高度
	private int find(int p) {
		if (p < 0 && p >= parent.length)
			throw new IllegalArgumentException("P is out of bound.");

		while (p != parent[p])
			p = parent[p];
		return p;
	}

	// 查看元素p和元素q是否所属一个集合
	// o(h)复杂度,h为树的高度
	public boolean isConnected(int p, int q) {
		return find(p) == find(q);
	}

	// 根
	// o(h)复杂度,h为树的高度
	public void unionElements(int p, int q) {
		int pRoot = find(p);
		int qRoot = find(q);
		if (pRoot == qRoot)
			return;
		// 根据两个元素所在树的rank不同判断合并方向
		// 将rank低的集合合并到rank高的集合上
		if (rank[pRoot] < rank[qRoot])
			parent[pRoot] = qRoot;
		else if (rank[qRoot] < rank[pRoot])
			parent[qRoot] = pRoot;
		else {// rank[qRoot]==rank[pRoot]
			parent[qRoot] = pRoot;
			rank[pRoot] += 1;

		}

	}
}

public class UnionFind5 implements UF {
	private int[] parent;
	private int[] rank;// rank[i]表示以i为根的集合中元素个数

	public UnionFind5(int size) {
		parent = new int[size];
		rank = new int[size];
		for (int i = 0; i < size; i++) {
			parent[i] = i;
			rank[i] = 1;
		}
	}

	public int getSize() {
		return parent.length;
	}

	// 查找过程,查找元素p所对应的集合编号
	// o(h)复杂度,h为树的高度
	private int find(int p) {
		if (p < 0 && p >= parent.length)
			throw new IllegalArgumentException("P is out of bound.");

		while (p != parent[p])
			parent[p] = parent[parent[p]];
		p = parent[p];
		return p;
	}

	// 查看元素p和元素q是否所属一个集合
	// o(h)复杂度,h为树的高度
	public boolean isConnected(int p, int q) {
		return find(p) == find(q);
	}

	// 根
	// o(h)复杂度,h为树的高度
	public void unionElements(int p, int q) {
		int pRoot = find(p);
		int qRoot = find(q);
		if (pRoot == qRoot)
			return;
		// 根据两个元素所在树的rank不同判断合并方向
		// 将rank低的集合合并到rank高的集合上
		if (rank[pRoot] < rank[qRoot])
			parent[pRoot] = qRoot;
		else if (rank[qRoot] < rank[pRoot])
			parent[qRoot] = pRoot;
		else {// rank[qRoot]==rank[pRoot]
			parent[qRoot] = pRoot;
			rank[pRoot] += 1;

		}

	}
}

public class UnionFind6 implements UF {
	private int[] parent;
	private int[] rank;// rank[i]表示以i为根的集合中元素个数

	public UnionFind6(int size) {
		parent = new int[size];
		rank = new int[size];
		for (int i = 0; i < size; i++) {
			parent[i] = i;
			rank[i] = 1;
		}
	}

	public int getSize() {
		return parent.length;
	}

	// 查找过程,查找元素p所对应的集合编号
	// o(h)复杂度,h为树的高度
	private int find(int p) {
		if (p < 0 && p >= parent.length)
			throw new IllegalArgumentException("P is out of bound.");

		if (p != parent[p])
			parent[p] = find(parent[p]);
		return parent[p];
	}

	// 查看元素p和元素q是否所属一个集合
	// o(h)复杂度,h为树的高度
	public boolean isConnected(int p, int q) {
		return find(p) == find(q);
	}

	// 根
	// o(h)复杂度,h为树的高度
	public void unionElements(int p, int q) {
		int pRoot = find(p);
		int qRoot = find(q);
		if (pRoot == qRoot)
			return;
		// 根据两个元素所在树的rank不同判断合并方向
		// 将rank低的集合合并到rank高的集合上
		if (rank[pRoot] < rank[qRoot])
			parent[pRoot] = qRoot;
		else if (rank[qRoot] < rank[pRoot])
			parent[qRoot] = pRoot;
		else {// rank[qRoot]==rank[pRoot]
			parent[qRoot] = pRoot;
			rank[pRoot] += 1;

		}

	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值