数据结构判断无向图是否连通

上传人:自*** 文档编号:80634645 上传时间:2019-02-19 格式:DOC 页数:7 大小:81.80KB
返回 下载 相关 举报
数据结构判断无向图是否连通_第1页
第1页 / 共7页
数据结构判断无向图是否连通_第2页
第2页 / 共7页
数据结构判断无向图是否连通_第3页
第3页 / 共7页
数据结构判断无向图是否连通_第4页
第4页 / 共7页
数据结构判断无向图是否连通_第5页
第5页 / 共7页
点击查看更多>>
资源描述

《数据结构判断无向图是否连通》由会员分享,可在线阅读,更多相关《数据结构判断无向图是否连通(7页珍藏版)》请在金锄头文库上搜索。

1、数据结构实验题目:实现教材P215例8.3的完整算法(判断无向图是否联通)源码:#include malloc.h#include #include #define MAXV 6#define InfoType int#define Vertex inttypedef structint no;InfoType info; VertexType;typedef structint edgeMAXVMAXV;int n,e;VertexType vexsMAXV;MGraph;typedef struct ANodeint adjvex;struct ANode *nextarc;InfoTyp

2、e info;ArcNode;typedef struct VNodeVertex data;ANode *firstarc;VNode;typedef VNode AdjListMAXV;typedef structAdjList adjlist;int n,e;ALGraph;int visitedMAXV=0,0,0,0,0,0;void ListToMat(MGraph g,ALGraph * &G)int i,j;ArcNode *p;for(i=0;in;i+)for(j=0;jn;j+)g.edgeij=0;for(i=0;iadjlisti.firstarc;while(p!=

3、NULL)g.edgeip-adjvex=1;p=p-nextarc;g.n=G-n;g.e=G-e;void BFS(ALGraph *G,int v)ArcNode *p;int i,j;visitedv=1;printf(%d,v);p=G-adjlistv.firstarc;while(p!=NULL) printf(%d,p-adjvex);if(visitedp-adjvex!=1)BFS(G,p-adjvex);p=p-nextarc;void BFSbook(ALGraph *G,int v)ArcNode *p;int queueMAXV,front=0,rear=0; in

4、t w ,i;printf(%2d,v);visitedv=1;rear=(rear+1)%MAXV;queuerear=v;while(front!=rear)front=(front+1)%MAXV;w=queuefront;p=G-adjlistw.firstarc;while(p!=NULL)if(visitedp-adjvex=0)printf(%2d,p-adjvex);visitedp-adjvex=1;rear=(rear+1)%MAXV;queuerear=p-adjvex;p=p-nextarc;printf(n);void DFS(ALGraph *G,int v)cou

5、tDFSnadjlistv.firstarc;visitedv=1;printf(=%d= n,v);while(p!=NULL) printf(|%d| n,p-adjvex);if(visitedp-adjvex=0)DFS(G,p-adjvex);p=p-nextarc; bool connect(ALGraph * G)int i;bool flag=true;for(i=0;in;i+)visitedi=0;BFSbook(G,3); coutDispVisitedendl;for(i=0;in;i+)coutvisitediendl;for(i=0;in;i+)coutntiend

6、l;if(visitedi=0)flag=false;break;coutnnendl;return flag;void MatToList(MGraph g,ALGraph * &G) int i,j,k=0,flag=1;ArcNode * p;G=(ALGraph *)malloc(sizeof(ALGraph);for(i=0;iadjlisti.firstarc=NULL;for(i=0;i=0;j-)if(g.edgeij=1)p=(ArcNode *)malloc(sizeof(ArcNode);p-adjvex=j;p-nextarc=G-adjlisti.firstarc;G

7、-adjlisti.firstarc=p; G-n=g.n; G-e=g.e;void DispGraph(ALGraph * &G,int num)int i;ArcNode *p;for(i=0;i,i);p=G-adjlisti.firstarc;while(p!=NULL)printf(%d-,p-adjvex);p=p-nextarc;printf(n);void DispMat(MGraph g)int i,j;for(i=0;ig.n;i+)for(j=0;jg.n;j+)printf(%dt,g.edgeij);printf(n);MGraph DataToMat(MGraph

8、 mgraph,int dataMAXVMAXV)int i,j;for(i=0;iMAXV;i+)for(j=0;jMAXV;j+)mgraph.edgeij=dataij;mgraph.n=MAXV;mgraph.e=14;return mgraph;int main()/矩阵图数据源int dataMAXVMAXV=0,1,0,1,0,1,1,0,1,0,0,0,0,1,0,0,1,0,1,0,0,0,1,0,0,0,1,1,0,1,1,0,0,0,1,0;ALGraph * algraph;MGraph mgraph;mgraph=DataToMat(mgraph,data);DispMat(mgraph);MatToList(mgraph,algraph);DispGraph(algraph,MAXV);cout是否连通:(connect(algraph)?连通:非连通)endl;return 0;执行结果:心得体会和建议: 本次实验遇到了很多困难,对连通图理解不够深入,导致编程出现了很多错误,经过查阅资料,同学讨论得到了完成,以后要仔细、认真听课。课下及时复习,争取对学过的知识点都熟练掌握。

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 办公文档 > 其它办公文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号