图结构及应用Word文档下载推荐.docx
- 文档编号:16776117
- 上传时间:2022-11-26
- 格式:DOCX
- 页数:17
- 大小:95.60KB
图结构及应用Word文档下载推荐.docx
《图结构及应用Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《图结构及应用Word文档下载推荐.docx(17页珍藏版)》请在冰豆网上搜索。
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、深度优先搜索:
3、简单路径:
4、最短路径:
四、实验总结
这次的图的操作实验,与树的操作类似,但又比树复杂,包含更多的存储
结构和遍历方法的操作,而且图的遍历需要沿着弧进行,以便输出弧上的信息。
本实验中图的遍历采用邻接表的存储结构,在输入图的信息时,首先要画出图的邻接表信息。
图有两种遍历的形式,一种为深度优先搜索,另一种为广度优先搜索。
由于能力有限,没能实现图的深度非递归优先搜索,而是实现了图的深度递归优先搜索。
本实验基本完成了图的操作,也学到了很多关于图的知识和算法。
五、程序代码:
#include<
stdio.h>
stdlib.h>
#include<
conio.h>
#defineMAX_NUM20
boolvisited[MAX_NUM];
//访问标致数组
boolfound;
intfomer=0;
charv1,v2;
inttfind;
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{
s.size--;
}
voidprintstack(Stacks)
while(s.base!
=s.top)
{
printf("
%c"
*s.base);
s.base++;
printf("
\n"
);
voidprintstack2(Stacks)
*--s.top);
intintitqueue(linkqueue&
q)//初始化队列
q.front=q.rear=(queueptr)malloc(sizeof(qnode));
q.front->
next=NULL;
intemptyqueue(linkqueueq)//判断对了是否为空
if(q.front==q.rear)
return0;
intenqueue(linkqueue&
q,inte)//元素入队
queueptrp;
p=(queueptr)malloc(sizeof(qnode));
if(!
p)exit(0);
p->
data=e;
q.rear->
next=p;
q.rear=p;
intdequeue(linkqueue&
q,int&
e)//元素出队
if(q.front==q.rear)return0;
p=q.front->
next;
e=p->
data;
next=p->
if(q.rear==p)q.rear=q.front;
free(p);
intLocateVex(ALGraph&
G,charv)
inti;
for(i=0;
i<
G.vexnum;
i++)
if(G.vertices[i].data==v)
returni;
return-1;
intFirstAdjVex(ALGraphG,intv)
if(G.vertices[v].firstarc!
=NULL)
returnG.vertices[v].firstarc->
adjvex;
intNextAdjVex(ALGraphG,intv,intw)
while(G.vertices[v].firstarc->
nextarc!
if(G.vertices[v].firstarc->
adjvex==w)
returnG.vertices[v].firstarc->
nextarc->
elseG.vertices[v].firstarc=G.vertices[v].firstarc->
nextarc;
voidCreate(ALGraph&
G)
inti,j,k;
charv1,v2;
ArcNode*p,*q,*h;
q=NULL;
h=NULL;
输入节点个数和弧的个数:
scanf("
%d%d"
&
G.vexnum,&
G.arcnum);
fflush(stdin);
输入节点名称:
scanf("
%c"
G.vertices[i].data);
G.vertices[i].firstarc=NULL;
G.vertices[i].data2=i;
for(k=0;
k<
G.arcnum;
k++)
输入弧:
a,b:
%c,%c"
v1,&
v2);
i=LocateVex(G,v1);
j=LocateVex(G,v2);
p=(ArcNode*)malloc(sizeof(ArcNode));
adjvex=j;
nextarc=NULL;
if(G.vertices[i].firstarc==NULL)
G.vertices[i].firstarc=p;
else
{
q=G.vertices[i].firstarc;
while(q->
q=q->
q->
nextarc=p;
}
}
voidDFSTraverse(ALGraphG)//深度遍历
intv;
for(v=0;
v<
v++)
visited[v])DFS(G,v);
voidDFS(ALGraphG,intv)//深度遍历
intw;
visited[v]=true;
printf("
G.vertices[v].data);
for(w=FirstAdjVex(G,v);
(w>
=0)&
&
(tfind==0);
w=NextAdjVex(G,v,w))
visited[w])DFS(G,w);
voidDFSTree(ALGraphG)//广度遍历
intw,u,v;
linkqueueq;
intitqueue(q);
for(v=0;
if(!
visited[v])
visited[v]=true;
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("
G.vertices[w].data);
if(w>
0)
enqueue(q,w);
}
voidDFS2(ALGraphG,intv)//用深度遍历算法实现搜索简单路径
chare;
visited[v]=true;
push(s,G.vertices[v].data);
(!
found);
if(G.vertices[w].data==v2)
found=true;
push(s,G.vertices[w].data);
elseif(!
visited[w])DFS2(G,w);
found)pop(s,e);
voidSimplepath(ALGraphG)//搜索简单路径
输入要搜索路径的两点:
fflush(stdin);
v1);
DFS2(G,LocateVex(G,v1));
if(!
found)
cannotfoundzhepath!
elseprintstack(s);
voidDFSTree2(ALGraphG,intv)//用广度优先求最短路径
intw,u;
visited[v]=true;
G.vertices[v].sngle=-1;
enqueue(q,v);
dequeue(q,u);
for(w=FirstAdjVex(G,u);
0)&
if(!
{
visited[w]=true;
G.vertices[w].sngle=u;
if(w>
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;
}
}
}
voidshortcut(ALGraphG)//搜索最短路径
DFSTree2(G,LocateVex(G,v1));
push(s,v1);
printstack2(s);
voidmain()
ALGraphG;
found=false;
initstack(s);
Create(G);
while
(1)
visited[v]=false;
G.vertices[v].sngle=-2;
tfind=0;
system("
cls"
---------------------\n"
1、深度优先遍历\n"
2、广度优先遍历\n"
3、搜索简单路径\n"
4、搜索最短路径\n"
switch(getch())
case'
1'
:
DFSTraverse(G);
break;
2'
DFSTree(G);
3'
Simplepath(G);
4'
shortcut(G);
0'
exit(0);
system("
pause"
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 结构 应用