在图的遍历中,深度优先的思想很简单,我们先指定一个起点,然后循环进行下列操作:如果这个节点没有被访问就访问它,并标记为已访问,然后继续访问它在邻接矩阵中第一个邻接节点,其中当一个节点的邻接节点被访问完就回溯到上一个节点,这个回溯操作并不需要我们自己完成,因为我们的深度优先搜索使用的是递归方式查找节点,所以当某个节点的邻接节点查找完,DFS函数自动销毁一层,返回到上一个节点继续进行查找节点操作,直到循环结束,整个遍历结果自然也就出来了。与广度优先遍历相比,实现深度优先更简单一点,只需要用到递归的思想,而广度优先遍历还需要借用队列来实现虽然也不复杂。
邻接矩阵图的存储:
邻接矩阵是对称的
1表示有边,也可也用其他数值表示为权值
0表示无边
邻接矩阵对称赋值操作
未进行对称前:
进行对称操作后:
对应的邻接矩阵实现,我在代码里就简单暴力手动输入了这些数据,只输入了上三角的数据,下三角根据邻接矩阵对称的特点进行自动赋值,黄色部分为上三角
我们将创建这个图,它的深度优先遍历为:ABCDHEFG
这里由于我们的邻接矩阵已经按字母顺序存储的情况下,根据DFS算法固然只有一种遍历结果,当然其他的遍历结果也可以实现,受邻接矩阵存储的顺序影响。
DFS核心代码:
完整代码(包括两大部分:邻接矩阵、DFS算法):
#include "stdio.h"
#include "stdlib.h"
#include "math.h"
#include "time.h"
// 禁用特定的警告
#pragma warning(disable:4996)
// 定义一些常量和数据类型
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define MAXVEX 8 /* 最大顶点数,用户定义 */
#define GRAPH_INFINITY 0 /* 用0表示∞,表示不存在边 */
/* 定义状态、顶点和边的类型 */
typedef int Status; /* Status是函数的返回类型,如OK表示成功 */
typedef char VertexType; /* 顶点的类型,用字符表示 */
typedef int EdgeType; /* 边上的权值类型,用整数表示 */
typedef int Boolean; /* 布尔类型 */
/* 访问标记数组 */
Boolean visited[MAXVEX];
/* 图的邻接矩阵结构体 */
typedef struct
{
VertexType vexs[MAXVEX]; /* 顶点表 */
EdgeType arc[MAXVEX][MAXVEX]; /* 邻接矩阵,表示边的权值 */
int numNodes, numEdges; /* 图中当前的顶点数和边数 */
} MGraph;
/* 创建一个无向网图的邻接矩阵表示 */
void CreateMGraph(MGraph* G)
{
int i, j, k, w;
// 初始化图的顶点数和边数
G->numNodes = 8;
G->numEdges = 12;
// 定义顶点标签
char Array[] = "ABCDEFGHI";
// 初始化邻接矩阵和顶点表
for (i = 0; i < G->numNodes; i++) {
for (j = 0; j < G->numNodes; j++) {
G->arc[i][j] = GRAPH_INFINITY; /* 初始化邻接矩阵为∞ */
}
G->vexs[i] = Array[i]; /* 初始化顶点表 */
}
G->arc[0][0] = GRAPH_INFINITY;
G->arc[0][1] = 1;
G->arc[0][2] = GRAPH_INFINITY;
G->arc[0][3] = GRAPH_INFINITY;
G->arc[0][4] = GRAPH_INFINITY;
G->arc[0][5] = 1;
G->arc[0][6] = GRAPH_INFINITY;
G->arc[0][7] = GRAPH_INFINITY;
G->arc[1][0] = GRAPH_INFINITY;
G->arc[1][1] = GRAPH_INFINITY;
G->arc[1][2] = 1;
G->arc[1][3] = GRAPH_INFINITY;
G->arc[1][4] = GRAPH_INFINITY;
G->arc[1][5] = GRAPH_INFINITY;
G->arc[1][6] = 1;
G->arc[1][7] = GRAPH_INFINITY;
G->arc[2][0] = GRAPH_INFINITY;
G->arc[2][1] = GRAPH_INFINITY;
G->arc[2][2] = GRAPH_INFINITY;
G->arc[2][3] = 1;
G->arc[2][4] = GRAPH_INFINITY;
G->arc[2][5] = GRAPH_INFINITY;
G->arc[2][6] = GRAPH_INFINITY;
G->arc[2][7] = GRAPH_INFINITY;
G->arc[3][0] = GRAPH_INFINITY;
G->arc[3][1] = GRAPH_INFINITY;
G->arc[3][2] = GRAPH_INFINITY;
G->arc[3][3] = GRAPH_INFINITY;
G->arc[3][4] = GRAPH_INFINITY;
G->arc[3][5] = GRAPH_INFINITY;
G->arc[3][6] = GRAPH_INFINITY;
G->arc[3][7] = 1;
G->arc[4][0] = GRAPH_INFINITY;
G->arc[4][1] = GRAPH_INFINITY;
G->arc[4][2] = GRAPH_INFINITY;
G->arc[4][3] = GRAPH_INFINITY;
G->arc[4][4] = GRAPH_INFINITY;
G->arc[4][5] = 1;
G->arc[4][6] = GRAPH_INFINITY;
G->arc[4][7] = 1;
G->arc[5][0] = GRAPH_INFINITY;
G->arc[5][1] = GRAPH_INFINITY;
G->arc[5][2] = GRAPH_INFINITY;
G->arc[5][3] = GRAPH_INFINITY;
G->arc[5][4] = GRAPH_INFINITY;
G->arc[5][5] = GRAPH_INFINITY;
G->arc[5][6] = 1;
G->arc[5][7] = 0;
G->arc[6][0] = GRAPH_INFINITY;
G->arc[6][1] = GRAPH_INFINITY;
G->arc[6][2] = GRAPH_INFINITY;
G->arc[6][3] = GRAPH_INFINITY;
G->arc[6][4] = GRAPH_INFINITY;
G->arc[6][5] = GRAPH_INFINITY;
G->arc[6][6] = GRAPH_INFINITY;
G->arc[6][7] = 1;
G->arc[7][0] = GRAPH_INFINITY;
G->arc[7][1] = GRAPH_INFINITY;
G->arc[7][2] = GRAPH_INFINITY;
G->arc[7][3] = GRAPH_INFINITY;
G->arc[7][4] = GRAPH_INFINITY;
G->arc[7][5] = GRAPH_INFINITY;
G->arc[7][6] = GRAPH_INFINITY;
G->arc[7][7] = GRAPH_INFINITY;
// 由于是无向图,邻接矩阵是对称的,需要将其对称
for (int i = 0; i < G->numNodes; i++) {
for (int j = 0; j < G->numNodes; j++) {
G->arc[j][i] = G->arc[i][j];
}
}
// 打印邻接矩阵
printf("邻接矩阵为:\n ");
for (int i = 0; i < G->numNodes; i++) {
printf("%d ", i); /* 打印列索引 */
}
printf("\n ");
for (int i = 0; i < G->numNodes; i++) {
printf("%c ", G->vexs[i]); /* 打印顶点标签 */
}
printf("\n");
for (int i = 0; i < G->numNodes; i++) {
printf("%d ", i); /* 打印行索引 */
printf("%c ", G->vexs[i]); /* 打印顶点标签 */
for (int j = 0; j < G->numNodes; j++) {
if (G->arc[i][j] == 1) {
printf("\033[31m%d \033[0m", G->arc[i][j]); /* 打印邻接矩阵中的权值 */
}
else {
printf("%d ", G->arc[i][j]); /* 打印邻接矩阵中的权值 */
}
}
printf("\n");
}
}
/* 深度优先搜索(DFS)函数 */
void DFS(MGraph G, int i) {
int j;
visited[i] = TRUE; /* 标记当前顶点为已访问 */
printf("%c->", G.vexs[i]); /* 打印当前顶点 */
/* 遍历所有相邻的顶点 */
for (j = 0; j < G.numNodes; j++) {
if (G.arc[i][j] == 1 && !visited[j]) { /* 如果存在边且未被访问 */
DFS(G, j); /* 递归调用DFS */
}
}
}
/* 深度优先遍历图的所有顶点 */
void DFSTraverse(MGraph G) {
int i;
/* 初始化所有顶点为未访问 */
for (i = 0; i < G.numNodes; i++) {
visited[i] = FALSE;
}
/* 对每个未访问的顶点调用DFS */
for (i = 0; i < G.numNodes; i++) {
if (!visited[i]) {
DFS(G, i);
}
}
}
int main(void)
{
MGraph G;
/* 创建图 */
CreateMGraph(&G);
/* 深度优先遍历图 */
DFSTraverse(G);
return 0;
}
运行结果: