相比普里姆算法来说,克鲁斯卡尔的想法是从边出发,不管是理解上还是实现上都更简单,实现思路:我们先把找到所有边存到一个边集数组里面,并进行升序排序,然后依次从里面取出每一条边,如果不存在回路,就说明可以取,否则就跳过去看下一条边。其中看是否是回路这个操作利用到了并查集,就是判断新加入的这条边的两个顶点是否在同一个集合中,如果在就说明产生回路,如果没在同一个集合那么说明没有回路可以加入,这里只需要理解并查集中的查找、更新操作就行。并查集在上一篇博文里介绍过了,有需要的可以翻看,这里就不再具体介绍。
我们将创建下面的无向权值图:
最小生成树示意图:
邻接矩阵的绘制还是手动赋值上三角,并通过矩阵对称性生成整个邻接矩阵,其中最小生成树中需要用到权值,对应原本有边的地方之前我是用1表示,现在改成边对应的权值,之前的0表示没有边,现在改成99表示为无穷,其实应该换成更大的值以确保树的边权值都小于这个最大值,但为了方便对齐显示看邻接矩阵,就使用了比本图中各边长较大的99来表示最大值。
边集数组使用上三角矩阵生成,因为是无向图只需要取上三角或下三角就可以得到全部的边:
获取边集数组代码:
// 从图的邻接矩阵中提取边,并按权重排序
Edge* GetEdges(MGraph G) {// 动态分配内存存储边Edge* Edges = (Edge*)malloc(sizeof(Edge) * G.numEdges);if (Edges == NULL) {printf("内存分配失败\n");exit(1);}int edgeIndex = 0;// 遍历邻接矩阵提取边for (int i = 0; i < G.numNodes; i++) {for (int j = i + 1; j < G.numNodes; j++) { // 从 i + 1 开始,避免重复的边if (G.arc[i][j] != 99) { // 99 表示没有边Edges[edgeIndex].begin = i;Edges[edgeIndex].end = j;Edges[edgeIndex].weight = G.arc[i][j];edgeIndex++;}}}// 按边的权重排序(冒泡排序)for (int i = 0; i < G.numEdges - 1; i++) {for (int j = 0; j < G.numEdges - i - 1; j++) {if (Edges[j].weight > Edges[j + 1].weight) {// 交换边Edge tmpEdge = Edges[j];Edges[j] = Edges[j + 1];Edges[j + 1] = tmpEdge;}}}return Edges;
}
Kruskal算法代码:
// 查找节点所在集合的根
int Find(int parent[], int f) {while (parent[f] > 0) {f = parent[f];}return f;
}// 使用 Kruskal 算法生成最小生成树
void MiniSpanTree_Kruskal(MGraph G, Edge Edges[]) {int i, n, m;Edge edges[MAXEDGE]; // 声明边数组(可能用于存储生成树的边)int parent[MAXVEX]; // 记录每个节点的父节点// 初始化每个节点的父节点为 0for (i = 0; i < G.numNodes; i++) {parent[i] = 0;}printf("\n");// 遍历每条边for (i = 0; i < G.numEdges; i++) {n = Find(parent, Edges[i].begin);m = Find(parent, Edges[i].end);if (n != m) {// 如果两个端点不在同一集合中,则将边加入最小生成树parent[n] = m;printf("第%d条边为:(%c,%c) %d\n", i + 1, Array[Edges[i].begin], Array[Edges[i].end], Edges[i].weight);}// 打印当前边处理后的 parent 数组printf("循环第%d条边后parent的值为:", i + 1);for (int j = 0; j < MAXVEX; j++) {printf("%d-", parent[j]);}printf("\n\n");}
}
完整代码(包含邻接矩阵的创建,边集的创建,Kruskal算法)
#include "stdio.h"
#include "stdlib.h"
#include "math.h"
#include "time.h"// 禁用特定的警告
#pragma warning(disable:4996)// 定义一些常量和数据类型
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define MAXVEX 8 /* 最大顶点数,用户定义 */
#define MAXEDGE 10 /* 最大边数,用户定义 */
#define GRAPH_INFINITY 99 /* 用0表示∞,表示不存在边 *//* 定义状态、顶点和边的类型 */
typedef int Status; /* Status是函数的返回类型,如OK表示成功 */
typedef char VertexType; /* 顶点的类型,用字符表示 */
typedef int EdgeType; /* 边上的权值类型,用整数表示 */
typedef int Boolean; /* 布尔类型 */
// 定义顶点标签
char Array[] = "ABCDEFGHI";
/* 访问标记数组 */
Boolean visited[MAXVEX];/* 图的邻接矩阵结构体 */
typedef struct
{VertexType vexs[MAXVEX]; /* 顶点表 */EdgeType arc[MAXVEX][MAXVEX]; /* 邻接矩阵,表示边的权值 */int numNodes, numEdges; /* 图中当前的顶点数和边数 */
} MGraph;/* 边集数组结构体,用于辅助克鲁斯卡尔算法 */
typedef struct {int begin; // 边的起始顶点int end; // 边的终止顶点int weight; // 边的权重
} Edge;/* 创建一个无向网图的邻接矩阵表示 */
void CreateMGraph(MGraph* G)
{int i, j, k, w;// 初始化图的顶点数和边数G->numNodes = 8;G->numEdges = 10;// 初始化邻接矩阵和顶点表for (i = 0; i < G->numNodes; i++) {for (j = 0; j < G->numNodes; j++) {G->arc[i][j] = GRAPH_INFINITY; /* 初始化邻接矩阵为∞ */}G->vexs[i] = Array[i]; /* 初始化顶点表 */}G->arc[0][0] = GRAPH_INFINITY;G->arc[0][1] = 10;G->arc[0][2] = GRAPH_INFINITY;G->arc[0][3] = GRAPH_INFINITY;G->arc[0][4] = GRAPH_INFINITY;G->arc[0][5] = 11;G->arc[0][6] = GRAPH_INFINITY;G->arc[0][7] = GRAPH_INFINITY;G->arc[1][0] = GRAPH_INFINITY;G->arc[1][1] = GRAPH_INFINITY;G->arc[1][2] = 23;G->arc[1][3] = GRAPH_INFINITY;G->arc[1][4] = GRAPH_INFINITY;G->arc[1][5] = GRAPH_INFINITY;G->arc[1][6] = 12;G->arc[1][7] = GRAPH_INFINITY;G->arc[2][0] = GRAPH_INFINITY;G->arc[2][1] = GRAPH_INFINITY;G->arc[2][2] = GRAPH_INFINITY;G->arc[2][3] = 21;G->arc[2][4] = GRAPH_INFINITY;G->arc[2][5] = GRAPH_INFINITY;G->arc[2][6] = GRAPH_INFINITY;G->arc[2][7] = GRAPH_INFINITY;G->arc[3][0] = GRAPH_INFINITY;G->arc[3][1] = GRAPH_INFINITY;G->arc[3][2] = GRAPH_INFINITY;G->arc[3][3] = GRAPH_INFINITY;G->arc[3][4] = GRAPH_INFINITY;G->arc[3][5] = GRAPH_INFINITY;G->arc[3][6] = GRAPH_INFINITY;G->arc[3][7] = 11;G->arc[4][0] = GRAPH_INFINITY;G->arc[4][1] = GRAPH_INFINITY;G->arc[4][2] = GRAPH_INFINITY;G->arc[4][3] = GRAPH_INFINITY;G->arc[4][4] = GRAPH_INFINITY;G->arc[4][5] = 47;G->arc[4][6] = GRAPH_INFINITY;G->arc[4][7] = 80;G->arc[5][0] = GRAPH_INFINITY;G->arc[5][1] = GRAPH_INFINITY;G->arc[5][2] = GRAPH_INFINITY;G->arc[5][3] = GRAPH_INFINITY;G->arc[5][4] = GRAPH_INFINITY;G->arc[5][5] = GRAPH_INFINITY;G->arc[5][6] = 6;G->arc[5][7] = GRAPH_INFINITY;G->arc[6][0] = GRAPH_INFINITY;G->arc[6][1] = GRAPH_INFINITY;G->arc[6][2] = GRAPH_INFINITY;G->arc[6][3] = GRAPH_INFINITY;G->arc[6][4] = GRAPH_INFINITY;G->arc[6][5] = GRAPH_INFINITY;G->arc[6][6] = GRAPH_INFINITY;G->arc[6][7] = 8;G->arc[7][0] = GRAPH_INFINITY;G->arc[7][1] = GRAPH_INFINITY;G->arc[7][2] = GRAPH_INFINITY;G->arc[7][3] = GRAPH_INFINITY;G->arc[7][4] = GRAPH_INFINITY;G->arc[7][5] = GRAPH_INFINITY;G->arc[7][6] = GRAPH_INFINITY;G->arc[7][7] = GRAPH_INFINITY;// 由于是无向图,邻接矩阵是对称的,需要将其对称for (int i = 0; i < G->numNodes; i++) {for (int j = 0; j < G->numNodes; j++) {G->arc[j][i] = G->arc[i][j];}}// 打印邻接矩阵printf("邻接矩阵为:\n");printf(" ");for (int i = 0; i < G->numNodes; i++) {printf("%2d ", i); /* 打印列索引 */}printf("\n ");for (int i = 0; i < G->numNodes; i++) {printf("%2c ", G->vexs[i]); /* 打印顶点标签 */}printf("\n");for (int i = 0; i < G->numNodes; i++) {printf("%2d", i); /* 打印行索引 */printf("%2c ", G->vexs[i]); /* 打印顶点标签 */for (int j = 0; j < G->numNodes; j++) {if (G->arc[i][j] != 99) {printf("\033[31m%02d \033[0m", G->arc[i][j]); /* 打印邻接矩阵中的权值 */}else {printf("%02d ", G->arc[i][j]); /* 打印邻接矩阵中的权值 */}}printf("\n");}
}// 从图的邻接矩阵中提取边,并按权重排序
Edge* GetEdges(MGraph G) {// 动态分配内存存储边Edge* Edges = (Edge*)malloc(sizeof(Edge) * G.numEdges);if (Edges == NULL) {printf("内存分配失败\n");exit(1);}int edgeIndex = 0;// 遍历邻接矩阵提取边for (int i = 0; i < G.numNodes; i++) {for (int j = i + 1; j < G.numNodes; j++) { // 从 i + 1 开始,避免重复的边if (G.arc[i][j] != 99) { // 99 表示没有边Edges[edgeIndex].begin = i;Edges[edgeIndex].end = j;Edges[edgeIndex].weight = G.arc[i][j];edgeIndex++;}}}// 按边的权重排序(冒泡排序)for (int i = 0; i < G.numEdges - 1; i++) {for (int j = 0; j < G.numEdges - i - 1; j++) {if (Edges[j].weight > Edges[j + 1].weight) {// 交换边Edge tmpEdge = Edges[j];Edges[j] = Edges[j + 1];Edges[j + 1] = tmpEdge;}}}return Edges;
}// 打印边集数组
void EdgesPrint(Edge Edges[], int EdgeNumber) {// 打印标题行printf("\n\t边集数组\n");printf("\t %-5s %-5s %-7s\n", "begin", "end", "weight");// 打印每条边的详细信息for (int i = 0; i < EdgeNumber; i++) {printf("Edge[%d] (%c)%-5d (%c)%-5d %-7d\n", i, Array[Edges[i].begin], Edges[i].begin, Array[Edges[i].end], Edges[i].end, Edges[i].weight);}
}// 查找节点所在集合的根
int Find(int parent[], int f) {while (parent[f] > 0) {f = parent[f];}return f;
}// 使用 Kruskal 算法生成最小生成树
void MiniSpanTree_Kruskal(MGraph G, Edge Edges[]) {int i, n, m;Edge edges[MAXEDGE]; // 声明边数组(可能用于存储生成树的边)int parent[MAXVEX]; // 记录每个节点的父节点// 初始化每个节点的父节点为 0for (i = 0; i < G.numNodes; i++) {parent[i] = 0;}printf("\n");// 遍历每条边for (i = 0; i < G.numEdges; i++) {n = Find(parent, Edges[i].begin);m = Find(parent, Edges[i].end);if (n != m) {// 如果两个端点不在同一集合中,则将边加入最小生成树parent[n] = m;printf("第%d条边为:(%c,%c) %d\n", i + 1, Array[Edges[i].begin], Array[Edges[i].end], Edges[i].weight);}// 打印当前边处理后的 parent 数组printf("循环第%d条边后parent的值为:", i + 1);for (int j = 0; j < MAXVEX; j++) {printf("%d-", parent[j]);}printf("\n\n");}
}int main(void)
{MGraph G;/* 创建图 */CreateMGraph(&G);//获取边集数组Edge * Edges =GetEdges(G);//打印边集数组EdgesPrint(Edges,G.numEdges);//使用克鲁斯卡尔算法MiniSpanTree_Kruskal(G, Edges);return 0;
}
运行结果(标黄色的就是没有加入最小生成树的边,即产生了回路):
最小生成树示意图