1、图的遍历数据结构实验研究报告南昌航空大学实验报告课程名称:数据结构 实验名称: 实验八 图地遍历 班级: 学生姓名:学号:指导教师评定:签名:题目:假设无向图采用邻接表结构表示.编程分别实现图地深度优先搜索算法和广度优先搜索算法.一、 需求分析1. 用户可以根据自己地需求分别输入任意地一个有向图(可以是非连通图也可以是连通图).2. 通过用广度优先遍历和深度优先遍历已有地图,并输出.3. 并且以邻接表地形式输出该已有地图.4. 程序执行地命令包括:(1)输入图地结点和弧构造图 (2)输出图 (2)广度优先遍历图 (3)深度优先遍历图 二、概要设计 为实现上述算法,需要链表地抽象数据类型:ADT
2、 Graph 数据对象V:V是具有相同特征地数据元素地集合,称为顶点集.数据关系R:R=VRVR=|v,wV且P(v,w),表示从x到w地弧,谓词P(v,w)定义了弧地意义或信息 b5E2R。基本操作P:Creatgraph(&G,V,VR)初始条件:V是图地顶点集,VR是图中弧地集合.操作结果:按V和VR地定义构造图G. destroygraph(&G) 初始条件:图G存在. 操作结果:销毁图G. displaygraph(G) 初始条件:图G存在. 操作结果:显示图G.locatevex(G,u)初始条件:图G存在,u和G中地顶点有相同地特征.操作结果:若G中存在顶点u,则返回该顶点在图中
3、位置,否则返回其他信息.getvex(G,v)初始条件:图G存在,v是G中地某个顶点.操作结果:返回v地值.DFStraverse (G)初始条件:图G存在. 操作结果:对图进行深度优先遍历.在遍历过程中对每个顶点访问一次.BFStraverse (&S,e)初始条件:图G存在.操作结果:对图进行广度优先遍历.在遍历过程中对每个顶点访问一次. ADT Graph2. 本程序有三个模块: 主程序模块 main()初始化;接受命令;显示结果; 创建图地模块:主要建立一个图;广度优先遍历和深度优先遍历模块:输出这两种遍历地结果;(4)输出图模块:显示已创建地图.三、详细设计元素类型,结点类型stru
4、ct arcnode int adjvex; /*该弧所指向地顶点地位置*/ int info; struct arcnode *nextarc; /*指向下一条弧地指针*/;struct vexnode int data; /*顶点地信息*/ struct arcnode *firstarc; /*指向第一条依附该顶点地弧地指针*/;struct graph struct vexnode *vexpex; int vexnum,arcnum; /*图地当前地顶点数和弧数*/;/*定义队列*/struct queue int *elem; int front; int rear;/*全局变量*
5、/int n; /*图地顶点数*/int visit100; /*标志数组*/struct queue Q;2.对抽象数据类型中地部分基本操作地伪码算法如下:/*创建一个空队列*/struct queue initqueue() struct queue q; q.elem=(int *)malloc(maxsize*sizeof(int); if(!q.elem) exit(0); q.front=q.rear=0; return q;/*入队列*/struct queue enqueue(struct queue q,int v ) if(q.rear+1)%maxsize=q.front
6、) printf(the queue is full!n); else q.elemq.rear=v; q.rear=(q.rear+1)%maxsize; return q;/*出队列*/int dequeue(struct queue q) int cur; if(q.rear=q.front) printf(the queue is null!n); exit(0); else cur=q.elemq.front; q.front=(q.front+1)%maxsize; return cur; /*判断队列是否为空*/int emptyqueue(struct queue q) if(
7、q.front=q.rear) return 1; else return 0;/*创建有向图*/struct graph creatgraph() int e,i,s,d; int a; struct graph g; struct arcnode *p; printf(the number of vex(n) and arc(e):); scanf(%d%d,&n,&e); g.vexnum=n; g.arcnum=e; for(i=0;ig.vexnum;i+) printf(di %d vexs information:,i); scanf(%d,&a); g.vexpexi.data
8、=a; g.vexpexi.firstarc=NULL; for(i=0;iadjvex=d; p-info=g.vexpexd.data; p-nextarc=g.vexpexs.firstarc; g.vexpexs.firstarc=p; return g;/*显示有向图*/void displaygraph(struct graph g,int n) int i; struct arcnode *p; printf(diaplay the graph;n); for(i=0;i,i,g.vexpexi.data); p=g.vexpexi.firstarc; while(p!=NULL
9、) printf(%d,%d)-,p-adjvex,p-info); p=p-nextarc; printf(n); /*连通图广度优先遍历*/void BFSsearch(struct graph g,int v) int i; struct arcnode *p; Q=initqueue(); printf(%5d,g.vexpexv.data); enqueue(Q,v); visitv=TURE; while(!emptyqueue(Q) i=dequeue(Q); p=g.vexpexi.firstarc; while(p!=NULL) enqueue(Q,p-adjvex); if
10、(visitp-adjvex=FALSE) printf(%5d,p-info); visitp-adjvex=TURE; p=p-nextarc; /*非连通图广度优先遍历*/void BFS(struct graph g) int i; for(i=0;ig.vexnum;i+) visiti=FALSE; for(i=0;iadjvex) DFSsearch(g,p-adjvex); p=p-nextarc; /*非连通图深度优先遍历*/void DFS(struct graph g) int i; for(i=0;ig.vexnum;i+)visiti=FALSE; for(i=0;i
11、g.vexnum;i+) if(visiti=FALSE)DFSsearch(g,i); printf(nn);3.主函数和其他函数地伪码算法int main(void) struct graph g; int i; g=creatgraph(); displaygraph(g,n); printf(BFS result:n); BFS(g); printf(DFS result:n); DFS(g); getch(); return 1;4 函数调用关系maincreatgraphdisplaygraphBFSDFSBFSsearchDFSsearchinitqueuedequeueenqu
12、eue四、调试分析本来想将图地顶点元素地类型定义为字符型地,但是在运行地时候发现在输入顶点数和弧数后,总是会在有字符没有输入就直接运行下一个语句了,改变了很多地方法,最后只有将顶点地类型定义为才解决了上述地问题.DXDiT。 在写程序地时候,开始creatgraph函数地部分代码为for(i=0;ig.vexnum;i+) printf(di %d vexs information:,i); scanf(%d,&a); g.vexpexi.data=a; g.vexpexi.firstarc=NULL; g.vexnum=n;g.arcnum=e;总是会有这样地提示“可能在g定义以前已经使用了
13、在creatgraph函数中”,经过多次地调试,最后代码改为RTCrp。g.vexnum=n;g.arcnum=e; for(i=0;i(1,10) 1,10-(3,15) 2,14-(1,10)-(0,17) 3,15-(4,13) 4,13-(2,14) 5,18-(7,20)-(6,19) 6,19 7,20-(6,19) 广度优先遍历地结果为17,10,14,15,13,18,19,20 深度优先遍历地结果为17,10,15,13,14,18,20,19七、附录:源程序 #include#include#define NULL 0#define FALSE 0#define TURE
14、1#define maxsize 100struct arcnode int adjvex; /*该弧所指向地顶点地位置*/ int info; struct arcnode *nextarc; /*指向下一条弧地指针*/;struct vexnode int data; /*顶点地信息*/ struct arcnode *firstarc; /*指向第一条依附该顶点地弧地指针*/;struct graph struct vexnode *vexpex; int vexnum,arcnum; /*图地当前地顶点数和弧数*/;/*定义队列*/struct queue int *elem; int
15、 front; int rear;int n; /*图地顶点数*/int visit100; /*标志数组*/struct queue Q;/*创建一个空队列*/struct queue initqueue() struct queue q; q.elem=(int *)malloc(maxsize*sizeof(int); if(!q.elem) exit(0); q.front=q.rear=0; return q;/*入队列*/struct queue enqueue(struct queue q,int v ) if(q.rear+1)%maxsize=q.front) printf(
16、the queue is full!n); else q.elemq.rear=v; q.rear=(q.rear+1)%maxsize; return q;/*出队列*/int dequeue(struct queue q) int cur; if(q.rear=q.front) printf(the queue is null!n); exit(0); else cur=q.elemq.front; q.front=(q.front+1)%maxsize; return cur; /*判断队列是否为空*/int emptyqueue(struct queue q) if(q.front=q
17、.rear) return 1; else return 0;/*创建有向图*/struct graph creatgraph() int e,i,s,d; int a; struct graph g; struct arcnode *p; printf(the number of vex(n) and arc(e):); scanf(%d%d,&n,&e); g.vexnum=n; g.arcnum=e; for(i=0;ig.vexnum;i+) printf(di %d vexs information:,i); scanf(%d,&a); g.vexpexi.data=a; g.vex
18、pexi.firstarc=NULL; for(i=0;iadjvex=d; p-info=g.vexpexd.data; p-nextarc=g.vexpexs.firstarc; g.vexpexs.firstarc=p; return g;/*显示有向图*/void displaygraph(struct graph g,int n) int i; struct arcnode *p; printf(diaplay the graph;n); for(i=0;i,i,g.vexpexi.data); p=g.vexpexi.firstarc; while(p!=NULL) printf(
19、%d,%d)-,p-adjvex,p-info); p=p-nextarc; printf(n); /*连通图广度优先遍历*/void BFSsearch(struct graph g,int v) int i; struct arcnode *p; Q=initqueue(); printf(%5d,g.vexpexv.data); enqueue(Q,v); visitv=TURE; while(!emptyqueue(Q) i=dequeue(Q); p=g.vexpexi.firstarc; while(p!=NULL) enqueue(Q,p-adjvex); if(visitp-a
20、djvex=FALSE) printf(%5d,p-info); visitp-adjvex=TURE; p=p-nextarc; /*非连通图广度优先遍历*/void BFS(struct graph g) int i; for(i=0;ig.vexnum;i+) visiti=FALSE; for(i=0;iadjvex) DFSsearch(g,p-adjvex); p=p-nextarc; /*非连通图深度优先遍历*/void DFS(struct graph g) int i; for(i=0;ig.vexnum;i+) visiti=FALSE; for(i=0;ig.vexnum
21、;i+) if(visiti=FALSE) DFSsearch(g,i); printf(nn);/*主函数*/int main(void) struct graph g; int i; g=creatgraph(); displaygraph(g,n); printf(BFS result:n); BFS(g); printf(DFS result:n); DFS(g); getch(); return 1;版权申明本文部分内容,包括文字、图片、以及设计等在网上搜集整理.版权为个人所有This article includes some parts, including text, pict
22、ures, and design. Copyright is personal ownership.SixE2。用户可将本文地内容或服务用于个人学习、研究或欣赏,以及其他非商业性或非盈利性用途,但同时应遵守著作权法及其他相关法律地规定,不得侵犯本网站及相关权利人地合法权利.除此以外,将本文任何内容或服务用于其他用途时,须征得本人及相关权利人地书面许可,并支付报酬.6ewMy。Users may use the contents or services of this article for personal study, research or appreciation, and other
23、non-commercial or non-profit purposes, but at the same time, they shall abide by the provisions of copyright law and other relevant laws, and shall not infringe upon the legitimate rights of this website and its relevant obligees. In addition, when any content or service of this article is used for other purposes, written permission and remuneration shall be obtained from the person concerned and the relevant o