-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgraph.h
1501 lines (1383 loc) · 45.1 KB
/
graph.h
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
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifndef _GRAPH_H_
#define _GRAPH_H_
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <stdlib.h>
#include <stack.h>
#include <map>
#include <typeinfo>
#include <climits>
#include <algorithm> //std::min
namespace amo {
#define RESET "\033[0m"
#define BLACK "\033[30m" /* Black */
#define RED "\033[31m" /* Red */
#define GREEN "\033[32m" /* Green */
#define YELLOW "\033[33m" /* Yellow */
#define BLUE "\033[34m" /* Blue */
#define MAGENTA "\033[35m" /* Magenta */
#define CYAN "\033[36m" /* Cyan */
#define WHITE "\033[37m" /* White */
#define ERROR_CODE -1
typedef enum {UNDISCOVERED, DISCOVERED, VISITED} VStatus;
typedef enum {UNDETERMINED, TREE, CROSS, FORWARD, BACKWARD} EType;
template<typename Tv>
class Vertex {
typedef struct functor_traverse {
void operator() (Vertex<Tv> const& v) {
std::cout << v << WHITE << std::endl;
}
} FUNCTOR_TRAVERSER;
private:
public:
Tv data;
int inDegree;
int outDegree;
VStatus status;
int dTime;
int fTime;
int parent;
int priority;
Vertex(const Tv& d = (Tv) 0) : data(d), inDegree(0), outDegree(0), status(UNDISCOVERED), dTime(-1), fTime(-1), parent(-1), priority(INT_MAX) {}
Vertex(const Vertex& v) : data(v.data), inDegree(v.inDegree), outDegree(v.outDegree), status(v.status), dTime(v.dTime), fTime(v.fTime), parent(v.parent), priority(v.priority) {}
Vertex& operator=(const Vertex& vertex) {
data = vertex.data;
inDegree = vertex.inDegree;
outDegree = vertex.outDegree;
status = vertex.status;
dTime = vertex.dTime;
fTime = vertex.fTime;
parent = vertex.parent;
priority = vertex.priority;
return *this;
}
~Vertex() {}
friend std::ostream& operator<<(std::ostream& os, const Vertex& vertex) {
if (&vertex == NULL) {
os << RED << "no vertex instance";
return os;
}
os << WHITE << "[this] :" << &vertex << endl;
os << WHITE << "[data] :" << vertex.data << endl;
os << WHITE << "[inDegree] :" << vertex.inDegree << endl;
os << WHITE << "[outDegree]:" << vertex.outDegree << endl;
switch (vertex.status) {
case UNDISCOVERED:
os << WHITE << "[status] :" << "UNDISCOVERED" << endl;
break;
case DISCOVERED:
os << WHITE << "[status] :" << "DISCOVERED" << endl;
break;
case VISITED:
os << WHITE << "[status] :" << "VISITED" << endl;
break;
}
os << WHITE << "[dTime] :" << vertex.dTime << endl;
os << WHITE << "[fTime] :" << vertex.fTime << endl;
os << WHITE << "[parent] :" << vertex.parent << endl;
os << WHITE << "[priority] :" << vertex.priority << endl;
return os;
}
};
template<typename Te>
class Edge {
private:
public:
EType type;
Te data;
int weight;
Edge(const Te& d = (Te) 0, int w = 0) : data(d), weight(w), type(UNDETERMINED) {}
~Edge() {}
friend std::ostream& operator<<(std::ostream& os, const Edge& edge) {
if (&edge == NULL) {
os << BLUE << "null" << endl;
return os;
}
os << WHITE << "[this] :" << &edge << endl;
os << WHITE << "[data] :" << edge.data << endl;
os << WHITE << "[weight]:" << edge.weight << endl;
switch (edge.type) {
case UNDETERMINED:
os << WHITE << "[type] :" << "UNDETERMINED" << endl;
break;
case TREE:
os << WHITE << "[type] :" << "TREE" << endl;
break;
case CROSS:
os << WHITE << "[type] :" << "CROSS" << endl;
break;
case FORWARD:
os << WHITE << "[type] :" << "FORWARD" << endl;
break;
case BACKWARD:
os << WHITE << "[type] :" << "BACKWARD" << endl;
break;
}
return os;
}
};
template<typename Tv, typename Te>
class Graph {
typedef struct functor_traverse {
void operator() (Graph<Tv,Te> const& tree) {
std::cout << tree << WHITE << std::endl;
}
} FUNCTOR_TRAVERSER;
private:
public:
int n;
int e;
Graph(): n(0), e(0) {
//std::cout << "[Graph::Graph()]: this:" << this << ", Tv type:" << typeid(Tv).name() << ", Te type:" << typeid(Te).name() << WHITE << std::endl;
}
~Graph() {
//std::cout << "[Graph::~Graph()]: this:" << this << WHITE << std::endl;
}
#if 0 //to be an abstract class
//vertex
virtual int insert(const Tv& data) = 0;
virtual Tv remove(int i) = 0;
virtual Tv& vertex(int i) = 0;
virtual int inDegree(int i) = 0;
virtual int outDegree(int i) = 0;
virtual int firstNbr(int i) = 0;
virtual int nextNbr(int i, int j) = 0;
virtual VStatus& status(int i) = 0;
virtual int& dTime(int i) = 0;
virtual int& fTime(int i) = 0;
virtual int& parent(int i) = 0;
virtual int& priority(int i) = 0;
//edge
virtual bool exist(int i, int j) = 0;
virtual void insert(const Te& e, int i, int j, int weight) = 0;
virtual Te remove(int i, int j) = 0;
virtual EType& type(int i, int j) = 0;
virtual Te& edge(int i, int j) = 0;
virtual int weight(int i, int j) = 0;
#else
void reset();
//vertex
int insert(const Tv& d);
Tv remove(int i);
Tv& vertex(int i);
int inDegree(int i);
int outDegree(int i);
int firstNbr(int i);
int nextNbr(int i, int j);
VStatus& status(int i);
int& dTime(int i);
int& fTime(int i);
int& parent(int i);
int& priority(int i);
//edge
bool exist(int i, int j);
void insert(const Te& d, int i, int j, int weight);
Te remove(int i, int j);
Te& edge(int i, int j);
EType& type(int i, int j);
int weight(int i, int j);
#endif
friend std::ostream& operator<<(std::ostream& os, const Graph<Tv,Te>& graph) {
os << WHITE << "[this]:" << &graph << endl;
return os;
}
};
template<typename Tv, typename Te>
class AdjaMatrix : public Graph<Tv, Te> {
private:
public:
//Unhide members of base class template with a using declaration
//using amo::Graph<Tv, Te>::insert;
std::vector<Vertex<Tv>*> V;
std::vector<std::vector<Edge<Te>*>> E;
AdjaMatrix(): Graph<Tv, Te>() {}
~AdjaMatrix() {}
void reset() {
for (int i=0; i<this->n; i++) {
status(i) = UNDISCOVERED;
dTime(i) = -1;
fTime(i) = -1;
parent(i) = -1;
priority(i) = INT_MAX;
for (int j=0; j<this->n; j++) {
if (exist(i, j)) {
type(i, j) = UNDETERMINED;
}
}
}
}
/**
* overload for V
*/
int inDegree(int i) { return V[i]->inDegree; }
int outDegree(int i) { return V[i]->outDegree; }
#if 0 //from tail
int firstNbr(int i) { return nextNbr(i, this->n-1); }
int nextNbr(int i, int j) { //next of [0, j)
while (--j >= 0) {
if (exist(i, j)) break;
}
if (j >= 0) {
cout << i << " -> " << j << WHITE << endl;
return j;
}
else return -1;
}
#else //from head
int firstNbr(int i) { return nextNbr(i, -1); }
int nextNbr(int i, int j) { //next of (j, n)
while (++j < this->n) {
if (exist(i, j)) break;
}
if (j < this->n) {
//cout << i << " -> " << j << WHITE << endl;
return j;
}
else return -1;
}
#endif
VStatus& status(int i) { return V[i]->status; }
Tv& vertex(int i) { return V[i]->data; }
int& dTime(int i) { return V[i]->dTime; }
int& fTime(int i) { return V[i]->fTime; }
int& parent(int i) { return V[i]->parent; }
int& priority(int i) { return V[i]->priority; }
int insert(const Tv& d);
Tv remove(int i); //[0, n)
/**
* overload for E
*/
Te& edge(int i, int j);
EType& type(int i, int j);
int weight(int i, int j);
void insert(const Te& d, int i, int j, int weight); //[0, e)
Te remove(int i, int j);
bool exist(int i, int j);
/**
* extend
*/
void print(std::ostream& os);
/**
* algorithm
*/
void BFS(int v);
void BFS(int v, int*& distance, int*& predecessor);
void DFS(int v);
void DFS(int v, int& time, int*& distance, int*& predecessor, int*& discover, int*& finish);
void collapse(int*& predecessor);
void CCDFS(int v);
AdjaMatrix transpose();
void quickSort(int*& predecessor, int front, int end);
void quickSortAndRank(int*& rank, int*& sort, int front, int end);
void SCCDFS(int v);
void TopoSort(int v);
void BCC(int v);
void BCC(int v, int*& discover, int*& finish, int*& predecessor, int*& hca, std::stack<int>& stack, int& clock);
template<typename Interface> void PFS(int s, Interface priority_updater, int& clock, int*& predecessor, int*& discover, int*& finish);
template<typename Interface> void PFS(int s, Interface priority_updater, int*& predecessor, int*& distance);
template<typename Interface> void PFS(int s, Interface priority_updater, std::vector<int>& cut, std::vector<Edge<Te>*>& cross);
void PFS_DFS(int v);
void PFS_BFS(int v);
void PFS_Prim(int v);
void Dijkstra(int v);
friend std::ostream& operator<<(std::ostream& os, AdjaMatrix<Tv,Te>& matrix) {
matrix.print(os);
return os;
}
};
template<typename Tv, typename Te>
void amo::AdjaMatrix<Tv, Te>::print(std::ostream& os) {
typename std::vector<Vertex<Tv>*>::iterator itV; //points to V[i]
typename std::vector<std::vector<Edge<Te>*>>::iterator itE; //points to E
typename std::vector<Edge<Te>*>::iterator it_adjs; //points to E[i]
int n_check = 0;
int e_check = 0;
os << CYAN << "size of V:" << V.size() << WHITE << std::endl;
os << CYAN << "size of E:" << E.size() << WHITE << std::endl;
for (itE=E.begin(); itE!=E.end(); itE++) {
os << CYAN << "size of E["<< distance(E.begin(), itE) << "]:" << itE->size() << WHITE << std::endl;
}
os << GREEN << "--- VERTEX TOP ------" << WHITE << std::endl;
for (itV=V.begin(); itV!=V.end(); itV++) {
os << CYAN << "V[" << distance(V.begin(), itV) << "]" << WHITE << endl;
os << **itV; //prints vertex
n_check++;
}
os << GREEN << "--- VERTEX BOTTOM ------" << WHITE << std::endl;
os << GREEN << "--- EDGE TOP ------" << WHITE << std::endl;
for (itE=E.begin(); itE!=E.end(); itE++) {
for (it_adjs=itE->begin(); it_adjs!=itE->end(); it_adjs++) {
if (*it_adjs == NULL) continue;
os << CYAN << "E[" << distance(E.begin(), itE) << "][" << distance(itE->begin(), it_adjs) << "]" << WHITE << endl;
os << **it_adjs;
e_check++;
}
}
if (this->n == n_check) os << GREEN << "right n:" << this->n << WHITE << endl;
else os << RED << "wrong n:" << this->n << " and n_check:" << n_check << WHITE << endl;
if (this->e == e_check) os << GREEN << "right e:" << this->e << WHITE << endl;
else os << RED << "wrong e:" << this->e << " and e_check:" << e_check << WHITE << endl;
os << GREEN << "--- EDGE BOTTOM ------" << WHITE << std::endl;
os << GREEN << "--- GRAPH TOP ------" << WHITE << std::endl;
string bars, nbrs;
//char c[2];
string c;
for (int i=0; i<this->n; i++) {
os << CYAN << "-------" << WHITE << endl;
//os << CYAN << "[" << i << "]" << WHITE << endl;
os << CYAN << "[" << V[i]->data << "]" << WHITE << endl;
bars.clear();
nbrs.clear();
for (int j=0; j<this->n; j++) {
if (exist(i, j)) {
bars.insert(bars.length(), " | ");
nbrs.append("[");
//sprintf(c, "%d", j);
//nbrs.append(c);
c.insert(c.begin(), V[j]->data);
nbrs.append(c);
c.clear();
nbrs.append("]");
}
}
os << CYAN << bars << WHITE << endl;
os << CYAN << nbrs << WHITE << endl;
}
os << GREEN << "--- GRAPH BOTTOM ------" << WHITE << std::endl;
}
//For V
/**
* When a class template derives from a base class template, the base members are not visible in the derived class template definition.
* (This makes sense, until you specialize, there is no class, and so there are no members.
* Explicit specializations can always change the meaning of any given template class.)
*/
template<typename Tv, typename Te>
int amo::AdjaMatrix<Tv, Te>::insert(const Tv& data) {
Vertex<Tv>* vertex = new Vertex<Tv>(data);
typename std::vector<Vertex<Tv>*>::iterator itV;
typename std::vector<std::vector<Edge<Te>*>>::iterator itE;
typename std::vector<Edge<Te>*>::iterator it_adjs;
int tmp = this->n;
std::vector<Edge<Te>*>* adjs = new std::vector<Edge<Te>*>();
while (0<tmp--) {
adjs->push_back((Edge<Te>*)0);
}
itE = this->E.insert(E.end(), *adjs);
for (itE=E.begin(); itE!=E.end(); itE++) {
it_adjs = itE->insert(itE->end(), (Edge<Te>*)0);
}
itV = this->V.insert(V.end(), vertex);
(this->n)++;
std::cout << YELLOW << "inserted a vector at V[" << distance(V.begin(), itV) << "]" << WHITE << std::endl;
//std::cout << YELLOW << "n:" << this->n << WHITE << std::endl;
return distance(V.begin(), itV);
}
template<typename Tv, typename Te> //This implement deploys operator[] instead of iterators but now n matters
Tv amo::AdjaMatrix<Tv, Te>::remove(int i) {
for (int j=0; j<this->n; j++) { //i->j
if (exist(i, j)) {
delete this->E[i][j]; //delete the instance of edge pointed by E[i][j]
this->E[i][j] = NULL;
this->V[j]->inDegree--;
this->e--;
}
}
this->E.erase(std::next(this->E.begin(), i)); //delete instance of vector of E[i], which decreases size of E by 1
this->n--;
for (int j=0; j<this->n; j++) { //j->i
if (exist(j, i)) {
delete this->E[j][i]; //delete instance of edge pointed by E[j][i]
this->E[j][i] = NULL;
this->V[j]->outDegree--;
this->e--;
}
this->E[j].erase(std::next(this->E[j].begin(), i));
}
Tv data = this->V[i]->data;
this->V.erase(std::next(this->V.begin(), i));
return data;
}
//For E
template<typename Tv, typename Te>
void amo::AdjaMatrix<Tv, Te>::insert(const Te& d, int i, int j, int weight) { //there should have been a space prepared since inserting a vertex before
if (exist(i, j)) {
cout << RED << "edge between " << i << " and " << j << " already exists" << WHITE << std::endl;
return;
}
Edge<Te>* edge = new Edge<Te>(d, weight);
E[i][j] = edge;
this->e++;
V[i]->outDegree++;
V[j]->inDegree++;
cout << CYAN << "inserted an edge at E[" << i << "][" << j << "]" << WHITE << std::endl;
}
template<typename Tv, typename Te>
Te amo::AdjaMatrix<Tv, Te>::remove(int i, int j) { //keeps the space of but replaces E[i][j] with NULL
if (!exist(i, j)) {
cout << RED << "edge between " << i << " and " << j << " doesn't exist" << WHITE << std::endl;
return NULL;
}
#if 1
typename std::vector<std::vector<Edge<Te>*>>::iterator itE;
typename std::vector<Edge<Te>*>::iterator it_adjs;
itE = std::next(this->E.begin(), i);
it_adjs = std::next(itE->begin(), j);
Te data = (*it_adjs)->data;
cout << CYAN << "going to delete an edge form E and replace with NULL:" << data << WHITE << std::endl;
delete *it_adjs;
*it_adjs = NULL;
#else
Te data = E[i][j]->data;
cout << CYAN << "going to delete an edge form E and replace with NULL:" << data << WHITE << std::endl;
delete E[i][j];
E[i][j] = NULL;
#endif
this->e--;
V[i]->outDegree--;
V[j]->inDegree--;
return data;
}
template<typename Tv, typename Te>
bool amo::AdjaMatrix<Tv, Te>::exist(int i, int j) {
bool ret = true;
ret &= (0<=i);
//'this' is always implicitly dependent in a template and the lookup is therefore deferred until the template is actually instantiated.
if (!ret) {
//cout << RED << "out of limit, i < 0" << WHITE << std::endl;
return ret;
}
ret &= i<this->n;
if (!ret) {
//cout << RED << "out of limit, i >= " << this->n << WHITE << std::endl;
return ret;
}
ret &= (0<=j);
if (!ret) {
//cout << RED << "out of limit, j < 0" << WHITE << std::endl;
return ret;
}
ret &= (j<this->n);
if (!ret) {
//cout << RED << "out of limit, j >= " << this->n << WHITE << std::endl;
return ret;
}
ret &= (this->E[i][j] != NULL);
if (!ret) {
//cout << RED << "E[" << i << "][" << j << "] is NULL" << WHITE << std::endl;
return ret;
}
return ret;
}
template<typename Tv, typename Te>
EType& amo::AdjaMatrix<Tv, Te>::type(int i, int j) {
if (!exist(i, j)){
cout << RED << "no edge exists and return UNDETERMINED (" << i << "," << j << ")" << WHITE << std::endl;
Edge<Te>* edge = E[i][j];
edge->type = UNDETERMINED;
return edge->type;
}
Edge<Te>* edge = E[i][j];
return edge->type;
}
template<typename Tv, typename Te>
Te& amo::AdjaMatrix<Tv, Te>::edge(int i, int j) {
if (!exist(i, j)){
cout << RED << "no edge exists and return NULL" << WHITE << std::endl;
Te* dummy = new Te();
return *dummy;
}
Edge<Te>* edge = E[i][j];
return edge->data;
}
template<typename Tv, typename Te>
int amo::AdjaMatrix<Tv, Te>::weight(int i, int j) {
if (!exist(i, j)){
cout << RED << "no edge exists and return NULL" << WHITE << std::endl;
return std::numeric_limits<int>::max();
}
Edge<Te>* edge = E[i][j];
return edge->weight;
}
template<typename Tv, typename Te>
void amo::AdjaMatrix<Tv, Te>::BFS(int v) {
int root = v;
int* distance = new int[this->n];
int* predecessor = new int[this->n];
for (int i=0; i<this->n; i++) {
predecessor[i] = -1;
distance[i] = -1;
}
do {
if (status(v) == UNDISCOVERED) {
cout << GREEN << "going to BFS the subgraph of v:" << v << WHITE << endl;
BFS(v, distance, predecessor);
}
} while (root!=(v=(++v%this->n)));
//print
for (int i=0; i<this->n; i++) {
cout << WHITE << "distance[" << vertex(i) << "]:" << distance[i] << WHITE << endl;
}
for (int i=0; i<this->n; i++) {
int p = predecessor[i];
if (p >= 0) cout << YELLOW << "predecessor[" << vertex(i) << "]:" << vertex(p) << WHITE << endl;
else cout << YELLOW << "predecessor[" << vertex(i) << "]:" << predecessor[i] << WHITE << endl;
}
}
template<typename Tv, typename Te>
void amo::AdjaMatrix<Tv, Te>::BFS(int v, int*& distance, int*& predecessor) {
#if 1 //info
string str;
char c[2]; //for a char and a null-terminated character
#endif
std::queue<int> queue;
queue.push(v);
distance[v] = 0;
predecessor[v] = -1;
while (!queue.empty()) {
int u = -1;
int i = queue.front();
queue.pop();
while ((u=nextNbr(i, u))>=0) {
#if 0 //info
str.clear();
c[0] = vertex(i);
str.append(c, 1);
str.append(" - ");
c[0] = vertex(u);
str.append(c, 1);
cout << WHITE << str << WHITE << endl;
#endif
if (status(u) == UNDISCOVERED) {
queue.push(u);
status(u) = DISCOVERED;
type(i, u) = TREE;
parent(u) = i;
distance[u] = distance[i]+1;
predecessor[u] = i;
cout << YELLOW << vertex(i) << " tree " << vertex(u) << WHITE << endl;
} else {
type(i, u) = CROSS;
cout << vertex(i) << " cross " << vertex(u) << endl;
}
}
status(i) = VISITED;
}
}
template<typename Tv, typename Te>
void amo::AdjaMatrix<Tv, Te>::DFS(int v) {
int root = v;
int time = 0;
int* distance = new int[this->n];
int* predecessor = new int[this->n];
int* discover = new int[this->n]; //all of dTime of V
int* finish = new int[this->n]; //all of fTime of V
for (int i=0; i<this->n; i++) {
predecessor[i] = -1;
}
do {
if (status(v) == UNDISCOVERED) {
cout << GREEN << "going to DFS the subgraph of v:" << v << WHITE << endl;
DFS(v, time, distance, predecessor, discover, finish);
}
} while (root!=(v=(++v%this->n)));
for (int i=0; i<this->n; i++) {
int p = predecessor[i];
if (p >= 0) cout << YELLOW << "predecessor[" << vertex(i) << "]:" << vertex(p) << WHITE << endl;
else cout << YELLOW << "predecessor[" << vertex(i) << "]:" << predecessor[i] << WHITE << endl;
}
for (int i=0; i<this->n; i++) {
cout << WHITE << "discover[" << vertex(i) << "]:" << discover[i] << WHITE << endl;
}
for (int i=0; i<this->n; i++) {
cout << WHITE << "finish[" << vertex(i) << "]:" << finish[i] << WHITE << endl;
}
}
template<typename Tv, typename Te>
void amo::AdjaMatrix<Tv, Te>::DFS(int v, int& time, int*& distance, int*& predecessor, int*& discover, int*& finish) {
status(v) = DISCOVERED;
dTime(v) = ++time;
discover[v] = dTime(v);
int u = -1;
while ((u=nextNbr(v, u))>=0) {
if (status(u) == UNDISCOVERED) {
cout << vertex(v) << " tree " << vertex(u) << endl;
type(v, u) = TREE;
parent(u) = v;
predecessor[u] = v;
DFS(u, time, distance, predecessor, discover, finish);
}
else if (status(u) == DISCOVERED) {
cout << vertex(v) << " backward " << vertex(u) << endl;
type(v, u) = BACKWARD;
}
else if (status(u) == VISITED) {
if (dTime(v) > dTime(u)) {
cout << vertex(v) << " cross " << vertex(u) << endl;
type(v, u) = CROSS;
}
else if (dTime(v) < dTime(u)) {
cout << vertex(v) << " forward " << vertex(u) << endl;
type(v, u) = FORWARD;
}
else cout << RED << "Exception of dTime of v:" << v << WHITE << endl;
}
else cout << RED << "Exception of status of u:" << u << WHITE << endl;
}
status(v) = VISITED;
fTime(v) = ++time;
finish[v] = fTime(v);
}
template<typename Tv, typename Te>
void amo::AdjaMatrix<Tv, Te>::collapse(int*& predecessor) {
int i = -1;
while (++i<this->n) {
if (predecessor[i] < 0) {
cout << CYAN << "root:" << i << WHITE << endl;
continue;
}
while (predecessor[predecessor[i]]>=0) {
predecessor[i] = predecessor[predecessor[i]];
cout << CYAN << "changed predecessor[" << i << "]:" << vertex(predecessor[i]) << WHITE << endl;
}
}
}
template<typename Tv, typename Te>
void amo::AdjaMatrix<Tv, Te>::CCDFS(int v) {
int root = v;
int time = 0;
int* distance = new int[this->n];
int* predecessor = new int[this->n];
int* discover = new int[this->n]; //all of dTime of V
int* finish = new int[this->n]; //all of fTime of V
for (int i=0; i<this->n; i++) {
predecessor[i] = -1;
}
do {
if (status(v) == UNDISCOVERED) {
cout << GREEN << "going to DFS the subgraph of v:" << v << WHITE << endl;
DFS(v, time, distance, predecessor, discover, finish);
}
} while (root!=(v=(++v%this->n)));
cout << GREEN << "going to collapse" << WHITE << endl;
collapse(predecessor);
for (int i=0; i<this->n; i++) {
int p = predecessor[i];
if (p >= 0) cout << YELLOW << "predecessor[" << vertex(i) << "]:" << vertex(p) << WHITE << endl;
else cout << YELLOW << "predecessor[" << vertex(i) << "]:" << predecessor[i] << WHITE << endl;
}
for (int i=0; i<this->n; i++) {
cout << WHITE << "discover[" << vertex(i) << "]:" << discover[i] << WHITE << endl;
}
for (int i=0; i<this->n; i++) {
cout << WHITE << "finish[" << vertex(i) << "]:" << finish[i] << WHITE << endl;
}
}
template<typename Tv, typename Te>
AdjaMatrix<Tv, Te> amo::AdjaMatrix<Tv, Te>::transpose(){
typename std::vector<Vertex<Tv>*>::iterator itV;
typename std::vector<std::vector<Edge<Te>*>>::iterator itE;
typename std::vector<Edge<Te>*>::iterator ite;
typename amo::AdjaMatrix<Tv, Te> T;
for (itV=V.begin(); itV!=V.end(); itV++) {
T.insert((*itV)->data);
}
int v = -1;
int u = -1;
for (itV=V.begin(); itV!=V.end(); itV++) {
v = std::distance(V.begin(), itV);
u = -1;
while ((u=nextNbr(v, u))>=0) {
T.insert("", u, v, 0);
}
}
return T;
}
template<typename Tv, typename Te>
void amo::AdjaMatrix<Tv, Te>::quickSort(int*& sort, int front, int end) { //[front, end]
if (front >= end) return;
//partition start
int pivot = end;
int l = front-1;
for (int i=front; i<end; i++) {
if (sort[i] > sort[pivot]) {
l++; //one more left
std::swap(sort[i], sort[l]);
}
}
l++; //positions pivot
std::swap(sort[l], sort[pivot]);
pivot = l;
//partition end
quickSort(sort, front, pivot-1);
quickSort(sort, pivot+1, end);
}
/**
* rank holds indexes corresponding to the sort in the same order
*/
template<typename Tv, typename Te>
void amo::AdjaMatrix<Tv, Te>::quickSortAndRank(int*& rank, int*& sort, int front, int end) { //[front, end]
if (front >= end) return;
//partition start
int pivot = end;
int l = front-1;
for (int i=front; i<end; i++) {
if (sort[i] > sort[pivot]) {
l++; //one more left
std::swap(sort[i], sort[l]);
std::swap(rank[i], rank[l]);
}
}
l++; //positions pivot
std::swap(sort[l], sort[pivot]);
std::swap(rank[l], rank[pivot]);
pivot = l;
//partition end
quickSortAndRank(rank, sort, front, pivot-1);
quickSortAndRank(rank, sort, pivot+1, end);
}
template<typename Tv, typename Te>
void amo::AdjaMatrix<Tv, Te>::SCCDFS(int v) {
int root = v;
int time = 0;
int* distance = new int[this->n];
int* predecessor = new int[this->n];
int* discover = new int[this->n]; //all of dTime of V
int* finish = new int[this->n]; //all of fTime of V
int* finish_sort = new int[this->n]; //descent
int* finish_rank = new int[this->n];
for (int i=0; i<this->n; i++) {
predecessor[i] = -1;
}
//first DFS
cout << GREEN << "going to 1st DFS" << WHITE << endl;
do {
if (status(v) == UNDISCOVERED) {
cout << GREEN << "going to DFS the subgraph of v:" << v << WHITE << endl;
DFS(v, time, distance, predecessor, discover, finish);
}
} while (root!=(v=(++v%this->n)));
//print
for (int i=0; i<this->n; i++) {
int p = predecessor[i];
if (p >= 0) cout << YELLOW << "predecessor[" << vertex(i) << "]:" << vertex(p) << WHITE << endl;
else cout << YELLOW << "predecessor[" << vertex(i) << "]:" << predecessor[i] << WHITE << endl;
}
for (int i=0; i<this->n; i++) {
cout << WHITE << "discover[" << vertex(i) << "]:" << discover[i] << WHITE << endl;
}
for (int i=0; i<this->n; i++) {
cout << WHITE << "finish[" << vertex(i) << "]:" << finish[i] << WHITE << endl;
}
//ranks the finish after the first DFS
for (int i=0; i<this->n; i++) {
finish_sort[i] = finish[i];
finish_rank[i] = i;
}
#if 0 //O(n^2)
cout << GREEN << "going to quick sort" << WHITE << endl;
quickSort(finish_sort, 0, (this->n)-1);
for (int rank=0; rank<this->n; rank++) {
for (int i=0; i<this->n; i++) {
if (finish[i] == finish_sort[rank]) {
finish_rank[rank] = i;
}
}
}
#else //O(nLogn)
cout << GREEN << "going to quick sort and rank" << WHITE << endl;
quickSortAndRank(finish_rank, finish_sort, 0, (this->n)-1);
#endif
for (int i=0; i<this->n; i++) {
cout << WHITE << "finish_rank[" << i << "]:" << finish_rank[i] << WHITE << endl;
}
//transpose
cout << GREEN << "going to transpose" << WHITE << endl;
typename amo::AdjaMatrix<Tv, Te> T = this->transpose();
//second DFS vertexes of the transpose in the order latest toward earliest of finish
int timeT = 0;
int* distanceT = new int[T.n];
int* predecessorT = new int[T.n];
int* discoverT = new int[T.n]; //all of dTime of V
int* finishT = new int[T.n]; //all of fTime of V
for (int i=0; i<T.n; i++) {
predecessorT[i] = -1;
discoverT[i] = 0;
finishT[i] = 0;
}
for (int i=0; i<T.n; i++) {
v = finish_rank[i];
cout << GREEN << "... v:" << v << WHITE << endl;
if (T.status(v) == UNDISCOVERED) {
cout << GREEN << "going to 2nd DFS from vertex:" << vertex(v) << WHITE << endl;
T.DFS(v, timeT, distanceT, predecessorT, discoverT, finishT);
}
}
//print
for (int i=0; i<this->n; i++) {
int p = predecessorT[i];
if (p >= 0) cout << YELLOW << "predecessorT[" << vertex(i) << "]:" << vertex(p) << WHITE << endl;
else cout << YELLOW << "predecessorT[" << vertex(i) << "]:" << predecessorT[i] << WHITE << endl;
}
for (int i=0; i<this->n; i++) {
cout << WHITE << "discoverT[" << vertex(i) << "]:" << discoverT[i] << WHITE << endl;
}
for (int i=0; i<this->n; i++) {
cout << WHITE << "finishT[" << vertex(i) << "]:" << finishT[i] << WHITE << endl;
}
//collapse
cout << GREEN << "going to collapse" << WHITE << endl;
T.collapse(predecessorT);
for (int i=0; i<this->n; i++) {
int p = predecessorT[i];
if (p >= 0) cout << YELLOW << "predecessorT[" << vertex(i) << "]:" << vertex(p) << WHITE << endl;
else cout << YELLOW << "predecessorT[" << vertex(i) << "]:" << predecessorT[i] << WHITE << endl;
}
//print SCC
cout << *this << endl;
std::string scc[this->n][3];
char c[2];
for (int i=0; i<this->n; i++) {
int p = predecessorT[i];
if (p == -1) {
c[0] = vertex(i);
scc[i][0].append(GREEN);
scc[i][0].append("Strong connected component#");
scc[i][0].append(c, 1);
scc[i][0].append("\n");
scc[i][0].append(CYAN);
scc[i][0].append("-------\n");
scc[i][0].append("[");
scc[i][0].append(c, 1);
scc[i][0].append("]");
}
}
for (int i=0; i<this->n; i++) {
int p = predecessorT[i];
if (p >= 0) {
c[0] = vertex(i);
scc[p][1].append(CYAN);
scc[p][1].append(" | ");
scc[p][2].append("[");
scc[p][2].append(c, 1);
scc[p][2].append("]");
}
}
int i = -1;
while (++i < this->n) {
if (!scc[i][0].empty()) {
cout << scc[i][0] << endl;
cout << scc[i][1] << endl;
cout << scc[i][2] << endl;
cout << "-------" << endl;
}
}
}
template<typename Tv, typename Te>
void amo::AdjaMatrix<Tv, Te>::TopoSort(int v) {
int root = v;
int time = 0;
int* distance = new int[this->n];
int* predecessor = new int[this->n];
int* discover = new int[this->n]; //all of dTime of V
int* finish = new int[this->n]; //all of fTime of V
for (int i=0; i<this->n; i++) {
predecessor[i] = -1;
}
do {
if (status(v) == UNDISCOVERED) {
cout << GREEN << "going to DFS the subgraph of v:" << v << WHITE << endl;
DFS(v, time, distance, predecessor, discover, finish);
}
} while (root!=(v=(++v%this->n)));
typename std::vector<std::vector<Edge<Te>*>>::iterator itE;
typename std::vector<Edge<Te>*>::iterator it_adjs;
for (itE=this->E.begin(); itE!=this->E.end(); itE++) {
for (it_adjs=itE->begin(); it_adjs!=itE->end(); it_adjs++) {
//cout << **it_adjs << endl;
if (*it_adjs == NULL) continue;
if (((*it_adjs)->type) == BACKWARD) {
cout << RED << "NOT DAG" << WHITE << endl;
return;
}
}
}
int* finish_sort = new int[this->n];
int* finish_rank = new int[this->n];
for (int i=0; i<this->n; i++) {
finish_sort[i] = finish[i];
finish_rank[i] = i;
}
quickSortAndRank(finish_rank, finish_sort, 0, (this->n)-1);
//print
cout << GREEN << "Topological sort:";
for (int i=0; i<this->n; i++) {
cout << GREEN << vertex(finish_rank[i]);
if (i != (this->n)-1) cout << GREEN << " -> ";
}
cout << WHITE << endl;
}
template<typename Tv, typename Te>
void AdjaMatrix<Tv, Te>::BCC(int v) {
int lim = std::numeric_limits<int>::max();
int *predecessor = new int[this->n];
int *discover = new int[this->n];
int *finish = new int[this->n];
int *hca = new int[this->n];
std::stack<int> stack;
int clock = 0;
int s = v;
for (int i=0; i<this->n; i++) {
predecessor[i] = -1;
hca[i] = lim;
}
do {
if (status(v) == UNDISCOVERED) {
cout << GREEN << "going to BCC from v:" << v << WHITE << endl;
BCC(v, discover, finish, predecessor, hca, stack, clock);