克鲁斯卡尔【kruskal】算法
思路:
1、将邻接矩阵转化成边表数组
2、对边表数组根据权值按照从小到大的顺序排序
3、遍历所有的边,通过parent 数组找到边的连接信息;避免闭环问题;
4、如果不存在闭环问题,则加入到最小生成树中,并且修改parent 数组。
先排序,然后按边表顺序去parent 数组查找
parent 数组查找存放的是顶点最终的连接关系(直接连接或者是间接连接)
如下图:第一次执行,begin = 4, end = 7,weight = 7(begin边的小顶点,end边的大顶点,weight 权重值)
定义两个变量 n,m
4对应parent 数组中的值为0,n直接返回4
7对应parent 数组中的值为0,m直接返回7
n != m 所以存入边 parent[4] = 7 (4顶点连接7顶点)
第二次执行,begin = 2, end = 8,weight = 8(begin边的小顶点,end边的大顶点,weight 权重值)
(两个变量 n,m)
2对应parent 数组中的值为0,n直接返回2
8对应parent 数组中的值为0,m直接返回8
n != m 所以存入边 parent[2] = 8 (2顶点连接8顶点)
之后循环如上进行
当 n == m 的时候就说明会形成闭环就不能存入
然后继续
代码实现
#include "stdio.h"
#include "stdlib.h"
#include "math.h"
#include "time.h"
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define MAXEDGE 20
#define MAXVEX 20
#define INFINITYC 65535
typedef int Status;
typedef struct
{
int arc[MAXVEX][MAXVEX];
int numVertexes, numEdges;
}MGraph;
/* 对边集数组Edge结构的定义 */
typedef struct
{
int begin;
int end;
int weight;
}Edge ;
/*9.1 创建邻接矩阵*/
void CreateMGraph(MGraph *G)
{
int i, j;
/* printf("请输入边数和顶点数:"); */
G->numEdges=15;
G->numVertexes=9;
for (i = 0; i < G->numVertexes; i++)/* 初始化图 */
{
for ( j = 0; j < G->numVertexes; j++)
{
if (i==j)
G->arc[i][j]=0;
else
G->arc[i][j] = G->arc[j][i] = INFINITYC;
}
}
G->arc[0][1]=10;
G->arc[0][5]=11;
G->arc[1][2]=18;
G->arc[1][8]=12;
G->arc[1][6]=16;
G->arc[2][8]=8;
G->arc[2][3]=22;
G->arc[3][8]=21;
G->arc[3][6]=24;
G->arc[3][7]=16;
G->arc[3][4]=20;
G->arc[4][7]=7;
G->arc[4][5]=26;
G->arc[5][6]=17;
G->arc[6][7]=19;
for(i = 0; i < G->numVertexes; i++)
{
for(j = i; j < G->numVertexes; j++)
{
G->arc[j][i] =G->arc[i][j];
}
}
}
/* 交换权值以及头和尾 */
void Swapn(Edge *edges,int i, int j)
{
int tempValue;
//交换edges[i].begin 和 edges[j].begin 的值
tempValue = edges[i].begin;
edges[i].begin = edges[j].begin;
edges[j].begin = tempValue;
//交换edges[i].end 和 edges[j].end 的值
tempValue = edges[i].end;
edges[i].end = edges[j].end;
edges[j].end = tempValue;
//交换edges[i].weight 和 edges[j].weight 的值
tempValue = edges[i].weight;
edges[i].weight = edges[j].weight;
edges[j].weight = tempValue;
}
/* 对权值进行排序 */
void sort(Edge edges[],MGraph *G)
{
//对权值进行排序(从小到大)
int i, j;
for ( i = 0; i < G->numEdges; i++)
{
for ( j = i + 1; j < G->numEdges; j++)
{
if (edges[i].weight > edges[j].weight)
{
Swapn(edges, i, j);
}
}
}
printf("边集数组根据权值排序之后的为:\n");
for (i = 0; i < G->numEdges; i++)
{
printf("(%d, %d) %d\n", edges[i].begin, edges[i].end, edges[i].weight);
}
}
/* 查找连线顶点的尾部下标 */
//根据顶点f以及parent 数组,可以找到当前顶点的尾部下标; 帮助我们判断2点之间是否存在闭环问题;
int Find(int *parent, int f)
{
while ( parent[f] > 0)
{
f = parent[f];
}
return f;
}
/* 生成最小生成树 */
void MiniSpanTree_Kruskal(MGraph G)
{
int i, j, n, m;
int sum = 0;
int k = 0;
/* 定义一数组用来判断边与边是否形成环路
用来记录顶点间的连接关系. 通过它来防止最小生成树产生闭环;*/
int parent[MAXVEX];
/* 定义边集数组,edge的结构为begin,end,weight,均为整型 */
Edge edges[MAXEDGE];
/*1. 用来构建边集数组*/
for ( i = 0; i < G.numVertexes-1; i++)
{
for (j = i + 1; j < G.numVertexes; j++)
{
//如果当前路径权值 != ∞
if (G.arc[i][j]<INFINITYC)
{
//将路径对应的begin,end,weight 存储到edges 边集数组中.
edges[k].begin = i;
edges[k].end = j;
edges[k].weight = G.arc[i][j];
//边集数组计算器k++;
k++;
}
}
}
//2. 对边集数组排序
sort(edges, &G);
//3.初始化parent 数组为0. 9个顶点;
// for (i = 0; i < G.numVertexes; i++)
for (i = 0; i < MAXVEX; i++)
parent[i] = 0;
//4. 计算最小生成树
printf("打印最小生成树:\n");
/* 循环每一条边 G.numEdges 有15条边*/
for (i = 0; i < G.numEdges; i++)
{
//获取begin,end 在parent 数组中的信息;
//如果n = m ,将begin 和 end 连接,就会产生闭合的环.
n = Find(parent,edges[i].begin);
m = Find(parent,edges[i].end);
//printf("n = %d,m = %d\n",n,m);
/* 假如n与m不等,说明此边没有与现有的生成树形成环路 */
if (n != m)
{
/* 将此边的结尾顶点放入下标为起点的parent中。 */
/* 表示此顶点已经在生成树集合中 */
parent[n] = m;
/*打印最小生成树路径*/
printf("(%d, %d) %d\n", edges[i].begin, edges[i].end, edges[i].weight);
sum += edges[i].weight;
}
}
printf("sum = %d\n",sum);
}
验证
int main(int argc, const char * argv[]) {
printf("Hello,最小生成树_Kruskal算法\n");
MGraph G;
CreateMGraph(&G);
MiniSpanTree_Kruskal(G);
return 0;
}
Hello,最小生成树_Kruskal算法
边集数组根据权值排序之后的为:
(4, 7) 7
(2, 8) 8
(0, 1) 10
(0, 5) 11
(1, 8) 12
(3, 7) 16
(1, 6) 16
(5, 6) 17
(1, 2) 18
(6, 7) 19
(3, 4) 20
(3, 8) 21
(2, 3) 22
(3, 6) 24
(4, 5) 26
打印最小生成树:
(4, 7) 7
(2, 8) 8
(0, 1) 10
(0, 5) 11
(1, 8) 12
(3, 7) 16
(1, 6) 16
(6, 7) 19
sum = 99