-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAGraph.cpp
More file actions
356 lines (284 loc) · 7.09 KB
/
AGraph.cpp
File metadata and controls
356 lines (284 loc) · 7.09 KB
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
/********************************************************************
Time: 2015/10/09
Filename: AGraph
Author: dinglj
Purpose: 邻接表实现图
*********************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
#define MAXSIZE 20 // 顶点最大数目
// 边元素
typedef struct
{
int weight; // 变权值
}EdgeType;
// 边节点
typedef struct ENode
{
EdgeType e;
int adjvex; // 邻接点
struct ENode *next; // 指向下一条边
}ENode;
// 顶点元素
typedef struct
{
int no; // 编号
char info; // 信息
}VertexType;
// 顶点节点结构
typedef struct
{
VertexType v;
struct ENode *firstEdge; // 指向第一条边
}VNode;
// 邻接表
typedef struct
{
VNode vArr[MAXSIZE]; // 顶点数组
int vertexNum, edgeNum;
}AGraph;
//////////////////////////////////////////////////////////////////////
// 基本函数的申明
bool InitGraph(AGraph &G);
bool CreateGraph(AGraph &G);
bool DestroyGraph(AGraph &G);
bool PrintGraph(AGraph G);
/********************************************************************
Method: getVertexType
Parameter:
Returns:
Purpose: 根据顶点信息返回节点编号
*********************************************************************/
int getVertexNo(AGraph G, char info)
{
for (int vertexNo = 0; vertexNo < G.vertexNum; vertexNo++)
{
if (info == G.vArr[vertexNo].v.info)
{
return vertexNo;
}
}
return -1;
}
//////////////////////////////////////////////////////////////////////
// 遍历
int isVisited[MAXSIZE]; // 用于标记节点是否被访问
// 访问单个节点
void visit(VNode v)
{
printf("%c\t", v.v.info);
}
// 深度优先遍历
bool DFS(AGraph G, int v);
bool DFSTraverse(AGraph G);
// 广度优先搜素
bool BFS(AGraph G, int v);
bool BFSTraverse(AGraph G);
//////////////////////////////////////////////////////////////////////
/********************************************************************
Method: InitGraph
Parameter:
Returns:
Purpose: 初始化邻接表
*********************************************************************/
bool InitGraph(AGraph &G)
{
for (int vertexNo = 0; vertexNo < MAXSIZE; vertexNo++)
{
G.vArr[vertexNo].firstEdge = NULL;
}
G.vertexNum = 0;
G.edgeNum = 0;
return true;
}
/********************************************************************
Method: CreateGraph
Parameter:
Returns:
Purpose: 建立邻接表
*********************************************************************/
bool CreateGraph(AGraph &G)
{
cout << "请输入顶点数" << endl;
cin >> G.vertexNum;
cout << "请输入顶点信息" << endl;
for (int vertexNo = 0; vertexNo < G.vertexNum; vertexNo++)
{
cin >> G.vArr[vertexNo].v.info;
}
cout << "请输入边的数目" << endl;
cin >> G.edgeNum;
for (int edgeNo = 0; edgeNo < G.edgeNum; edgeNo++)
{
cout << "请输入起点,终点和边权值" << endl;
char start, end;
int weight;
cin >> start >> end >> weight;
int startNo, endNo;
startNo = getVertexNo(G, start);
endNo = getVertexNo(G, end);
// 新建一个边节点
ENode *pNewENode = (ENode *)malloc(sizeof(ENode));
pNewENode->e.weight = weight;
pNewENode->adjvex = endNo;
pNewENode->next = NULL;
// 插入
pNewENode->next = G.vArr[startNo].firstEdge;
G.vArr[startNo].firstEdge = pNewENode;
}
return true;
}
/********************************************************************
Method: DestroyGraph
Parameter:
Returns:
Purpose: 销毁邻接表
*********************************************************************/
bool DestroyGraph(AGraph &G)
{
for (int vertexNo = 0; vertexNo < G.vertexNum; vertexNo++)
{
ENode *pDeleteENode = G.vArr[vertexNo].firstEdge;
while (pDeleteENode)
{
G.vArr[vertexNo].firstEdge = pDeleteENode->next;
free(pDeleteENode);
pDeleteENode = G.vArr[vertexNo].firstEdge;
}
}
G.vertexNum = 0;
G.edgeNum = 0;
return true;
}
/********************************************************************
Method: PrintGraph
Parameter:
Returns:
Purpose: 输出每一条边的信息
*********************************************************************/
bool PrintGraph(AGraph G)
{
for (int vertexNo = 0; vertexNo < G.vertexNum; vertexNo++)
{
ENode *pPrintENode = G.vArr[vertexNo].firstEdge;
while (pPrintENode)
{
printf("%d,%d\t", pPrintENode->adjvex, pPrintENode->e.weight);
pPrintENode = pPrintENode->next;
}
printf("\n");
}
return true;
}
//////////////////////////////////////////////////////////////////////
/********************************************************************
Method: DFS,DFSTraverse
Parameter:
Returns:
Purpose: 深度优先遍历
*********************************************************************/
bool DFS(AGraph G, int v)
{
visit(G.vArr[v]); // 访问
isVisited[v] = 1;
ENode *pENode = G.vArr[v].firstEdge;
while (pENode) // 类似于二叉树的先序遍历
{
if (!isVisited[pENode->adjvex])
{
DFS(G, pENode->adjvex);
}
pENode = pENode->next;
}
return true;
}
bool DFSTraverse(AGraph G)
{
for (int vertexNo = 0; vertexNo < G.vertexNum; vertexNo++)
{
isVisited[vertexNo] = 0;
}
// int num = 0;
// 对每一个节点进行DFS,如果是连通图,则for循环实际上只会执行一次
for (int vertexNo = 0; vertexNo < G.vertexNum; vertexNo++)
{
if (!isVisited[vertexNo])
{
DFS(G, vertexNo);
// num++; // num为连通分量数目
}
}
printf("\n");
return true;
}
/********************************************************************
Method: BFS
Parameter:
Returns:
Purpose: 广度优先搜素
*********************************************************************/
bool BFS(AGraph G, int v)
{
ENode *pENode;
// 辅助队列
int queue[MAXSIZE];
int front = 0, rear = 0;
queue[rear++] = v; // 顶点入队列
visit(G.vArr[v]); // 访问,标记
isVisited[v] = 1;
while (front != rear) // 队列不空
{
v = queue[front++]; // 出队列
pENode = G.vArr[v].firstEdge; // pENode指向第一条边
while (pENode)
{
if (!isVisited[pENode->adjvex])
{
queue[rear++] = pENode->adjvex; // 入队,访问,标记
visit(G.vArr[pENode->adjvex]);
isVisited[pENode->adjvex] = 1;
}
pENode = pENode->next;
}
}
return true;
}
bool BFSTraverse(AGraph G)
{
for (int vertexNo = 0; vertexNo < G.vertexNum; vertexNo++)
{
isVisited[vertexNo] = 0;
}
for (int vertexNo = 0; vertexNo < G.vertexNum; vertexNo++)
{
if (!isVisited[vertexNo])
{
BFS(G, vertexNo);
}
}
return true;
}
/*
测试函数
*/
bool OperationTest()
{
AGraph G;
InitGraph(G);
CreateGraph(G);
PrintGraph(G);
DFSTraverse(G);
BFSTraverse(G);
DestroyGraph(G);
return true;
}
/*
主函数
*/
int main()
{
OperationTest();
return true;
}