图的最小生成树--克鲁斯卡尔算法 C语言

克鲁斯卡尔算法构造最小生成树。
还是根据书上的例子进行构造:
在这里插入图片描述
步骤为(代码用邻接矩阵表示上图):
在这里插入图片描述
完整代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MaxInt 32767//无穷值设置 
#define MVNum 100 //图的最大容量 ,也可以称为图的最大顶点数 

void Interrupt(void)//创建一个中断函数 
{
	while(1)//用于检测换行符,使函数脱离scanf的连续输出 
		if(getchar()=='\n')
			break;
} 

typedef struct//图的结构体 
{
	char vexs[MVNum];//顶点表 
	int arcs[MVNum][MVNum];//邻接矩阵 
	int vexnum,arcnum;//图的当前点数和边数 
}AMGraph;

struct//辅助数组Edge的定义 
{
	char Head;//边的始点 
	char Tail;//边的终点 
	int lowcoat;//边的权值 
}Edge[MVNum];

void InitGraph(AMGraph &G)//图的初始化 
{
	int i,j;
	for(i = 0;i<MVNum;i++)
		for(j = 0;j<MVNum;j++)
			G.arcs[i][j] = MaxInt;//使邻接矩阵中的数据都初始化为0 
}

void CreateGraph(AMGraph &G)//图的创建 
{
	int i;//记录次数 
	char a;//顶点变量 
	printf("请输入顶点数和边数:");
	scanf("%d %d",&G.vexnum,&G.arcnum);
	Interrupt();//该函数用于检测并吸收换行符 
	printf("请输入顶点名称(连续输入):");
	for(i=0;i<G.vexnum;i++)
	{
		scanf("%c",&a);
		G.vexs[i] = a;//第i个顶点的命名 
	}
	Interrupt();//该函数用于检测并吸收换行符
	char b,c;//顶点变量 
	int w,j,k;//w为权值变量,j和k是用来记录次数的 
	for(i=0;i<G.arcnum;i++)
	{
		printf("请输入边的两个顶点和权值w:");
		scanf("%c %c %d",&b,&c,&w);//输入 
		Interrupt();//该函数用于检测并吸收换行符
		Edge[i].Head = b;
		Edge[i].Tail = c;
		Edge[i].lowcoat = w;//辅助数组Edge[]的初始化 
		for(j=0;j<G.vexnum;j++)
		{
			if(G.vexs[j] == b)//找到输入的顶点b的位置 
			break;
		}
		for(k=0;k<G.vexnum;k++)
		{
			if(G.vexs[k] == c)//找到输入的顶点c的位置 
			break;
		}
		G.arcs[j][k] = G.arcs[k][j] = w;//权值赋值 
	}
}

void InputGraph(AMGraph G)//邻接矩阵的输出 
{
	int i,j;//记录次数 
	printf("邻接矩阵为:\n   ");
	for(i=0;i<G.vexnum;i++)//打印顶点名称 
		printf("%c  ",G.vexs[i]);
	printf("\n");
	for(i=0;i<G.vexnum;i++)
	{
		printf("%c  ",G.vexs[i]);//打印顶点名称 
		for(j=0;j<G.vexnum;j++)//遍历邻接矩阵 
			printf("%d  ",G.arcs[i][j]);
		printf("\n");
	}
}

int Vexset[MVNum];//再申请一个辅助数组Vexset 
void Sort(AMGraph G)//书上的Sort函数,将数组Edege中的元素按权值从小到大排序,这里用 冒泡排序 
{
	int i,j;//记录次数 
	for(i=G.arcnum-1;i>=0;i--)//相邻两数的比较终点 
	{
		for(j=0;j<i;j++)//进行相邻两数的比较 
		{
			if(Edge[j].lowcoat > Edge[j+1].lowcoat)//如果大于,互换位置 
			{
				Edge[G.arcnum] = Edge[j];
				Edge[j] = Edge[j+1];
				Edge[j+1] = Edge[G.arcnum];
			}
		}
	}
}
int LocateVex(AMGraph G,char b)//定位函数,用于定位顶点b在顶点集的位置 并返回其位置 
{
	int j;
	for(j=0;j<G.vexnum;j++)//利用循环找到和b相同的顶点 
	{
		if(G.vexs[j] == b)//找到输入的顶点b的位置 
		break;
	}
	return j;//返回其位置j 
}
void MiniSpanTree_Kruskal(AMGraph G)//无向网G以邻接矩阵形式存储,构造最小生成树,克鲁斯卡尔算法 
{
	int i,j;
	Sort(G);//根据权值排序 
	for(i=0;i<G.vexnum;i++)//辅助数组,表示各顶点自成一个连通分量 
		Vexset[i] = i;
	int v1,v2,
		vs1,vs2;
	for(i=0;i<G.arcnum;i++)//依次查看数组Edge中的边 
	{
		v1 = LocateVex(G,Edge[i].Head);//v1为边的始点的下标  定位函数 
		v2 = LocateVex(G,Edge[i].Tail);//v2为边的终点的下标 
		vs1 = Vexset[v1];//获取边Edge[i]的始点所在的连通分量vs1 
		vs2 = Vexset[v2];//获取边Edge[i]的终点所在的连通分量vs2
		if(vs1 != vs2)//边的两个顶点分属不同的连通分量 
		{
			printf("%c->%c  ",Edge[i].Head,Edge[i].Tail);//输出打印次边 
			for(j=0;j<G.vexnum;j++)//合并vs1和vs2两个分量,即两个集合统一编号 
				if(Vexset[j] == vs2)//集合编号为vs2的都改为vs1 
					Vexset[j] = vs1;
		}
	}
}

