图生成最小树

#include"stdio.h" #include"malloc.h" #define L 10 #define C 10 #define MAX_Ver 30 typedef int VertexType; typedef int Graphkind; typedef int InfoType ; typedef struct ArcCellj { int adj; InfoType *info; }ArcCell, AdjMatrix[L][C]; typedef struct { VertexType vex[MAX_Ver]; AdjMatrix arcs; int vexnum,arcnum; Graphkind kind; }MGraph; typedef struct closedge1 { VertexType adjvex; int lowcost; }closedge[MAX_Ver]; int visit[MAX_Ver]; int LocateVex(MGraph * G,VertexType v) { int i; for(i=0;ivexnum;i++) if(v==G->vex[i]) break; return i; } int CreateUDN(MGraph *G) { int info1,i; VertexType v1,v2; int w,Li,Ci;; printf("请输入结点数,边数,info/n"); scanf("%d %d %d",&G->vexnum,&G->arcnum,&info1); for(int k=0;kvexnum;k++) { printf("输入结点值:"); scanf("%d",&G->vex[k]); } for(i=0;ivexnum;i++) for (int j=0;jvexnum;j++) { G->arcs[i][j].adj=100; G->arcs[i][j].info=NULL; } for(int r=0;rarcnum;r++) { printf("输入接点,权值/n"); scanf("%d %d %d",&v1,&v2,&w); Li=LocateVex(G,v1); Ci=LocateVex(G,v2); G->arcs[Li][Ci].adj=w; if(info1==1) { InfoType lin;scanf("%d",&lin);*(G->arcs[Li][Ci].info)=lin;} G->arcs[Ci][Li].adj=G->arcs[Li][Ci].adj; } return 0; } void vis(MGraph *G) { for(int i=0;ivexnum;i++) { for(int j=0;jvexnum;j++) printf(" %d ",G->arcs[i][j].adj); printf("/n"); } printf("/n"); } void DFSTra(MGraph *G) { for(int i=0;ivexnum;i++) visit[i]=0; } int FirstAdj(MGraph *G,int v) { for(int i=0;ivexnum;i++) if(G->arcs[v][i].adj<100) {return i;break;} return -1; } int nextAdj(MGraph *G,int v,int w) { for(int i=w+1;ivexnum;i++) if(G->arcs[v][i].adj<100){ return i;break;} return -1; } void DFS(MGraph *G,int v) { visit[v]=1; printf("%d",G->vex[v]); for(int w=FirstAdj(G,v);w>=0;w=nextAdj(G,v,w)) { if(visit[w]!=1) DFS(G,w); } } void initfind(int Lin[L][C],int p) { if(p==0) { for(int i=0;iclosed[i].lowcost) { min=closed[i].lowcost; k=i; } } } return k; } int Mintree(MGraph *G,int v) { closedge closed; int Lin[L][C]; int k=0; initfind(Lin,0); for(int i=0;ivexnum;i++) { if(i!=v) { closed[i].lowcost=G->arcs[v][i].adj; closed[i].adjvex=G->vex[v]; } } closed[v].lowcost=0; closed[v].adjvex=G->vex[v]; for(int j=1;jvexnum;j++) { k=findmin(closed,G->vexnum); printf("%d-->%d/n",closed[k].adjvex,G->vex[k]); // Lin[v][k]=G->arcs[v][k].adj; closed[k].lowcost=0; for(int e=0;evexnum;e++) { if(closed[e].lowcost>G->arcs[k][e].adj) { closed[e].lowcost=G->arcs[k][e].adj; closed[e].adjvex=G->vex[k]; } } } // initfind(Lin,G->vexnum); return 0; } int main() { MGraph G; CreateUDN(&G); vis(&G); DFSTra(&G); DFS(&G,1); printf("/n"); Mintree(&G,1); return 0; }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值