首页 > 其他 > 详细

并查集

时间:2016-10-02 19:26:29      阅读:191      评论:0      收藏:0      [点我收藏+]

1、quick-find 算法

import java.util.Scanner ;

public class UF{
	private int[] id ;
	private int count ;

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

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

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

	public void union(int p, int q){
		int pID = id[p] ;
		int qID = id[q] ;
		if (pID == qID) {
			return ;
		}else{
			for (int i=0;i<id.length;i++) {
				if(id[i]==pID){
					id[i] = qID ;
				}
			}
			count-- ;
		}
	}

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in) ;
		System.out.println("please input the number of nodes:") ;
		int N = sc.nextInt() ;
		UF uf = new UF(N) ;
		System.out.println("please input the number of relateon of nodes:") ;
		int m = sc.nextInt() ;
		for(int i=0;i<m;i++){
			int p = sc.nextInt() ;
			int q = sc.nextInt() ;
			if(uf.connected(p,q)){
				continue ;
			}else{
				uf.union(p,q) ;
				//System.out.println(p + " " + q) ;
			}
		}
		System.out.println(uf.count() + " components.") ;
	}
}

  

2、quick-union 算法

 

import java.util.Scanner ;

public class UF02{
	private int[] id ;
	private int count ;

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

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

	public int find02(int p){
		while(p!=id[p]){
			p = id[p] ;
		}
		return p ;
	}

	public void union02(int p, int q){
		int pRoot = find02(p) ;
		int qRoot = find02(q) ;

		if (pRoot == qRoot) {
			return ;
		}else{
			id[pRoot] = qRoot ;
			count-- ;
		}
	}

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in) ;
		System.out.println("please input the number of nodes:") ;
		int N = sc.nextInt() ;
		UF02 uf = new UF02(N) ;
		System.out.println("please input the number of relateon of nodes:") ;
		int m = sc.nextInt() ;
		for(int i=0;i<m;i++){
			int p = sc.nextInt() ;
			int q = sc.nextInt() ;
			if(uf.connected(p,q)){
				continue ;
			}else{
				uf.union02(p,q) ;
				//System.out.println(p + " " + q) ;
			}
		}
		System.out.println(uf.count() + " components.") ;
	}
}

  

3、加权 quick-union 算法

 

import java.util.Scanner ;

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

	public UF03(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 count(){
		return count ;
	}

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

	public int find03(int p){
		while(p!=id[p]){
			p = id[p] ;
		}
		return p ;
	}

	public void union03(int p, int q){
		int pRoot = find03(p) ;
		int qRoot = find03(q) ;

		if (pRoot == qRoot) {
			return ;
		}else{
			if (sz[pRoot]<sz[qRoot]) {
				id[pRoot] = qRoot ;
				sz[qRoot] += sz[pRoot] ; 
			}else{
				id[qRoot] = pRoot ;
				sz[pRoot] += sz[qRoot] ;
			}
			count-- ;
		}
	}

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in) ;
		System.out.println("please input the number of nodes:") ;
		int N = sc.nextInt() ;
		UF03 uf = new UF03(N) ;
		System.out.println("please input the number of relateon of nodes:") ;
		int m = sc.nextInt() ;
		for(int i=0;i<m;i++){
			int p = sc.nextInt() ;
			int q = sc.nextInt() ;
			if(uf.connected(p,q)){
				continue ;
			}else{
				uf.union03(p,q) ;
				//System.out.println(p + " " + q) ;
			}
		}
		System.out.println(uf.count() + " components.") ;
	}
}

  

 

并查集

原文:http://www.cnblogs.com/lshl/p/5927803.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!