数据结构课程设计用Kruskal 算法求最小生成树我要的是Kruskal 算法求最小生成树

来源:学生作业帮助网 编辑:作业帮 时间:2024/04/25 19:20:59
数据结构课程设计用Kruskal 算法求最小生成树我要的是Kruskal 算法求最小生成树

数据结构课程设计用Kruskal 算法求最小生成树我要的是Kruskal 算法求最小生成树
数据结构课程设计用Kruskal 算法求最小生成树
我要的是Kruskal 算法求最小生成树

数据结构课程设计用Kruskal 算法求最小生成树我要的是Kruskal 算法求最小生成树
将城市看成是点,城市之间的距离看成是点之间的权值.
下面是PRIM算法实现的最小生成树代码.,利用邻接矩阵存储边的信息.程序已通过编译了,可以直接运行.
#include <stdio.h>
#include <string.h>
typedef int VRType;
typedef char InfoType;
#define MAX_NAME 3
/*顶点字符串的最大长度+1*/
#define MAX_INFO 20
/*相关信息字符串的最大长度+1*/
typedef char VertexType[MAX_NAME];
#define INFINITY 32767
/*用整型最大值代替无穷大*/
#define MAX_VERTEX_NUM 20
/*最大顶点个数*/
typedef enum GraphKind;
/**/
typedef int PathMatrix[MAX_VERTEX_NUM][MAX_VERTEX_NUM];
typedef int ShortPathTable[MAX_VERTEX_NUM];
typedef struct
{
VRType adj;
/*顶点关系类型.对无权图,用1(是)或0(否)表示相邻否*/
/*对带全图,则为权值类型*/
InfoType *info;
/*该弧相关信息的指针(可无)*/
}ArcCell,AdjMatrix[MAX_VERTEX_NUM][MAX_VERTEX_NUM];
typedef struct
{
VertexType vexs[MAX_VERTEX_NUM];
/*顶点向量*/
AdjMatrix arcs;
/*邻接矩阵*/
int vexnum,arcnum;
/*图的当前顶点数和弧数*/
GraphKind kind;
/*图的种类标志*/
}MGraph;
int LocateVex(MGraph G,VertexType u)
{ /*初始条件:图G存在,u和G中顶点有相同特征*/
/*操作结果:若G中存在顶点u,则返回该顶点在图中位置;否则返回-1*/
int i;
for(i=0;i<G.vexnum;++i)
if(strcmp(u,G.vexs[i])==0)
return i;
return -1;
}
int CreateAN(MGraph *G)
{/*采用数组(邻接矩阵)表示法,构造无向网G*/
int i,j,k,w,IncInfo;
char s[MAX_INFO],*info;
VertexType va,vb;
printf("please input number of acmes and arcs in G,and there is some information in arc,if yes is 1,else is 0:");
scanf("%d,%d,%d",&(*G).vexnum,&(*G).arcnum,&IncInfo);
printf("please input the value of %d acmes(<%d character):\n",(*G).vexnum,MAX_NAME);
for(i=0;i<(*G).vexnum;++i)
/*构造顶点向量*/
scanf("%s",(*G).vexs[i]);
for(i=0;i<(*G).vexnum;++i)
/*初始化邻接矩阵*/
for(j=0;j<(*G).vexnum;++j)
{(*G).arcs[i][j].adj=INFINITY;
/*网*/
(*G).arcs[i][j].info=NULL;
}
printf("please input %d the first and the second of acr and weigh:\n",(*G).arcnum);
for(k=0;k<(*G).arcnum;++k)
{
scanf("%s %s %d",va,vb,&w);
/*%*c吃掉回车符*/
i=LocateVex(*G,va);
j=LocateVex(*G,vb);
(*G).arcs[i][j].adj=(*G).arcs[j][i].adj=w;
/*无向*/
if(IncInfo)
{
printf("please input some information about the arc(<%d character): ",MAX_INFO);
gets(s);
w=strlen(s);
if(w)
{
info=(char*)malloc((w+1)*sizeof(char));
strcpy(info,s);
(*G).arcs[i][j].info=(*G).arcs[j][i].info=info;
/*无向*/
}
}
}
(*G).kind=DN;
return 1;
}
typedef struct
{/*记录从顶点集U到V-U的代价最小的边的辅助数组定义*/
VertexType adjvex;
VRType lowcost;
}minside[MAX_VERTEX_NUM];
int mininum(minside SZ,MGraph G)
{/*求closedege,lowcost的最小正值*/
int i=0,j,k,min;
while(!SZ[i].lowcost)
i++;
min=SZ[i].lowcost;
k=i;
for(j=i+1;j<G.vexnum;j++)
if(SZ[j].lowcost>0)
if(min>SZ[j].lowcost)
{
min=SZ[j].lowcost;
k=j;
}
return k;
}
void MiniSpanTree_PRIM(MGraph G,VertexType u)
{/*用普利姆算法从第u个顶点出发构造网G的最小生成树T,输出T的各条边*/
int i,j,k;
minside closedge;
k=LocateVex(G,u);
for(j=0;j<G.vexnum;++j)
/*辅助数组初始化*/
{
if(j!=k)
{ strcpy(closedge[j].adjvex,u);
closedge[j].lowcost=G.arcs[k][j].adj;
}
}
closedge[k].lowcost=0;
/*初始U=*/
printf("zuixiaodaijiashengchengshudegetiaobianwei:\n");
for(i=1;i<G.vexnum;++i)
{/*选择其余G.vexnum-1个顶点*/
k=mininum(closedge,G);
/*求出T的下一个结点:第K顶点*/
printf("(%s-%s)\n",closedge[k].adjvex,G.vexs[k]);
/*输出生成树的边*/
closedge[k].lowcost=0;
/*第K顶点并入U集*/
for(j=0;j<G.vexnum;++j)
if(G.arcs[k][j].adj<closedge[j].lowcost)
{/*新顶点并入U集后重新选择最小边*/
strcpy(closedge[j].adjvex,G.vexs[k]);
closedge[j].lowcost=G.arcs[k][j].adj;
}
}
}
void main()
{
MGraph G;
CreateAN(&G);
MiniSpanTree_PRIM(G,G.vexs[0]);
getch();
}