-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraph.cpp
241 lines (213 loc) · 5.03 KB
/
Graph.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
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
#include <iostream>
#include <stdlib.h>
#define MAX 100
using namespace std;
/**
* 邻接矩阵
*/
typedef struct AMGraph{
char vexs[MAX];
int arcs[MAX][MAX];
int vexnum , arcnum;
}AMGraph;
/**
* 邻接表
*/
typedef struct EdgeNode{
int weight;
int adjvex;
EdgeNode *next;
}EdgeNode;
typedef struct VNode{
char data;
EdgeNode *firstNode;
}VNode , AdjList[MAX];
typedef struct Graph{
AdjList adjlist;
int vexNum;
int arcNum;
}Graph;
typedef struct Queue{
VNode node[MAX];
int Capacity;
int front , rear;
}Queue;
void CreateUDGraph(AMGraph *graph){
cout << "输入顶点数及边数:"<< endl;
cin >> graph->vexnum;
cin >> graph->arcnum;
cout << "输入顶点名称" <<endl;
for(int i = 0 ;i <graph->vexnum ;i ++){
cin >> graph->vexs[i];
}
cout << "输入边信息" <<endl;
for(int i = 0 ;i < graph->vexnum;i ++){
for(int j = 0;j < graph->vexnum ;j ++){
graph->arcs[i][j] = MAX;
}
}
for(int i = 0 ;i < graph->arcnum ;i ++){
int v1 , v2 ,weight;
cin >> v1 >> v2 >>weight;
graph->arcs[v1-1][v2-1] = weight;
graph->arcs[v2-1][v1-1] = weight;
}
}
void printUDGraph(AMGraph *graph){
for(int i = 0 ;i < graph->vexnum;i ++){
for(int j = 0 ;j < graph->vexnum ;j ++){
cout << graph->arcs[i][j] << " ";
}
cout << "" <<endl;
}
}
void CreateGraph(Graph **graph){
*graph = new Graph;
cout << "输入顶点数及边数:" <<endl;
cin >> (*graph)->vexNum >> (*graph)->arcNum;
cout <<"输入顶点名称" <<endl;
for(int i = 0 ;i < (*graph)->vexNum;i ++){
cin >> (*graph)->adjlist[i].data;
(*graph)->adjlist[i].firstNode = NULL;
}
cout <<"输入边的顶点及权重:" <<endl;
for(int a = 0 ;a < (*graph)->arcNum ; a ++){
int i , j , weight;
cin >> i >> j >>weight;
EdgeNode *node = new EdgeNode;
node->adjvex = j - 1;
node->weight = weight;
node->next = (*graph)->adjlist[i - 1].firstNode;
(*graph)->adjlist[i - 1].firstNode = node;
node = new EdgeNode;
node->adjvex = i - 1;
node->weight = weight;
node->next = (*graph)->adjlist[j - 1].firstNode;
(*graph)->adjlist[j - 1].firstNode = node;
}
}
bool Visited[MAX];
/**
* 邻接矩阵深度优先遍历
*/
void DFS(AMGraph * graph , int i){
Visited[i] = true;
cout << "已遍历" << graph->vexs[i] <<endl;
for(int j = 0 ; j < graph->vexnum;j ++){
if(graph->arcs[i][j] != MAX && !Visited[j]){
DFS(graph , j);
}
}
}
void DFSTraverse(AMGraph *graph){
cout << "DFS遍历:" <<endl;
for(int i = 0 ;i < graph->vexnum ; i ++){
Visited[i] = false;
}
for(int i = 0 ;i < graph->vexnum ; i++){
if(!Visited[i]){
DFS(graph , i);
}
}
}
/**
* 邻接表深度优先遍历
*/
void DFS2(Graph *graph , int i){
Visited[i] = true;
EdgeNode *node = graph->adjlist[i].firstNode;
cout << "已遍历" <<graph->adjlist[i].data << endl;
while(node != NULL){
if(!Visited[node->adjvex])
DFS2(graph , node->adjvex);
node = node->next;
}
}
void DFSTraverse2(Graph *graph){
cout << "DFS遍历:" <<endl;
for(int i = 0 ;i < graph->vexNum ;i ++){
Visited[i] = false;
}
for(int i = 0 ;i < graph->vexNum ;i ++){
if(!Visited[i]){
DFS2(graph , i);
}
}
}
/**
* 队满
*/
bool isQueueFull(Queue *queue){
return (queue->rear + 1) % queue->Capacity == queue->front;
}
/**
* 队空
*/
bool isQueueEmpty(Queue *queue){
return queue->rear == queue->front;
}
/**
* 入队
*/
bool EnQueue(Queue *queue , VNode vnode){
if(!isQueueFull(queue)){
queue->node[++ queue->rear] = vnode;
return true;
}
return false;
}
/**
* 出队
*/
VNode DeQueue(Queue *queue){
if(!isQueueEmpty(queue)){
VNode node = queue->node[queue->rear];
queue->rear --;
return node;
}
}
/**
* 广度优先遍历
*/
void BFS(Graph *graph , int i ,Queue *queue){
VNode node = graph->adjlist[i];
cout <<"已遍历:" << node.data << endl;
Visited[i] = true;
if(!EnQueue(queue , node)){
VNode vnode = DeQueue(queue);
EdgeNode *enode = vnode.firstNode;
while(enode){
if(!Visited[enode->adjvex]){
EnQueue(queue , graph->adjlist[enode->adjvex]);
}
enode = enode->next;
}
}
}
void BFSTraverse(Graph *graph){
Queue *queue = new Queue;
queue->Capacity = graph->vexNum;
queue->front = 0;
queue->rear = 0;
//queue->node[0] = NULL;
for(int i = 0 ; i < graph->vexNum ;i ++){
Visited[i] = false;
}
for(int i = 0 ;i < graph->vexNum ;i ++){
if(!Visited[i]){
BFS(graph , i , queue);
}
}
}
int main(){
//AMGraph *graph = new AMGraph;
//CreateUDGraph(graph);
//printUDGraph(graph);
//DFSTraverse(graph);
Graph *graph;
CreateGraph(&graph);
DFSTraverse2(graph);
cout << "BFS遍历:" <<endl;
BFSTraverse(graph);
return 0;
}