构造可以使n个城市连接的最小生成树_第1页
构造可以使n个城市连接的最小生成树_第2页
构造可以使n个城市连接的最小生成树_第3页
构造可以使n个城市连接的最小生成树_第4页
构造可以使n个城市连接的最小生成树_第5页
已阅读5页,还剩12页未读 继续免费阅读

下载本文档

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

文档简介

1、数据结构 课程设计报告 设计题目:构造可以使设计题目:构造可以使 n n 个城市连接的最小生成个城市连接的最小生成 树树 姓名:姓名: 学号:学号: 专业:物联网工程(嵌入式培养)专业:物联网工程(嵌入式培养) 院系:计算机技术与科学学院院系:计算机技术与科学学院 班级:班级:14051405 指导教师:指导教师: 20162016 年年 0101 月月 0909 日日 摘要 本次课程设计的要求是给定一个地区的本次课程设计的要求是给定一个地区的 n n 个城市间的距离个城市间的距离 网,用网,用 primprim 算法建立最小生成树,并计算得到的最小生成算法建立最小生成树,并计算得到的最小生成

2、 树的代价。将该地区的城市用顶点表示,城市间的公路用树的代价。将该地区的城市用顶点表示,城市间的公路用 边表示,公路的长度作为边的权值,最终这个问题可以归边表示,公路的长度作为边的权值,最终这个问题可以归 结为网图中,求顶点结为网图中,求顶点 a a 到顶点到顶点 b b 的所有路径中,边的权值的所有路径中,边的权值 之和最少的那条路径。之和最少的那条路径。 关键词:关键词: 最小生成树最小生成树 primprim 算法算法 c+c+语言源程序语言源程序 abstract thethe currcurriculumiculum designdesign requirementsrequirem

3、ents isis givengiven a a regionregion n n city,city, thethe distancedistance betweenbetween thethe netnet withwith thethe primprim algorithmalgorithm toto establishestablish minimumminimum spanningspanning tree,tree, andand calculatedcalculated thethe priceprice ofof minimumminimum spanningspanning

4、tree.tree. citiescities inin thethe regionregion withwith vertexvertex said,said, betweenbetween highwayhighway inin thethe citycity edge,edge, saidsaid thethe lengthlength ofof thethe roadroad asas thethe edgeedge ofof thethe rightright values,values, finallyfinally thethe problemproblem cancan beb

5、e summedsummed upup inin networknetwork diagram,diagram, andand allall thethe pathspaths ofof vertexvertex a a toto b,b, thethe edgeedge ofof thethe weightsweights ofof thethe sumsum ofof thethe minimumminimum path.path. keywords:keywords: minimumminimum spanningspanning treetree primprim algorithma

6、lgorithm c+c+ languagelanguage sourcesource programprogram 目 录 一、问题描述一、问题描述 .4 1.1 题目内容题目内容 .4 1.2 基本要求基本要求 .4 二、需求分析二、需求分析 .4 三、概要设计三、概要设计 .4 3.1 邻接矩阵的建立邻接矩阵的建立.5 3.2 图的建立图的建立 .5 3.3 求最小生成树求最小生成树.6 四、数据结构设计四、数据结构设计.7 五、算法设计五、算法设计 .8 5.1 算法分析算法分析 .8 5.2 算法实现算法实现 .8 六、程序测试与实现六、程序测试与实现 .9 6.1 主程序主程序.9

7、 6.2 测试结果测试结果.10 七、调试分析七、调试分析.10 八、遇到的问题及解决办法八、遇到的问题及解决办法.10 九、心得体会九、心得体会.10 十、附录十、附录 .11 一、一、 问题描述问题描述 1 题目内容:给定一个地区的 n 个城市间的距离网,用 prim 算 法建立最小生成树,并计算得到的最小生成树的代价。 2 基本要求: a) 城市间的距离网采用邻接矩阵表示,若两个城市之间不存 在道路,则将相应边的权值设为自己定义的无穷大值。 (要 求至少 10 个城市,15 条边) b) 最小生成树中包括的边及其权值,并显示得到的最小生成 树的代价。 二、二、 需求分析需求分析 本程序的

8、功能包括图的建立(采用邻接矩阵存储)和求最小生成树。 1图的建立涉及到顶点数组 datan和邻接矩阵 edgenn,顶点 数组运用顺序表完成,操作有:顶点的插入即顺序表的插入;邻接 矩阵的建立,操作有:插入弧和对应的权值,输出邻接矩阵 2最小生成树是通过 prim 算法完成的。prim 里涉及到候选最 短边集,用数组 shortedgen表示候选最短边集,数组元素包括候选 最短边的的邻接点(adjvex)和权值(lowcost)两个域 三、三、 概要设计概要设计 1. 邻接矩阵的建立邻接矩阵的建立 1邻接矩阵的初始化,顶点自己对自己的权值为 0,其余的 权值均为 maxweight(自定义的无

9、穷大,999) adjmatgraph:adjmatgraph(const int sz)/sz 是顶点数,有参构造函数 for(int i=0;isz;i+) for(int j=0;jsz;j+) if(i=j) edgeij=0; else edgeij=maxweight;/无穷大 numofedges=0; 2邻接矩阵中点与点之间有弧的,则将两个顶点之间的权值 设为 weight 取代 maxweight(无穷大,999) void adjmatgraph:insertedge(const int v1,const int v2,int weight)/插入弧, 权为 weight

