茄子的个人空间

【课程设计】图的创建和遍历

字数统计: 1.3k阅读时长: 7 min
2021/08/13
loading

问题描述

本课程设计主要完成邻接矩阵和邻接表两种不同存储方式的图的建立和遍历,其中遍历部分分别进行了DFS和BFS两种不同形式的遍历。

实现代码

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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<stack>
#include<queue>
using namespace std;

/********************************图的存储结构定义***********************/
#define MaxVerNum 30
#define Vextype char
#define EdgeInfoType int

#define INF 999 //无穷大
#define MAXSIZE 100

typedef struct
{
Vextype vexs[MaxVerNum];
EdgeInfoType edges[MaxVerNum][MaxVerNum];
int n, e;
}MGragh;

typedef struct node
{
int adjvex;
EdgeInfoType Info;
struct node * next;
}EdgeNode;

typedef struct vnode
{
Vextype vertex;
EdgeNode *firstedge;
}VertexNode;

typedef struct
{
VertexNode adjlist[MaxVerNum];
int n, e;
}ALGraph;

int visited[MaxVerNum]; //顶点访问标记

/*建立图G的邻接矩阵 */
int returnId(MGragh *g, char c){
//返回c在数组中的下标
for (int i = 0; i<MaxVerNum; ++i)
{
if (g->vexs[i] == c) return i;
}
return -1;
}
void CreateGraph(MGragh *g)
{
scanf("%d %d\n", &(g->n), &(g->e));

char c;
int i = 0;
while (1)
{
while ((c = getchar()) == ' ');
if (c == '\n') break;
g->vexs[i++] = c;
}
Vextype s, e;
EdgeInfoType cost;

for (i = 0; i<g->n; i++)
{
for (int j = 0; j<g->n; j++)
{
g->edges[i][j] = 0;
}
}

for (int i = 0; i<g->e; ++i)
{
scanf("%c %c %d\n", &s, &e, &cost);
g->edges[returnId(g, s)][returnId(g, e)] = cost;
}

}

/* 根据图的邻接矩阵建立图的邻接表 */
void CreateALGraph(MGragh *mg, ALGraph *alg)
{
alg->n = mg->n; alg->e = mg->e;
for (int i = 0; i<alg->n; ++i)
{
alg->adjlist[i].vertex = mg->vexs[i];
}
int i, j;
EdgeNode *s;
for (int i = 0; i < alg->n; ++i)
{
for (int j = 0; j < alg->n; ++j)
{
if (mg->edges[i][j] != 0)
{
s = (EdgeNode*)malloc(sizeof(EdgeNode));
s->adjvex = j;
s->Info = mg->edges[i][j];
s->next = NULL;
EdgeNode*p = alg->adjlist[i].firstedge;
s->next = p;
alg->adjlist[i].firstedge = s;
}
}
}
}

//打印图(邻接矩阵)
void printGragh(MGragh *g)
{
printf("\n图G的邻接矩阵\n");
printf("顶点:\n");
for (int i = 0; i<g->n; i++)
{
printf("%c\t", g->vexs[i]);
}
printf("\n邻接矩阵:\n");
for (int i = 0; i<g->n; i++)
{
for (int j = 0; j<g->n; j++)
{
printf("%d\t", g->edges[i][j]);
}
printf("\n");
}
}

//打印图(邻接表)
void printALGragh(ALGraph *g)
{
printf("\n图G的邻接表\n");

for (int i = 0; i<g->n; i++)
{
printf("%c:", g->adjlist[i].vertex);

EdgeNode* edge = g->adjlist[i].firstedge;
while (edge)
{
printf("-->");
printf("%d:%d\t", edge->adjvex, edge->Info);
edge = edge->next;
}
printf("%\n");
}

}



/**********************DFS*********************/
//从顶点v开始图(邻接矩阵)的深度遍历
void DFS_MG(MGragh *g, int v)
{
int j;
visited[v] = 1;
printf("%c ", g->vexs[v]);

for (j = 0; j<g->n; ++j)
{
if (g->edges[v][j] != 0 && !visited[j])
DFS_MG(g, j);
}

}

//图的(邻接矩阵)的深度遍历
void DFSTranverse_MG(MGragh *g)
{
int i;
for (i = 0; i<g->n; ++i){
visited[i] = 0; //初始化访问数组visited的元素值为false
}
for (i = 0; i<g->n; ++i){
if (!visited[i]){ //节点尚未访问
DFS_MG(g, i);
}
}
}

