朋友圈--并查集

在有5个宠物,分别是小猫1,小猫2,小猫3,小狗1,小狗2。再告诉你小猫1和小狗1是好朋友,小猫2和小狗1是好朋友,小猫3和小狗2是好朋友。这样它们之间就形成了2个朋友圈。

假如给你n个宠物和m对朋友关系,你能计算出这些宠物之间能够形成多小个朋友圈吗?

解法一:数据量小的时候,可以用按位与做

一对好友关系就用一个bitmap来存,判断两个bitmap是否有交集,只需要进行与操作,而融合的话只需要进行并操作

解法二:并查集

package 并查集_0324;

/**
 * @author cobblepot.ymh on 2019/3/24 5:40 PM.
 */

public class bcj {

   private int[] elements;

   bcj(int n) {
      elements = new int[n];
      for (int i = 0; i < n; i++) {
         elements[i] = -1;
      }
   }

   public int find(int x) {
      while (elements[x] != -1) {
         x = elements[x];
      }
      return x;
   }

   public void union(int x, int y) {
      int rootX = find(x);
      int rootY = find(y);
      if (rootX != rootY)
         elements[rootX] = rootY;
   }

   public int count() {
      int count = 0;
      for (int i = 0; i < elements.length; i++) {
         if (elements[i] == -1)
            count++;
      }
      return count;
   }

   public void print() {
      for (int i = 0; i < elements.length; i++)
         System.out.print(elements[i] + " ");
   }

   public static void main(String[] args) {
      bcj bcj = new bcj(10);

      bcj.union(0, 1);
      bcj.union(0, 2);
      bcj.union(3, 4);
      bcj.union(3, 1);
      bcj.union(5, 7);
      bcj.union(7, 8);
      bcj.union(7, 8);

      System.out.println(bcj.count());
      bcj.print();
   }
}

但是这里有个问题,存在树退化成链表的情况,所以可以用秩来优化,两个子树合并时,矮的向高的合并(并查集的路径压缩)

合并压缩

package 并查集_0324;

/**
 * @author cobblepot.ymh on 2019/3/24 8:54 PM.
 * 并查集,按秩优化
 */

public class bcj_optimized {
   private int[] elements;
   private int[] heights;

   public bcj_optimized(int n) {
      elements = new int[n];
      heights = new int[n];
      for (int i = 0; i < n; i++) {
         elements[i] = -1;
         heights[i] = 1;//Attention
      }
   }

   public int find(int x) {
      while (elements[x] != -1) {
         x = elements[x];
      }
      return x;
   }

   public void union(int x, int y) {
      int rootx = find(x);
      int rooty = find(y);
      if (rootx != rooty) {//Attention
         if (heights[rootx] > heights[rooty])
            elements[rooty] = rootx;
         else if (heights[rootx] < heights[rooty])
            elements[rootx] = rooty;
         else {
            elements[rootx] = rooty;
            heights[rooty]++;
         }

      }
   }

   public int count() {
      int count = 0;
      for (int i = 0; i < elements.length; i++)
         if (elements[i] == -1) count++;
      return count;
   }

   public static void main(String[] args) {
      bcj bcj = new bcj(10);

      bcj.union(0, 1);
      bcj.union(0, 2);
      bcj.union(3, 4);
      bcj.union(3, 1);
      bcj.union(5, 7);
      bcj.union(7, 8);
      bcj.union(7, 8);

      System.out.println(bcj.count());
      bcj.print();
   }
}

 另一种优化思路:查找优化

package 并查集_0324;

/**
 * @author cobblepot.ymh on 2019/3/24 10:38 PM.
 */

public class bcj_search_optimized {

	public int[] elements;

	public bcj_search_optimized(int n) {
		elements = new int[n]; //Attention
		for (int i = 0; i < n; i++) {
			elements[i] = -1;
		}
	}

	public int find(int x) {
		int originX = x;
		while(elements[x] != -1){
			int tempX = originX;
			originX = elements[originX];
			elements[tempX] = x;
		}
		return x;
	}

	public void union(int x, int y){
		int rootx = find(x);
		int rooty = find(y);
		if(rootx != rooty)
			elements[rootx] = rooty;
	}

	public int count() {
		int count = 0;
		for(int i=0; i<elements.length; i++) {
			if(elements[i] == -1) {
				count++;
			}
		}
		return count;
	}

	// 打印并查集
	public void print() {
		for(int i=0; i<elements.length; i++) {
			System.out.print(elements[i] + " ");
		}
		System.out.println();
	}

	public static void main(String[] args) {
		bcj bcj = new bcj(10);

		bcj.union(0, 1);
		bcj.union(0, 2);
		bcj.union(3, 4);
		bcj.union(3, 1);
		bcj.union(5, 7);
		bcj.union(7, 8);
		bcj.union(7, 8);

		System.out.println(bcj.count());
		bcj.print();
	}

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值