int main()
{
	AMGraph G;
	InitGraph(G);//图的初始化 
	CreateGraph(G);//创建邻接矩阵 
	InputGraph(G);//输出邻接矩阵
	MiniSpanTree_Kruskal(G);//克鲁斯卡尔算法 
	return 0;
}

结果演示:
在这里插入图片描述
(完)

  • 14
    点赞
  • 86
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是最小生成树的普里姆算法克鲁斯卡尔算法C语言实现: 1. 普里姆算法 ```c #include <stdio.h> #include <limits.h> #define V 5 int minKey(int key[], bool mstSet[]) { int min = INT_MAX, min_index; for (int v = 0; v < V; v++) if (mstSet[v] == false && key[v] < min) min = key[v], min_index = v; return min_index; } void printMST(int parent[], int graph[V][V]) { printf("Edge \tWeight\n"); for (int i = 1; i < V; i++) printf("%d - %d \t%d \n", parent[i], i, graph[i][parent[i]]); } void primMST(int graph[V][V]) { int parent[V]; int key[V]; bool mstSet[V]; for (int i = 0; i < V; i++) key[i] = INT_MAX, mstSet[i] = false; key[0] = 0; parent[0] = -1; for (int count = 0; count < V - 1; count++) { int u = minKey(key, mstSet); mstSet[u] = true; for (int v = 0; v < V; v++) if (graph[u][v] && mstSet[v] == false && graph[u][v] < key[v]) parent[v] = u, key[v] = graph[u][v]; } printMST(parent, graph); } int main() { int graph[V][V] = { { 0, 2, 0, 6, 0 }, { 2, 0, 3, 8, 5 }, { 0, 3, 0, 0, 7 }, { 6, 8, 0, 0, 9 }, { 0, 5, 7, 9, 0 } }; primMST(graph); return 0; } ``` 2. 克鲁斯卡尔算法 ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #define V 5 #define E 9 struct Edge { int src, dest, weight; }; struct Graph { int V, E; struct Edge* edge; }; struct Graph* createGraph(int V, int E) { struct Graph* graph = (struct Graph*)malloc(sizeof(struct Graph)); graph->V = V; graph->E = E; graph->edge = (struct Edge*)malloc(graph->E * sizeof(struct Edge)); return graph; } struct subset { int parent; int rank; }; int find(struct subset subsets[], int i) { if (subsets[i].parent != i) subsets[i].parent = find(subsets, subsets[i].parent); return subsets[i].parent; } void Union(struct subset subsets[], int x, int y) { int xroot = find(subsets, x); int yroot = find(subsets, y); if (subsets[xroot].rank < subsets[yroot].rank) subsets[xroot].parent = yroot; else if (subsets[xroot].rank > subsets[yroot].rank) subsets[yroot].parent = xroot; else { subsets[yroot].parent = xroot; subsets[xroot].rank++; } } int myComp(const void* a, const void* b) { struct Edge* a1 = (struct Edge*)a; struct Edge* b1 = (struct Edge*)b; return a1->weight > b1->weight; } void KruskalMST(struct Graph* graph) { int V = graph->V; struct Edge result[V]; int e = 0; int i = 0; qsort(graph->edge, graph->E, sizeof(graph->edge[0]), myComp); struct subset* subsets = (struct subset*)malloc(V * sizeof(struct subset)); for (int v = 0; v < V; ++v) { subsets[v].parent = v; subsets[v].rank = 0; } while (e < V - 1 && i < graph->E) { struct Edge next_edge = graph->edge[i++]; int x = find(subsets, next_edge.src); int y = find(subsets, next_edge.dest); if (x != y) { result[e++] = next_edge; Union(subsets, x, y); } } printf("Following are the edges in the constructed MST\n"); for (i = 0; i < e; ++i) printf("%d -- %d == %d\n", result[i].src, result[i].dest, result[i].weight); return; } int main() { int V = 5; int E = 9; struct Graph* graph = createGraph(V, E); graph->edge[0].src = 0; graph->edge[0].dest = 1; graph->edge[0].weight = 2; graph->edge[1].src = 0; graph->edge[1].dest = 3; graph->edge[1].weight = 6; graph->edge[2].src = 1; graph->edge[2].dest = 2; graph->edge[2].weight = 3; graph->edge[3].src = 1; graph->edge[3].dest = 4; graph->edge[3].weight = 5; graph->edge[4].src = 2; graph->edge[4].dest = 4; graph->edge[4].weight = 7; graph->edge[5].src = 3; graph->edge[5].dest = 4; graph->edge[5].weight = 9; KruskalMST(graph); return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值