存储结构
typedef struct
{
char vexs[MAX]; //顶点表
int arcs[MAX][MAX]; //邻接矩阵
int arc,vex; //边和点数
}Graph;
//普利姆算法的closedge
struct
{
char adjvex;
int lowcost;
}closedge[MAX];
//克鲁斯卡尔算法的 edge
typedef struct
{
char head,tail;
int lowcost;
}ed;
普利姆:
void prim(Graph G,char c)
{
int i,j,k;
k=locate(G,c);
closedge[k].lowcost=0;
//赋初值
for(i=0;i<G.vex;i++)
{
if(i!=k)
{
closedge[i].adjvex=c;
closedge[i].lowcost=G.arcs[k][i];
}
}
for(i=1;i<G.vex;i++)
{
int min=10000000;
j=0;k=0;
//找最小的,k记录
for(j=0;j<G.vex;j++)
{
if(closedge[j].lowcost!=0&&closedge[j].lowcost<min)
{
min=closedge[j].lowcost;
k=j;
}
}
printf("%c-%c权值:%d ",closedge[k].adjvex,G.vexs[k],closedge[k].lowcost);
closedge[k].lowcost=0;
for(j=0;j<G.vex;j++)
{
if(closedge[j].lowcost!=0&&G.arcs[k][j]<closedge[j].lowcost)
{
closedge[j].lowcost=G.arcs[k][j];
closedge[j].adjvex=G.vexs[k];
}
}
}
}
克鲁斯卡尔
void kruskal(Graph G)
{
ed edge[G.arc];
int vexset[MAX];
int i,j,k;
k=0;
//为edge数组赋值
for(i=0;i