构造可以使n个城市连接的最小生成树.docx

上传人:b****5 文档编号:12152440 上传时间:2023-04-17 格式:DOCX 页数:11 大小:26.44KB
下载 相关 举报
构造可以使n个城市连接的最小生成树.docx_第1页
第1页 / 共11页
构造可以使n个城市连接的最小生成树.docx_第2页
第2页 / 共11页
构造可以使n个城市连接的最小生成树.docx_第3页
第3页 / 共11页
构造可以使n个城市连接的最小生成树.docx_第4页
第4页 / 共11页
构造可以使n个城市连接的最小生成树.docx_第5页
第5页 / 共11页
点击查看更多>>
下载资源
资源描述

构造可以使n个城市连接的最小生成树.docx

《构造可以使n个城市连接的最小生成树.docx》由会员分享,可在线阅读,更多相关《构造可以使n个城市连接的最小生成树.docx(11页珍藏版)》请在冰豆网上搜索。

构造可以使n个城市连接的最小生成树.docx

构造可以使n个城市连接的最小生成树

《数据结构》

课程设计报告

 

设计题目:

构造可以使n个城市连接的最小生成树

姓名:

学号:

专业:

物联网工程(嵌入式培养)

院系:

计算机技术与科学学院

班级:

1405

指导教师:

 

2016年01月09日

摘要

本次课程设计的要求是给定一个地区的n个城市间的距离网,用Prim算法建立最小生成树,并计算得到的最小生成树的代价。

将该地区的城市用顶点表示,城市间的公路用边表示,公路的长度作为边的权值,最终这个问题可以归结为网图中,求顶点A到顶点B的所有路径中,边的权值之和最少的那条路径。

关键词:

最小生成树

Prim算法

C++语言源程序

Abstract

Thecurriculumdesignrequirementsisgivenaregionncity,thedistancebetweenthenetwiththePrimalgorithmtoestablishminimumspanningtree,andcalculatedthepriceofminimumspanningtree.Citiesintheregionwithvertexsaid,betweenhighwayinthecityedge,saidthelengthoftheroadastheedgeoftherightvalues,finallytheproblemcanbesummedupinnetworkdiagram,andallthepathsofvertexAtoB,theedgeoftheweightsofthesumoftheminimumpath.

Keywords:

minimumspanningtree

Primalgorithm

C++languagesourceprogram

一、问题描述4

题目内容4

基本要求4

二、需求分析4

三、概要设计4

邻接矩阵的建立5

图的建立5

求最小生成树6

四、数据结构设计7

五、算法设计8

算法分析8

算法实现8

六、程序测试与实现9

主程序9

测试结果10

七、调试分析10

八、遇到的问题及解决办法10

九、心得体会10

十、附录11

 

一、问题描述

1.题目内容:

给定一个地区的n个城市间的距离网,用Prim算法建立最小生成树,并计算得到的最小生成树的代价。

2.基本要求:

a)城市间的距离网采用邻接矩阵表示,若两个城市之间不存在道路,则将相应边的权值设为自己定义的无穷大值。

(要求至少10个城市,15条边)

b)最小生成树中包括的边及其权值,并显示得到的最小生成树的代价。

二、需求分析

本程序的功能包括图的建立(采用邻接矩阵存储)和求最小生成树。

1 图的建立涉及到顶点数组data[n]和邻接矩阵Edge[n][n],顶点数组运用顺序表完成,操作有:

顶点的插入即顺序表的插入;邻接矩阵的建立,操作有:

插入弧和对应的权值,输出邻接矩阵

2 最小生成树是通过Prim算法完成的。

Prim里涉及到候选最短边集,用数组shortEdge[n]表示候选最短边集,数组元素包括候选最短边的的邻接点(adjvex)和权值(lowcost)两个域

三、概要设计

1.邻接矩阵的建立

1 邻接矩阵的初始化,顶点自己对自己的权值为0,其余的权值均为MaxWeight(自定义的无穷大,999)

AdjMatGraph:

:

AdjMatGraph(constintsz)ow,E[i].col,E[i].weight);

}

2.求最小生成树

structshortEdge

