图——最小生成树(kruskal算法)

import java.util.Collection;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.PriorityQueue;
import java.util.Set;

//kruskal算法,生成最小生成树
//最小生成树:在图的基础上令各点连通,且去除了权值比较大的边,保持权值最小
//适用范围:要求无向图

//首先我们把所有的边按照权值先从小到大排列,接着按照顺序选取每条边,如果这条边的两个端点不属于同一集合,
//那么就将它们合并,直到所有的点都属于同一个集合为止。
//undirected graph only
public class Kruskal {

	// Union-Find Set
	public static class UnionFind {
		private HashMap<Node, Node> fatherMap;
		private HashMap<Node, Integer> rankMap;

		public UnionFind() {
			fatherMap = new HashMap<Node, Node>();
			rankMap = new HashMap<Node, Integer>();
		}

		//查找某一个节点所在的集合
		private Node findFather(Node n) {
			Node father = fatherMap.get(n);
			if (father != n) {
				father = findFather(father);
			}
			fatherMap.put(n, father);
			return father;
		}

		//初始化sets?
		public void makeSets(Collection<Node> nodes) {
			fatherMap.clear();
			rankMap.clear();
			for (Node node : nodes) {
				//fatherMap放入节点信息
				//rankMap放入value为1的节点信息
				fatherMap.put(node, node);
				rankMap.put(node, 1);
			}
		}

		//判断Node a和Node b是否存在同一个集合中
		public boolean isSameSet(Node a, Node b) {
			return findFather(a) == findFather(b);
		}

		//将a节点和b节点放到同一个集合中
		public void union(Node a, Node b) {
			if (a == null || b == null) {
				return;
			}
			Node aFather = findFather(a);
			Node bFather = findFather(b);
			if (aFather != bFather) {
				int aFrank = rankMap.get(aFather);
				int bFrank = rankMap.get(bFather);
				if (aFrank <= bFrank) {
					fatherMap.put(aFather, bFather);
					rankMap.put(bFather, aFrank + bFrank);
				} else {
					fatherMap.put(bFather, aFather);
					rankMap.put(aFather, aFrank + bFrank);
				}
			}
		}
	}

	public static class EdgeComparator implements Comparator<Edge> {

		@Override
		public int compare(Edge o1, Edge o2) {
			return o1.weight - o2.weight;
		}

	}

	public static Set<Edge> kruskalMST(Graph graph) {
		UnionFind unionFind = new UnionFind();
		//makeSets图的所有节点
		unionFind.makeSets(graph.nodes.values());

		PriorityQueue<Edge> priorityQueue = new PriorityQueue<>(new EdgeComparator());
		//将所有的边加入到优先队列中
		for (Edge edge : graph.edges) {
			priorityQueue.add(edge);
		}
		Set<Edge> result = new HashSet<>();
		//当优先队列不为空,弹出边
		while (!priorityQueue.isEmpty()) {
			Edge edge = priorityQueue.poll();
			//判断边的入点和出点是否在相同集合中
			if (!unionFind.isSameSet(edge.from, edge.to)) {

				result.add(edge);
				unionFind.union(edge.from, edge.to);
			}
		}
		return result;
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值