已阅读5页,还剩3页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
数据结构实验报告实验七实验题目: 图的算法学号: 201411130001 日期:2015.11.26班级:计机14.1姓名: 刘方铮Email:实验目的: 1、掌握图的基本概念,描述方法;遍历方法。任务要求:1、创建图类。二叉树的存储结构使用邻接矩阵或链表。2、提供操作:遍历、BFS、DFS3、 对建立好的图,执行上述各操作。4、 输出生成树。5、输出最小生成树。软件环境:Win7 操作系统开发工具:visual C+ 6.0实验代码:#ifndef GRAPH_H#define GRAPH_H#include#include using namespace std;template class edge public: edge(int i,int j,int w) vertex1=i; vertex2=j;weight=w; edge(int i,int j) vertex1=i; vertex2=j;weight=1; T vertex1; T vertex2; T weight;template class graph public: graph() graph() virtual int numberOfVertices()=0; /返回图顶点数 virtual int numberOfEdges()=0; /返回图边数 virtual bool existsEdge(int,int)=0; /如果边(i,j)存在,返回true,否则返回false virtual void insertEdge(edge theEdge)=0; /插入边 virtual void eraseEdge(int,int)=0; /删除边 virtual int degree(int)=0; /返回顶点i的度,只用于无向图 virtual int inDegree(int)=0; /返回顶点i的入度 virtual int outDegree(int)=0; /返回顶点i的出度 virtual bool directed()=0; /若为有向图返回true virtual bool weighted()=0; /若为加权图返回true protected: private:;template class adjacencyWgraph : public graph /加权图的邻接矩阵结构 protected: int n; /顶点个数 int e; /边的个数 T *a; /邻接数组 T noEdge; /表示不存的边 T maxWeight;/最大权 public: adjacencyWgraph(int numberOfVertices =0 , T theNoEdge=0 ) if(numberOfVertices =0; return; n = numberOfVertices; e = 0; noEdge = theNoEdge; maxWeight=0; make2dArry(a,n+1,n+1); adjacencyWgraph() for(int i=0;i=n;i+) delete ai; delete a; a =NULL; int numberOfVertices() return n; int numberOfEdges() return e; bool weighted() return true; bool directed() return false; bool existsEdge(int i,int j) /返回值为真当且仅当(i,j)是图的一条边 if (i1 | jn | jn | aij=noEdge) return false; else return true; void insEdge(int i,int j,int w) /插入边 if(wmaxWeight) maxWeight=w; edge ed(i,j,w); insertEdge(ed); void eraseEdge(int i,int j) /删除边(i,j) if(i=1 & j=1 & i=n & j=n & aij!=noEdge) aij = noEdge; aji = noEdge; e-; bool checkVertex(int theVertex) /确定为有效顶点 if(theVertexn) return false; else return true; int degree(int theVertex)/返回顶点theVertex的度 if(!checkVertex(theVertex) coutno vertextheVertexendl; return -1; int sum=0; for(int i=1;i=n;i+) if(atheVertexi!=noEdge) sum+; return sum; int inDegree(int theVertex) coutinDegree() undefined; return 0; int outDegree(int theVertex) coutoutDegree() undefined; return 0; void output() /输出邻接矩阵 for(int i =1;i=n;i+) for(int j =1;j=n;j+) coutaij ; coutendl; void bfs(int v) int reachn+1; for(int i=1;i=n;i+)reachi=0; bfs1(v,reach,-1); void dfs(int v) int reachn+1; for(int i=1;i=n;i+)reachi=0; dfs1(v,reach,-1); void makeTree(int v) /输出生成树 adjacencyWgraph awg0(n,noEdge); int reachn+1; for(int i=1;i=n;i+)reachi=0; queue *q=new queue(); reachv =-1; q-push(v); while(!q-empty() int w =q-front(); q-pop(); for(int u=1;upush(u); reachu=-1; cout生成树邻接矩阵:endl; awg0.output(); cout生成树bfs:endl; awg0.bfs(v);coutendl; void makeMinTree(int theVertex) /输出最小生成 adjacencyWgraph awg1(n,noEdge); for(int i=1;i=n;i+) for(int j=1;j=n;j+) awg1.aij=aij; int *reach; make2dArry(reach,n+1,n+1); for(int k=1;k=(e-n+1);k+)/删除多余的边 awg1.findMaxWeight(maxWeight,reach,-1); for(int h=0;h=n;h+) delete reachh; delete reach; reach =NULL; cout最小生成树邻接矩阵:endl; awg1.output(); cout最小生成树bfs:endl; awg1.bfs(theVertex); void findMaxWeight(int w,int *&reach,int lable) /找到“最大权”,删除边 int num=noEdge,row,col; for(int i=1;i=n;i+) /找到“最大权”边 for(int j=1;j=num & reachij!=lable & aij=w) num=aij; row=i;col=j; reachrowcol=lable; reachcolrow=lable; if(degree(row)=1 | degree(col)=1)/边不能删 findMaxWeight(num,reach,lable); else /删除边 eraseEdge(row,col); private: void make2dArry(T * &x,int rows,int clos) /创建二维数组 x = new T *rows; /创建行指针 for(int i=0;irows ;i+) /为每一行分配空间 xi=new T clos; for(int j=0; jrows;j+) /初始化邻接矩阵 for(int k=1;k=clos;k+) xjk = noEdge; void insertEdge(edge theEdge) /插入边,如果该边已存在,则修改权值 int v1=theEdge.vertex1; int v2=theEdge.vertex2; if(v11 | v2n | v2n | v1=v2) cout (v1,v2) is not a permissible edge; return; if(av1v2 = noEdge)/新的边 e+; av1v2 = theEdge.weight; av2v1 = theEdge.weight; void bfs1(int v,int reach,int lable) /广度先搜索,reachi用来标记所有邻接顶点v的可达到顶点 queue *q=new queue(); reachv =lable; q-push(v); while(!q-empty() int w =q-front(); q-pop(); coutw ; for(int u=1;upush(u); reachu=lable; void dfs1(int v,int reach,int lable) reachv=lable;coutv ; int i=1; while(avi=noEdge | reachi=lable) i+; if(in+1) v=i; dfs1(v,reach,lable); ;#endif / GRAPH_H#include #include using namespace std;int main() adjacencyWgraph awg(8,0); awg.insEdge(1,2,5); awg.insEdge(1,3,4); awg.insEdge(1,4,10); awg.insEdge(2,5,30); awg.insEdge(3,5,25); awg.insEdge(4,6,2); awg.insEdge(4,7,60); awg.insE
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025西安市第五十八中学教师招聘笔试考试参考题库附答案解析
- 2025复旦大学附属妇产科医院招聘妇产科专培医生笔试考试备考试题及答案解析
- 陕西航空职业技术学院《列车运行控制系统》2024-2025学年第一学期期末试卷
- 高速公路枢纽互通工程实施方案
- 互联网公司用户增长策略与渠道计划
- 2026年中国航空服务项目经营分析报告
- 社交媒体营销方案及内容创作指南
- 2026-2031年中国智能指套行业市场发展趋势与前景展望战略研究报告
- 2025广西百色那坡县水电水土保持站就业见习基地招募就业见习人员1人笔试考试备考试题及答案解析
- ISO9001质量管理体系内部审核实务
- 2025年大学《应用气象学》专业题库- 气象健康风险评估与社会管理
- 2025年湖南机电职业技术学院单招职业技能考试题库及参考答案详解
- 酒店浴室安全管理制度
- 2026年江西电力职业技术学院单招职业技能考试必刷测试卷及答案1套
- 防诈骗宣传活动方案
- 养猪场除臭方案
- 湖北省黄冈市黄梅县育才高级中学2025-2026学年高一上学期10月期中考试政治试题(解析版)
- 供电营业规则培训
- 新版银行工资代发业务操作规范
- 中国远洋海运集团航运先进技术研究院招聘笔试真题2024
- (2025)职业生涯规划考试试题及答案
评论
0/150
提交评论