当前位置:网站首页>6-5 图的深度遍历-邻接矩阵实现
6-5 图的深度遍历-邻接矩阵实现
2022-06-22 20:23:00 【白—】
6-5 图的深度遍历-邻接矩阵实现
本题要求实现邻接矩阵存储图的深度优先遍历。
函数接口定义:
void DFS(MGraph G,Vertex v);
其中MGraph是邻接矩阵存储的图,定义如下:
#define MaxVertexNum 10 /定义最大顶点数/
typedef int Vertex;/* 用顶点下标表示顶点,为整型 */
typedef struct{
int arcs[MaxVertexNum][MaxVertexNum]; /邻接矩阵/
int vexnum,arcnum; /图中的顶点数vexnum和边数arcnum/
}MGraph; /用邻接矩阵表示的图的类型/
裁判测试程序样例:
#include"stdio.h"
#include"stdlib.h"
typedef enum{FALSE,TRUE} Boolean;
#define MaxVertexNum 10 /定义最大顶点数/
typedef int Vertex;/* 用顶点下标表示顶点,为整型 /
typedef struct{
int arcs[MaxVertexNum][MaxVertexNum]; /邻接矩阵/
int vexnum,arcnum; /图中的顶点数vexnum和边数arcnum/
}MGraph; /用邻接矩阵表示的图的类型/
Boolean visited[MaxVertexNum]; / 顶点的访问标记 */
void CreatMGraph(MGraph G);/ 创建图并且将Visited初始化为false;裁判实现,细节不表 /
void DFS(MGraph G,Vertex v);
int main()
{
Vertex v;
MGraph G;
CreatMGraph(&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 1 3 0 2 4 6
代码:
void DFS(MGraph G,Vertex v)
{
visited[v] = 1;
printf(" %d",v);
for(int i = 0; i < G.vexnum ; i++)
if(G.arcs[v][i] ==1 && !visited[i])
DFS(G, i);
return;
}
202206201636一
边栏推荐
- One line of code binds swiftui view animation for a specific state
- 第030讲:文件系统:介绍一个高大上的东西 | 课后测试题及答案
- ACM. Hj24 chorus ●●
- Lesson 029: Documents: a task? After class test questions and answers
- [redis]redis6 transaction operations
- 杰理之硬件上 DACL 输出,DAC 输出左右声道的声音【篇】
- 牛客 52次月赛 C 说谎的机器 (区间赋值操作由O(n^2)转为O(n)的复杂度)
- 什么是数据资产?数据资产管理应该如何落地?
- Arcgis中las点云数据抽稀
- Lesson 020: functions: embedded functions and closures | after class test questions and answers
猜你喜欢

Arcgis中las点云数据抽稀

杰理之开启四声道通话近端卡顿问题【篇】

万字长文 | 使用 RBAC 限制对 Kubernetes 资源的访问
![[redis] cluster and common errors](/img/a5/94906b62b1ec0d549f9b72ff3db7f2.png)
[redis] cluster and common errors

The third training of Hongmeng

How swiftui simulates the animation effect of view illumination increase

The second harmonyos application development training

第030讲:文件系统:介绍一个高大上的东西 | 课后测试题及答案

2022 a special equipment related management (elevator) examination questions and simulation examination
![[redis]redis6 transaction operations](/img/50/639867a2fcb92082ea262a8a19bb68.png)
[redis]redis6 transaction operations
随机推荐
ACM. HJ45 名字的漂亮度 ●●
Lesson 018: function: flexible is powerful after class test questions and answers
Redis core technology and practice: learning summary directory
Jericho uses DP and DM for IO key detection [chapter]
90- review of several recently optimized Oracle databases
优化求解器 | Gurobi的MVar类:矩阵建模利器、求解对偶问题的备选方案 (附详细案例+代码)
基于AI驱动大分子药物发现,「华深智药」获近5亿元A轮融资
第029讲:文件:一个任务 | 课后测试题及答案
94-SQL优化案例一则(用到的写法经常是被嫌弃的)
2022 a special equipment related management (elevator) examination questions and simulation examination
Leetcode daily question - 513 Find the value in the lower left corner of the tree
第022讲:函数:递归是神马 | 课后测试题及答案
为了不曾忘却的纪念:孙剑专题
88- widely circulated parameter optimization, honey or poison?
Summary of differences between localstorage, sessionstorage and cookies
Jerry's plug-in 4m flash to view the processing method with a size of only 1m on the PC [chapter]
Adblock屏蔽百度热搜
杰理之使用 DP 和 DM 做 IO 按键检测注意点【篇】
[redis]redis6 transaction operations
杰理之开启四声道通话近端变调问题【篇】