Java Kruskal 最小生成树 (MST) 算法(Kruskal’s Minimum Spanning Tree (MST) Algorithm)

        对于加权、连通、无向图,最小生成树(MST) 或最小权重生成树是权重小于或等于其他所有生成树权重的生成树。

Kruskal算法简介:
        在这里,我们将讨论Kruskal 算法来查找给定加权图的 MST。 

        在 Kruskal 算法中,按升序对给定图的所有边进行排序。然后,如果新添加的边不形成循环,它会继续在 MST 中添加新边和节点。它首先选择最小权重边,最后选择最大权重边。因此,我们可以说它在每一步中都做出局部最优选择以找到最优解。因此,这是一种贪婪算法。

如何使用 Kruskal 算法查找 MST?
以下是使用 Kruskal 算法查找 MST 的步骤:

1.按照权重的非递减顺序对所有边进行排序。 
2.选择最小的边。检查它是否与目前形成的生成树形成一个循环。如果没有形成循环,则包括这条边。否则,丢弃它。 
3.重复步骤#2,直到生成树中有 (V-1) 条边。

第 2 步使用并查集算法来检测循环。 
因此,我们建议先阅读以下文章:

并查表算法 | 集合 1(检测图中的循环) 

javascript 不相交集简介(并查集算法):https://blog.youkuaiyun.com/hefeng_aspnet/article/details/142524938

C# 不相交集简介(并查集算法):https://blog.youkuaiyun.com/hefeng_aspnet/article/details/142524824

python 不相交集简介(并查集算法):https://blog.youkuaiyun.com/hefeng_aspnet/article/details/142524698

java 不相交集简介(并查集算法):https://blog.youkuaiyun.com/hefeng_aspnet/article/details/142524698

c++ 不相交集简介(并查集算法):https://blog.youkuaiyun.com/hefeng_aspnet/article/details/142523025

并查集算法 | 集合 2(按秩并集和路径压缩)

c语言 并查集算法中的按秩联合和路径压缩:https://blog.youkuaiyun.com/hefeng_aspnet/article/details/142526674

javascript 并查集算法中的按秩联合和路径压缩:https://blog.youkuaiyun.com/hefeng_aspnet/article/details/142526517

C# 并查集算法中的按秩联合和路径压缩:https://blog.youkuaiyun.com/hefeng_aspnet/article/details/142526449

python 并查集算法中的按秩联合和路径压缩:https://blog.youkuaiyun.com/hefeng_aspnet/article/details/142526387

java 并查集算法中的按秩联合和路径压缩:https://blog.youkuaiyun.com/hefeng_aspnet/article/details/142526336

c++ 并查集算法中的按秩联合和路径压缩:https://blog.youkuaiyun.com/hefeng_aspnet/article/details/142525945

        Kruskal 寻找最小成本生成树的算法采用贪婪方法。贪婪选择是选择迄今为止构建的 MST 中不会引起循环的最小权重边。让我们通过一个例子来理解它:

插图:
下面是上述方法的说明:

输入图:

该图包含 9 个顶点和 14 条边。因此,形成的最小生成树将具有 (9 - 1) = 8 条边。 


排序后: 

WeightSourceDestination
176
282
265
401
425
686
723
778
807
812
934
1054
1117
1435

现在从排序的边列表中逐一选择所有边 

步骤 1:选取边 7-6。未形成循环,将其包括在内。

步骤 2:拾取边 8-2。未形成循环,将其包括在内。    

步骤 3:选取边 6-5。未形成循环,将其包括在内。  

步骤 4:选取边 0-1。没有形成循环,将其包括在内。  

步骤 5:选取边 2-5。未形成循环,将其包括在内。 

步骤 6:选择边 8-6。由于包含此边会导致循环,因此将其丢弃。选择边 2-3:未形成循环,因此将其包括在内。 

步骤 7:选择边 7-8。由于包含此边会导致循环,因此将其丢弃。选择边 0-7。未形成循环,因此将其包括在内。   

步骤 8:选择边 1-2。由于包含此边会导致循环,因此将其丢弃。选择边 3-4。未形成循环,因此将其包括在内。 

注意:由于MST中包含的边数等于(V-1),因此算法在此停止

下面是上述方法的实现: 

// Java program for Kruskal's algorithm 
  
import java.util.ArrayList; 
import java.util.Comparator; 
import java.util.List; 
  
public class KruskalsMST { 
  
    // Defines edge structure 
    static class Edge { 
        int src, dest, weight; 
  
