实验四.docx
- 文档编号:30251623
- 上传时间:2023-08-13
- 格式:DOCX
- 页数:16
- 大小:17.43KB
实验四.docx
《实验四.docx》由会员分享,可在线阅读,更多相关《实验四.docx(16页珍藏版)》请在冰豆网上搜索。
实验四
授课题目
图的遍历操作
授课类型
实验课
首次授课时间
年月日
学时
4
教学目标
1.掌握有向图和无向图的概念;
2.掌握邻接矩阵和邻接链表建立图的存储结构;
3.掌握DFS、BFS的基本思想及对图的遍历操作;
4.了解图结构在人工智能、工程等领域的广泛应用。
重点与难点
重点:
图的两种存储结构的建立
难点:
图的两种遍历方法
教学手段与方法
上机编程、调试并进行验证
教学过程:
(包括授课思路、过程设计、讲解要点及各部分具体内容、时间分配等)
实验内容:
设计一个有向图和一个无向图,任选一种存储结构,完成有向图和无向图的DFS(深度优先遍历)和BFS(广度优先遍历)的操作。
一、有向图采用邻接矩阵存储结构
编程思路:
首先建立有向图的邻接矩阵存储结构,然后设计如下函数:
CreateGraphM(G)(建立有向图的邻接矩阵)、DFSTraverse(G)(深度优先遍历有向图)、BFSTraverse(G)(广度优先遍历有向图),程序描述如下:
#include
#include
#defineMAX_VERTEX_NUM20
typedefstruct{
charvexs[MAX_VERTEX_NUM];
intarcs[MAX_VERTEX_NUM][MAX_VERTEX_NUM];
intvexnum,arcnum;
}MGraph;
intvisited[MAX_VERTEX_NUM];
#defineMAXSTACK20
typedefstruct{//定义栈的存储结构
int*base;
inttop;
}SqStack;
#defineMAXQUEUE20
typedefstruct{//定义队列的存储结构
int*base;
intfront,rear;
}SqQueue;
//栈操作的相关函数
voidInitStack(SqStack&S)
{S.base=(int*)malloc(sizeof(int));
if(!
S.base)exit(0);
S.top=0;
}
voidPush(SqStack&S,inte)
{
if(S.top==MAXSTACK)
exit(0);
S.base[S.top]=e;
S.top++;
}
voidPop(SqStack&S,int&e)
{
if(S.top==0)
exit(0);
S.top--;
e=S.base[S.top];
}
intGetTop(SqStackS,int&e)
{
if(S.top==0)
return0;
e=S.base[S.top-1];
return1;
}
intStackEmpty(SqStackS)
{
if(S.top==0)
return1;
elsereturn0;
}
//队列操作的相关函数
voidInitQueue(SqQueue&Q)
{
Q.base=(int*)malloc(sizeof(int));
if(!
Q.base)
exit(0);
Q.front=Q.rear=0;
}
voidEnQueue(SqQueue&Q,inte)
{
if((Q.rear+1)%MAXQUEUE==Q.front)
exit(0);
Q.base[Q.rear]=e;
Q.rear=(Q.rear+1)%MAXQUEUE;
}
voidDeQueue(SqQueue&Q,int&e)
{
if(Q.front==Q.rear)
exit(0);
e=Q.base[Q.front];
Q.front=(Q.front+1)%MAXQUEUE;
}
intQueueEmpty(SqQueueQ)
{
if(Q.front==Q.rear)
return1;
elsereturn0;
}
//下面是对有向图的相关操作
intLocateVex(MGraphG,charv)//查找顶点v在图G中的位置
{
inti;
for(i=0;i if(G.vexs[i]==v) returni; return-1;//查找不成功 } voidCreateGraphM(MGraph&G)//建立有向图的邻接矩阵 { inti,j,k; charv1,v2; printf(“输入有向图的顶点数及弧数: \n”); scanf(“%d%d”,&G.vexnum,&G.arcnum); getchar(); printf(“输入有向图的所有顶点: \n”); for(i=0;i {G.vexs[i]=getchar();getchar();} for(i=0;i for(j=0;j G.arcs[i][j]=0; printf(“输入有向图的所有弧: \n”); for(k=0;k { v1=getchar();getchar(); v2=getchar();getchar(); i=LocateVex(G,v1);j=LocateVex(G,v2); G.arcs[i][j]=1; } } voidDFS(MGraphG,intv)//从第v顶点出发深度优先遍历有向图 { intk; printf(“%4c”,G.vexs[v]);visited[v]=1; for(k=0;k if(G.arcs[v][k]==1&&! visited[k]) DFS(G,k); } voidDFSTraverse(MGraphG)//深度优先遍历有向图的递归算法 { intv; for(v=0;v visited[v]=0; for(v=0;v if(! visited[v]) DFS(G,v); } voidDFSTraverse1(MGraphG)//深度优先遍历有向图的非递归算法 { intv,u,k; SqStackS; for(v=0;v visited[v]=0; for(v=0;v if(! visited[v]) { printf(“%4c”,G.vexs[v]);visited[v]=1; InitStack(S);Push(S,v); while(! StackEmpty(S)) { GetTop(S,u);k=0; while(k if(G.arcs[u][k]==0||visited[k]) k++; else {printf(“%4c”,G.vexs[k]); visited[k]=1;Push(S,k); u=k;k=0; } if(! StackEmpty(S)) Pop(S,u); }//while(! StackEmpty) }//if } voidBFSTraverse1(MGraphG)//广度优先遍历有向图 { SqQueueQ; intv,u,k; for(v=0;v visited[v]=0; InitQueue(Q); for(v=0;v if(! visited[v]) { printf(“%4c”,G.vexs[v]);visited[v]=0; EnQueue(Q,v); while(! QueueEmpty(Q)) {DeQueue(Q,u); for(k=0;k if(G.arcs[u][k]==1&&! visited[k]) {printf(“%4c”,G.vexs[k]); visited[k]=1; EnQueue(Q,k); }//if }//while }//if } voidmain() { MGraphG; CreateGraphM(G); printf(“\n深度优先遍历序列(递归算法): \n”); DFSTraverse(G); printf(“\n深度优先遍历序列(非递归算法): \n”); DFSTraverse1(G); printf(“\n广度优先遍历序列: \n”); BFSTraverse(G); printf(“\n”); } 二、无向图采用邻接矩阵存储结构 编程思路: 首先建立无向图的邻接表存储结构,然后设计如下函数: CreateGraphAL(G)(建立无向图的邻接表)、DFSTraverse(G)(深度优先遍历无向图)、BFSTraverse(G)(广度优先遍历无向图),程序描述如下: #include #include #defineMAX_VERTEX_NUM20 typedefstructArcNode{ intadjvex; structArcNode*nextarc; }ArcNode; typedefstructVNode{ chardata; ArcNode*firstarc; }VNode,AdjList[MAX_VERTEX_NUM]; typedefstruct{ AdjListvertices; intvexnum,arcnum; }ALGraph; intvisited[MAX_VERTEX_NUM]; #defineMAXSTACK20 typedefstruct{//定义栈的存储结构 ArcNode*base; inttop; }SqStack; #defineMAXQUEUE20 typedefstruct{//定义队列的存储结构 int**base; intfront,rear; }SqQueue; //栈操作的相关函数 voidInitStack(SqStack&S) {S.base=(ArcNode**)malloc(sizeof(ArcNode*)); if(! S.base)exit(0); S.top=0; } voidPush(SqStack&S,ArcNode*e) { if(S.top==MAXSTACK) exit(0); S.base[S.top]=e; S.top++; } voidPop(SqStack&S,ArcNode*&e) { if(S.top==0) exit(0); S.top--; e=S.base[S.top]; } intStackEmpty(SqStackS) { if(S.top==0) return1; elsereturn0; } //队列操作的相关函数 voidInitQueue(SqQueue&Q) { Q.base=(int*)malloc(sizeof(int)); if(! Q.base) exit(0); Q.front=Q.rear=0; } voidEnQueue(SqQueue&Q,inte) { if((Q.rear+1)%MAXQUEUE==Q.front) exit(0); Q.base[Q.rear]=e; Q.rear=(Q.rear+1)%MAXQUEUE; } voidDeQueue(SqQueue&Q,int&e) { if(Q.front==Q.rear) exit(0); e=Q.base[Q.front]; Q.front=(Q.front+1)%MAXQUEUE; } intQueueEmpty(SqQueueQ) { if(Q.front==Q.rear) return1; elsereturn0; } //下面是对有向图的相关操作 intLocateVex(ALGraphG,charv)//查找顶点v在图G中的位置 { inti; for(i=0;i if(G.vertices[i].data==v) returni; return-1;//查找不成功 } voidCreateGraphM(ALGraph&G)//建立无向图的邻接表 { inti,j,k; charv1,v2; ArcNode*pre,*p; printf(“输入无向图的顶点数及边数: \n”); scanf(“%d%d”,&G.vexnum,&G.arcnum); getchar(); printf(“输入无向图的所有顶点: \n”); for(i=0;i {G.vertices[i].data=getchar();getchar(); G.vertices[i].firstarc=NULL; } printf(“输入无向图的所有边: \n”); for(k=0;k { v1=getchar();getchar(); v2=getchar();getchar(); i=LocateVex(G,v1);j=LocateVex(G,v2); //下面是利用尾插法建立无向图的邻接表 p=(ArcNode*)malloc(sizeof(ArcNode)); p->adjvex=j;p->nextarc=NULL; if(! G.vertices[i].firstarc) G.vertices[i].firstarc=p; else {pre=G.vertices[i].firstarc; while(pre->nextarc) pre=pre->nextarc; pre->nextarc=p; } p=(ArcNode*)malloc(sizeof(ArcNode)); p->adjvex=i;p->nextarc=NULL; if(! G.vertices[j].firstarc) G.vertices[j].firstarc=p; else {pre=G.vertices[j].firstarc; while(pre->nextarc) pre=pre->nextarc; pre->nextarc=p; } }//for } voidDFS(ALGraphG,intv)//从第v顶点出发深度优先遍历无向图 { ArcNode*p; printf(“%4c”,G.vertices[v].data);visited[v]=1; p=G.vertices[v].firstarc; while(p) { if(! visited[p->adjvex]) DFS(G,p->adjvex); p=p->nextarc; } } voidDFSTraverse(ALGraphG)//深度优先遍历无向图的递归算法 { intv; for(v=0;v visited[v]=0; for(v=0;v if(! visited[v]) DFS(G,v); } voidDFSTraverse1(ALGraphG)//深度优先遍历无向图的非递归算法 { ArcNode*p; intv; SqStackS; for(v=0;v visited[v]=0; for(v=0;v if(! visited[v]) { printf(“%4c”,G.vertices[v]);visited[v]=1; p=G.vertices[v].firstarc; InitStack(S);Push(S,p); while(! StackEmpty(S)||p) { while(p) if(p&&visited[p->adjvex]) p=p->nextarc; else {printf(“%4c”,G.vertices[p->adjvex]); visited[p->adjvex]=1;Push(S,p); p=G.vertices[p->adjvex].firstarc; } if(! StackEmpty(S)) {Pop(S,p);p=p->nextarc;} }//while(! StackEmpty) }//if } voidBFSTraverse(ALGraphG)//广度优先遍历无向图 { SqQueueQ; ArcNode*p; intv; for(v=0;v visited[v]=0; InitQueue(Q); for(v=0;v if(! visited[v]) { printf(“%4c”,G.vertices[v].data);visited[v]=1; EnQueue(Q,v); while(! QueueEmpty(Q)) {DeQueue(Q,v); p=G.vertices[v].firstarc; while(p) { if(! visited[p->adjvex]) {printf(“%4c”,G.vertices[p->adjvex]); visited[p->adjvex]=1; EnQueue(Q,p->adjvex); }//if p=p->nextarc; }//while(p) }//while(! QueueEmpty(Q)) }//if } voidmain() { ALGraphG; CreateGraphM(G); printf(“\n深度优先遍历序列(递归算法): \n”); DFSTraverse(G); /*printf(“\n深度优先遍历序列(非递归算法): \n”); DFSTraverse1(G);*/ printf(“\n广度优先遍历序列: \n”); BFSTraverse(G); printf(“\n”); }
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 实验