当前位置:网站首页>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一
边栏推荐
- Lesson 028: Documents: because I know you, I will never forget the after-school test questions and answers [no title]
- Final review of scientific and technological literature of Shandong University (Personal Crash Course)
- How swiftui simulates the animation effect of view illumination increase
- Lesson 027: Set: in my world, you are the only after-school test question and answer
- 嵌入式开发基础之任务管理(线程管理)
- Lesson 022: function: recursion is god horse after class test questions and answers
- The interception of Chinese and English strings in Oracle database is different
- 校园跑腿管理端APP—陕西格创
- 5分钟快速上线Web应用和API(Vercel)
- NFT,只可远观不可亵玩焉
猜你喜欢
![Jerry's plug-in 4m flash to view the processing method with a size of only 1m on the PC [chapter]](/img/8a/49b23eb63ff7e8814d1d49282c9fa2.png)
Jerry's plug-in 4m flash to view the processing method with a size of only 1m on the PC [chapter]

Lesson 019: function: my site listen to my after-school test questions and answers

查询es分页下标超过1万

Redis usage scenario sharing (project practice)

Microsoft edge browser will support network speed measurement, built-in calculator and unit conversion tool

Lesson 029: Documents: a task? After class test questions and answers

Install MySQL in ECS (version 2022)

Redis core technology and practice: learning summary directory

The second harmonyos application development training
![[redis]redis persistence](/img/83/9af9272bd485028062067ee2d7a158.png)
[redis]redis persistence
随机推荐
2022 a special equipment related management (elevator) examination questions and simulation examination
(duc/ddc) digital up mixing / quadrature down mixing principle and MATLAB simulation
[redis]redis persistence
Lesson 032: exception handling: you can't always be right | after class test questions and answers
Learning cloud network from teacher Tang - openstack network implementation
万字长文 | 使用 RBAC 限制对 Kubernetes 资源的访问
Lesson 025: Dictionary: after class test questions and answers when the index is not easy to use
92 match for several_ Recognize SQL write example
las 点云创建网格
Lesson 030: file system: introduce a big thing | after class test questions and answers
LeetCode#20.有效的括号
Microsoft edge browser will support network speed measurement, built-in calculator and unit conversion tool
Cannot re-register id: PommeFFACompetition-v0问题解决
第030讲:文件系统:介绍一个高大上的东西 | 课后测试题及答案
Can the characteristics of different network structures be compared? Ant & meituan & NTU & Ali proposed a cross architecture self supervised video representation learning method CaCl, performance SOTA
Lesson 021: functions: lambda expressions | after class test questions and answers
es 按条件查询数据总条数
IDC发布中国数据治理报告 亿信华辰第一
85- have you learned any of these SQL tuning tips?
杰理之列免晶振一拖八烧录升级【篇】