0%

图的深度优先遍历

问题描述

分别用邻接矩阵和邻接链表两种数据结构实现图的深度优先遍历算法,输出遍历的结点序列,并分析算法的时间复杂度。

提交格式:

邻接矩阵数据结构实现void solveA(int n, int m, int e[][2], int out[])函数。

邻接链表数据结构实现void solveB(int n, int m, int e[][2], int out[])函数。

参数n为结点个数,m为边条数,e为所有边,out为输出序列。1<=n<=3000,1<=m<=100000,0<=e[i][j]<n。

遍历的起始结点为0,邻接矩阵数据结构中按行从左到右遍历邻居结点,邻接链表数据结构中按存储顺序遍历邻居结点,图为无向图。

请不要printf输出任何内容。

输入样例:

1
n=5,m=10,e={{2,4},{3,0},{0,2},{3,1},{4,1},{0,1},{3,4},{2,3},{1,2},{0,4}} 

输出样例:

1
2
solveA:out={0,1,2,3,4} 
solveB:out={0,3,1,4,2}
核心思路
  1. 每种solve里都需调用相应的create和dfs;
  2. 实现相应的create和dfs;
  3. 构造主函数进行调试。
实现要点
  1. 动态分配内存创建数组;
  2. 函数间参数的传递;
  3. visited[]的初始化;
  4. 非连通图的考虑;
  5. 邻接表深度遍历时的回退。
code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#include <stdio.h>
#include <stdlib.h>

int i, j,k,s;

typedef struct
{
int** arc; //邻接矩阵 可看作边表
int numVertexes, numEdges;
}MGraph;

//图的邻接链表存储结构
//边表节点结构,一个adjvex用来存储邻接点的位置,一个next指针用来指向下一个节点
typedef struct EdgeNode
{
int adjvex; //存储顶点下标信息
struct EdgeNode* next;
} EdgeNode;

//顶点表节点结构
typedef struct
{
int Vexs; //用来存储顶点信息
EdgeNode* firstedge; //用来存储当前顶点的下一个顶点
} VexList;

//这里用动态数组存储顶点表,然后numVertex,numEdge是一个图的顶点数和边数
typedef struct
{
VexList* q;
int Vertexs, Edges;
} LGraph;

//参数n为结点个数,m为边条数,e为所有边,out为输出序列。1 <= n <= 3000, 1 <= m <= 100000, 0 <= e[i][j] < n。
void solveA(int n, int m, int e[][2], int out[]);//邻接矩阵
void create(MGraph* G, int e[][2]);
void DFS(MGraph* G, int i, int* visited, int outA[]);
void solveB(int n, int m, int e[][2], int out[]);//邻接链表
void Lcreate(LGraph* G, int e[][2]);
void LDFS(LGraph* G, int i, int* visited, int outB[]);

int main() {
int N = 0, M = 0;
printf_s("请输入顶点数:\n");
scanf_s("%d", &N);
printf_s("请输入连边数:\n");
scanf_s("%d", &M);
if (N < 1 || N > 3000 || M < 1 || M > 100000)
{
return -1;
}
int(*E)[2] = NULL;
E = (int(*)[2])malloc(sizeof(int) * M * 2);
int* OUT = (int*)malloc(sizeof(int) * N);
if (E == NULL ||OUT==NULL)
{
return -2;
}
printf_s("请输入所有连边:\n");
for (int i = 0; i < M; i++)
{
for (int j = 0; j < 2; j++)
{
scanf_s("%d", &E[i][j]);
}
}
solveA(N, M, E, OUT);
for (i = 0; i < N; i++)
{
printf("%d ", OUT[i]);
}
printf("\n");
solveB(N, M, E, OUT);
for (i = 0; i < N; i++)
{
printf("%d ", OUT[i]);
}
system("pause");
return 0;
}

void solveA(int n, int m, int e[][2], int out[])
{
s = 0;
MGraph* G = (MGraph*)malloc(sizeof(MGraph));
G->numVertexes = n;
G->numEdges = m;
create(G, e);
int* visited;
visited = (int*)calloc(G->numVertexes, sizeof(int));
for (i = 0; i < G->numVertexes; i++)
if (visited[i] == 0)
DFS(G, i, visited, out);
}

void create(MGraph* G, int e[][2])
{
G->arc = (int**)malloc(sizeof(int*) * G->numVertexes);
for (i = 0; i < G->numVertexes; i++)
{
G->arc[i] = (int*)calloc(G->numVertexes, sizeof(int));
}
for (j = 0; j < G->numEdges; j++)
{
G->arc[e[j][0]][e[j][1]] = 1;
G->arc[e[j][1]][e[j][0]] = 1;
}
}

void DFS(MGraph* G, int i, int* visited, int outA[])
{
outA[s++] = i;
visited[i] = 1; //被访问的标记
for (int k = 0; k < G->numVertexes; k++)
{
if (G->arc[i][k] == 1 && visited[k] == 0) //边(i,j)存在且j顶点未被访问,递归
DFS(G, k, visited, outA);
}
}

void solveB(int n, int m, int e[][2], int out[])
{
s = 0;
LGraph* G = (LGraph*)malloc(sizeof(LGraph));
G->Vertexs = n;
G->Edges = m;
Lcreate(G, e);
int* visited;
visited = (int*)calloc(G->Vertexs, sizeof(int));
for (i = 0; i < G->Vertexs; i++)
if (visited[i] == 0)
{
k = 1;
LDFS(G, i, visited, out);
}

}

void Lcreate(LGraph* G, int e[][2])
{
G->q = (VexList*)malloc(sizeof(VexList) * G->Vertexs);
for (i = 0; i < G->Vertexs; i++)
{
G->q[i].Vexs = i;
G->q[i].firstedge = NULL;
}
for (j = 0; j < G->Edges; j++)
{
EdgeNode* node1 = (EdgeNode*)malloc(sizeof(EdgeNode));
node1->adjvex = e[j][1];
node1->next = NULL;
if (G->q[e[j][0]].firstedge == NULL)
{
G->q[e[j][0]].firstedge = node1;
}
else
{
EdgeNode* w = G->q[e[j][0]].firstedge;
while (w->next != NULL)
{
w = w->next;
}
w->next = node1;
}
EdgeNode* node2 = (EdgeNode*)malloc(sizeof(EdgeNode));
node2->adjvex = e[j][0];
node2->next = NULL;
if (G->q[e[j][1]].firstedge == NULL)
{
G->q[e[j][1]].firstedge = node2;
}
else
{
EdgeNode* w = G->q[e[j][1]].firstedge;
while (w->next != NULL)
{
w = w->next;
}
w->next = node2;
}
}
}

void LDFS(LGraph* G, int i, int* visited, int outB[])
{
if (k==1)
{
j = s;
outB[s++] = i;
visited[i] = 1; //被访问的标记
}
if (G->q[i].firstedge != NULL)
{
EdgeNode* t = (G->q[i].firstedge);
while (visited[t->adjvex] == 1 && t->next != NULL)
{
t = t->next;
}
if (visited[t->adjvex] == 1 && t->next == NULL)
{
if (j != -1)
{
k = 0;
j--;
LDFS(G, outB[j], visited, outB);
}
else
{
return;
}
}
else
{
k = 1;
LDFS(G, t->adjvex, visited, outB);
}
}
}
-------------本文结束感谢您的阅读-------------

欢迎关注我的其它发布渠道