克鲁斯卡尔算法

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

import javax.lang.model.type.UnionType;

import java.util.ArrayList;

public class Kruskal {
  public class Node {
    public int value;
    public int in, out;
    public ArrayList<Node> nexts;
    public ArrayList<Edge> edges;

    Node(int value) {
      this.value = value;
      in = out = 0;
      nexts = new ArrayList<Node>();
      edges = new ArrayList<Edge>();
    }
  }

  public class Edge {
    public int weight;
    public Node from, to;

    Edge(int weight, Node from, Node to) {
      this.weight = weight;
      this.from = from;
      this.to = to;
    }
  }

  public class Graph {
    public HashMap<Integer, Node> nodes;
    public HashSet<Edge> edges;

    Graph() {
      nodes = new HashMap<Integer, Node>();
      edges = new HashSet<Edge>();
    }
  }

  public class UnionFind {
    private HashMap<Node, Node> fathermap;
    private HashMap<Node, Integer> rankmap;

    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;
    }

    public void makeSets(Collection<Node> nodes) { //初始化并查集
      fathermap.clear();
      rankmap.clear();
      for (Node node : nodes) {
        fathermap.put(node, node);
        rankmap.put(node, 1);
      }
    }

    public boolean isSameSet(Node a, Node b) {
      return findFather(a) == findFather(b);
    }

    public void union(Node a, Node b) {
      if (a == null || b == null)
        return;
      Node aF = findFather(a);
      Node bF = findFather(b);

      if (aF != bF) {
        int aFrank = rankmap.get(aF);
        int bFrank = rankmap.get(bF);
        if (aFrank <= bFrank) {
          fathermap.put(aF, bF);
          rankmap.put(bF, aFrank + bFrank);
        } else {
          fathermap.put(bF, aF);
          rankmap.put(aF, aFrank + bFrank);
        }
      }
    }
  }

  public static class EdgeCompartor implements Comparator<Edge> {
    public int compare(Edge a, Edge b) { //升序排列
      return a.weight - b.weight;
    }
  }

  public static Set<Edge> kruskalMST(Graph graph) {
    UnionFind unionFind = new UnionFind();
    unionFind.makeSets(graph.nodes.values());
    PriorityQueue<Edge> priorityQueue =
        new PriorityQueue<>(new EdgeCompartor());
    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;
  }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值