//从顶点v开始图(邻接表)的深度遍历
void DFS_ALG(ALGraph *g, int v)
{
visited[v] = 1;
printf("%c ", g->adjlist[v].vertex);

EdgeNode *p =g->adjlist[v].firstedge;
while (p){
if (!visited[p->adjvex]){
DFS_ALG(g, p->adjvex); //递归深度遍历
}
p = p->next;
}

}

//图(邻接表)的深度遍历
void DFSTranverse_ALG(ALGraph *g)
{
int i;
for (i = 0; i<g->n; ++i){
visited[i] = 0; //初始化访问数组visited的元素值为false
}
for (i = 0; i<g->n; ++i){
if (!visited[i]){ //节点尚未访问
DFS_ALG(g, i);
}
}
}

/**************************BFS****************************/

//从顶点v开始图(邻接矩阵)的广度遍历
void BFS_MG(MGragh *g, int v)
{

int j;
queue<int> Q;
visited[v] = 1;
printf("%c ", g->vexs[v]);

Q.push(v);

while (!Q.empty())
{

v = Q.front();
Q.pop();

for (j = 0; j<g->n; ++j)
{
if (!visited[j] && g->edges[v][j] !=0)// INFINITY)
{
visited[j] = 1;
printf("%c ", g->vexs[j]);
Q.push(j);
}
}
}
}

//图(邻接矩阵)的广度遍历
void BFSTranverse_MG(MGragh *g)
{
int i;
for (i = 0; i<g->n; ++i){
visited[i] = 0; //初始化访问数组visited的元素值为false
}
for (i = 0; i<g->n; ++i){
if (!visited[i]){ //节点尚未访问
BFS_MG(g, i);
}
}
}

//从顶点v开始图(邻接表)的广度遍历
void BFS_ALG(ALGraph *g, int v)
{
queue<int > Q;
visited[v] = 1;
printf("%c ", g->adjlist[v].vertex);

Q.push(v);
while (!Q.empty()){
v = Q.front();
Q.pop();
EdgeNode *p = g->adjlist[v].firstedge;
while (p){
if (!visited[p->adjvex]){
visited[p->adjvex] = 1;
printf("%c ", g->adjlist[p->adjvex].vertex);
Q.push(p->adjvex);
}
p = p->next;
}
}

}

//图(邻接表)的广度遍历
void BFSTranverse_ALG(ALGraph *g)
{
int i;
for (i = 0; i<g->n; ++i){
visited[i] = 0; //初始化访问数组visited的元素值为false
}
for (i = 0; i<g->n; ++i){
if (!visited[i]){ //节点尚未访问
BFS_ALG(g, i);
}
}
}

/************************初始化与销毁********************************/
MGragh *init_MGraph()
{
MGragh *mg = (MGragh *)malloc(sizeof(MGragh));
if (mg)
{
mg->n = 0;
mg->e = 0;
}

return mg;
}

ALGraph *init_ALGraph()
{
ALGraph *alg = (ALGraph *)malloc(sizeof(ALGraph));
if (alg)
{
alg->n = 0;
alg->e = 0;
for (int i = 0; i<MaxVerNum; i++)
alg->adjlist[i].firstedge = NULL;
}

return alg;
}

void destroy_MGraph(MGragh **g)
{
if (*g)
{
free(*g);
*g = NULL;
}
}

void destroy_ALGraph(ALGraph **g)
{
for (int i = 0; i < (*g)->n; ++i)
{
EdgeNode *p = (*g)->adjlist[i].firstedge;
while (p)
{
EdgeNode *q = p->next;
free(p);
p = q;
}
}

if (*g)
{
free(*g);
*g = NULL;
}
}

/****main函数*************/
int main()
{
freopen("数据.txt", "r", stdin);
//创建图(邻接矩阵)
MGragh *mG = init_MGraph();
CreateGraph(mG);
printGragh(mG);

//创建图(邻接表)
ALGraph *alG = init_ALGraph();
CreateALGraph(mG, alG);
printALGragh(alG);


//DFS遍历
printf("\nDFS遍历:\n");
printf("邻接矩阵:\n");
DFSTranverse_MG(mG);
printf("\n邻接表:\n");
DFSTranverse_ALG(alG);

//BFS遍历
printf("\n\nBFS遍历:\n");
printf("邻接矩阵:\n");
BFSTranverse_MG(mG);
printf("\n邻接表:\n");
BFSTranverse_ALG(alG);

//销毁图
destroy_MGraph(&mG);
destroy_ALGraph(&alG);

}
//运行成功 20195140:19:36

运行结果

img
CATALOG
  1. 1. 问题描述
  2. 2. 实现代码
  3. 3. 运行结果