对于加权、连通、无向图,最小生成树(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 条边。
排序后:
| Weight | Source | Destination |
| 1 | 7 | 6 |
| 2 | 8 | 2 |
| 2 | 6 | 5 |
| 4 | 0 | 1 |
| 4 | 2 | 5 |
| 6 | 8 | 6 |
| 7 | 2 | 3 |
| 7 | 7 | 8 |
| 8 | 0 | 7 |
| 8 | 1 | 2 |
| 9 | 3 | 4 |
| 10 | 5 | 4 |
| 11 | 1 | 7 |
| 14 | 3 | 5 |
现在从排序的边列表中逐一选择所有边
步骤 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 是边的数量。
812

被折叠的 条评论
为什么被折叠?