        public Edge(int src, int dest, int weight) 
        { 
            this.src = src; 
            this.dest = dest; 
            this.weight = weight; 
        } 
    } 
  
    // Defines subset element structure 
    static class Subset { 
        int parent, rank; 
  
        public Subset(int parent, int rank) 
        { 
            this.parent = parent; 
            this.rank = rank; 
        } 
    } 
  
    // Starting point of program execution 
    public static void main(String[] args) 
    { 
        int V = 4; 
        List<Edge> graphEdges = new ArrayList<Edge>( 
            List.of(new Edge(0, 1, 10), new Edge(0, 2, 6), 
                    new Edge(0, 3, 5), new Edge(1, 3, 15), 
                    new Edge(2, 3, 4))); 
  
        // Sort the edges in non-decreasing order 
        // (increasing with repetition allowed) 
        graphEdges.sort(new Comparator<Edge>() { 
            @Override public int compare(Edge o1, Edge o2) 
            { 
                return o1.weight - o2.weight; 
            } 
        }); 
  
        kruskals(V, graphEdges); 
    } 
  
    // Function to find the MST 
    private static void kruskals(int V, List<Edge> edges) 
    { 
        int j = 0; 
        int noOfEdges = 0; 
  
        // Allocate memory for creating V subsets 
        Subset subsets[] = new Subset[V]; 
  
        // Allocate memory for results 
        Edge results[] = new Edge[V]; 
  
        // Create V subsets with single elements 
        for (int i = 0; i < V; i++) { 
            subsets[i] = new Subset(i, 0); 
        } 
  
        // Number of edges to be taken is equal to V-1 
        while (noOfEdges < V - 1) { 
  
            // Pick the smallest edge. And increment 
            // the index for next iteration 
            Edge nextEdge = edges.get(j); 
            int x = findRoot(subsets, nextEdge.src); 
            int y = findRoot(subsets, nextEdge.dest); 
  
            // If including this edge doesn't cause cycle, 
            // include it in result and increment the index 
            // of result for next edge 
            if (x != y) { 
                results[noOfEdges] = nextEdge; 
                union(subsets, x, y); 
                noOfEdges++; 
            } 
  
            j++; 
        } 
  
        // Print the contents of result[] to display the 
        // built MST 
        System.out.println( 
            "Following are the edges of the constructed MST:"); 
        int minCost = 0; 
        for (int i = 0; i < noOfEdges; i++) { 
            System.out.println(results[i].src + " -- "
                               + results[i].dest + " == "
                               + results[i].weight); 
            minCost += results[i].weight; 
        } 
        System.out.println("Total cost of MST: " + minCost); 
    } 
  
    // Function to unite two disjoint sets 
    private static void union(Subset[] subsets, int x, 
                              int y) 
    { 
        int rootX = findRoot(subsets, x); 
        int rootY = findRoot(subsets, y); 
  
        if (subsets[rootY].rank < subsets[rootX].rank) { 
            subsets[rootY].parent = rootX; 
        } 
        else if (subsets[rootX].rank 
                 < subsets[rootY].rank) { 
            subsets[rootX].parent = rootY; 
        } 
        else { 
            subsets[rootY].parent = rootX; 
            subsets[rootX].rank++; 
        } 
    } 
  
    // Function to find parent of a set 
    private static int findRoot(Subset[] subsets, int i) 
    { 
        if (subsets[i].parent == i) 
            return subsets[i].parent; 
  
        subsets[i].parent 
            = findRoot(subsets, subsets[i].parent); 
        return subsets[i].parent; 
    } 

// This code is contributed by Salvino D'sa 

输出

以下是构建的 MST 中的边
2 -- 3 == 4 
0 -- 3 == 5 
0 -- 1 == 10

最小成本生成树:19

时间复杂度: O(E * logE)或O(E * logV) 

    1.边的排序需要 O(E * logE) 时间。 

    2.排序后,我们遍历所有边并应用查找并集算法。查找和并集操作最多需要 O(logV) 时间。

    3.因此总体复杂度为 O(E * logE + E * logV) 时间。 

    4.E 的值最多为 O(V 2 ),因此 O(logV) 和 O(logE) 相同。因此,总体时间复杂度为 O(E * logE) 或 O(E*logV)

辅助空间: O(V + E),其中 V 是图中顶点的数量,E 是边的数量。

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

hefeng_aspnet

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

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

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

打赏作者

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

抵扣说明:

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

余额充值