图结构及应用.docx
《图结构及应用.docx》由会员分享,可在线阅读,更多相关《图结构及应用.docx(17页珍藏版)》请在冰豆网上搜索。
图结构及应用
成绩评定
教师签名
嘉应学院计算机学院
《数据结构》实验报告
课程名称:
数据结构
开课学期:
2016-2017学年第1学期
班级:
1401
指导老师:
钟治初
实验题目:
图结构及应用
学号:
141110043
姓名:
苏永达
提交时间:
2016年12月27日
一、实验要求
1、建立图的邻接矩阵存储结构
2、建立图的邻接表存储结构
3、图结构的输出
4、图的深度优先遍历的非递归算法和广度优先遍历算法
5、判断一个图是否连通,强连通
6、图的最小生成树
7、图(带权图)中任意两点之间的一条路径,所有路径
8、图(带权图)中任意两点之间的最短路径,最短距离
9、求所向图的一个拓扑序列,多个不同的拓扑序列
二、算法描述
1、图的邻接表存储表示:
对图的每个顶点建立一个单链表,第i个单链表表示所有依附于第i个点的边(对于有向图表示以该顶点为尾的弧);链表的每个节点存储两个信息,该弧指向的顶点在图中的位置(adjvex)和指向下一条弧的指针(nextarc)。
每个连表的头结点存储顶点的数据:
顶点信息(data)和指向依附于它的弧的链表域。
存储表示如下:
typedefstructArcNode{
intadjvex;//该弧所指向的顶点的位置
structArcNode*nextarc;
//指向下一条弧的指针
//InfoType*info;//该弧相关信息的指针
}ArcNode;
typedefstructVNode{
chardata;//顶点信息
intdata2;
intsngle;
ArcNode*firstarc;
//指向第一条依附该顶点的弧
}VNode,AdjList[MAX_NUM];
typedefstruct{
AdjListvertices;
intvexnum,arcnum;
intkind;//图的种类标志
}ALGraph;
2、深度优先搜索:
假设初始态是图中所有定点未被访问,从图中的某个顶点v开始,访问此顶点,然后依次从v的未访问的邻接点出发深度优先遍历,直至途中所有和v有相同路径的点都被访问到;若图中仍有点未被访问,则从图中另选一个未被访问的点作为起点重复上述过程,直到图中所有点都被访问到。
为了便于区分途中定点是否被访问过,需要附设一个访问标致数组visited[0..n-1],将其初值均设为false,一旦某个顶点被访问,将对应的访问标志赋值为true。
2、广度优先搜索:
假设初始态是图中所有顶点未被访问,从图中的某个顶点v开始依次访问v的各个未被访问的邻接点,然后分别从这些邻接点出发以此访问他们的邻接点,并使“先被访问的邻接顶点”先于“后被访问的邻接顶点”被访问,直至图中所有已被访问过的顶点的邻接顶点都被访问。
若图中仍有未被访问的顶点,选择另一个未被访问的顶点开始,重复上述操作,直到图中所有顶点都被访问。
为了使“先被访问的邻接顶点”先于“后被访问的邻接顶点”被访问,在次算法中加入一个队列,queue暂时存储被访问的顶点。
3、搜索简单路径:
利用深度优先搜索,以一个要搜索的起点v顶点为起始点,搜索到要找的终点s结束。
为了方便记录路径,此算法中加入栈。
访问第v个顶点时将v入栈,以v为顶点进行深度优先搜索,分别将其邻接点vi入栈,若找到s,将s入栈,若没有找到,将vi出栈;对vi+1深度优先搜索,直到找到s,或者图中所有顶点都被访问。
4、搜索最短路径:
搜索最短路径时,要记录被访问的顶点的上一个顶点在图中的位置,所以添加一个上一个顶点的标识single;访问v时将其标识置为-1;搜索从v到s的最短路径,从v开始进行广度优先搜索,直到找到s,将s以及它的之前的顶点依次入栈,直到将v入栈,然后将栈内元素输出。
三、运行结果:
1、深度优先搜索:
2、广度优先搜索:
3、简单路径:
4、最短路径:
四、实验总结
这次的图的操作实验,与树的操作类似,但又比树复杂,包含更多的存储
结构和遍历方法的操作,而且图的遍历需要沿着弧进行,以便输出弧上的信息。
本实验中图的遍历采用邻接表的存储结构,在输入图的信息时,首先要画出图的邻接表信息。
图有两种遍历的形式,一种为深度优先搜索,另一种为广度优先搜索。
由于能力有限,没能实现图的深度非递归优先搜索,而是实现了图的深度递归优先搜索。
本实验基本完成了图的操作,也学到了很多关于图的知识和算法。
五、程序代码:
#include
#include
#include
#defineMAX_NUM20
boolvisited[MAX_NUM];//访问标致数组
boolfound;
intfomer=0;
charv1,v2;
inttfind;
typedefstructArcNode{
intadjvex;//该弧所指向的顶点的位置
structArcNode*nextarc;
//指向下一条弧的指针
//InfoType*info;//该弧相关信息的指针
}ArcNode;
typedefstructVNode{
chardata;//顶点信息
intdata2;
intsngle;
ArcNode*firstarc;
//指向第一条依附该顶点的弧
}VNode,AdjList[MAX_NUM];
typedefstruct{
AdjListvertices;
intvexnum,arcnum;
intkind;//图的种类标志
}ALGraph;
voidDFS(ALGraphG,intv);
typedefstructqnode//队列类型
{
intdata;
qnode*next;
}qnode,*queueptr;
typedefstruct
{
queueptrfront;
queueptrrear;
}linkqueue;
typedefstructstack//用栈存储路径
{
char*base;
char*top;
intstacksize;
intsize;
}Stack;
Stacks;
intinitstack(Stack&s)
{
s.base=(char*)malloc(40*sizeof(char));
s.top=s.base;
s.stacksize=40;
s.size=0;
return1;
}
intpush(Stack&s,chare)
{
*s.top++=e;
s.size++;
return1;
}
intpop(Stack&s,char&e)
{
if(s.base==s.top)
e=*--s.top;
else{
e=*--s.top;
s.size--;
}
return1;
}
voidprintstack(Stacks)
{
while(s.base!
=s.top)
{
printf("%c",*s.base);
s.base++;
}
printf("\n");
}
voidprintstack2(Stacks)
{
while(s.base!
=s.top)
{
printf("%c",*--s.top);
}
printf("\n");
}
intintitqueue(linkqueue&q)//初始化队列
{
q.front=q.rear=(queueptr)malloc(sizeof(qnode));
q.front->next=NULL;
return1;
}
intemptyqueue(linkqueueq)//判断对了是否为空
{
if(q.front==q.rear)
return1;
return0;
}
intenqueue(linkqueue&q,inte)//元素入队
{
queueptrp;
p=(queueptr)malloc(sizeof(qnode));
if(!
p)exit(0);
p->data=e;p->next=NULL;
q.rear->next=p;
q.rear=p;
return1;
}
intdequeue(linkqueue&q,int&e)//元素出队
{
queueptrp;
if(q.front==q.rear)return0;
p=q.front->next;
e=p->data;
q.front->next=p->next;
if(q.rear==p)q.rear=q.front;
free(p);
return1;
}
intLocateVex(ALGraph&G,charv)
{
inti;
for(i=0;iif(G.vertices[i].data==v)
returni;
return-1;
}
intFirstAdjVex(ALGraphG,intv)
{
if(G.vertices[v].firstarc!
=NULL)
returnG.vertices[v].firstarc->adjvex;
return-1;
}
intNextAdjVex(ALGraphG,intv,intw)
{
while(G.vertices[v].firstarc->nextarc!
=NULL)
{
if(G.vertices[v].firstarc->adjvex==w)
returnG.vertices[v].firstarc->nextarc->adjvex;
elseG.vertices[v].firstarc=G.vertices[v].firstarc->nextarc;
}
return-1;
}
voidCreate(ALGraph&G)
{
inti,j,k;
charv1,v2;
ArcNode*p,*q,*h;
q=NULL;
h=NULL;
printf("输入节点个数和弧的个数:
\n");
scanf("%d%d",&G.vexnum,&G.arcnum);
for(i=0;i{
fflush(stdin);
printf("输入节点名称:
\n");
scanf("%c",&G.vertices[i].data);
G.vertices[i].firstarc=NULL;
G.vertices[i].data2=i;
}
for(k=0;k{
printf("输入弧:
a,b:
\n");
fflush(stdin);
scanf("%c,%c",&v1,&v2);
i=LocateVex(G,v1);
j=LocateVex(G,v2);
p=(ArcNode*)malloc(sizeof(ArcNode));
p->adjvex=j;
p->nextarc=NULL;
if(G.vertices[i].firstarc==NULL)
G.vertices[i].firstarc=p;
else
{
q=G.vertices[i].firstarc;
while(q->nextarc!
=NULL)
q=q->nextarc;
q->nextarc=p;
}
}
}
voidDFSTraverse(ALGraphG)//深度遍历
{
intv;
for(v=0;vif(!
visited[v])DFS(G,v);
printf("\n");
}
voidDFS(ALGraphG,intv)//深度遍历
{
intw;
visited[v]=true;
printf("%c",G.vertices[v].data);
for(w=FirstAdjVex(G,v);(w>=0)&&(tfind==0);w=NextAdjVex(G,v,w))
{
if(!
visited[w])DFS(G,w);
}
}
voidDFSTree(ALGraphG)//广度遍历
{
intw,u,v;
linkqueueq;
intitqueue(q);
for(v=0;v{
if(!
visited[v])
{
visited[v]=true;
printf("%c",G.vertices[v].data);
enqueue(q,v);
}
while(!
emptyqueue(q))
{
dequeue(q,u);
for(w=FirstAdjVex(G,u);w>0;w=NextAdjVex(G,u,w))
if(!
visited[w])
{
visited[w]=true;
printf("%c",G.vertices[w].data);
if(w>0)
enqueue(q,w);
}
}
}
printf("\n");
}
voidDFS2(ALGraphG,intv)//用深度遍历算法实现搜索简单路径
{
intw;
chare;
visited[v]=true;
push(s,G.vertices[v].data);
for(w=FirstAdjVex(G,v);(w>=0)&&(!
found);w=NextAdjVex(G,v,w))
{
if(G.vertices[w].data==v2)
{
found=true;
push(s,G.vertices[w].data);
}
elseif(!
visited[w])DFS2(G,w);
}
if(!
found)pop(s,e);
}
voidSimplepath(ALGraphG)//搜索简单路径
{
printf("输入要搜索路径的两点:
\n");
fflush(stdin);
scanf("%c",&v1);
fflush(stdin);
scanf("%c",&v2);
DFS2(G,LocateVex(G,v1));
if(!
found)
{
printf("cannotfoundzhepath!
\n");
}
elseprintstack(s);
}
voidDFSTree2(ALGraphG,intv)//用广度优先求最短路径
{
intw,u;
linkqueueq;
intitqueue(q);
if(!
visited[v])
{
visited[v]=true;
G.vertices[v].sngle=-1;
enqueue(q,v);
}
while(!
emptyqueue(q))
{
dequeue(q,u);
for(w=FirstAdjVex(G,u);(w>0)&&(!
found);w=NextAdjVex(G,u,w))
if(!
visited[w])
{
visited[w]=true;
G.vertices[w].sngle=u;
if(w>0)
enqueue(q,w);
if(G.vertices[w].data==v2)
{
found=true;
while(G.vertices[w].sngle!
=-1)
{
push(s,G.vertices[w].data);
w=G.vertices[w].sngle;
}
}
}
}
printf("\n");
}
voidshortcut(ALGraphG)//搜索最短路径
{
printf("输入要搜索路径的两点:
\n");
fflush(stdin);
scanf("%c",&v1);
fflush(stdin);
scanf("%c",&v2);
DFSTree2(G,LocateVex(G,v1));
push(s,v1);
printstack2(s);
printf("\n");
}
voidmain()
{
intv;
ALGraphG;
found=false;
initstack(s);
Create(G);
while
(1)
{
for(v=0;v{
visited[v]=false;
G.vertices[v].sngle=-2;
}
tfind=0;
system("cls");
printf("---------------------\n");
printf("1、深度优先遍历\n");
printf("2、广度优先遍历\n");
printf("3、搜索简单路径\n");
printf("4、搜索最短路径\n");
printf("---------------------\n");
switch(getch())
{
case'1':
DFSTraverse(G);break;
case'2':
DFSTree(G);break;
case'3':
Simplepath(G);break;
case'4':
shortcut(G);break;
case'0':
exit(0);
}
system("pause");
}
}