-
Notifications
You must be signed in to change notification settings - Fork 0
/
GraphMatrix.cpp
725 lines (673 loc) · 16.8 KB
/
GraphMatrix.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
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
#include "GraphMatrix.h"
#include <iostream>
#include <fstream>
int GraphMatrix::CreateFromFile(char * file_path)
{
const int TYPE_NORMAL_MATRIX = 1;
using namespace std;
ifstream *input_file = new ifstream;//ifstream 在std命名空间中,必须要using一下
char buffer[4 * MAX_VERTEX];
char *buffer_pointer;
int column, row;
int loading_type;
int edge_weigh;
input_file->open(file_path,ios::in);
if (!input_file->is_open())
{
cout << "初始化图错误:文件无法打开" << endl;
return 1;
}
input_file->getline(buffer, 20);
//load graph type
input_file->getline(buffer, 20);
sscanf_s(buffer, "%d", &graph_type);//这个函数的格式类似于scanf
//load vertex_count
input_file->getline(buffer, 20);
sscanf_s(buffer, "%d", &count_vertex);
//load loading type
input_file->getline(buffer, 20);
sscanf_s(buffer, "%d", &loading_type);
//load vertex info
input_file->getline(buffer, sizeof(buffer));
column = 0;
buffer_pointer = buffer;
while (*buffer_pointer != '\0')
{
sscanf_s(buffer_pointer, "%d", &vertex_info[column++]);
while (*buffer_pointer == ' ')
buffer_pointer++;
while (*buffer_pointer != ' '&& *buffer_pointer != '\0')
buffer_pointer++;
}
//load whole matrix
if (loading_type == TYPE_NORMAL_MATRIX)
{
for (row = 0; row < count_vertex; row++)
{
input_file->getline(buffer, sizeof(buffer));
column = 0;
buffer_pointer = buffer;
while (*buffer_pointer != '\0')
{
sscanf_s(buffer_pointer, "%d", &edge[row][column]);
if (edge[row][column++] != 0)
count_edge++;
while (*buffer_pointer == ' ')
buffer_pointer++;
while (*buffer_pointer != ' '&& *buffer_pointer != '\0')
buffer_pointer++;
}
}
if (graph_type == TYPE_UNDIRECTED)
{
count_edge /= 2;
//检查一下矩阵是否真的对称
for (row = 0; row<count_vertex; row++)
for (column = row; column<count_vertex; column++)
if (edge[row][column] != edge[column][row])
{
cout << "警告:此无向图的邻接矩阵并不对称" << endl;
row = count_vertex;
column = count_vertex;
}
}
}
else
{
for (row = 0; row < count_vertex; row++)
for (column = 0; column < count_vertex; column++)
edge[row][column] = 0;//初始化
for (;;)
{
input_file->getline(buffer, sizeof(buffer));
buffer_pointer = buffer;
if (strcmp(buffer_pointer, "*/")==0)
break;
sscanf_s(buffer_pointer, "%d", &row);
while (*buffer_pointer == ' ')
buffer_pointer++;
while (*buffer_pointer != ' '&& *buffer_pointer != '\0')
buffer_pointer++;
sscanf_s(buffer_pointer, "%d", &column);
while (*buffer_pointer == ' ')
buffer_pointer++;
while (*buffer_pointer != ' '&& *buffer_pointer != '\0')
buffer_pointer++;
sscanf_s(buffer_pointer, "%d", &edge_weigh);
edge[row - 1][column - 1] = edge_weigh;
count_edge++;
if (graph_type == TYPE_UNDIRECTED)
{
edge[column - 1][row - 1] = edge_weigh;
}
}
}
input_file->close();
return 0;
}
GraphMatrix::GraphMatrix()
{
}
GraphMatrix::~GraphMatrix()
{
}
int GraphMatrix::DFTraverseRecursion(int vertex_visiting, int(*visit)(GraphMatrix *, int))
{
int visiting_neighbor;
visit(this, vertex_visiting);
SetVisited(vertex_visiting);
for (visiting_neighbor = FirstNeighbor(vertex_visiting); visiting_neighbor != 0; \
visiting_neighbor = NextNeighbor(vertex_visiting, visiting_neighbor))
{
if(!VertexVisited(visiting_neighbor))//务必注意要先确认没有访问过再去访问
DFTraverseRecursion(visiting_neighbor, visit);
}
return 0;
}
int GraphMatrix::ExistEdge(int vertex_1, int vertex_2)
{
if (!VertexLegal(vertex_1, vertex_2))
return false;
return edge[vertex_1 - 1][vertex_2 - 1] != NULL_EDGE1 &&
edge[vertex_1 - 1][vertex_2 - 1] != NULL_EDGE2;
}
void GraphMatrix::ShowVertexNeighbors(int vertex)
{
using namespace std;
int row;
if (vertex<1 || vertex>count_vertex)
std::cout << "错误:此顶点不存在" << std::endl;
else
{
if (graph_type == TYPE_DIRECTED)
{
std::cout << "从顶点" << vertex << "出发能到达的顶点包括:"<<'\t';
for (row =1; row <= count_vertex; row++)
{
if (ExistEdge(vertex,row))
cout << row <<" ";
}
std::cout << std::endl;
std::cout << "能到达顶点" << vertex << "的顶点包括:"<<"\t\t";
for (row = 1; row <= count_vertex; row++)
{
if (ExistEdge(row, vertex))
cout << row << " ";
}
std::cout << std::endl;
}
else
{
std::cout << "与顶点" << vertex << "相邻的顶点包括:" << '\t';
for (row = 1; row <= count_vertex; row++)
{
if (ExistEdge(row, vertex))
cout << row << " ";
}
std::cout << std::endl;
}
}
}
void GraphMatrix::InsertVertex()
{
if (count_vertex == MAX_VERTEX)
{
std::cout << "错误:顶点数已达到最大" << std::endl;
}
int i;
for (i = 0; i <= count_vertex; i++)
edge[i][count_vertex] = 0;
for (i = 0; i <= count_vertex; i++)
edge[count_vertex][i] = 0;
count_vertex++;
}
void GraphMatrix::DeleteVertex(int vertex_to_delete)
{
int row, column;
//下面的向上移动
for (row = vertex_to_delete; row < count_vertex; row++)
for (column = 0; column < vertex_to_delete-1; column++)
edge[row - 1][column] = edge[row][column];
//右边的向左移动
for (row = 0; row < vertex_to_delete - 1; row++)
for (column = vertex_to_delete; column < count_vertex; column++)
edge[row][column-1] = edge[row][column];
//右下的向左上移动
for (row = vertex_to_delete; row < count_vertex; row++)
for (column = vertex_to_delete; column < count_vertex; column++)
edge[row-1][column - 1] = edge[row][column];
count_vertex--;
}
int GraphMatrix::AddEdge(int vertex_1, int vertex_2, int weight)
{
if (ExistEdge(vertex_1, vertex_2))
return 1;
edge[vertex_1 - 1][vertex_2 - 1] = weight;
if(graph_type==TYPE_UNDIRECTED)
edge[vertex_2 - 1][vertex_1 - 1] = weight;
return 0;
}
int GraphMatrix::RemoveEdge(int vertex_1, int vertex_2)
{
if (ExistEdge(vertex_1, vertex_2))
{
vertex_1--; vertex_2--;
edge[vertex_1][vertex_2] = 0;
if(graph_type == TYPE_UNDIRECTED)
edge[vertex_2][vertex_1] = 0;
return 0;
}
else
return 1;
}
int GraphMatrix::FirstNeighbor(int vertex)
{
if (!VertexLegal(vertex))
return 0;
int column;
for (column = 1; column <= count_vertex; column++)
if (ExistEdge(vertex, column))
return column;
return 0;
}
int GraphMatrix::NextNeighbor(int vertex, int last_neighbor)
{
if (!VertexLegal(vertex, last_neighbor))
return 0;
int column;
for (column = last_neighbor+1; column <= count_vertex; column++)
if (ExistEdge(vertex, column))
return column;
return 0;
}
int GraphMatrix::ShowVertex(GraphMatrix *graph, int vertex)
{
if (graph->VertexLegal(vertex))
{
std::cout << graph->GetVertexInfo(vertex)<< " ";
return 0;
}
else
return 1;
}
int GraphMatrix::Degree(int vertex)
{
if (VertexLegal(vertex))
return Degree(vertex, IN_DEGREE) + Degree(vertex, OUT_DEGREE);
else
return 0;
}
int GraphMatrix::ShowAllVertex(int order)
{
int (*visit)(GraphMatrix *,int) = &(GraphMatrix::ShowVertex);
Traverse(order, visit);
return 0;
}
int GraphMatrix::Traverse(int order, int(*visit)(GraphMatrix *, int))
{
switch (order)
{
case DEPTH_FIRST:DFTraverse(visit); break;
case BROAD_FIRST:BFTraverse(visit); break;
case DEPTH_FIRST_NO_RECURSION:DFNoRecursionTraverse(visit); break;
case DEPTH_FIRST_NO_RECURSION_V2:DFNoRecursionTraverseV2(visit); break;
default:
std::cout << "错误:参数错误" <<std::endl;
return 1;
}
std::cout << std::endl;
return 0;
}
int GraphMatrix::BFTraverse(int(*visit)(GraphMatrix *, int))
{
int i;
int q[MAX_VERTEX], rear,front;
int visiting_vertex, visiting_neighbor;
Visited_Initialization();
for (i = 1; i <= count_vertex; i++)//对每一个连通分量都执行一次BFS
{
if (!VertexVisited(i))
{
visit(this,i);//进队列之前访问
SetVisited(i);
rear = 0; front = 0;
q[rear++] = i;
while (rear != front)
{
visiting_vertex = q[front++];
for (visiting_neighbor = FirstNeighbor(visiting_vertex); visiting_neighbor != 0; \
visiting_neighbor = NextNeighbor(visiting_vertex, visiting_neighbor))
{
if (!VertexVisited(visiting_neighbor))
{
visit(this,visiting_neighbor);
SetVisited(visiting_neighbor);
q[rear++] = visiting_neighbor;
}//if
}//for
}//while
}//if
}//for
return 0;
}
int GraphMatrix::DFTraverse(int(*visit)(GraphMatrix *, int))
{
Visited_Initialization();
for (int i = 1; i <= count_vertex; i++)
if (!VertexVisited(i))
DFTraverseRecursion(i, visit);
return 0;
}
int GraphMatrix::BFSingleOriginShortestDistance(int origin)
{
if (!VertexLegal(origin))
return 1;
const int INACCESSIBLE=999999;
int i;
int distance[MAX_VERTEX+1];
for (i = 1; i <= count_vertex; i++)
distance[i] = INACCESSIBLE;
Visited_Initialization();
int rear = 0, front = 0;
int q[MAX_VERTEX];
distance[origin] = 0;
SetVisited(origin);
q[rear++] = origin;
int visiting_vertex, visiting_neighbor;
while (front != rear)
{
visiting_vertex = q[front++];
for (visiting_neighbor = FirstNeighbor(visiting_vertex); visiting_neighbor != 0; \
visiting_neighbor = NextNeighbor(visiting_vertex, visiting_neighbor))
{
if (!VertexVisited(visiting_neighbor))
{
distance[visiting_neighbor] = distance[visiting_vertex]+1;
SetVisited(visiting_neighbor);
q[rear++] = visiting_neighbor;
}
}
}
for (i = 1; i <= count_vertex; i++)
{
if (distance[i] == INACCESSIBLE)
std::cout << "顶点" << i << "不可到达"<<std::endl;
else
std::cout << "到顶点" << i << "的最短距离为"<<distance[i] << std::endl;
}
return 0;
}
void GraphMatrix::Visited_Initialization()
{
for (int i = 0; i < count_vertex; i++)
vertex_visted[i] = false;;
}
int GraphMatrix::VertexVisited(int vertex)
{
if (VertexLegal(vertex))
return 0!=vertex_visted[vertex - 1];
else
return true;
}
int GraphMatrix::SetVisited(int vertex)
{
if (VertexLegal(vertex))
{
vertex_visted[vertex - 1] = true;
return 0;
}
else
return 1;
}
int GraphMatrix::GetVertexInfo(int vertex)
{
return vertex_info[vertex-1];
}
int GraphMatrix::DFNoRecursionTraverse(int(*visit)(GraphMatrix *, int))
{
const int NULL_VERTEX=0;
Visited_Initialization();
int stack[MAX_VERTEX], top;//visiting stack
int visiting_neighbor[MAX_VERTEX];//temporarily record last neighbor visited by the vertex on each level of stack
int visiting_vertex;
for (int i = 1; i <= count_vertex; i++)
{
top = 0;
if (!VertexVisited(i))
{
//对每个联通分量调用一次dfs,这里每次在进栈之前进行访问
visiting_vertex = i;
while (!(visiting_vertex == NULL_VERTEX && top == 0))
{
if (visiting_vertex != NULL_VERTEX && !VertexVisited(visiting_vertex))
{
visit(this, visiting_vertex);
SetVisited(visiting_vertex);
stack[top] = visiting_vertex;
visiting_vertex = FirstNeighbor(visiting_vertex);
visiting_neighbor[top] = visiting_vertex;
top++;
}
else
{
visiting_vertex = stack[top - 1];
visiting_vertex = NextNeighbor(visiting_vertex, visiting_neighbor[top - 1]);
visiting_neighbor[top - 1] = visiting_vertex;
if (visiting_vertex == NULL_VERTEX)
top--;
}
}
}
}
return 0;
}
int GraphMatrix::DFNoRecursionTraverseV2(int(*visit)(GraphMatrix *, int))
{
int s[MAX_VERTEX], top;
int k, w;
Visited_Initialization();
for (int i = 1; i < count_vertex; i++)
{
if (!VertexVisited(i))
{
top = 0;
s[top++] = i;
SetVisited(i);
while (top != 0)
{
k = s[--top];
visit(this,k);
for(w=FirstNeighbor(k);w!=0;w=NextNeighbor(k,w))
if (!VertexVisited(w))
{
s[top++] = w;
SetVisited(w);
}
}
}
}
return 0;
}
int GraphMatrix::DijkstraShortestPath(int vertex)
{
if (!VertexLegal(vertex))
return 1;
Visited_Initialization();
int i,j;
int distance[MAX_VERTEX];
//由于我用0表示不可到达,所以这里额外调一下
for(i=0;i<count_vertex;i++)
for (j = 0; j < count_vertex; j++)
{
if (i != j &&edge[i][j] == 0)
edge[i][j] = NULL_EDGE2;
}
for (i = 0; i < count_vertex; i++)
{
distance[i] = edge[vertex - 1][i];//初始化值为源点到各个点的直线距离
}
int close_vertex,close_distance;
int path_order[MAX_VERTEX],path_order_position=1;
vertex_visted[vertex - 1] = true;
path_order[0] = vertex - 1;
while (path_order_position != count_vertex)
{
//找出剩下的顶点中距离最短的
close_distance = 0x7fffffff;
for (i = 0; i < count_vertex; i++)
{
if (!vertex_visted[i] && distance[i] < close_distance)
{
close_distance = distance[i];
close_vertex = i;
}
}
//选中这个顶点
path_order[path_order_position++] = close_vertex;
vertex_visted[close_vertex] = true;
//更新距离
for (i = 0; i < count_vertex; i++)
{
//如果绕过这个新的节点更近,就走新的节点
if (distance[close_vertex] + edge[close_vertex][i] < distance[i])
distance[i] = distance[close_vertex] + edge[close_vertex][i];
}
}
//output
for (i = 0; i < count_vertex; i++)
{
std::cout << "从顶点" << vertex << "到顶点" << i + 1 \
<< "的最短距离为" << distance[i] << std::endl;
}
std::cout << "节点选中的顺序为:";
for (i = 0; i < count_vertex; i++)
{
std::cout << path_order[i] + 1 << "->";
}
std::cout << std::endl;
return 0;
}
int GraphMatrix::FloydShortestPath()
{
int i, j, k;
int distance[MAX_VERTEX][MAX_VERTEX];
//由于我用0表示不可到达,所以这里额外调一下
for (i = 0; i<count_vertex; i++)
for (j = 0; j < count_vertex; j++)
{
if (i != j &&edge[i][j] == 0)
edge[i][j] = NULL_EDGE2;
distance[i][j] = edge[i][j];
}
for (k = 0; k < count_vertex; k++)//i到j从节点k绕行
for (i = 0; i<count_vertex; i++)
for (j = 0; j < count_vertex; j++)
{
if (distance[i][j] > distance[i][k] + edge[k][j])
distance[i][j] = distance[i][k] + edge[k][j];
}
//output
for (i = 0; i < count_vertex; i++)
{
for (j = 0; j < count_vertex; j++)
std::cout << distance[i][j]<<'\t';
std::cout << std::endl;
}
return 0;
}
GraphMatrix *GraphMatrix::Copy()
{
GraphMatrix *gm = new GraphMatrix();
gm->count_edge = count_edge;
gm->count_vertex = count_vertex;
gm->graph_type = graph_type;
int i, j;
for (i = 0; i < count_vertex; i++)
{
gm->vertex_info[i]=vertex_info[i];
for (j = 0; j < count_vertex; j++)
gm->edge[i][j] = edge[i][j];
}
return gm;
}
int GraphMatrix::TopologicalSorting()
{/*
GraphMatrix *gm = Copy();
int sorted_array[MAX_VERTEX];
int i, j, k;
Visited_Initialization();
for (i = 0; i < gm->count_vertex; i++)
{
//首先寻找一个入度为0的节点
for (j = 0; j < gm->count_vertex; j++)
if (Degree(j + 1, IN_DEGREE) == 0 && !VertexVisited(j + 1))
break;
//record it
if (j == gm->count_vertex)
{
std::cout << "拓扑序列不存在" << std::endl;
return 1;
}
sorted_array[i] = j;
SetVisited(j + 1);
//由于不好直接删顶点,把它出发的边都删掉,留一条到达它的边
for (k = 0; k < gm->count_vertex; k++)
gm->edge[j][k] = 0;
gm->edge[sorted_array[0]][j] = 1;
}
//output
for (i = 0; i < gm->count_vertex; i++)
std::cout << sorted_array[i]+1;
std::cout << std::endl;
return 0;
*/
int s[MAX_VERTEX], top = 0;
int degeree[MAX_VERTEX],visiting_vertex,vertex_neighbor;
int i;
int sorted[MAX_VERTEX], sorted_count = 0;
for (i = 1; i <= count_vertex; i++)
{
degeree[i-1] = Degree(i, IN_DEGREE);
if (degeree[i-1] == 0)
s[top++] = i;
}
while (top != 0)
{
visiting_vertex = s[--top];
sorted[sorted_count++] = visiting_vertex;
for (vertex_neighbor = FirstNeighbor(visiting_vertex); vertex_neighbor != 0; \
vertex_neighbor = NextNeighbor(visiting_vertex, vertex_neighbor))
{
degeree[vertex_neighbor - 1]--;
if (degeree[vertex_neighbor - 1] == 0)
s[top++] = vertex_neighbor;
}
}
if (sorted_count == count_vertex)
{
std::cout << "拓扑序列为" ;
for (i = 0; i < count_vertex;i++)
std::cout << sorted[i] << " ";
std::cout << std::endl;
return 0;
}
else
{
std::cout << "拓扑序列不存在" << std::endl;
return 1;
}
}
int GraphMatrix::Degree(int vertex,int type)
{
int i,degree=0;
if (VertexLegal(vertex))
switch (type)
{
case IN_DEGREE:
for (i = 1; i < count_vertex; i++)
if (ExistEdge(i,vertex))
degree++;
return degree;
break;
case OUT_DEGREE:
for (i = 1; i < count_vertex; i++)
if (ExistEdge(vertex,i))
degree++;
return degree;
break;
default:return 0;
}
else
return 0;
}
void DFSTopologicalSortingRecusion(GraphMatrix *gm,int vertex, int &time,int *finish_time)
{
int vertex_neighbor;
gm->SetVisited(vertex);
for (vertex_neighbor = gm->FirstNeighbor(vertex); vertex_neighbor != 0; \
vertex_neighbor = gm->NextNeighbor(vertex, vertex_neighbor))
{
if(!gm->VertexVisited(vertex_neighbor))
DFSTopologicalSortingRecusion(gm, vertex_neighbor, time, finish_time);
}
time++;
finish_time[vertex - 1] = time;
}
int GraphMatrix::DFSTopologicalSorting()
{
Visited_Initialization();
int i= 0;
int time = 0, finish_time[MAX_VERTEX];
for (i = 1; i <= count_vertex; i++)
if (!VertexVisited(i))
DFSTopologicalSortingRecusion(this, i, time, finish_time);
int sorted[MAX_VERTEX];
for (i = 0; i < count_vertex; i++)
{
sorted[count_vertex - finish_time[i]] = i + 1;
}
for (i = 0; i < count_vertex; i++)
std::cout << sorted[i] << " ";
std::cout << std::endl;
return 0;
}