#include <stdio.h>
#include <stdlib.h>
#define OK 1
#define ERROR 0
#define OVERFLOW -2
typedef int status;
typedef int ElemType;
typedef int bool;
#define true 1
#define false 0
#define MaxInt 32767
#define MVNum 100
typedef char VerTexType;
typedef int ArcType;
typedef struct
{
VerTexType vexs[MVNum];
ArcType arcs[MVNum][MVNum];
int vexnum,arcnum;
}AMGraph;
int LocateVex_AM(AMGraph G,VerTexType v)
{
int i=0;
//while(i<G.vexnum&&v!=G.vexs[i])
//i++;
for(;i<G.vexnum;i++)
if(G.vexs[i]==v)
break;
return i;
}
status CreateUDN_AM(AMGraph *G,VerTexType *V)
{
int i,j,k;
for(i=0;i<G->vexnum;i++)
G->vexs[i]=V[i];
for(i=0;i<G->vexnum;i++)
for(j=0;j<G->vexnum;j++)
G->arcs[i][j]=MaxInt;
char v1,v2;
int w;
for(k=0;k<G->arcnum;k++)
{
scanf("%c %c %d",&v1,&v2,&w);
getchar();
int i=LocateVex_AM(*G,v1);
int j=LocateVex_AM(*G,v2);
G->arcs[i][j]=w;
G->arcs[j][i]=G->arcs[i][j];
}
return OK;
}
void printAMG(AMGraph G)
{
int i,j;
for(i=0;i<G.vexnum;i++)
{
for(j=0;j<G.vexnum;j++)
printf("%5d ",G.arcs[i][j]);
printf("\n");
}
}
struct
{
VerTexType adjvex;
ArcType lowcost;
}closedge[MVNum];
int Min(int num)
{
ArcType min=MaxInt,p;
int i=0,result=0;
for(;i<num;i++)
{
p=closedge[i].lowcost;
if(p>0&&p<min)
{
min=p;
result=i;
}
}
return result;
}
void MiniSpanTree_Prim(AMGraph G,VerTexType u)
{
int k=LocateVex_AM(G,u);
int j,i;
for(j=0;j<G.vexnum;j++)
if(j!=k)
{
closedge[j].adjvex=u;
closedge[j].lowcost=G.arcs[k][j];
}
closedge[k].lowcost=0;
for(j=1;j<G.vexnum;j++)
{
k=Min(G.vexnum);
VerTexType u0=closedge[k].adjvex;
VerTexType v0=G.vexs[k];
printf("(%c,%c)\n",u0,v0);
closedge[k].lowcost=0;
for(i=0;i<G.vexnum;i++)
if(G.arcs[k][i]<closedge[i].lowcost)
{
closedge[i].adjvex=G.vexs[k];
closedge[i].lowcost=G.arcs[k][i];
}
}
}
int arcnum=10;
struct E
{
VerTexType Head;
VerTexType Tail;
ArcType lowcost;
}Edge[10];
int Vexset[MVNum];
//Swap、partition、Sort均用于快速排序
void Swap(int i,int j)
{
struct E e;
e=Edge[i];
Edge[i]=Edge[j];
Edge[j]=e;
}
int partition(int low,int high)
{
int p=high,lastlow=low-1,i;
ArcType pivot=Edge[p].lowcost;
for(i=low;i<high;i++)
{
if(Edge[i].lowcost<=pivot)
{
lastlow+=1;
if(lastlow<i)
Swap(i,lastlow);
}
}
if(pivot<Edge[lastlow+1].lowcost)
Swap(p,lastlow+1);
return lastlow+1;
}
void Sort(int low,int high)
{
if(low<high)
{
int p=partition(low,high);
Sort(low,p-1);
Sort(p+1,high);
}
}
void MiniSpanTree_Kruskal(AMGraph G)
{
int i,j,k=0;
for(i=0;i<G.vexnum;i++)
for(j=0;j<i+1;j++)
{
if(G.arcs[i][j]<MaxInt)
{
Edge[k].Head=G.vexs[i];
Edge[k].Tail=G.vexs[j];
Edge[k].lowcost=G.arcs[i][j];
k++;
}
}
Sort(0,arcnum-1);
for(i=0;i<G.vexnum;i++)
Vexset[i]=i;
for(i=0;i<G.arcnum;i++)
{
int v1=LocateVex_AM(G,Edge[i].Head);
int v2=LocateVex_AM(G,Edge[i].Tail);
int vs1=Vexset[v1];
int vs2=Vexset[v2];
if(vs1!=vs2)
{
printf("(%c,%c)\n",Edge[i].Head,Edge[i].Tail);
for(j=0;j<G.vexnum;j++)
if(Vexset[j]==vs2)
Vexset[j]=vs1;
}
}
}
int main()
{
AMGraph G;
G.vexnum=6;
G.arcnum=10;
VerTexType V[G.vexnum];
int i;
for(i=0;i<G.vexnum;i++)
V[i]=i+'A';
printf("Create AMGraph:\n");
CreateUDN_AM(&G,V);
printAMG(G);
printf("MiniSpanTree Prim:\n");
MiniSpanTree_Prim(G,G.vexs[0]);
printf("MiniSpanTree Kruskal:\n");
MiniSpanTree_Kruskal(G);
return 0;
}
数据结构C语言-最小生成树之Prim算法与Kruskal算法
最新推荐文章于 2023-04-02 19:29:19 发布