-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBDFS.cpp
211 lines (197 loc) · 3.61 KB
/
BDFS.cpp
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
/*图的遍历,深度优先,广度优先及其应用*/
#include<stdio.h>
#include<stdlib.h>
#define MAXV 10
#define MAX 30
#define INF 32767
typedef struct {
int no;
int info;
}Vertex;
typedef struct{
int edges[MAXV][MAXV];
int n, e;
int vexs[MAXV];
}matGraph;
typedef struct Anode{
int adjvex;
struct Anode* nextarc;
int weight;
}arcNode;
typedef struct Vnode{
int info;
arcNode* firstarc;
}Vnode;
typedef struct{
Vnode adjlist[MAXV];
int e, n;
}adjGraph;
void matToLink(matGraph g, adjGraph* &G) //由邻接矩阵创建邻接表
{
G = (adjGraph*)malloc(sizeof(adjGraph));
for(int i = 0; i < g.n; i ++)
{
G -> adjlist[i].info = i;
G -> adjlist[i].firstarc = NULL;
}
arcNode* p;
for(int i = 0; i < g.n; i ++)
{
for(int j = g.n - 1; j >= 0; j --)
{
if(g.edges[i][j] != 0 && g.edges[i][j] != INF)
{
p = (arcNode*)malloc(sizeof(arcNode));
p -> weight = g.edges[i][j];
p -> adjvex = j;
p -> nextarc = G -> adjlist[i].firstarc;
G -> adjlist[i].firstarc = p;
}
}
}
G -> n = g.n;
G -> e = g.e;
}
int visited[MAXV] = {0};
void DFS(adjGraph* G, int v) //深度优先遍历图
{
arcNode*p = G -> adjlist[v].firstarc;
printf("%d ",v);
visited[v] = 1;
while(p)
{
if(visited[p -> adjvex] == 0)
DFS(G, p -> adjvex);
p = p -> nextarc;
}
}
void DFS1(adjGraph* G) //对于非连通图使用深度优先
{
for(int i = 0; i < G -> n; i ++)
{
if(visited[i] == 0)
DFS(G, i);
}
}
void BFS(adjGraph* G, int v) //广度优先遍历图
{
int queue[MAX];
int rear = -1, front = -1;
printf("%d ", v);
rear ++;
queue[rear] = v;
visited[v] = 1;
while(rear != front)
{
int u = queue[++front];
arcNode* p = G -> adjlist[u].firstarc;
while(p)
{
if(visited[p -> adjvex] == 0)
{
printf("%d ", p -> adjvex);
visited[p -> adjvex] = 1;
rear ++;
queue[rear] = p -> adjvex;
}
p = p -> nextarc;
}
}
}
void BFS1(adjGraph* G) //对于非连通图使用广度优先遍历
{
for(int i = 0; i < MAXV; i ++)
visited[i] = 0;
for(int i = 0; i < G -> n; i ++)
{
if(visited[i] == 0)
{
BFS(G, i);
}
}
}
void print(adjGraph* G) //打印邻接表
{
arcNode* p;
for(int i = 0; i < G -> n; i ++)
{
printf("%d: ", G -> adjlist[i].info);
p = G -> adjlist[i].firstarc;
while(p)
{
printf("->[%d]%d ", p -> weight, p -> adjvex);
p = p -> nextarc;
}
printf("\n");
}
printf("\n");
}
bool connect(adjGraph* G) //判断该图是否连通
{
for(int i = 0; i < G -> n; i ++)
{
visited[i] = 0;
}
DFS(G, 1);
for(int i = 0; i < G -> n; i ++)
{
if(visited[i] == 0)
return false;
}
return true;
}
void destroy(adjGraph* & G) //销毁邻接表
{
arcNode* pre, *p;
for(int i = 0; i < G -> n; i ++)
{
pre = G -> adjlist[i].firstarc;
if(pre)
{
p = pre -> nextarc;
while(p)
{
free(pre);
pre = p;
p = p -> nextarc;
}
free(pre);
}
}
free(G);
}
int main()
{
matGraph g;
adjGraph* G;
g.n = 5;
g.e = 6;
for(int i = 0; i < g.n; i ++)
{
for(int j = 0; j < g.n; j ++)
g.edges[i][j] = INF;
}
g.edges[1][0] = 10;
g.edges[0][3] = 7;
g.edges[2][0] = 9;
g.edges[1][2] = 8;
g.edges[2][3] = 6;
//g.edges[2][4] = 5; //使5成为孤立点,G成为非连通图
matToLink(g, G);
print(G);
/*
DFS(G, 1); //对于连通图的深度遍历和广度遍历
printf("\n");
BFS(G, 1);
*/
DFS1(G);
printf("\n");
BFS1(G);
printf("\n");
if(connect(G))
printf("it is connect!");
else
printf("it is unconnect!");
destroy(G);
return 0;
}