数据结构图的操作.docx
实验7:图的操作算法一、实验目的1 .熟悉各种图的存储结构。2 .掌握图的各种搜索路径的遍历方法。二、实验内容L设计一个有向图和一个无向图,任选一种存储结构,完成有向图和无向图的DFS(深度优先遍历)和BFS(广度优先遍历)的操作。算法:#include<iostream>#include<math.h>#include<stdio.h>#include<malloc.h>#defineMAXV10#defineINF980708usingnamespacestd;typedefintInfoType;邻接矩阵typedefstruct(intno;InfoTypeinfo;JVertexType;顶点类型typedefstruct(intedgesMAXVMAXV;intnze;VertexTypeve×sMAXV;MatGraph;完整的图邻接矩阵类型邻接表typedefstructANode(intadjve×structANode*nextarc;intweight;ArCNOde;边结点类型typedefstructVnodeInfoTypeinfo;ArcNode*firstarc;VNode;邻接表的头结点类型typedefstruct(VNodeadjlistMAXV;intn,e;AdjGraph;完整的图邻接表类型typedefstruct(InfoTypedataMAXV;intfront,rear;SqQueue;队列初始化队列voidInitQUeUe(SqQUeUe*&q)(q=(SqQueue*)malloc(sizeof(SqQueue);q->front=q->rear=-l;)判断队列是否为空boolQueueEmptyfSqQueue*q)(return(q->front=q->rear);)进队列booleQueue(SqQueue*&q,InfoTypee)(if(q->rear=MAXV)returnfalse;q->rear+;q->dataq->rear=e;returntrue;)出队列booldeQueue(SqQueue*&q,InfoType&e)(if(q->front=q->rear)returnfalse;q->front=(q->front+l)%MAXV;e=q->dataq->front;returntrue;)创建邻接表voidCreateAdj(AdjGraph*&GJntAMAXVMAXVJntnjnte)(intij;ArcNode*p;G=(AdjGraph*)malloc(sizeof(AdjGraph);for(i=0;i<n;i+)G->adjlisti.firstarc=NULL;for(i=0;i<n;i+)for(j=n-lj>=OJ-)if(Aij!=O&&Ai0!=INF)p=(ArcNode*)malloc(sizeof(ArcNode);p->adjvex=j;p->nextarc=G->adjIisti.firstarc;G->adjlisti.firstarc=p;)G->n=n;G->e=e;)输出邻接表voidDispAdj(AdjGraph*G)(COUt<<“邻接表存储:"<<endl;inti;ArcNode*p;for(i=0;i<G->n;i+)(p=G->adjlisti.firstarc;printf("%3d"zi);printf("%3d->,J);while(p!=NULL)(printf("%3d->,p->adjvex);p=p->nextarc;)cout«"A"«endl;)/DFS深度优先遍历intvisitedMAXV=0;voidDFSfAdjGraph*G,intv)(ArcNode*p;visitedv=l;cout<<v;p=G->adjlistv.firstarc;while(p!=NULL)(if(visitedp->adjvex=O)DFS(G,p->adjvex);p=p->nextarc;)/BFS广度优先遍历voidBFS(AdjGraph*GJntv)(intWjjArcNode*p;SqQueue*qu;InitQueue(qu);intvisitedlMAXV;for(inti=0;i<G->n;i+)visitedli=O;printf("%2d',v);visitedlv=l;enQueue(qu,v);while(!QueueEmpty(qu)(deQueue(quzw);p=G->adjlistw.firstarc;while(p!=NULL)(if(visitedlp->adjvex=O)(printf("%2d",p->adjvex);visitedlp->adjvex=l;eQueue(qu,p->adjvex);p=p->nextarc;)cout<<endl;)销毁邻接表voidDestroyAdj(AdjGraph*&G)(inti;ArcNode*pre,*p;for(i=0;i<G->n;i+)(pre=G->adjlisti.firstarc;if(pre!=NULL)(p=pre->nextarc;while(p!=NULL)(free(pre);pre=p;p=p->nextarc;free(pre);)free(G);)创建邻接矩阵voidCreatMat(MatGraph*&GJntnjnte)(intijljjlznuml,num2;G=(MatGraph*)malloc(sizeof(MatGraph);邻接矩阵初始化for(i=0;i<n;i+)(for(j=0;j<n;j+)G->edgesij=O;)顶点信息COUt<<"请输入顶点编号"<<endl;for(i=0;i<n;i+)cin>>G->vexsi.no;边信息for(i=0;i<e;i+)(COUt<<”请输入起始端点编号和终止端点编号*<endl;cin>>il>>jl;for(j=0;j<n;j+)(if(G->vexsj.no=il)numl=j;if(G->vexsj.no=jl)num2=j;)G->edgesnumlnum2=l;)G->n=n;G->e=e;)输出邻接矩阵voidDispMat(MatGraph*G)(cout<<”邻接矩阵存储:"<<endl;cout<<"tt"for(inti=0;i<G->n;i+)cout<<G->vexsi.no<<"t"cout<<endl;for(inti=0;i<G->n;i+)(cout<<"t"<<G->vexsi.no<<"t"for(intj=O;j<G->n;j+)cout<<G->edgesij<<"t"cout<<endl;)intmain()(intnze,½vl;MatGraph*G;AdjGraph*p;COUt<<"请输入顶点个数"<<endl;cin>>n;COUt<<"请输入边的个数"<<endl;cin>>e;CreatMat(G,nze);DispMat(G);CreateAdj(p,G->edges,nze);DispAdj(p);COUt<<"进行DFS深度优先遍历,请输入初始点:cin>>v;DFS(pzv);cout<<endl;COUt<<"进行BFS广度优先遍历,请输入初始点:cin>>vl;BFS(p,vl);COUt<<"销毁图"<<endl;DestroyAdj(p);)测试数据:有向图:无向图:12345101110200010300001400100501000邻接表存储:0:0->IE ->2->3->'1:13运行结果:请输入顶点个数3输入边的个数1输入顶点编号12345请输入起始端点编号和终止端点编号13请输入起始端点编号和终止端点编号14请输入起始端点编号和终止端点编号12请输入起始端点编号和终止端点编号43请输入起始端点编号和终止端点编号2 4请输入起始端点编号和终止端点编号3 5请输入起始端点编号和终止端点编号5 2邻接矩阵存储:2->3->请输入初始点:1请输入初始点:1>>>>>,-STJTJ-IJnJTJLLLLL1342It>>>>>t:-S/(mlIi-J深1o1234S接O.1:2:3:4:F4存在问题:无2.求两点之间最短路径。算法设计:求两点之间最短路径。#include<iostream>#include<math.h>#include<stdio.h>#include<malloc.h>#defineINF32767#defineMAXVIOOusingnamespacestd;typedefcharInfoType;以下定义邻接矩阵类型typedefstructintno;顶点编号InfoTypeinfo;顶点其他信息VertexType;typedefstruct顶点类型intedgesMAXVMAXV;邻接矩阵数组intnze;顶点数,边数VertexTypevexsMAXV;存放顶点信息MatGraph;完整的图邻接矩阵类型voidCreateMat(MatGraph&g,intAMAXVMAXVJntnjnte)仓IJ建图的邻接矩阵inti,j;g.n=n;g.e=e;for(i=0;i<g,n;i+)for(j=O;j<g.n;j+)g.edgesij=Aij;voidDispMat(MatGraphg)输出邻接矩阵ginti,j;for(i=0;i<g,n;i+)for(j=O;j<g,n;j+)if(g.edgesij!=INF)printf("%4d,g.edgesi