最小生成树

1584. 连接所有点的最小费用

给你一个points 数组,表示 2D 平面上的一些点,其中 points[i] = [xi, yi] 。连接点 [xi, yi] 和点 [xj, yj] 的费用为它们之间的 曼哈顿距离 :|xi - xj| + |yi - yj| ,其中 |val| 表示val的绝对值。请你返回将所有点连接的最小总费用。只有任意两点之间 有且仅有 一条简单路径时,才认为所有点都已连接。

Prim 算法

从某一个点出发,将与该点相连的所有边加入小顶堆,每次最小边出堆,同时将该边的另一个顶点的邻接边全部入堆。
class Solution {
    public int minCostConnectPoints(int[][] points) {
        int n = points.length;
        List<int[]>[] graph = buildGraph(n, points);
        return new Prim(graph).getMST();
    }

    public List<int[]>[] buildGraph(int n, int[][] points){  // 构建邻接表
        List<int[]>[] graph = new LinkedList[n];
        for (int i = 0; i < n; i++){
            graph[i] = new LinkedList<>();
        }

        for (int i = 0; i < n; i++){
            for (int j = i+1; j < n; j++){
                int x1 = points[i][0], y1 = points[i][1];
                int x2 = points[j][0], y2 = points[j][1];
                int weight = Math.abs(x1-x2)+Math.abs(y1-y2);
                graph[i].add(new int[]{i, j, weight});
                graph[j].add(new int[]{j, i, weight});
            }
        }
        return graph;
    }
}

class Prim{
    private PriorityQueue<int[]> pq;
    private boolean[] isMST;
    private int weightNum;
    private List<int[]>[] graph;
    public Prim(List<int[]>[] graph){
        this.graph = graph;
        this.pq = new PriorityQueue<>((a,b)-> {return a[2]-b[2];});
        int n = graph.length;
        isMST = new boolean[n];
        isMST[0] = true;
        cut(0);
        while (!pq.isEmpty()){
            int[] edge = pq.poll();
            int point = edge[1];
            if(isMST[point]){
                continue;
            }
            isMST[point] = true;
            weightNum += edge[2];
            cut(point);
        }
    }

    public void cut(int k){
        for (int[] e : graph[k]){
            int v = e[1];
            if (!isMST[v]){
                pq.offer(e);
            }
        }
    }

    public int getMST(){
        return weightNum;
    }

    public boolean allConnected() {
        for (int i = 0; i < isMST.length; i++) {
            if (!isMST[i]) {
                return false;
            }
        }
        return true;
    }
}

Kruskal 算法

将所有边升序排序,然后利用并查集判断边的两个点是否连通,不连通则加入最小生成树
class Solution {
    public int minCostConnectPoints(int[][] points) {
        int n = points.length;
        List<int[]> graph = new ArrayList<>();
        for (int i = 0; i < n; i++){
            for (int j = i+1; j < n; j++){
                int x1 = points[i][0], y1 = points[i][1];
                int x2 = points[j][0], y2 = points[j][1];
                graph.add(new int[]{i, j, Math.abs(x1-x2)+Math.abs(y1-y2)});
            }
        }

        Collections.sort(graph, (a,b) -> {return a[2] - b[2];});
        UF uf = new UF(n);
        int minCost = 0;
        for (int[] e : graph){
            if (!uf.connected(e[0],e[1])){
                uf.union(e[0],e[1]);
                minCost += e[2];
            }
        }

        return minCost;
    }
}

class UF {
    // 连通分量个数
    private int count;
    // 存储一棵树
    private int[] parent;
    // 记录树的「重量」
    private int[] size;

    // n 为图中节点的个数
    public UF(int n) {
        this.count = n;
        parent = new int[n];
        size = new int[n];
        for (int i = 0; i < n; i++) {
            parent[i] = i;
            size[i] = 1;
        }
    }

    // 将节点 p 和节点 q 连通
    public void union(int p, int q) {
        int rootP = find(p);
        int rootQ = find(q);
        if (rootP == rootQ)
            return;

        // 小树接到大树下面,较平衡
        if (size[rootP] > size[rootQ]) {
            parent[rootQ] = rootP;
            size[rootP] += size[rootQ];
        } else {
            parent[rootP] = rootQ;
            size[rootQ] += size[rootP];
        }
        // 两个连通分量合并成一个连通分量
        count--;
    }

    // 判断节点 p 和节点 q 是否连通
    public boolean connected(int p, int q) {
        int rootP = find(p);
        int rootQ = find(q);
        return rootP == rootQ;
    }

    // 返回节点 x 的连通分量根节点
    private int find(int x) {
        while (parent[x] != x) {
            // 进行路径压缩
            parent[x] = parent[parent[x]];
            x = parent[x];
        }
        return x;
    }

    // 返回图中的连通分量个数
    public int count() {
        return count;
    }
}
posted @   学海无涯#  阅读(23)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示