17.5:最小生成树算法

最小生成树算法

在这里插入图片描述

刚开始每一个节点都是一个集合,先从边的最小权重开始,判断当前边的左右节点是否在同一个集合,如果不是就划分在同一个集合中。然后进行循环操作,直至所有节点都在同一个集合中。

在这里插入图片描述

所以我们用到的数据结构:并查集,堆。并查集主要用来合并集合,小根堆主要用来根据边的权重进行由小到大的弹出边。

https://juejin.cn/post/7161250651702296612

package algorithmbasic.class17;

import java.util.*;

//最小生成树算法
public class Kruskal {
    public static Set<Edge> kruskalMST(Graph graph) {
        //生成好我的并查集。
        UnionFind unionFind = new UnionFind();
        //图中的每个节点都是一个集合。
        unionFind.makeCollection(graph);
        //小根堆:根据边的权重比较。
        PriorityQueue<Edge> queue = new PriorityQueue<>(new myComparator());
        for (Edge e : graph.edges) {
            queue.add(e);
        }
        Set<Edge> set = new HashSet<>();
        while (!queue.isEmpty()) {
            Edge e = queue.poll();
            if (!unionFind.isSameSet(e.from, e.to)) {
                unionFind.union(e.from, e.to);
                set.add(e);
            }
        }
        return set;
    }

    public static class UnionFind {
        static HashMap<Node, Node> fatherMap = new HashMap<>();
        static HashMap<Node, Integer> sizeMap = new HashMap<>();
        static Stack<Node> stack = new Stack<>();

        public static void makeCollection(Graph graph) {
            //**
            fatherMap.clear();
            sizeMap.clear();
            for (Node node : graph.nodes.values()) {
                fatherMap.put(node, node);
                sizeMap.put(node, 1);
            }
        }

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

        public static void union(Node a, Node b) {
            Node fatherA = findAncestor(a);
            Node fatherB = findAncestor(b);
            if (fatherA != fatherB) {
                Node big = sizeMap.get(fatherA) > sizeMap.get(fatherB) ? fatherA : fatherB;
                Node small = big == fatherA ? fatherB : fatherA;
                fatherMap.put(small, big);
                sizeMap.put(small, 0);
                sizeMap.put(big, sizeMap.get(big) + sizeMap.get(small));
            }
        }

        //出入一个节点,寻找这个节点的祖先节点
        public static Node findAncestor(Node node) {
            while (fatherMap.get(node) != node) {
                stack.add(node);
                node = fatherMap.get(node);
            }
            //fatherMap.get(node) == node
            //先进行优化
            while (!stack.isEmpty()) {
                Node cur = stack.pop();
                fatherMap.put(cur, node);
            }
            return node;
        }
    }

    public static class myComparator implements Comparator<Edge> {
        @Override
        public int compare(Edge o1, Edge o2) {
            return o1.weight - o2.weight;
        }
    }
}

for test

 public static void main(String[] args) {
        Graph graph = new Graph();
        Node a = new Node(1);
        Node b = new Node(2);
        Node c = new Node(3);
        Node e = new Node(5);
        Node f = new Node(6);
        Node g = new Node(7);
        Node h = new Node(8);
        graph.nodes.put(1, a);
        graph.nodes.put(2, b);
        graph.nodes.put(3, c);
        graph.nodes.put(5, e);
        graph.nodes.put(6, f);
        graph.nodes.put(7, g);
        graph.nodes.put(8, h);
        Edge ac = new Edge(1, a, c);
        Edge bc = new Edge(1, b, c);
        Edge ab = new Edge(3, a, b);
        Edge be = new Edge(10, b, e);
        Edge ec = new Edge(12, e, c);
        Edge fc = new Edge(50, f, c);
        Edge eg = new Edge(1, g, e);
        Edge fg = new Edge(3, f, g);
        Edge fh = new Edge(6, f, h);
        Edge gh = new Edge(9, g, h);
        graph.edges.add(ac);
        graph.edges.add(bc);
        graph.edges.add(ab);
        graph.edges.add(be);
        graph.edges.add(ec);
        graph.edges.add(fc);
        graph.edges.add(eg);
        graph.edges.add(fg);
        graph.edges.add(fh);
        graph.edges.add(gh);
        Set<Edge> set = new HashSet<>();
        set = kruskalMST(graph);
        System.out.println(1);
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

橙-橙

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值