10、if(v1vertices.listsize()|v2vertices.listsize() cout参数 v1,v2 有误 2endl; exit(0); edgev1v2=weight; edgev2v1=weight; numofedges+; 2. 图的建立图的建立 struct rowcolweight/边信息三元组 int row; int col; int weight; ; void adjmatcreategraph(adjmatgraph for( i=0;in;i+) g.insertvertex(vi);/填充顶点信息 for(i=0;ie;i+) g.inserted

11、ge(ei.row,ei.col,ei.weight); 3. 求最小生成树求最小生成树 struct shortedge int lowcost; int adjvex; ; void adjmatgraph:prim() int k,w,cost=0; for(int i=1;inumofvertices();i+) shortedgei.lowcost=edge0i; shortedgei.adjvex=0; shortedge0.lowcost=0; for(int i=1;inumofvertices();i+) w=maxweight ; for(int j=1;jnumofver

12、tices();j+) if(shortedgej.lowcost!=0 k=j; shortedgek.lowcost=0; for(int j=1;jnumofvertices();j+) if(edgekj shortedgej.lowcost) shortedgej.lowcost=edgekj; shortedgej.adjvex=k; cout最小生成树为:endl; for(int i=1;inumofvertices();i+) coutishortedgei.adjvex- edgeishortedgei.adjvexendl; cost=cost+edgeishortedg

13、ei.adjvex; cout最小生成树代价为:costendl; 四、 数据结构设计数据结构设计 元素类型,结点类型 class seqlist private: datatype datamaxlistsize; int size; public: seqlist(); int listsize()const;/返回元素的个数 size void insert(const datatype /在位置 pos 插入元素 item ; struct rowcolweight/边信息三元组 int row; int col; int weight; ; struct rowcolweight/边

14、信息三元组 int row; int col; int weight; ; class adjmatgraph private: seqlist vertices;/用顺序表存储结点信息 int edgemaxverticesmaxvertices;/用数组存储带权或不带权的边 int numofedges;/边数 shortedge shortedgemaxsize; public: adjmatgraph(const int sz=maxvertices);/有参构造函数,sz 为顶点数 int numofvertices()/返回结点数目 return vertices.listsize

15、(); int numofedge()/返回边数 return numofedges; void insertvertex(const vert /插入结点 vertex void insertedge(const int v1,const int v2,int weight);/插入弧,权为 weight void printmgraph(); void prim(); ; 五、五、 算法设计算法设计 1. 算法分析算法分析 根据用 prim 算法求最小生成树,设 g=(v,e)是具有 n 个顶点的连通 网,t=(u,te)是 g 的最小生成树,t 的初始状态为 u=u0( u0v)), t

16、e= ,重复执行下述操作:在所有 uu,vv-u 的边中找一条代价 最小的边(u,v)并入集合 te,同时 v 并入 u,直至 u=v0,最后计算 得到的最小生成树的代价 2. 算法实现算法实现 void adjmatgraph:prim() int k,w,cost=0; for(int i=1;inumofvertices();i+) shortedgei.lowcost=edge0i; shortedgei.adjvex=0; shortedge0.lowcost=0; for(int i=1;inumofvertices();i+) w=maxweight ; for(int j=1;

17、jnumofvertices();j+) if(shortedgej.lowcost!=0 k=j; shortedgek.lowcost=0; for(int j=1;jnumofvertices();j+) if(edgekj shortedgej.lowcost) shortedgej.lowcost=edgekj; shortedgej.adjvex=k; cout最小生成树为:最小生成树为:endl; for(int i=1;inumofvertices();i+) coutishortedgei.adjvex- edgeishortedgei.adjvexendl; cost=co

18、st+edgeishortedgei.adjvex; cout最小生成树代价为:最小生成树代价为:costendl; 六、六、 程序测试与实现程序测试与实现 1.主程序主程序 void main() adjmatgraph g; char a=a,b,c,d,e,f,g,h,i,j; rowcolweight rcw=0,4,1,0,1,2,4,5,3,0,5,4,1,5,5,5,6,6,1,2,7,2,6,8, 2,7,9,2,3,10,3,7,11,3,9,12,8,9,13,7,8,14,6,7,15; int n=10,e=15;/10 个顶点,个顶点,15 条边条边 adjmatcr

19、eategraph(g,a,n,rcw,e); cout 当前的顶点个数为:当前的顶点个数为: g.numofvertices() endl; cout 当前的边的条数为:当前的边的条数为: g.numofedge() endl; g.printmgraph(); g.prim(); 2.测试结果(测试结果(999 是我自己设置的无穷大)是我自己设置的无穷大) 七、 调试分析调试分析 1图的邻接矩阵是一个 n*n 的矩阵,所以其空间代价是 o(n2) 2在求解最小生成树的过程中,会不断的读取任意两个顶点之 间边的权值,所以采用邻接矩阵 八、 遇到的问题及解决办法遇到的问题及解决办法 在求解利用

20、 prim 求解最小生成树的过程中,算法能够看懂,但 是利用 c+程序去实现,还是有问题的。最后反复看代码,构造了 一个最短候选边集,即数组 shortedgen。 九、九、 心得体会心得体会 本次课程设计用到了顺序表的建立与插入,图的邻接矩阵存储,最本次课程设计用到了顺序表的建立与插入,图的邻接矩阵存储,最 终实现了求终实现了求 n 个个城市城市之间的相同公路之间的最短距离,我主要学到之间的相同公路之间的最短距离,我主要学到 了将实际问题转换为自己所学到的知识,做到了学以致用。了将实际问题转换为自己所学到的知识,做到了学以致用。 十、十、 附录(源代码)附录(源代码) seqlist.h #

21、include using namespace std; class seqlist private: datatype datamaxlistsize; int size; public: seqlist(); int listsize()const;/返回元素的个数返回元素的个数 size void insert(const datatype /在位置在位置 pos 插入元素插入元素 item ; seqlist:seqlist() size=0; int seqlist:listsize()const return size; void seqlist:insert(const data

22、type if(size=maxlistsize) cout顺序表已满无法插入!顺序表已满无法插入!endl; exit(1); if(possize)/当当 pos 等于等于 size 时表示插入在最后时表示插入在最后 cout参数参数 pos 越界出错越界出错!pos;i-) datai=datai-1; datapos=item;/在在 pos 位置插入位置插入 item size+;/数据元素个数数据元素个数 size 加加 1 adjmatgraphlib.h struct rowcolweight/边信息三元组边信息三元组 int row; int col; int weight;

23、 ; void adjmatcreategraph(adjmatgraph for( i=0;in;i+) g.insertvertex(vi);/填充顶点信息填充顶点信息 for(i=0;ie;i+) g.insertedge(ei.row,ei.col,ei.weight); adjmatgraph.h #include const int maxsize=100; struct shortedge int lowcost; int adjvex; ; class adjmatgraph private: seqlist vertices;/用顺序表存储结点信息用顺序表存储结点信息 int

24、 edgemaxverticesmaxvertices;/用数组存储带权或不带权的边用数组存储带权或不带权的边 int numofedges;/边数边数 shortedge shortedgemaxsize; public: adjmatgraph(const int sz=maxvertices);/有参构造函数有参构造函数,sz 为顶点数为顶点数 int numofvertices()/返回结点数目返回结点数目 return vertices.listsize(); int numofedge()/返回边数返回边数 return numofedges; void insertvertex(

25、const vert /插入结点插入结点 vertex void insertedge(const int v1,const int v2,int weight);/插入弧插入弧,权为,权为 weight void printmgraph(); void prim(); ; adjmatgraph:adjmatgraph(const int sz)/sz 是顶点数,有参构造函数是顶点数,有参构造函数 for(int i=0;isz;i+) for(int j=0;jsz;j+) if(i=j) edgeij=0; else edgeij=maxweight;/无穷大无穷大 numofedges

26、=0; void adjmatgraph:insertvertex(const vert void adjmatgraph:insertedge(const int v1,const int v2,int weight)/插入弧插入弧, 权为权为 weight if(v1vertices.listsize()|v2vertices.listsize() cout参数参数 v1,v2 有误有误 2endl; exit(0); edgev1v2=weight; edgev2v1=weight; numofedges+; void adjmatgraph:printmgraph() cout邻接矩阵

27、是:邻接矩阵是:endl; for(int i=0;inumofvertices();i+) for(int j=0;jnumofvertices();j+) coutsetw(10)edgeij; coutendl; coutendl; void adjmatgraph:prim() int k,w,cost=0; for(int i=1;inumofvertices();i+) shortedgei.lowcost=edge0i; shortedgei.adjvex=0; shortedge0.lowcost=0; for(int i=1;inumofvertices();i+) w=maxweight ; for(int j=1;jnumofvertices();j+) if(shortedgej.lowcost!=0 k=j; shorte

温馨提示

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

最新文档

评论

0/150

提交评论