1、实验报告实验六 图的应用及其实现一、实验目的1进一步功固图常用的存储结构。2熟练掌握在图的邻接表实现图的基本操作。3理解掌握 AOV 网、AOE 网在邻接表上的实现以及解决简单的应用问题。二、实验内容 一. 基础题目:( 本类题目属于验证性的,要求学生独立完成) 题目一:从键盘上输入 AOV 网的顶点和有向边的信息, 建立其邻接表存储结构,然后对该图拓扑排序,并输出拓扑序列. 试设计程序实现上述 AOV 网的类型定义和基本操作,完成上述功能。题目二:从键盘上输入 AOE 网的顶点和有向边的信息, 建立其邻接表存储结构,输出其关键路径和关键路径长度。 试设计程序实现上述 AOE 网类型定义和基本
2、操作,完成上述功能。测试数据:教材图 7.29 【题目五】连通 OR 不连通描述:给定一个无向图,一共 n 个点,请编写一个程序实现两种操作:D x y 从原图中删除连接 x,y 节点的边。Q x y 询问 x,y 节点是否连通 输入第一行两个数 n,m(5adjvex!=wp=p-nextarc;p-nextarc=NULLreturn p-nextarc-adjvex;Return -1;利用邻接表存储结构构造有向图 void CreateGraph(ALGraph 输入顶点值存入图中i=1is; a=LocateVertices(G,s);cins; b=LocateVertices(G
3、,s);构造结点 p=new ArcNode;将此结点插入适当的位置p-adjvex=b;p-nextarc=G.verticesa.firstarc;G.verticesa.firstarc=p;i+结束查找图中位置 v 的第一个邻接点在图中所在的位置int FirstAdjVex(ALGraph G,int v)是否对图进行拓扑排序,并输出相应的拓扑序列int TopologicalSort(ALGraph G)是否否开始G.verticesv.firstarc=NULLreturn G.verticesv.firstarc-adjvex;return -1;开始求各定点入度 FindIn
4、Degree(G,indegree);找到入度为 0 的顶点并入栈栈是否为空栈顶元素出栈并输出 i;计数器 count+;求 i 顶点的第一个邻接点 wW 大于等于 0 吗if(!(-indegreew)PushStack(stack,w);求下一邻接点 w=NextAdjVex(G,i,w)if(count#include#define MAX_VERTEX_NUM 20typedef struct ArcNode/弧结点定义int adjvex; /该弧所指向的顶点的位置struct ArcNode *nextarc;/指向下一条弧的指针/InfoType *info;ArcNode;ty
5、pedef struct VNode/顶点结点定义char data; /顶点信息ArcNode *firstarc; /指向第一条依附该顶点的弧的指针VNode,AdjListMAX_VERTEX_NUM;typedef struct/图的定义AdjList vertices;int vexnum,arcnum; /图的当前顶点数和弧数int kind; /图的种类标志ALGraph;ALGraph G; /定义图变量bool visitedMAX_VERTEX_NUM; /访问标志数组int indegree20; /各结点入度数组struct Stack/栈类型定义int s21;int
6、 top;Stack stack; /定义一个栈int LocateVertices(ALGraph G,char a)/查找字符 a 在图中的位置for(int i=0;iG.vexnumG.arcnum;coutG.verticesi.data;G.verticesi.firstarc=NULL;couts;a=LocateVertices(G,s);cins;b=LocateVertices(G,s);p=new ArcNode;if(!p)return;p-adjvex=b;p-nextarc=G.verticesa.firstarc;G.verticesa.firstarc=p;in
7、t FirstAdjVex(ALGraph G,int v)/查找图中位置 v 的第一个邻接点在图中所在的位置if(G.verticesv.firstarc)return G.verticesv.firstarc-adjvex;return -1;int NextAdjVex(ALGraph G,int v,int w)/查找相对于图中位置 v 的邻接点 w 的下一邻接点在图中的位置ArcNode *p;p=G.verticesv.firstarc;while(p-adjvex!=w)p=p-nextarc;if(p-nextarc)return p-nextarc-adjvex;return
8、 -1;void DestroyGraph(ALGraph for(int i=0;inextarc;while(p)delete p;p=p1;if(p1)p1=p1-nextarc;void InitStack(Stack void PushStack(Stack void PopStack(Stack int StackEmpty(Stack stack)/判断栈是否为空,若为空则返回 1,否则返回 0if(!stack.top)return 1;return 0;void FindInDegree(ALGraph G,int *indegree)/求图中各个顶点的入度,并相应的存入入度
9、数组中 indegreeArcNode *p;for(int i=0;iadjvex+;p=p-nextarc;int TopologicalSort(ALGraph G)/对图进行拓扑排序,并输出相应的拓扑序列int count=0,w;FindInDegree(G,indegree);for(int i=0;i=0;w=NextAdjVex(G,i,w)if(!(-indegreew)PushStack(stack,w);if(count#include#define MAX_VERTEX_NUM 20typedef struct ArcNode/弧结点定义int adjvex; /该弧所
10、指向的顶点的位置struct ArcNode *nextarc;/指向下一条弧的指针int info;ArcNode;typedef struct VNode/顶点结点定义char data; /顶点信息ArcNode *firstarc; /指向第一条依附该顶点的弧的指针VNode,AdjListMAX_VERTEX_NUM;typedef struct/图的定义AdjList vertices;int vexnum,arcnum; /图的当前顶点数和弧数int kind; /图的种类标志ALGraph;ALGraph G; /定义图变量bool visitedMAX_VERTEX_NUM;
11、 /访问标志数组int indegree20; /各结点入度数组int ve20; /定义顶点最早开始时间存储数组int vl20; /定义顶点最迟开始时间存储数组struct Stack/栈类型定义int s21;int top;Stack stack; /定义一个栈,用于拓扑排序时存储入度为 0 的顶点Stack stack1; /存储逆拓扑排序有序序列Stack stack2; /存储关键路径int LocateVertices(ALGraph G,char a)/查找字符 a 在图中的位置for(int i=0;iG.vexnumG.arcnum;coutG.verticesi.dat
12、a;G.verticesi.firstarc=NULL;couts;a=LocateVertices(G,s);cins;b=LocateVertices(G,s);p=new ArcNode;if(!p)return;p-adjvex=b;coutp-info;p-nextarc=G.verticesa.firstarc;G.verticesa.firstarc=p;void DestroyGraph(ALGraph for(int i=0;inextarc;while(p)delete p;p=p1;if(p1)p1=p1-nextarc;void InitStack(Stack void
13、 PushStack(Stack void PopStack(Stack int StackEmpty(Stack stack)/判断栈是否为空,若为空则返回 1,否则返回 0if(!stack.top)return 1;return 0;int GetTopStack(Stack stack)/获得栈顶元素return stack.sstack.top-1;void FindInDegree(ALGraph G,int *indegree)/求图中各个顶点的入度,并相应的存入入度数组中 indegreeArcNode *p;for(int i=0;iadjvex+;p=p-nextarc;i
14、nt TopologicalSort(ALGraph G)/对图进行拓扑排序,并输出相应的拓扑序列int count=0;ArcNode *p;FindInDegree(G,indegree);InitStack(stack);InitStack(stack1);for(int i=0;inextarc)if(!(-indegreep-adjvex)PushStack(stack,p-adjvex);if(vei+p-infovep-adjvex)vep-adjvex=vei+p-info;if(countnextarc)if(vlp-adjvex-p-infoadjvex-p-info;fo
15、r(j=0;jnextarc)tag=(vej=vlp-adjvex-p-info)?*: ;coutadjvex.datainfoadjvex-p-adjvexadjvex);sum+=p-info;cout#include#define MAX_VERTEX_NUM 20typedef struct ArcNodeint adjvex; /该弧所指向的顶点的位置struct ArcNode *nextarc;/指向下一条弧的指针/InfoType *info;ArcNode;typedef struct VNodechar data; /顶点信息ArcNode *firstarc; /指向
16、第一条依附该顶点的弧的指针VNode,AdjListMAX_VERTEX_NUM;typedef structAdjList vertices;int vexnum,arcnum; /图的当前顶点数和弧数int kind; /图的种类标志ALGraph;ALGraph G; /定义图变量bool visitedMAX_VERTEX_NUM; /访问标志数组int num=0;bool found=false;int LocateVertices(ALGraph G,char a)/查找字符 a 在图中的位置for(int i=0;i=0)p=G.verticesLocateVertices(G
17、,a).firstarc;while(pif(p)return 1;return 0;return 0;void CreateGraph(ALGraph char s1,s2;ArcNode *p;for(int i=0;is1;a=LocateVertices(G,s1);if(as2;b=LocateVertices(G,s2);if(badjvex=b;p-nextarc=G.verticesa.firstarc;G.verticesa.firstarc=p;p=new ArcNode;if(!p)return;p-adjvex=a;p-nextarc=G.verticesb.first
18、arc;G.verticesb.firstarc=p;int FirstAdjVex(ALGraph G,int v)/查找图中位置 v 的第一个邻接点在图中所在的位置if(G.verticesv.firstarc)return G.verticesv.firstarc-adjvex;return -1;int NextAdjVex(ALGraph G,int v,int w)/查找相对于图中位置 v 的邻接点 w 的下一邻接点在图中的位置ArcNode *p;p=G.verticesv.firstarc;while(p-adjvex!=w)p=p-nextarc;if(p-nextarc)r
19、eturn p-nextarc-adjvex;return -1;void DestroyGraph(ALGraph for(int i=0;inextarc;while(p)delete p;p=p1;if(p1)p1=p1-nextarc;void DFSearch(ALGraph G,int i,int s)/查找 i 与 s 之间是否有路径,以此来判断二者是否连通,若连通found=true,否则 found=falseint w;/ for(int x=0;x=0w=NextAdjVex(G,i,w)if(w=s)found=true;visiteds=true;else if(!v
20、isitedw)DFSearch(G,w,s);void DeleteArc(ALGraph if(LocateVertices(G,a)=0)p1=G.verticesLocateVertices(G,a).firstarc;while(p1p1=p1-nextarc;if(!p1)return;if(p1=G.verticesLocateVertices(G,a).firstarc)G.verticesLocateVertices(G,a).firstarc=p1-nextarc;free(p1);elsep-nextarc=p1-nextarc;free(p1);return;void
21、main()char a,b,k;int n;cinG.vexnumG.arcnum;CreateGraph(G);cinn;for(int i=1;ik;switch(k)case Q:cinab;int x;for(x=0;xab;DeleteArc(G,a,b);DeleteArc(G,b,a);break;default:cout#include#define MAX_VERTEX_NUM 28typedef struct ArcNode/弧结点定义int adjvex; /该弧所指向的顶点的位置struct ArcNode *nextarc;/指向下一条弧的指针/InfoType *
22、info;ArcNode;typedef struct VNode/顶点结点定义char data; /顶点信息ArcNode *firstarc; /指向第一条依附该顶点的弧的指针VNode,AdjListMAX_VERTEX_NUM;typedef struct/图的定义AdjList vertices;int vexnum,arcnum; /图的当前顶点数和弧数int kind; /图的种类标志ALGraph;ALGraph G; /定义图变量bool visitedMAX_VERTEX_NUM; /访问标志数组int indegree28; /各结点入度数组int num=0; /记录
23、顶点字符种类数char a28; /存储拓扑排序序列struct Stack/栈类型定义int s21;int top;Stack stack; /定义一个栈int LocateVertices(ALGraph G,char a)/查找字符 a 在图中的位置for(int i=0;i=0)p=G.verticesLocateVertices(G,a).firstarc;while(pif(p)return 1;return 0;return 0;void CreateGraph(ALGraph char s1,s2;ArcNode *p;for(int i=0;is1;a=LocateVert
24、ices(G,s1);if(as2;cins2;b=LocateVertices(G,s2);if(badjvex=b;p-nextarc=G.verticesa.firstarc;G.verticesa.firstarc=p;int FirstAdjVex(ALGraph G,int v)/查找图中位置 v 的第一个邻接点在图中所在的位置if(G.verticesv.firstarc)return G.verticesv.firstarc-adjvex;return -1;int NextAdjVex(ALGraph G,int v,int w)/查找相对于图中位置 v 的邻接点 w 的下一
25、邻接点在图中的位置ArcNode *p;p=G.verticesv.firstarc;while(p-adjvex!=w)p=p-nextarc;if(p-nextarc)return p-nextarc-adjvex;return -1;void DestroyGraph(ALGraph for(int i=0;inextarc;while(p)delete p;p=p1;if(p1)p1=p1-nextarc;void InitStack(Stack void PushStack(Stack void PopStack(Stack int StackEmpty(Stack stack)/判
26、断栈是否为空,若为空则返回 1,否则返回 0if(!stack.top)return 1;return 0;void FindInDegree(ALGraph G,int *indegree)/求图中各个顶点的入度,并相应的存入入度数组中 indegreeArcNode *p;for(int i=0;iadjvex+;p=p-nextarc;int TopologicalSort(ALGraph G)/对图进行拓扑排序,并存储相应的拓扑序列int count=0,w;FindInDegree(G,indegree);for(int i=0;i=0;w=NextAdjVex(G,i,w)if(!(-indegreew)PushStack(stack,w);if(countG.vexnumG.arcnum;CreateGraph(G);if(G.vexnumDestroyGraph(G);