图的深度优先遍历实验报告.docx

上传人:b****2 文档编号:2394925 上传时间:2023-05-03 格式:DOCX 页数:16 大小:18.51KB
下载 相关 举报
图的深度优先遍历实验报告.docx_第1页
第1页 / 共16页
图的深度优先遍历实验报告.docx_第2页
第2页 / 共16页
图的深度优先遍历实验报告.docx_第3页
第3页 / 共16页
图的深度优先遍历实验报告.docx_第4页
第4页 / 共16页
图的深度优先遍历实验报告.docx_第5页
第5页 / 共16页
图的深度优先遍历实验报告.docx_第6页
第6页 / 共16页
图的深度优先遍历实验报告.docx_第7页
第7页 / 共16页
图的深度优先遍历实验报告.docx_第8页
第8页 / 共16页
图的深度优先遍历实验报告.docx_第9页
第9页 / 共16页
图的深度优先遍历实验报告.docx_第10页
第10页 / 共16页
图的深度优先遍历实验报告.docx_第11页
第11页 / 共16页
图的深度优先遍历实验报告.docx_第12页
第12页 / 共16页
图的深度优先遍历实验报告.docx_第13页
第13页 / 共16页
图的深度优先遍历实验报告.docx_第14页
第14页 / 共16页
图的深度优先遍历实验报告.docx_第15页
第15页 / 共16页
图的深度优先遍历实验报告.docx_第16页
第16页 / 共16页
亲,该文档总共16页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

图的深度优先遍历实验报告.docx

《图的深度优先遍历实验报告.docx》由会员分享,可在线阅读,更多相关《图的深度优先遍历实验报告.docx(16页珍藏版)》请在冰点文库上搜索。

图的深度优先遍历实验报告.docx

图的深度优先遍历实验报告

一.实验目的

熟悉图的存储结构,掌握用单链表存储数据元素信息和数据元素之间的关系的信息的方法,并能运用图的深度优先搜索遍历一个图,对其输出。

二.实验原理

深度优先搜索遍历是树的先根遍历的推广。

假设初始状态时图中所有顶点未曾访问,则深度优先搜索可从图中某个顶点v出发,访问此顶点,然后依次从v的未被访问的邻接点出发深度优先遍历图,直至图中所有与v有路径相通的顶点都被访问到;若此时图中尚有顶点未被访问,则另选图中一个未曾访问的顶点作起始点,重复上述过程,直至图中所有顶点都被访问到为止。

图的邻接表的存储表示:

#defineMAX_VERTEX_NUM20

#defineMAXNAME10

typedefcharVertexType[MAXNAME];

typedefstructArcNode{

intadjvex;

structArcNode*nextarc;

}ArcNode;

typedefstructVNode{

VertexTypedata;

ArcNode*firstarc;

}VNode,AdjList[MAX_VERTEX_NUM];

typedefstruct{

AdjListvertices;

intvexnum,arcnum;

intkind;

}ALGraph;

三.实验内容

编写LocateVex函数,Create函数,print函数,main函数,输入要构造的图的相关信息,得到其邻接表并输出显示。

 

四。

实验步骤

1)结构体定义,预定义,全局变量定义。

#include"stdio.h"

#include"stdlib.h"

#include"string.h"

#defineFALSE0

#defineTRUE1

#defineMAX20

typedefintBoolean;

#defineMAX_VERTEX_NUM20

#defineMAXNAME10

typedefcharVertexType[MAXNAME];

typedefstructArcNode{

intadjvex;

structArcNode*nextarc;

}ArcNode;

typedefstructVNode{

VertexTypedata;

ArcNode*firstarc;

}VNode,AdjList[MAX_VERTEX_NUM];

typedefstruct{

AdjListvertices;

intvexnum,arcnum;

intkind;

}ALGraph;

ALGraphG;

Booleanvisited[MAX];

intdegree[MAX_VERTEX_NUM];//定义一个数组求每一个顶点的总度数(无向图)或出度(有向图)。

2)编写LocateVex函数,确定所输入的边在G中的位置,返回该边所在的行数或或列数。

intLocateVex(ALGraphG,VertexTypev)

{

inti,n;

for(n=0;n

{

if(strcmp(v,G.vertices[n].data)==0)

i=n;

}

returni;

}

3)编写Create函数,采用邻接表构造图G,返回结构体变量G的值,并统计每一个顶点的总度数或出度。

