当前位置:网站首页>6-7 图的深度遍历-邻接表实现
6-7 图的深度遍历-邻接表实现
2022-06-22 20:23:00 【白—】
6-7 图的深度遍历-邻接表实现
本题要求实现邻接表存储图的深度优先遍历。
函数接口定义:
void DFS(ALGraph *G,int i);
其中ALGraph是邻接表存储的图,定义如下:
#define MAX_VERTEX_NUM 10 /定义最大顶点数/
typedef int Vertex;
typedef struct ArcNode{ /表结点/
int adjvex; /邻接点域/
struct ArcNode *nextarc; /指向下一个表结点/
}ArcNode;
typedef struct VNode{ /头结点/
Vertex data; /顶点域/
ArcNode *firstarc; /指向第一个表结点/
}VNode,AdjList[MAX_VERTEX_NUM]; /AdjList是数组类型/
typedef struct {
AdjList vertices; /邻接表中数组定义/
int vexnum,arcnum; /图中当前顶点数和边数/
} ALGraph; /图类型/
裁判测试程序样例:
#include"stdio.h"
#include"stdlib.h"
#define MAX_VERTEX_NUM 10 /定义最大顶点数/
typedef int Vertex;
typedef struct ArcNode{ /表结点/
int adjvex; /邻接点域/
struct ArcNode *nextarc; /指向下一个表结点/
}ArcNode;
typedef struct VNode{ /头结点/
Vertex data; /顶点域/
ArcNode *firstarc; /指向第一个表结点/
}VNode,AdjList[MAX_VERTEX_NUM]; /AdjList是数组类型/
typedef struct {
AdjList vertices; /邻接表中数组定义/
int vexnum,arcnum; /图中当前顶点数和边数/
} ALGraph; /图类型/
typedef enum{FALSE,TRUE} Boolean;
Boolean visited[MAX_VERTEX_NUM];/定义标志向量,为全局变量/
void CreatALGraph(ALGraph G);/ 创建图并且将Visited初始化为false;裁判实现,细节不表 */
void DFS(ALGraph G,int v);
int main()
{
Vertex v;
ALGraph G;
CreatALGraph(&G);
scanf(“%d”, &v);
printf(“DFS from %d:”,v);
DFS(&G,v);
return 0;
}
/ 你的代码将被嵌在这里 */
对于给定图:
输入样例:
第一行给出图的顶点数n和边数e,随后e行,每行给出一条边的两个顶点编号,最后一行给出遍历的起始顶点编号。
7 9
0 2
0 3
0 4
1 3
1 5
2 3
2 5
4 5
5 6
5
输出样例:
DFS from 5: 5 6 4 0 3 2 1
代码:
void DFS(ALGraph *G, int i) {
printf(" %d",i);
visited[i] = 1;
for (struct ArcNode * w= G->vertices[i].firstarc;w!=NULL;w=w->nextarc) {
if (visited[w->adjvex] == 0)
DFS(G, w->adjvex);
}
}
202206201639一
边栏推荐
- Learning cloud network from teacher Tang - openstack network implementation
- 第029讲:文件:一个任务 | 课后测试题及答案
- PHP image making
- 【ICML2022】利用虚拟节点促进图结构学习
- Final review of scientific and technological literature of Shandong University (Personal Crash Course)
- Lesson 016: sequence | after class test questions and answers
- 78- several methods to solve SQL performance problems without changing code in production system
- 杰理之开启四声道通话近端变调问题【篇】
- Lesson 020: functions: embedded functions and closures | after class test questions and answers
- The interception of Chinese and English strings in Oracle database is different
猜你喜欢

引入稀疏激活机制!Uni-Perceiver-MoE显著提升通才模型的性能
![[book delivery at the end of the article] AI has spread all over the Internet to color old photos. Here is a detailed tutorial!](/img/f0/4f237e7ab1bff9761b6092dd4ef3d9.png)
[book delivery at the end of the article] AI has spread all over the Internet to color old photos. Here is a detailed tutorial!
![kali2021安装RTL8188GU无线网卡[TL-WN726N]驱动](/img/29/8dd188cc4e909562862b5f2c57c898.png)
kali2021安装RTL8188GU无线网卡[TL-WN726N]驱动

杰理之硬件上 DACL 输出,DAC 输出左右声道的声音【篇】

杰理之动态切换 EQ 文件【篇】

【ICML2022】利用虚拟节点促进图结构学习
![[redis] cluster and common errors](/img/a5/94906b62b1ec0d549f9b72ff3db7f2.png)
[redis] cluster and common errors

Lesson 026: Dictionary: when the index is not easy to use 2 | after class test questions and answers

The third training of Hongmeng

长安旗下阿维塔科技增资扩股落定:宁德时代将持股约24%!
随机推荐
第028讲:文件:因为懂你,所以永恒 | 课后测试题及答案【无标题】
Analysis of fegin
[records of different objects required by QIPA]
Lesson 027: Set: in my world, you are the only after-school test question and answer
第025讲:字典:当索引不好用时 | 课后测试题及答案
Lesson 032: exception handling: you can't always be right | after class test questions and answers
LeetCode 每日一题——513. 找树左下角的值
Operation of simulation test platform for 2022 Shandong safety officer C certificate test
Introduce sparse activation mechanism! Uni perceiver MOE significantly improves the performance of generalist model
基于AI驱动大分子药物发现,「华深智药」获近5亿元A轮融资
TC397 Flash
When the AUX1 or aux2 channel is used in Jerry's aux mode, the program will reset the problem [chapter]
HarmonyOS应用开发培训第二次
LeetCode#20. Valid parentheses
Leetcode daily question - 513 Find the value in the lower left corner of the tree
300. longest increasing subsequence ●●
PlainSelect. getGroupBy()Lnet/sf/jsqlparser/statement/select/GroupByElement;
Laravel+ pagoda planning task
Microsoft edge browser will support network speed measurement, built-in calculator and unit conversion tool
LeetCode#20.有效的括号