Kruskal算法

#include<stdio.h>
#include<stdlib.h>
#define MAXVEX 10
#define MAXEDGE 20
#define INFINITY 65535

typedef struct{
	int vexs[MAXVEX];
	int arc[MAXVEX][MAXVEX];
	int numVertexes,numEdges;
}MGraph;

typedef struct {
	int begin;
	int end;
	int weight;
}Edge;


void creatMGraph(MGraph *G);
void changeEdge(MGraph G,Edge *edges);
void MiniSpanTree_Kruskal(MGraph G);
int Find(int *parent, int f);
void Sort(Edge *edges,int n);

void Sort(Edge *edges,int n)
{
	for(int i = 0;i<n;i++)
	{
		for(int j = i+1;j<n;j++)
		{
			if(edges[i].weight > edges[j].weight)
			{
				int temp = edges[i].weight;
				edges[i].weight = edges[j].weight;
				edges[j].weight = temp;
				
				temp = edges[i].begin;
				edges[i].begin = edges[j].begin;
				edges[j].begin = temp;
				
				temp = edges[i].end;
			    edges[i].end = edges[j].end;
				edges[j].end = temp;	
			}
		}
	}
}


void changeEdge(MGraph G,Edge *edges)         //将邻接矩阵G转化为边集数组edges并按权由小到大排序的代码
{
	int count = 0;
	for(int i = 0;i<G.numVertexes;i++)
	{
		for(int j = 0;j<G.numVertexes;j++)
		{
			if(G.arc[i][j]!=INFINITY)
			{
				int flag = 1;
				for(int p = 0;p<count;p++)
				{
					if(edges[p].begin == j && edges[p].end == i && edges[p].weight == G.arc[i][j])       //这里倒过来检查是因为
					{                                                                              //邻接矩阵是对称阵
						flag = 0;
						break;
					}
				}
				if(flag)
				{
					edges[count].begin = i;
					edges[count].end = j;
					edges[count].weight = G.arc[i][j];
					count++;
				}
			}
		}
	}
	
	Sort(edges,count);
}

void creatMGraph(MGraph *G)
{
	scanf("%d%d",&G->numVertexes,&G->numEdges);
	printf("%d %d\n",G->numVertexes,G->numEdges);
	
	for(int i = 0;i<G->numVertexes;i++)
	    scanf("%d",&G->vexs[i]);
	
	for(int i = 0;i<G->numVertexes;i++)
	printf("%d ",G->vexs[i]);
	printf("\n");
	
	for(int i = 0;i<G->numVertexes;i++)
	    for(int j = 0;j<G->numVertexes;j++)
	        G->arc[i][j] = INFINITY;
	        
	int i,j,w;
	for(int k = 0;k<G->numEdges;k++)
	{
		scanf("%d%d%d",&i,&j,&w);
		
		G->arc[i][j] = w;
		G->arc[j][i] = G->arc[i][j];
	}
	
	//printf("\n");
	for(int i = 0;i<G->numVertexes;i++)
	{
		for(int j = 0;j<G->numVertexes;j++)
	    {
	 	    if(G->arc[i][j]!=INFINITY)
		    printf("%2d ",G->arc[i][j]);
		    else
		    printf("&  ");
	    }
	    printf("\n");
	}
	printf("\n");
}



void MiniSpanTree_Kruskal(MGraph G)
{
	int i, n, m;
	Edge edges[MAXEDGE];              //定义边集数组
	int parent[MAXVEX];               //定义一数组用来判断边与边是否形成环路
	
	changeEdge(G, edges);              //将邻接矩阵G转化为边集数组edges并按权由小到大排序的代码

	for (i = 0; i < G.numVertexes; i++)
		parent[i] = 0;                //初始化数组值为 0

	for (i = 0; i < G.numEdges; i++)         //循环每一条边
	{
		n = Find(parent, edges[i].begin);
		m = Find(parent, edges[i].end);
		if (n != m)           //假如 n != m,说明此边没有与现有生成树形成环路
		{
			parent[n] = m;      //将此边的结尾顶点放入下标为起点的parent中
			                    //表示此顶点已经在生成树集合中
			printf("(%d,%d) %d ", edges[i].begin, edges[i].end, edges[i].weight);
		}
	}
}

int Find(int *parent, int f)      //查找连线顶点的尾部下标
{
	while (parent[f] > 0)
		f = parent[f];

	return f;
}

int main(void)
{
	freopen("最小生成树测试数据.txt","r",stdin);
	MGraph *G = (MGraph *)malloc(sizeof(MGraph));
	creatMGraph(G);
	
	MiniSpanTree_Kruskal(*G);
	
	for(int i = 0;i<G->numVertexes;i++)
	free(G->arc[i]);
	free(G->vexs);
	free(G);
	
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值