ImageVerifierCode 换一换
格式:DOCX , 页数:19 ,大小:91.66KB ,
资源ID:11161681      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/11161681.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(实验四图的操作及应用讲解.docx)为本站会员(b****7)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

实验四图的操作及应用讲解.docx

1、实验四图的操作及应用讲解实验四 图的操作及应用实验课程名: 数据结构与算法一、实验目的及要求1、理解图的基本概念及术语。2、掌握图的两种存储结构(邻接矩阵和邻接表)的表示方法。3、熟练掌握图的两种遍历(深度优先搜索遍历和广度优先搜索遍历)的算法思想、步骤,并能列出在两种存储结构上按上述两种遍历算法得到的序列。二、实验内容任务一:构造如图1所示的图的邻接矩阵存储结构和邻接表存储结构。图1解答:(1)源代码:#include#include #include #define INFINITY 1000 #define MAX_VERTEX_NUM 20 #define OK 1 #define S

2、TARTS * typedef enumDG,DN,UDG,UDN GraphKind; typedef int EType; typedef int InfoType; typedef int VertexType; typedef struct ArcCell /define structure MGraph EType adj; InfoType *info; ArcCell,AdjMatrixMAX_VERTEX_NUMMAX_VERTEX_NUM; typedef struct VertexType vexsMAX_VERTEX_NUM; AdjMatrix arcs; int ve

3、xnum,arcnum; GraphKind kind; MGraph; int CreatUDN(MGraph &G) /CreatUDN() sub-function int IncInfo,i,j,k,v1,v2,w; coutendlG.vexnum; /input the number of vex coutG.arcnum; /input the number of arc coutIncInfo; for(i=0;iG.vexnum;+i) for(j=0;jG.vexnum;+j) G.arcsij.adj=INFINITY; /initial G.arcsij.adj G.a

