-
Notifications
You must be signed in to change notification settings - Fork 0
/
Undirected_Graph.cpp
653 lines (515 loc) · 18.8 KB
/
Undirected_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
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
// 邻接表表示无向图
// 参考:https://github.com/Crystal-Dragon-Liu/C-algorithm_practise/blob/master/C_algorithm/C_algorithm/Dat_Structure/Dat_Structure/Graph_practise.cpp
#include <stdio.h>
#include <vector>
#include <iostream>
#include <string>
#include <fstream>
#include <map>
#include <set>
#include <list>
#include <stack>
using namespace std;
// ********************************** 用邻接表 表示无向图*******************************
class Vertex{
public:
Vertex(int val){ //构造函数
in_out = 0; //该节点的度数:连接边的数量
value = val;
}
int value;
int in_out;
list<Vertex *> neighbours;
};
class undirected_Graph{
public:
undirected_Graph(string path); // 构造函数
int get_num_Vertex(); //获得顶点数量
int get_num_Edge(); //获得边的数量
void addEdge(int firstVertex, int secondVertex); // 输入两个节点添加他们之间的边
void print_allVertexAndEdge();
map<int, Vertex*> getGraphVertexSet();
private:
int num_Vertex; // 无向图中顶点的数量
int num_Edge; // 无向图中边的数量
map<int, Vertex*> VertexSet;
bool isInserted(int v,map<int, Vertex*> VertexSet);
};
undirected_Graph::undirected_Graph(string path){
ifstream data(path);
int dat;
data >> dat;
cout << "the count of Vertex : " << dat << " ";
num_Vertex =dat;
data >> dat;
cout << "the count of Edge : " << dat << " ";
num_Edge = dat;
cout << endl;
for(int index = 0; index < num_Edge; index++){
cout << "读取顶点: " ;
data >> dat;
cout << dat << " ";
int firstVertex = dat;
cout << "读取顶点: " ;
data >> dat;
cout << dat << endl;
int secondVertex = dat;
addEdge(firstVertex, secondVertex);
}
}
void undirected_Graph::addEdge(int firstVertex, int secondVertex){
Vertex* firstVertexptr = new Vertex(firstVertex);
Vertex* secondVertexptr = new Vertex(secondVertex);
firstVertexptr->neighbours.push_back(secondVertexptr);
firstVertexptr->in_out ++;
secondVertexptr->neighbours.push_back(firstVertexptr);
secondVertexptr->in_out ++;
pair<int, Vertex*> v(firstVertex,firstVertexptr);
pair<int, Vertex*> w(secondVertex,secondVertexptr);
//
if(!isInserted(firstVertex,VertexSet)){
VertexSet.insert(v);
}
else //如果该节点已经存在的话,则在该节点对应的链表中加入新的值
{
VertexSet[firstVertex]->neighbours.push_back(secondVertexptr);
}
if(!isInserted(secondVertex,VertexSet)){
VertexSet.insert(w);
}
else
{
VertexSet[secondVertex]->neighbours.push_back(firstVertexptr);
}
}
// 在顶点集合中 v是否存在
bool undirected_Graph::isInserted(int v, map<int, Vertex*> VertexSet)
{
map<int, Vertex*>::iterator iter = VertexSet.begin();
for(; iter!=VertexSet.end(); iter++){
if(iter->first == v)
return true;
}
return false;
}
// 按照邻接表的形式打印出整个无向图
void undirected_Graph::print_allVertexAndEdge(){
map<int, Vertex*>:: iterator iter = VertexSet.begin();
for(; iter != VertexSet.end(); iter++){
cout << "顶点" << iter->first << "的邻节点: ";
for(list<Vertex* >::iterator j = iter->second->neighbours.begin();
j != iter->second->neighbours.end(); j++){
cout << (*j)->value << " ";
}
cout << endl;
}
}
map<int, Vertex*> undirected_Graph::getGraphVertexSet(){
return VertexSet;
}
int undirected_Graph::get_num_Edge(){
return num_Edge;
}
int undirected_Graph::get_num_Vertex(){
return num_Vertex;
}
//************************************************************ 深度优先搜索 **************************************************************************
class DFS{
public:
DFS(undirected_Graph uGraph, int s); //构造函数
bool isMarked(int v);
private:
vector<bool> marked;
void _dfs(undirected_Graph uGraph, int v);
};
DFS::DFS(undirected_Graph uGraph, int s){
marked = vector<bool>(uGraph.get_num_Edge(),false);
cout<< "*************** DFS Start****************" << endl;
_dfs(uGraph,s);
}
void DFS::_dfs(undirected_Graph uGraph, int s){
cout << "********check " << s << "****************" << endl;
marked[s] = true;
list<Vertex*> V_uGraphVertexSet = uGraph.getGraphVertexSet()[s]->neighbours;
list<Vertex*>::iterator iter = V_uGraphVertexSet.begin();
for(; iter != V_uGraphVertexSet.end(); iter++){
if( !isMarked((*iter)->value)){
cout << "************" << (*iter)->value <<" has not been marked ************" << endl;
_dfs(uGraph,(*iter)->value);
}
}
}
bool DFS::isMarked(int w){
return marked[w];
}
//************************************************************ 深度优先搜索路径 ***********************************************************
class DFS_Search_Path{
public:
DFS_Search_Path(undirected_Graph uGraph, int s); //构造函数
bool isMarked(int v);
void printPaths();
int getLastVertex(int v); // 获取第一次访问该节点的节点
int getStartPoint(); // 获取这个类的起始点
stack<int> pathStore(int v); // 将路径节点顺序存储(逆序查找,利用stack后进先出的特点,顺序输出)
private:
vector<bool> marked;
int startpoint;
map<int, int> EdgeTo_dfs; //记录每个顶点被第一次访问
void dfs(undirected_Graph uGraph, int s);
};
bool DFS_Search_Path::isMarked(int w){
return marked[w];
}
DFS_Search_Path::DFS_Search_Path(undirected_Graph uGraph, int s){
marked = vector<bool>(uGraph.get_num_Edge(),false);
startpoint = s;
cout<< "*************** DFS Start****************" << endl;
dfs(uGraph,s);
}
void DFS_Search_Path::dfs(undirected_Graph uGraph, int s){
cout << "********check " << s << "****************" << endl;
marked[s] = true;
list<Vertex*> V_uGraphVertexSet = uGraph.getGraphVertexSet()[s]->neighbours;
list<Vertex*>::iterator iter = V_uGraphVertexSet.begin();
for(; iter != V_uGraphVertexSet.end(); iter++){
if( !isMarked((*iter)->value)){
cout << "************" << (*iter)->value <<" has not been marked ************" << endl;
EdgeTo_dfs.insert(pair<int,int>((*iter)->value,s));
dfs(uGraph,(*iter)->value);
}
}
}
void DFS_Search_Path::printPaths(){
map<int,int>::iterator iter = EdgeTo_dfs.begin();
cout << "****************print Path**************************" << endl;
for(; iter != EdgeTo_dfs.end(); iter++){
cout << (*iter).first << "<----" << (*iter).second << endl;
}
}
int DFS_Search_Path::getLastVertex(int v){
auto iter = EdgeTo_dfs.find(v); // 利用查找的方式
if(iter != EdgeTo_dfs.end()){
return iter->second; // 返回第一个访问他的节点
}
return -1;
}
int DFS_Search_Path::getStartPoint(){
return startpoint;
}
stack<int> DFS_Search_Path::pathStore(int v){
stack<int> pathstore;
if(!isMarked(v)) // 终点必须是被访问过的
return stack<int>();
for(int x = v; x != getStartPoint(); x = getLastVertex(x)){ //! 这个for 循环值得看一下,一般第三个条件都是x++;
//! 其实从本质上看只是为了获取下一次进入循环的x值
//! 在这里直接利用函数 获取第一次访问他的节点,并存入路径当中
pathstore.push(x);
}
pathstore.push(getStartPoint()); // 最后加入起点
return pathstore;
}
//************************************************************ 广度优先搜索 ***********************************************************
class BFS{
public:
BFS(undirected_Graph uGraph, int s); // 构造函数
bool isMarked(int w);
private:
void _bfs(undirected_Graph uGraph, int s);
vector<bool> marked;
deque<int> queue_bfs;
};
bool BFS::isMarked(int w){
return marked[w];
}
BFS::BFS(undirected_Graph uGraph, int s){
marked = vector<bool>(uGraph.get_num_Edge(),false);
cout << "******************************* BFS Start **************************************" <<endl;
_bfs(uGraph, s);
}
void BFS::_bfs(undirected_Graph uGraph, int s){
marked[s] = true;
queue_bfs.push_back(s);
while(queue_bfs.size() != 0){
int v = queue_bfs.front();
queue_bfs.pop_front();
cout << "********check " << v << "****************" << endl;
list<Vertex*> V_uGraphVertexSet = uGraph.getGraphVertexSet()[v]->neighbours;
list<Vertex*>::iterator iter = V_uGraphVertexSet.begin();
for(; iter != V_uGraphVertexSet.end(); iter++){
if(!isMarked((*iter)->value)){
cout << "************" << (*iter)->value <<" has not been marked ************" << endl;
marked[(*iter)->value] = true;
queue_bfs.push_back((*iter)->value);
}
}
}
}
// **************************************************** 广度优先搜索路径 ****************************************************************
class BFS_Search_Paths{
public:
BFS_Search_Paths(undirected_Graph uGraph, int s); // 构造函数
bool isMarked(int w);
void printPath();
int getLastVertex(int v);
int getStartPoint();
stack<int> pathStore(int v);
private:
void _bfs(undirected_Graph uGraph, int s);
int startpoint;
vector<bool> marked;
deque<int> queue_bfs;
map<int,int> EdgeTo_bfs; // 记录第一次访问的节点,和访问该节点的节点
};
bool BFS_Search_Paths::isMarked(int w){
return marked[w];
}
BFS_Search_Paths::BFS_Search_Paths(undirected_Graph uGraph, int s){
startpoint = s;
marked = vector<bool>(uGraph.get_num_Edge(),false);
cout << "******************************* BFS Start **************************************" <<endl;
_bfs(uGraph, s);
}
void BFS_Search_Paths::_bfs(undirected_Graph uGraph, int s){
marked[s] = true;
queue_bfs.push_back(s);
while(queue_bfs.size() != 0){
int v = queue_bfs.front();
queue_bfs.pop_front();
cout << "********check " << v << "****************" << endl;
list<Vertex*> V_uGraphVertexSet = uGraph.getGraphVertexSet()[v]->neighbours;
list<Vertex*>::iterator iter = V_uGraphVertexSet.begin();
for(; iter != V_uGraphVertexSet.end(); iter++){
if(!isMarked((*iter)->value)){
cout << "************" << (*iter)->value <<" has not been marked ************" << endl;
EdgeTo_bfs.insert(pair<int,int>((*iter)->value,v));
marked[(*iter)->value] = true;
queue_bfs.push_back((*iter)->value);
}
}
}
}
void BFS_Search_Paths::printPath(){
map<int,int>::iterator iter = EdgeTo_bfs.begin();
for(; iter != EdgeTo_bfs.end(); iter++){
cout << (*iter).first << "<---" << (*iter).second <<endl;
}
}
int BFS_Search_Paths::getLastVertex(int v){
auto iter = EdgeTo_bfs.find(v); // 利用查找的方式
if(iter != EdgeTo_bfs.end()){
return iter->second; // 返回第一个访问他的节点
}
return -1;
}
int BFS_Search_Paths::getStartPoint(){
return startpoint;
}
stack<int> BFS_Search_Paths::pathStore(int v){
stack<int> pathstore;
if(!isMarked(v)) // 终点必须是被访问过的
return stack<int>();
for(int x = v; x != getStartPoint(); x = getLastVertex(x)){ //! 这个for 循环值得看一下,一般第三个条件都是x++;
//! 其实从本质上看只是为了获取下一次进入循环的x值
cout << "向堆栈中存入" << x << endl; //! 在这里直接利用函数 获取第一次访问他的节点,并存入路径当中
pathstore.push(x);
}
pathstore.push(getStartPoint()); // 最后加入起点
return pathstore;
}
//************************************************深度优先搜索应用: 查找连通分量 *********************************************************
class connected_components{
private:
vector<bool> marked;
map<int,int> id;
int count = 0;
public:
connected_components(undirected_Graph uGraph); //构造函数
void dfs(undirected_Graph uGraph,int v);
int get_num_components();
bool isMarked(int w);
bool isBelongTo(int i,int j);
void print_Connected_Components();
};
bool connected_components::isMarked(int w){
return marked[w];
}
void connected_components::dfs(undirected_Graph uGraph,int v){
marked[v] = true;
pair<int,int> id_element(v,count); // v是节点的值,count是连通分量的序号
id.insert(id_element);
list<Vertex*> V_uGraphVertexSet = uGraph.getGraphVertexSet()[v]->neighbours;
// 获取V顶点的邻接表
list<Vertex*>::iterator iter = V_uGraphVertexSet.begin();
for(; iter != V_uGraphVertexSet.end(); iter++){
if(!isMarked((*iter)->value)){
dfs(uGraph,(*iter)->value);
}
}
}
connected_components::connected_components(undirected_Graph uGraph){
marked = vector<bool>(uGraph.get_num_Vertex(),false);
for(int s = 0; s < uGraph.get_num_Vertex(); s++){
cout << " 这次从" << s << "开始搜索" << endl;
// 每一次进入if语句都说明是一个新的连通分量
if(!isMarked(s)){
dfs(uGraph,s);
count++; // count 是连通分量的序号,也可以看成是数量
}
}
int num_com = get_num_components();
vector<vector<int> > result(num_com); //! 定义vector要定义容量,不然又报错
cout << "连通分量的个数: " << num_com << endl;
for(int i = 0; i < num_com; i++){
for(int j = 0; j < uGraph.get_num_Vertex(); j++){
if(isBelongTo(j,i)){
result[i].push_back(j);
}
}
}
}
int connected_components::get_num_components(){
int c = 1;
int index = id.find(0)->second;
map<int, int>::iterator iter = id.begin();
for(iter; iter != id.end(); iter++)
{
if((*iter).second != index)
{
c++;
index = (*iter).second;
}
}
return c;
}
bool connected_components::isBelongTo(int i, int component){
if(id.find(i)->second == component)
return true;
else
return false;
}
void connected_components::print_Connected_Components(){
map<int,int>::iterator iter = id.begin();
for(; iter != id.end(); iter++){
cout << (*iter).first << " " <<(*iter).second << endl;
}
}
//***********************************************深度优先搜索应用:判断图中是否有环 *********************************************************
class Cycle{
public:
Cycle(undirected_Graph Graph);
bool isMarked(int v);
void dfs(undirected_Graph uGraph, int s,int v);
bool hasCycleOrNot();
private:
vector<bool> marked;
bool hasCycle = false;
};
bool Cycle::isMarked(int w){
return marked[w];
}
Cycle::Cycle(undirected_Graph uGraph){
marked = vector<bool>(uGraph.get_num_Vertex(),false);
for(int s = 0; s < uGraph.get_num_Vertex(); s++){
if(!isMarked(s)){
dfs(uGraph,s,s);
}
}
}
void Cycle::dfs(undirected_Graph uGraph, int s,int v){
marked[s] = true;
list<Vertex*> V_uGraphVertexSet = uGraph.getGraphVertexSet()[s]->neighbours;
// 获取V顶点的邻接表
list<Vertex*>::iterator iter = V_uGraphVertexSet.begin();
for(; iter != V_uGraphVertexSet.end(); iter++){
if(!isMarked((*iter)->value)){
dfs(uGraph,(*iter)->value,s);
}
else if((*iter)->value != v){ //! 在深度优先搜索的过程中,如果发现某个节点的相邻节点被标记过了,
//! 而且该相邻节点还不是他的父节点,则证明该图有环
hasCycle = true;
}
}
}
bool Cycle::hasCycleOrNot(){
return hasCycle;
}
//***********************************************深度优先搜索应用:判断该图是否为二分图 *********************************************************
class TwoColors{
public:
TwoColors(undirected_Graph uGraph);
bool isMarked(int v);
void dfs(undirected_Graph uGraph,int s);
bool isBipartite();
private:
vector<bool> marked;
vector<bool> color;
bool isTwocolorable = true;
};
bool TwoColors::isMarked(int w){
return marked[w];
}
TwoColors::TwoColors(undirected_Graph uGraph){
marked = vector<bool>(uGraph.get_num_Vertex(),false);
color = vector<bool>(uGraph.get_num_Vertex(),false);
for(int s = 0; s < uGraph.get_num_Vertex(); s++){
if(!isMarked(s))
dfs(uGraph,s);
}
}
void TwoColors::dfs(undirected_Graph uGraph,int s){
marked[s] = true;
list<Vertex*> V_uGraphVertexSet = uGraph.getGraphVertexSet()[s]->neighbours;
// 获取V顶点的邻接表
list<Vertex*>::iterator iter = V_uGraphVertexSet.begin();
for(; iter != V_uGraphVertexSet.end(); iter++){
if(!isMarked((*iter)->value)){
//! 每次向下进行深度优先搜索,都将父节点和子节点的颜色取成相反的颜色
color[(*iter)->value] = !color[s];
dfs(uGraph,(*iter)->value);
}
//! 一旦遇到被标记的节点,而且父节点和子节点颜色相同,则不是二分图
else if(color[(*iter)->value] == color[s]){
isTwocolorable = false;
}
}
}
bool TwoColors::isBipartite(){
return isTwocolorable;
}
int main(){
string path = "graph_initial.txt";
undirected_Graph graph(path);
graph.print_allVertexAndEdge();
DFS(graph,0);
cout << "------------------------间隔-------------------------" <<endl;
int startpoint = 0;
int endpoint = 4;
DFS_Search_Path Paths(graph,startpoint);
Paths.printPaths();
stack<int> Path_1 = Paths.pathStore(endpoint);
cout << "DFS_Path" <<endl;
while(!Path_1.empty()){
cout << Path_1.top() << endl;
Path_1.pop();
}
cout << "--" << endl;
BFS_Search_Paths Path_bfs(graph,0);
Path_bfs.printPath();
//! 下面这句话会导致程序卡死,还没有解决
auto Path_2 = Path_bfs.pathStore(endpoint);
cout << "BFS_Path" <<endl;
while(!Path_2.empty()){
cout << Path_2.top() << endl;
Path_2.pop();
}
//! 这有问题
connected_components components(graph);
components.print_Connected_Components();
Cycle hascycle_1(graph);
bool result_1 = hascycle_1.hasCycleOrNot();
cout << "该图是否有环: " << result_1 << endl;
TwoColors isTwoColor(graph);
bool result_2 = isTwoColor.isBipartite();
cout << "该图是否为二分图: " << result_2 << endl;
return 0;
}