ALGraphCreate(ALGraphG)

{

inti,j,k;VertexTypev1,v2;ArcNode*p;

printf("请输入要构造的图的顶点数和弧数:

\n");

scanf("%d%d",&G.vexnum,&G.arcnum);

printf("请输入每一个顶点的名字:

\n");

for(i=0;i

{

scanf("%s",&G.vertices[i].data);

G.vertices[i].firstarc=NULL;

}

printf("各顶点的位置以及名称为:

\n");

for(i=0;i

printf("%6d%6s\n",i,G.vertices[i].data);

printf("请输入要构造的是无向图还是有向图:

无向用0表示,有向用1表示:

\n");

scanf("%d",&G.kind);

for(i=0;i

degree[i]=0;

printf("请输入每条弧的始点和终点:

\n");

if(G.kind==1)

{

for(k=0;k

{

scanf("%s%s",&v1,&v2);

i=LocateVex(G,v1);j=LocateVex(G,v2);

p=(ArcNode*)malloc(sizeof(ArcNode));

p->adjvex=j;

p->nextarc=G.vertices[i].firstarc;

G.vertices[i].firstarc=p;

degree[i]++;

}

}

if(G.kind==0)

{

for(k=0;k

{

scanf("%s%s",&v1,&v2);

i=LocateVex(G,v1);j=LocateVex(G,v2);

p=(ArcNode*)malloc(sizeof(ArcNode));

p->adjvex=j;

p->nextarc=G.vertices[i].firstarc;

G.vertices[i].firstarc=p;

degree[i]++;

p=(ArcNode*)malloc(sizeof(ArcNode));

p->adjvex=i;

p->nextarc=G.vertices[j].firstarc;

G.vertices[j].firstarc=p;

degree[j]++;

}

}

returnG;

}

4)编写print函数,实现对所构建的图的邻接表的输出。

voidprint(ALGraphG)

{

inti;

ArcNode*p;

for(i=0;i

{

printf("%6d%6s",i,G.vertices[i].data);

for(p=G.vertices[i].firstarc;p;p=p->nextarc)

printf("%6d",p->adjvex);

printf("\n");

if(G.kind==1)

printf("出度为:

%6d\n",degree[i]);

if(G.kind==0)

printf("总度数为:

%6d\n",degree[i]);

}

}

5)编写FirstAdjVex函数,返回v的第一个邻接点的编号。

intFirstAdjVex(ALGraphG,intv)

{

ArcNode*p;

p=G.vertices[v].firstarc;

if(p)

return(p->adjvex);

else

return-1;

}

6)编写NextAdjVex函数,返回v第一个之后未被访问过的下一个邻接点。

intNextAdjVex(ALGraphG,intv,intw)

{

ArcNode*p;inti;

for(p=G.vertices[v].firstarc;p;p=p->nextarc)

{

if(w!

=p->adjvex)

i=0;

elsei=1;

if(i&&p)

returnp->nextarc->adjvex;

else

return-1;

}

}

7)编写DFS函数,从第i个顶点出发递归地深度优先遍历图G。

voidDFS(ALGraphG,intv)

{

intw;

visited[v]=TRUE;

printf("%s\n",G.vertices[v].data);

for(w=FirstAdjVex(G,v);w>=0;w=NextAdjVex(G,v,w))

if(!

visited[w])

DFS(G,w);

}

8)编写DFSTraverse函数,对图G作深度优先遍历。

voidDFSTraverse(ALGraphG)

{

intv;

for(v=0;v

visited[v]=FALSE;

for(v=0;v

if(!

visited[v])

DFS(G,v);

}

9)编写main函数,把以上几个函数结合到一起,用邻接表实现对一个图的构造,输入要构造的边的相关信息(总弧数,顶点数,边的两个顶点的名称,有向图还是无向图),对其进行输出显示,并用深度优先搜索的方法遍历所构建的图。

main()

{

ALGraphG;

G=Create(G);

printf("邻接表为:

\n");

print(G);

printf("深度遍历的结果为:

\n");

DFSTraverse(G);

}

五.实验结果

源程序代码:

#include"stdio.h"

#include"stdlib.h"

#include"string.h"

#defineFALSE0

#defineTRUE1

#defineMAX20

typedefintBoolean;

#defineMAX_VERTEX_NUM20

#defineMAXNAME10

typedefcharVertexType[MAXNAME];

typedefstructArcNode{

intadjvex;

structArcNode*nextarc;

}ArcNode;

typedefstructVNode{

VertexTypedata;

ArcNode*firstarc;

}VNode,AdjList[MAX_VERTEX_NUM];

typedefstruct{

AdjListvertices;

intvexnum,arcnum;

intkind;

}ALGraph;

ALGraphG;

Booleanvisited[MAX];

intdegree[MAX_VERTEX_NUM];

intLocateVex(ALGraphG,VertexTypev)

{

inti,n;

for(n=0;n

{

if(strcmp(v,G.vertices[n].data)==0)

i=n;

}

returni;

}

ALGraphCreate(ALGraphG)

{

inti,j,k;VertexTypev1,v2;ArcNode*p;

printf("请输入要构造的图的顶点数和弧数:

\n");

scanf("%d%d",&G.vexnum,&G.arcnum);

printf("请输入每一个顶点的名字:

\n");

for(i=0;i

{

scanf("%s",&G.vertices[i].data);

G.vertices[i].firstarc=NULL;

}

printf("各顶点的位置以及名称为:

\n");

for(i=0;i

printf("%6d%6s\n",i,G.vertices[i].data);

printf("请输入要构造的是无向图还是有向图:

无向用0表示,有向用1表示:

\n");

scanf("%d",&G.kind);

for(i=0;i

degree[i]=0;

printf("请输入每条弧的始点和终点:

\n");

if(G.kind==1)

{

for(k=0;k

{

scanf("%s%s",&v1,&v2);

i=LocateVex(G,v1);j=LocateVex(G,v2);

p=(ArcNode*)malloc(sizeof(ArcNode));

p->adjvex=j;

p->nextarc=G.vertices[i].firstarc;

G.vertices[i].firstarc=p;

degree[i]++;

}

}

if(G.kind==0)

{

for(k=0;k

{

scanf("%s%s",&v1,&v2);

i=LocateVex(G,v1);j=LocateVex(G,v2);

p=(ArcNode*)malloc(sizeof(ArcNode));

p->adjvex=j;

p->nextarc=G.vertices[i].firstarc;

G.vertices[i].firstarc=p;

degree[i]++;

p=(ArcNode*)malloc(sizeof(ArcNode));

p->adjvex=i;

p->nextarc=G.vertices[j].firstarc;

G.vertices[j].firstarc=p;

degree[j]++;

}

}

returnG;

}

voidprint(ALGraphG)

{

inti;

ArcNode*p;

for(i=0;i

{

printf("%6d%6s",i,G.vertices[i].data);

for(p=G.vertices[i].firstarc;p;p=p->nextarc)

printf("%6d",p->adjvex);

printf("\n");

if(G.kind==1)

printf("出度为:

%6d\n",degree[i]);

if(G.kind==0)

printf("总度数为:

%6d\n",degree[i]);

}

}

 

intFirstAdjVex(ALGraphG,intv)

{

ArcNode*p;

p=G.vertices[v].firstarc;

if(p)

return(p->adjvex);

else

return-1;

}

intNextAdjVex(ALGraphG,intv,intw)

{

ArcNode*p;inti;

for(p=G.vertices[v].firstarc;p;p=p->nextarc)

{

if(w!

=p->adjvex)

i=0;

elsei=1;

if(i&&p)

returnp->nextarc->adjvex;

else

return-1;

}

}

voidDFS(ALGraphG,intv)

{

intw;

visited[v]=TRUE;

printf("%s\n",G.vertices[v].data);

for(w=FirstAdjVex(G,v);w>=0;w=NextAdjVex(G,v,w))

if(!

visited[w])

DFS(G,w);

}

voidDFSTraverse(ALGraphG)

{

intv;

for(v=0;v

visited[v]=FALSE;

for(v=0;v

if(!

visited[v])

DFS(G,v);

}

main()

{

ALGraphG;

G=Create(G);

printf("邻接表为:

\n");

print(G);

printf("深度遍历的结果为:

\n");

DFSTraverse(G);

}

构造一个无向图G,如图所示。

V2

V4

V5

V8

V1

V6

V3

V7

图G

 

运行结果截图:

 

 

实验结果显示:

遍历的结果为:

v1-v3-v7-v6-v2-v5-v8-v4。

运行成功。

六.实验结论

可以运用深度优先搜索的方法遍历一个用邻接表构建的图。

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 医药卫生 > 基础医学

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2