4、rcsij.info=NULL; /initial G.arcsij.info coutV2), For example: (V1=1,V2=3),(V1=2,V2=4).endl; for(k=0;kG.arcnum;+k) /input arc(v1,v2) coutendlPlease input the k+1th arcs v1 (0v1v1; /input v1 coutPlease input the k+1th arcs v2 (0v2v2; /input v2 coutPlease input the k+1w; /input weight i=v1; j=v2;while(

5、iG.vexnum|jG.vexnum) /if (v1,v2) illegal,again coutPlease input the k+1th arcs v1 (0v1v1; coutPlease input the k+1th arcs v2 (0v1v2; coutPlease input the k+1w; i=v1; j=v2; /while end i-; j-; G.arcsij.adj=G.arcsji.adj=w; /weight coutG.arcsi+1j+1.adj=G.arcsj+1i+1.adj=wendl; if(IncInfo) coutPlease inpu

6、t the k+1*G.arcsij.info; /for end return (OK); /CreatUDN() end void Gprintf(MGraph G) /打印邻接矩阵 cout邻接矩阵数组为:n; for(int i=0;iG.vexnum;i+) for(int k=0;kG.vexnum;k+) coutG.arcsik.adjt; coutendl; /*邻接表*/ typedef struct ArcNode /define structure ALGraph int adjvex; struct ArcNode *nextarc; InfoType *info;

7、ArcNode; typedef struct VNode VertexType data; ArcNode *firstarc; VNode,AdjListMAX_VERTEX_NUM; typedef struct AdjList vertices; int vexnum,arcnum; int kind; ALGraph; int CreateDG(ALGraph &G) /CreateDG() subfunction int IncInfo,i,j,k,v1,v2,w; coutendlG.vexnum; /input the number of vex coutG.arcnum; /

8、input the numbe of arc coutIncInfo; /if no information, input 0 for(i=0;iG.vexnum;+i) G.verticesi.data=i; /initial G.verticesi.data G.verticesi.firstarc=NULL; /initial G.verticesi.firstarc coutV2), For example: (V1=1,V2=3),(V1=2,V2=4).endl; for(k=0;kG.arcnum;+k) /input arc(v1,v2) coutendlPlease inpu

9、t the k+1th arcs v1 (0v1v1; coutPlease input the k+1th arcs v2 (0v2v2; i=v1; j=v2; while(iG.vexnum|jG.vexnum) /if (v1,v2) illegal coutendlPlease input the k+1th arcs v1 (0v1v1; coutPlease input the k+1th arcs v2 (0v2v2; i=v1; j=v2; /while end i-; j-; ArcNode *p; p=(ArcNode *)malloc(sizeof(ArcNode);

10、/allocate memory if(!p) coutadjvex=j; /assign p-adjvex p-nextarc=G.verticesi.firstarc; p-info=NULL; G.verticesi.firstarc=p; if(IncInfo) cout*(p-info); /input information /for end return (OK); /CreateDG() end int main() MGraph MG; ALGraph AG; int a=-1; while(a!=0) coutSTARTSSTARTSendl; cout(1)邻接矩阵(无向

11、网)t(2)邻接表(有向图)t(3)退出endl; couta; switch(a) case 1: CreatUDN(MG);Gprintf(MG);break; case 2: CreateDG(AG);break; case 3: a=0;break; default:cout选择错误nendl; return 0; (2)运行结果:(3)运行结果分析: 运用数组与链表的结合来实现任务二:用邻接表的存储结构创建一个如图2所示的图a,分别打印出这两个图的深度优先遍历和广度优先遍历的结点信息序列。解答:(1)源代码:#include#include#includeusing namespac

12、e std;#define ERROR 1#define MAX_VERTEX_NUM 100typedef struct ArcNode int adjvex; struct ArcNode *nextarc; string info;ArcNode;typedef struct VNode char date; ArcNode * firstarc;VNode,AdjListMAX_VERTEX_NUM;typedef struct AdjList vertices; int vexnum,arcnum; /当前图的vexnum顶点数和arcnum弧数 int kind;ALGraph;i

13、nt LocateVex(ALGraph &G,char &v1) int i; for(i=0;i=G.vexnum) return ERROR; else return 0;void CreateDG(ALGraph &G) ArcNode *p,*q; char v1,v2; char v; int i,j,k,n; cout请输入图的顶点数和弧数:G.vexnum; cinG.arcnum; cout请输入顶点:endl; for(i=0;iv; G.verticesi.date=v; G.verticesi.firstarc=NULL; cout请输入弧尾和弧头:; for(k=0;

14、kv1; cinv2; i=LocateVex(G,v1);j=LocateVex(G,v2); if(G.verticesi.firstarc=NULL) p=(ArcNode *)new ArcNode; G.verticesi.firstarc=p; q=G.verticesi.firstarc; else q=G.verticesi.firstarc; for(n=0;nnextarc) if(!q-nextarc) break; p=(ArcNode *)new ArcNode; q-nextarc=p; q=q-nextarc; q-adjvex=j; q-nextarc=NULL

15、; coutadjvex; if(visitedi=false) n=i; return n;int NextAdjVex(ALGraph &G,int v) int i; int n=-1; ArcNode *p; p=G.verticesv.firstarc; for(i=p-adjvex;iadjvex; if(visitedi=false) n=i; break; else p=p-nextarc; return n;void VisitFuc(ALGraph &G,int v) coutG.verticesv.date=0;w=NextAdjVex(G,v) if(!visitedw

16、) DFS(G,w);void DFSTraverse(ALGraph &G) int v; for(v=0;vG.vexnum;v+) visitedv=false; cout深度优先搜索:endl; for(v=0;vG.vexnum;v+) if(!visitedv) DFS(G,v); /-广度优先遍历-/void BFSTraverse(ALGraph &G) int v; int w; queue q; /STL队列 for(v=0;vG.vexnum;v+) visitedv=false;/ InitQueue(Q); cout广度优先搜索:; for(v=0;v=0;w=Nex

17、tAdjVex(G,v) if(!visitedw) visitedw=true; VisitFuc(G,w); q.push(w); /void menu() coutn; cout /-图的基本操作-/endl; cout * 1、图的构建 *endl; cout * 2、深度优先遍历 *endl; cout * 3、广度优先遍历 *endl; cout -endl; cout请输入数字进行选择:i; while(i4) switch(i) case 1:CreateDG(G);break; case 2:DFSTraverse(G);coutendl;break; case 3:BFSTraverse(G);couti; return 0;(2)运行结果:(3)运行结果分析:运用图的深度优先和广度优先算法搜索。3、实验小结通过本次实验我知道了图的基本概念及术语。了解了图的两种存储结构(邻接矩阵和邻接表)的表示方法。熟练掌握图的两种遍历(深度优先搜索遍历和广度优先搜索遍历)的算法思想、步骤,并能列出在两种存储结构上按上述两种遍历算法得到的序列。

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1