数据结构实验-图及其应用_第1页
数据结构实验-图及其应用_第2页
数据结构实验-图及其应用_第3页
数据结构实验-图及其应用_第4页
数据结构实验-图及其应用_第5页
已阅读5页,还剩4页未读 继续免费阅读

下载本文档

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

文档简介

数据结构实验报告三实验名称:图及其应用实验目的及实验要求通过完成本实验,掌握图的两种基本的存储结构(邻接矩阵、邻接表),以及图的基本算法实现(建立、遍历),并能运用图结构分析解决一些实际问题。本实验训练的要点是:图的两种基本存储结构,及各种操作的算法实现(建立、遍历、图的典型应用)。2实验内容及实验步骤(附运行结果截屏)建立无向图和有向图的邻接矩阵存储,计算顶点的度,并输出图的基本信息。建立有向图的邻接表存储表示,并根据存储计算顶点的出度和入度,然后输出图的基本信息。编写完整的程序实现AOV网的拓扑排序。编程求AOE网的关键路径。编程实现单源点最短路径的Dijkstra算法。注:(1)~(2)必做,(3)~(5)选做。实验步骤: 总体来说,先编写类模板,实现各自的基础结构,之后按照要求编写适当的函数方法(公共接口),最后完成封装。编写主函数直接调用。但这一次考虑到图的处理方式与以往表和树的不同,并没有把所有功能都与类模板绑定到一起而是灵活地选择了合适的处理方式。核心代码://GraphMatrix.h邻接矩阵表示图//类的声明template<classT>classGraphMatrix{public:GraphMatrix(intp=0,inte=0):point_num(p),edge_num(e){};boolInsertPoint(charx);voidPrintGraph();protected:charpoint_name[maxn];doublegra[maxn][maxn];intpoint_num,edge_num;};//插入节点template<classT>boolGraphMatrix<T>::InsertPoint(charx){if(point_num==0){point_num=1;point_name[0]=x;}else{point_name[point_num]=x;for(inti=0;i<point_num;i++){cin>>gra[point_num][i];if(gra[point_num][i]!=0)edge_num++;}point_num++;}}//GraphChart.h邻接表表示图//类的声明template<classNameType,classDistType>classGraph;template<classNameType,classDistType>classVertex{public:Vertex(){padjEdge=NULL;m_vertexName=0;}~Vertex(){Edge<DistType>*pmove=padjEdge;while(pmove){padjEdge=pmove->m_pnext;deletepmove;pmove=padjEdge;}}private:friendclassGraph<NameType,DistType>;NameTypem_vertexName;Edge<DistType>*padjEdge;};template<classNameType,classDistType>classGraph{public:Graph(intsize=m_nDefaultSize){m_pVertexTable=newVertex<NameType,DistType>[size];if(m_pVertexTable==NULL){exit(1);}m_numVertexs=0;m_nmaxSize=size;m_nnumEdges=0;}~Graph(){Edge<DistType>*pmove;for(inti=0;i<this->m_numVertexs;i++){pmove=this->m_pVertexTable[i].padjEdge;if(pmove){this->m_pVertexTable[i].padjEdge=pmove->m_pnext;deletepmove;pmove=this->m_pVertexTable[i].padjEdge;}}delete[]m_pVertexTable;}intGetNumEdges(){returnm_nnumEdges/2;}intGetNumVertexs(){returnm_numVertexs;}boolIsGraphFull()const{//图满的?returnm_nmaxSize==m_numVertexs;}boolInsertEdge(intv1,intv2,DistTypeweight=m_Infinity);boolInsertVertex(constNameTypevertex);voidPrintGraph();private:Vertex<NameType,DistType>*m_pVertexTable;intm_numVertexs;intm_nmaxSize;staticconstintm_nDefaultSize=10;staticconstDistTypem_Infinity=65536;intm_nnumEdges;intGetVertexPosTable(constNameTypevertex);};//Dijkstra算法voidDijkstra(intn,intv,int*dist,int*prev,intc[maxnum][maxnum]){bools[maxnum];for(inti=1;i<=n;++i){dist[i]=c[v][i];s[i]=0;if(dist[i]==maxint)prev[i]=0;elseprev[i]=v;}dist[v]=0;s[v]=1;for(inti=2;i<=n;++i){inttmp=maxint;intu=v;for(intj=1;j<=n;++j)if((!s[j])&&dist[j]<tmp){u=j;tmp=dist[j];}s[u]=1;for(intj=1;j<=n;++j)if((!s[j])&&c[u][j]<maxint){intnewdist=dist[u]+c[u][j];if(newdist<dist[j]){dist[j]=newdist;prev[j]=u;}}}}//AOLinttopGraph(graphg){ EdgeNode*e; inti,k,gettop; inttop=0; intcount=0; int*stack; stack=(int*)malloc(g->numVertexes*sizeof(int)); for(i=0;i<g->numVertexes;i++) { if(g->headlist[i].in==0)//把入度为0的,即没有入度的点入栈 stack[++top]=i; } while(top) { gettop=stack[top--]; printf("%d",gettop); count++; for(e=g->headlist[gettop].fnode;e;e=e->next)//一次遍历链表,减少各个子节点的入度 { k=e->data; if(!(--g->headlist[k].in)) stack[++top]=

温馨提示

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

评论

0/150

提交评论