数据结构判断无向图是否连通.doc_第1页
数据结构判断无向图是否连通.doc_第2页
数据结构判断无向图是否连通.doc_第3页
数据结构判断无向图是否连通.doc_第4页
数据结构判断无向图是否连通.doc_第5页
已阅读5页,还剩2页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

数据结构实验题目:实现教材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;InfoType 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!=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; int 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)coutDFSnadjlistv.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+)coutntiendl;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-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 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

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论