{

intlowcost;

intadjvex;

};

voidAdjMatGraph:

:

Prim()

{

intk,w,cost=0;

for(inti=1;inumOfVertices();i++)

{

shortEdge[i].lowcost=Edge[0][i];

shortEdge[i].adjvex=0;

}

shortEdge[0].lowcost=0;

for(inti=1;inumOfVertices();i++)

{

w=MaxWeight;

for(intj=1;jnumOfVertices();j++)

{

if(shortEdge[j].lowcost!

=0&&shortEdge[j].lowcost

{

w=shortEdge[j].lowcost;

k=j;

}

}

shortEdge[k].lowcost=0;

for(intj=1;jnumOfVertices();j++)

{

if(Edge[k][j]

{

shortEdge[j].lowcost=Edge[k][j];

shortEdge[j].adjvex=k;

}

}

}

cout<<"最小生成树为:

"<

for(inti=1;inumOfVertices();i++)

{

cout<"<

cost=cost+Edge[i][shortEdge[i].adjvex];

}

cout<<"最小生成树代价为:

"<

}

四、数据结构设计

元素类型,结点类型

classSeqList

{

private:

DataTypedata[MaxListSize];

intsize;

public:

SeqList();

intListSize()const;owcost=Edge[0][i];

shortEdge[i].adjvex=0;

}

shortEdge[0].lowcost=0;

for(inti=1;inumOfVertices();i++)

{

w=MaxWeight;

for(intj=1;jnumOfVertices();j++)

{

if(shortEdge[j].lowcost!

=0&&shortEdge[j].lowcost

{

w=shortEdge[j].lowcost;

k=j;

}

}

shortEdge[k].lowcost=0;

for(intj=1;jnumOfVertices();j++)

{

if(Edge[k][j]

{

shortEdge[j].lowcost=Edge[k][j];

shortEdge[j].adjvex=k;

}

}

}

cout<<"最小生成树为:

"<

for(inti=1;inumOfVertices();i++)

{

cout<"<

cost=cost+Edge[i][shortEdge[i].adjvex];

}

cout<<"最小生成树代价为:

"<

}

五、程序测试与实现

1.主程序

voidmain()

{

AdjMatGraphg;

chara[]={'A','B','C','D','E','F','G','H','I','J'};

RowColWeightrcw[]={{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}};

intn=10,e=15;

ow,E[i].col,E[i].weight);

}

#include

constintMaxSize=100;

structshortEdge

{

intlowcost;

intadjvex;

};

classAdjMatGraph

{

private:

SeqListVertices;owcost=Edge[0][i];

shortEdge[i].adjvex=0;

}

shortEdge[0].lowcost=0;

for(inti=1;inumOfVertices();i++)

{

w=MaxWeight;

for(intj=1;jnumOfVertices();j++)

{

if(shortEdge[j].lowcost!

=0&&shortEdge[j].lowcost

{

w=shortEdge[j].lowcost;

k=j;

}

}

shortEdge[k].lowcost=0;

for(intj=1;jnumOfVertices();j++)

{

if(Edge[k][j]

{

shortEdge[j].lowcost=Edge[k][j];

shortEdge[j].adjvex=k;

}

}

}

cout<<"最小生成树为:

"<

for(inti=1;inumOfVertices();i++)

{

cout<"<

cost=cost+Edge[i][shortEdge[i].adjvex];

}

cout<<"最小生成树代价为:

"<

}

#include

typedefcharDataType;

constintMaxListSize=100;

constintSeqQMaxSize=100;

#include""

constintMaxVertices=100;

constintMaxWeight=999;

typedefcharVerT;

#include""

#include""

usingnamespacestd;

voidmain()

{

AdjMatGraphg;

chara[]={'A','B','C','D','E','F','G','H','I','J'};

RowColWeightrcw[]={{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}};

intn=10,e=15;//10个顶点,15条边

AdjMatCreateGraph(g,a,n,rcw,e);

cout<<"当前的顶点个数为:

"<<()<

cout<<"当前的边的条数为:

"<<()<

();

();

}

 

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 高中教育 > 理化生

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

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