forked from gabbage/fanmod-cmd
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmaingraph.cpp
1911 lines (1723 loc) · 91.1 KB
/
maingraph.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
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
#include "maingraph.hpp"
/*********************
* UTILITIES *
*********************/
// Tries to find the specified value in the array and replaces it.
// Important: assumes that the value exists!
void find_and_replace(edge* array, edge find, edge replace){
long i = 0;
while (array[i]!=find)
++i;
array[i]= replace;
}
/*********************
* MAIN-Definitions *
*********************/
// read a graph from "filename" and return it.
bool read_graph(maingraph & maing, string filename, bool directed,
bool has_vertex_colors, bool has_edge_colors, short G_N,
bool gen_dumpfile)
{
vector <long> num_neighbours; // stores the number of neighbours of a vertex
vector <long> num_undir; // stores the number of undirected edges from a vertex
vector <short> vertex_colors;
ifstream in(filename.c_str());
const unsigned short NO_COLOR = 255;
if (in.good()) {
// Initialize variables
maing.n = 0;
maing.num_nodes = 0;
maing.num_lonely_nodes = 0;
maing.num_vertex_colors = 0;
maing.num_edge_colors = 1;
maing.directed = directed;
unsigned short color1 = NO_COLOR, color2 = NO_COLOR, color3 = NO_COLOR,
edge_color = NO_COLOR, color_u = NO_COLOR, color_v = NO_COLOR;
unsigned short edge_color_limit = 100, vertex_color_limit = 100;
unsigned short curr_colors = 0; // number of colors in the current line
if (has_vertex_colors){
switch (G_N){
case 3:
case 4: vertex_color_limit = 15; break;
case 5: vertex_color_limit = 15; break;
case 6: vertex_color_limit = 15; break;
case 7: vertex_color_limit = 3; break;
case 8: vertex_color_limit = 0; break;
}
}
if (has_edge_colors){
switch (G_N){
case 3:
case 4: edge_color_limit = 7; vertex_color_limit = 15; break;
case 5: edge_color_limit = 3; vertex_color_limit = 7; break;
case 6: edge_color_limit = 3; vertex_color_limit = 0; break;
case 7: edge_color_limit = 1; vertex_color_limit = 0; break;
case 8: edge_color_limit = 1; vertex_color_limit = 0; break;
}
}
//wxLogMessage(wxString()<<edge_color_limit << " "<<vertex_color_limit);
vertex u;
vertex v;
unsigned long linenumber = 0;
edge e;
// Parse input File
string line;
stringstream ss;
while( std::getline( in, line ) ) {
line += " ";
ss.str(line);
ss >> u >> v;
if (!ss.fail()) {
curr_colors = 0;
if (ss >> color1) ++curr_colors;
if (ss >> color2) ++curr_colors;
if (ss >> color3) ++curr_colors;
ss.clear(); //Reset Stream
vertex maxuv = (u>v) ? u : v;
if (maxuv > maing.n) {
maing.n = maxuv;
if (gen_dumpfile && (maxuv > 65535)){
wxLogMessage(wxString(wxT("In Line ")) << linenumber << wxT(", vertex number ")
<< maxuv << wxT(" was specified.\nLargest vertex number allowed with ")
<< wxT(" an activated subgraph dump is 65535."),
wxT("Exceeded vertex limit"), wxICON_ERROR | wxOK);
return false;
}
if (num_neighbours.size() <= maing.n) {
num_neighbours.resize(maing.n+1);
num_undir.resize(maing.n+1);
if (has_vertex_colors) {
unsigned long oldsize = vertex_colors.size();
vertex_colors.resize(maing.n+1);
for (unsigned long i = oldsize; i != vertex_colors.size(); ++i)
vertex_colors[i] = NO_COLOR;
}
}
}
if (u != v) {
// which color belongs to which graph-part
switch (curr_colors){
case 0: edge_color = NO_COLOR; color_u = NO_COLOR;
color_v = NO_COLOR; break;
case 1: edge_color = color1; color_u = NO_COLOR;
color_v = NO_COLOR; break;
case 2: edge_color = NO_COLOR; color_u = color1;
color_v = color2; break;
case 3: edge_color = color3; color_u = color1;
color_v = color2; break;
}
// set number of colors
if (has_edge_colors && edge_color != NO_COLOR && edge_color > maing.num_edge_colors)
maing.num_edge_colors = edge_color;
if (has_vertex_colors && color_u != NO_COLOR && color_u > maing.num_vertex_colors)
maing.num_vertex_colors = color_u;
if (has_vertex_colors && color_v != NO_COLOR && color_v > maing.num_vertex_colors)
maing.num_vertex_colors = color_v;
// check color limits
if (has_edge_colors && edge_color == 0){
wxLogMessage(wxString(wxT("In Line ")) << linenumber << wxT(", edge-color 0")
<< wxT(" was given.\nThis color is not allowed - edges must always ")
<< wxT("have a color >=1."),
wxT("Exceeded color limits"), wxICON_ERROR | wxOK);
return false;
}
if (has_edge_colors && maing.num_edge_colors > edge_color_limit){
wxLogMessage(wxString(wxT("In Line ")) << linenumber << wxT(", an edge-color >")
<< edge_color_limit << wxT(" was given.\nLargest edge-color allowed with ")
<< wxT("your settings is ") << edge_color_limit << wxT("."),
wxT("Exceeded color limits"), wxICON_ERROR | wxOK);
return false;
}
if (has_vertex_colors && maing.num_vertex_colors > vertex_color_limit){
wxLogMessage(wxString(wxT("In Line ")) << linenumber << wxT(", a vertex-color >")
<< vertex_color_limit << wxT(" was given.\nLargest vertex-color allowed with ")
<< wxT("your settings is ") << edge_color_limit << wxT("."),
wxT("Exceeded color limits"), wxICON_ERROR | wxOK);
return false;
}
// set vertex colors
if (!has_edge_colors)
edge_color = 1;
if (has_vertex_colors && color_u != NO_COLOR) vertex_colors[u]= color_u;
if (has_vertex_colors && color_v != NO_COLOR) vertex_colors[v]= color_v;
if (u < v) {
e = new_edge(u, v);
if (maing.edges.find(e) == maing.edges.end()) {
num_neighbours[u]++;
num_neighbours[v]++;
maing.edges[e] = DIR_U_T_V;
if(edge_color == NO_COLOR)
set_color_u_v(maing.edges[e],1);
else
set_color_u_v(maing.edges[e],edge_color);
} else {
maing.edges[e] |= DIR_U_T_V;
if(edge_color == NO_COLOR) {
if (get_color_u_v == 0)
set_color_u_v(maing.edges[e],1);
} else {
set_color_u_v(maing.edges[e],edge_color);
}
}
if (!directed) {
edgetype new_et = UNDIR_U_V;
unsigned short new_et_color = (edge_color == NO_COLOR) ? 1 : edge_color;
set_color_v_u(new_et, new_et_color);
set_color_u_v(new_et, new_et_color);
maing.edges[e] = new_et;
// use the color of u_v (pass true to set_undir), if edge_color is not NO_COLOR
}
} else { // v < u
e = new_edge(v, u);
if (maing.edges.find(e) == maing.edges.end()) {
num_neighbours[u]++;
num_neighbours[v]++;
maing.edges[e] = DIR_V_T_U;
if(edge_color == NO_COLOR)
set_color_v_u(maing.edges[e],1);
else
set_color_v_u(maing.edges[e],edge_color);
} else {
maing.edges[e] |= DIR_V_T_U;
if(edge_color == NO_COLOR) {
if (get_color_v_u == 0)
set_color_v_u(maing.edges[e],1);
} else {
set_color_v_u(maing.edges[e],edge_color);
}
}
if (!directed) {
edgetype new_et = UNDIR_U_V;
unsigned short new_et_color = (edge_color == NO_COLOR) ? 1 : edge_color;
set_color_v_u(new_et, new_et_color);
set_color_u_v(new_et, new_et_color);
maing.edges[e] = new_et;
// use the color of v_u (pass false to set_undir) if edge_color is not NO_COLOR
}
}
if (rm_colors(maing.edges[e]) == UNDIR_U_V) {
++num_undir[u];
++num_undir[v];
}
}
} else { // if ss.fail
wxMessageBox(wxString(wxT("Wrong format for input file \'")) << wxString(filename.c_str(),wxConvUTF8) << wxT("\' in line ") << linenumber << wxT("\nExpected line format is <int> <int> [<int>] [<int>] [<int>]."), wxT("Error reading file"), wxICON_ERROR | wxOK);
return false;
}
++linenumber;
}
} else { // if !in.good
wxMessageBox(wxString(wxT("Unable to open file \'")) << wxString(filename.c_str(),wxConvUTF8) << wxT("\' for input."), wxT("Error reading file"), wxICON_ERROR | wxOK);
return false;
}
++maing.n;
in.close();
// Allocates the arrays of the maingraph
maing.num_neighbours = new unsigned long[maing.n+1];
maing.neighbours = new vertex *[maing.n+1];
if (has_vertex_colors)
maing.vertex_colors = new short[maing.n+1];
//maing.num_larger_neighbours = new unsigned long[maing.n+1];
maing.v_util = new unsigned long[maing.n+1];
for (vertex i = 0; i != maing.n; ++i) {
if (has_vertex_colors) maing.vertex_colors[i] = (vertex_colors[i] != NO_COLOR) ? vertex_colors[i] : 0; // copy vertex colors
if (maing.directed)
maing.neighbours[i] = new vertex[num_neighbours[i]+num_undir[i]];
else
maing.neighbours[i] = new vertex[num_neighbours[i]];
}
++maing.num_vertex_colors;
//++maing.num_edge_colors; <<- STARTS AT 1 AND NOT 0
// the vectors are no longer needed
vertex_colors.clear();
num_neighbours.clear();
num_undir.clear();
return true;
}
void build_graph(maingraph & maing)
{
for (vertex i = 0; i != maing.n; ++i) {
maing.v_util[i] = NILLVERTEX; // reset util fields;
//maing.num_larger_neighbours[i] = 0;
maing.num_neighbours[i] = 0; // num_neighbours might have changed during randomize
}
maing.m = maing.edges.size(); // Number of edges might have changed during "no regard"-randomization
//maing.maxnumlargerneighbors = 0;
//maing.numlargerneighbors = 0;
maing.maxnumneighbours = 0;
maing.num_undir_edges = 0;
maing.num_dir_edges = 0;
for (hash_map < edge, edgetype >::const_iterator iter = maing.edges.begin();
iter != maing.edges.end(); ++iter) {
vertex u = edge_get_u(iter->first);
vertex v = edge_get_v(iter->first);
edgetype etype = rm_colors(iter->second);
if (etype == UNDIR_U_V) {
++maing.num_undir_edges;
} else {
++maing.num_dir_edges;
}
(maing.neighbours[u])[maing.num_neighbours[u]] = v;
++maing.num_neighbours[u];
if (maing.num_neighbours[u] > maing.maxnumneighbours)
maing.maxnumneighbours = maing.num_neighbours[u];
(maing.neighbours[v])[maing.num_neighbours[v]] = u;
++maing.num_neighbours[v];
if (maing.num_neighbours[v] > maing.maxnumneighbours)
maing.maxnumneighbours = maing.num_neighbours[v];
//if (u>v) {
// ++maing.num_larger_neighbours[v];
// if (maing.num_larger_neighbours[v]> maing.maxnumlargerneighbors)
// ++maing.maxnumlargerneighbors;
//} else { // v>u
// ++maing.num_larger_neighbours[u];
// if (maing.num_larger_neighbours[u]> maing.maxnumlargerneighbors)
// ++maing.maxnumlargerneighbors;
//}
}
//Sort the neighbour array for every vertex
for (unsigned int i = 0; i != maing.n; ++i)
std::sort(maing.neighbours[i], maing.neighbours[i] + maing.num_neighbours[i]);
return;
}
// estimates the size of the subgraph tree using TREESMPLS samples.
uint64 est_tree_size(const maingraph & g, long* v_extension, uint64 TREESMPLS,
short G_N, randlib::rand &rand)
{
register vertex* v_subgraph = new vertex[G_N+1];
register long counter;
register vertex SOURCE_VERTEX;
register vertex FILL_VERTEX;
register long* min_scope = new long[G_N+1];
register long* max_scope = new long[G_N+1];
register long* scope_place = new long[G_N+1];
register short depth;
uint64 APPROX_TREE_SIZE = 0;
for (uint64 th = 0; th!=TREESMPLS; ++th) {
vertex v = ++rand % g.n;
uint64 approx = 1;
bool flag = false;
if (g.num_neighbours[v] > 0) {
depth = 1;
min_scope[depth] = 0;
max_scope[depth] = 1;
scope_place[depth] = 0;
v_extension[0] = v;
v_subgraph[depth-1] = NILLVERTEX;
g.v_util[v] = v;
while (depth != 0) {
if (flag) { //scope_place[depth] == max_scope[depth] || flag) {
if (min_scope[depth] != max_scope[depth]) {
counter = max_scope[depth] - 1;
while (counter >= 0 && g.v_util[v_extension[counter]] == v_subgraph[depth-1]) {
g.v_util[v_extension[counter]] = NILLVERTEX;
--counter;
}
}
--depth;
} else {
if (depth == G_N) {
flag = true;
while (scope_place[depth] != max_scope[depth]) {
v_subgraph[depth] = v_extension[scope_place[depth]];
++scope_place[depth];
}
} else {
if (scope_place[depth] == max_scope[depth]) {
approx = 0;
flag = true;
} else {
scope_place[depth] += (++rand) % (max_scope[depth]-scope_place[depth]);
SOURCE_VERTEX = v_extension[scope_place[depth]];
++scope_place[depth];
v_subgraph[depth] = SOURCE_VERTEX;
min_scope[depth+1] = max_scope[depth];
scope_place[depth+1] = scope_place[depth];
max_scope[depth+1] = min_scope[depth+1];
++depth;
counter = g.num_neighbours[SOURCE_VERTEX] - 1;
if (counter > 0) {
FILL_VERTEX = g.neighbours[SOURCE_VERTEX][counter];
}
while (counter > -1 && g.neighbours[SOURCE_VERTEX][counter] > v) {
FILL_VERTEX = g.neighbours[SOURCE_VERTEX][counter];
if (g.v_util[FILL_VERTEX] == NILLVERTEX) {
v_extension[max_scope[depth]] = FILL_VERTEX;
g.v_util[FILL_VERTEX] = SOURCE_VERTEX;
++max_scope[depth];
}
--counter;
}
approx *= (max_scope[depth] - scope_place[depth]);
}
}
}
}
g.v_util[v] = NILLVERTEX;
APPROX_TREE_SIZE += approx;
}
}
delete[] v_subgraph;
delete[] min_scope;
delete[] max_scope;
delete[] scope_place;
return APPROX_TREE_SIZE;
}
// Samples / Enumerates the graph returns results in result_graphs, and the time directly.
double sampling(const maingraph & maing, long* v_extension, short G_N,
bool fullenumeration, const double* prob, const uint64 equiv100p,
const int & perc_number, hash_map < graphcode64, uint64 > & result_graphs,
uint64 & count_subgr, wxFrame *frame, wxThread *thread,
randlib::rand &rand, bool gen_dumpfile, vector<subgraph>& subgraphdump)
{
// Init workgraph
graph64 g;
init_graph(g,G_N,maing.num_vertex_colors,maing.num_edge_colors,maing.directed);
// Init for Statusbar
register int perc_index = 0;
wxCommandEvent percentage_event(wxEVT_COMMAND_MENU_SELECTED,
ID_PERCENT_REACHED);
percentage_event.SetInt(perc_number);
// Init for main loop
count_subgr = 0;
register vertex v_subgraph[G_N_MAX+1];
register long counter;
register vertex SOURCE_VERTEX;
register vertex FILL_VERTEX;
register long min_scope[G_N_MAX+1];
register long max_scope[G_N_MAX+1];
register long scope_place[G_N_MAX+1];
register short depth;
const bool vcolors = (maing.num_vertex_colors > 1);
const bool ecolors = (maing.num_edge_colors > 1);
register vertex* start_vertices = new vertex[maing.n+2];//##%!
register short scope_place_point[G_N_MAX+1];//##%!
register unsigned long* scope_place_loc[G_N_MAX+1];
//for dumpfile
//vector<subgraph> subgraphdump; //stores all found subgraphs in three
//consecutive uint64
// Beginning actual sampling
for (int i = 0; i != G_N+1; ++i) {
scope_place_loc[i] = new unsigned long[maing.maxnumneighbours*G_N];
}
//scope_place_loc[8][maxnumlargerneighbors*G_N];//##%!
//gen_selection(0,tst,0.174,arr,rand);
if (fullenumeration) {
for (vertex v = 0; v != maing.n; ++v)
start_vertices[v] = v;
start_vertices[maing.n] = maing.n;
} else {
gen_selection(0,maing.n,prob[0],start_vertices,rand);
}
clock_t start_time(clock());
for (int th = 0; th!=1; ++th) // this line for time measurements only
{
int idx = 0;
while (start_vertices[idx]!=maing.n)
{
vertex v = start_vertices[idx];
++idx;
//if (fullenumeration || ((++rand)%MAXPROB) <= prob[0])///////////////////////////////////////////////
{
depth = 1;
min_scope[depth] = 0;
max_scope[depth] = 1;
scope_place[depth] = 0;
v_extension[0] = v;
v_subgraph[depth-1] = NILLVERTEX;
maing.v_util[v] = v;
scope_place_point[depth] = 0;
scope_place_loc[1][0] = 0;
scope_place_loc[1][1] = 1;
while (depth != 0) {
if (scope_place[depth] == max_scope[depth]) { // go lower
if (min_scope[depth] != max_scope[depth]) {
counter = max_scope[depth] - 1;
while (counter >= 0 && maing.v_util[v_extension[counter]] == v_subgraph[depth-1]) {
maing.v_util[v_extension[counter]] = NILLVERTEX;
--counter;
}
}
--depth;
} else {
if (depth == G_N) {
while (scope_place[depth] != max_scope[depth])
{
{
++count_subgr;
v_subgraph[depth] = v_extension[scope_place[depth]];
if (vcolors)
color_vertex(g,depth-1,maing.vertex_colors[v_subgraph[depth]]);
// edit nauty-graph
for (int i = 0; i != depth-1; ++i) {
vertex uc = v_subgraph[i+1];
vertex vc = v_subgraph[depth];
edge e_check = edge_code(uc, vc);
delete_edge(g,depth-1,i);
delete_edge(g,i,depth-1);
if (maing.edges.find(e_check) != maing.edges.end()) {
edgetype ec = maing.edges.find(e_check)->second;
if (ecolors)
{ // colored egdes
if (uc < vc) {
switch (rm_colors(ec)) {
case DIR_U_T_V : set_edge(g,i,depth-1,get_color_u_v(ec)); break;
case DIR_V_T_U : set_edge(g,depth-1,i,get_color_v_u(ec)); break;
case UNDIR_U_V : set_edge(g,i,depth-1,get_color_u_v(ec));
set_edge(g,depth-1,i,get_color_v_u(ec)); break;
}
} else {
switch (rm_colors(ec)) {
case DIR_U_T_V : set_edge(g,depth-1,i,get_color_u_v(ec)); break;
case DIR_V_T_U : set_edge(g,i,depth-1,get_color_v_u(ec)); break;
case UNDIR_U_V : set_edge(g,depth-1,i,get_color_u_v(ec));
set_edge(g,i,depth-1,get_color_v_u(ec)); break;
}
}
} else { //no colored edges
if (uc < vc) {
switch (rm_colors(ec)) {
case DIR_U_T_V : set_edge(g,i,depth-1); break;
case DIR_V_T_U : set_edge(g,depth-1,i); break;
case UNDIR_U_V : set_edge(g,i,depth-1);
set_edge(g,depth-1,i); break;
}
} else {
switch (rm_colors(ec)) {
case DIR_U_T_V : set_edge(g,depth-1,i); break;
case DIR_V_T_U : set_edge(g,i,depth-1); break;
case UNDIR_U_V : set_edge(g,i,depth-1);
set_edge(g,depth-1,i); break;
}
}
}
}
}
// end edit nauty-graph
// Canonical labelling with nauty
graphcode64 hashg = toHashCode(g);
result_graphs[ hashg ]++;
//Dump subgraph if requested
if (gen_dumpfile)
subgraphdump.push_back(get_subgraph(v_subgraph, G_N, hashg));
// end nauty
if (fullenumeration)
{
++scope_place[depth];
} else {
++scope_place_point[depth];
scope_place[depth] = scope_place_loc[depth][scope_place_point[depth]];
}
}
}
} else { //go deeper
SOURCE_VERTEX = v_extension[scope_place[depth]];
if (fullenumeration) {
++scope_place[depth];
} else {
++scope_place_point[depth];
scope_place[depth] = scope_place_loc[depth][scope_place_point[depth]];
}
{//!!
v_subgraph[depth] = SOURCE_VERTEX;
if (vcolors)
color_vertex(g,depth-1,maing.vertex_colors[SOURCE_VERTEX]);
// edit nauty-graph
for (int i = 0; i != depth-1; ++i) {
vertex uc = v_subgraph[i+1];
vertex vc = SOURCE_VERTEX;
edge e_check = edge_code(uc, vc);
delete_edge(g,depth-1,i);
delete_edge(g,i,depth-1);
if (maing.edges.find(e_check) != maing.edges.end()) {
edgetype ec = maing.edges.find(e_check)->second;
if (ecolors)
{ // colored egdes
if (uc < vc) {
switch (rm_colors(ec)) {
case DIR_U_T_V : set_edge(g,i,depth-1,get_color_u_v(ec)); break;
case DIR_V_T_U : set_edge(g,depth-1,i,get_color_v_u(ec)); break;
case UNDIR_U_V : set_edge(g,i,depth-1,get_color_u_v(ec));
set_edge(g,depth-1,i,get_color_v_u(ec)); break;
}
} else {
switch (rm_colors(ec)) {
case DIR_U_T_V : set_edge(g,depth-1,i,get_color_u_v(ec)); break;
case DIR_V_T_U : set_edge(g,i,depth-1,get_color_v_u(ec)); break;
case UNDIR_U_V : set_edge(g,depth-1,i,get_color_u_v(ec));
set_edge(g,i,depth-1,get_color_v_u(ec)); break;
}
}
} else { //no colored edges
if (uc < vc) {
switch (rm_colors(ec)) {
case DIR_U_T_V : set_edge(g,i,depth-1); break;
case DIR_V_T_U : set_edge(g,depth-1,i); break;
case UNDIR_U_V : set_edge(g,i,depth-1);
set_edge(g,depth-1,i); break;
}
} else {
switch (rm_colors(ec)) {
case DIR_U_T_V : set_edge(g,depth-1,i); break;
case DIR_V_T_U : set_edge(g,i,depth-1); break;
case UNDIR_U_V : set_edge(g,i,depth-1);
set_edge(g,depth-1,i); break;
}
}
}
}
}
// end edit nauty-graph
min_scope[depth+1] = max_scope[depth];
scope_place[depth+1] = scope_place[depth];
max_scope[depth+1] = min_scope[depth+1];
++depth;
counter = maing.num_neighbours[SOURCE_VERTEX] - 1;
if (counter > 0) {
FILL_VERTEX = maing.neighbours[SOURCE_VERTEX][counter];
}
while (counter > -1 && maing.neighbours[SOURCE_VERTEX][counter] > v) {
FILL_VERTEX = maing.neighbours[SOURCE_VERTEX][counter];
if (maing.v_util[FILL_VERTEX] == NILLVERTEX) {
v_extension[max_scope[depth]] = FILL_VERTEX;
maing.v_util[FILL_VERTEX] = SOURCE_VERTEX;
++max_scope[depth];
}
--counter;
}
}//!!
//GENERATE SELECTION AND RESET SCOPE PLACE POINT
if (! fullenumeration)
{
scope_place_point[depth] = 0;
gen_selection(scope_place[depth], max_scope[depth],
prob[depth-1],scope_place_loc[depth],rand);
scope_place[depth] = scope_place_loc[depth][0];
}
}
}
}
maing.v_util[v] = NILLVERTEX;
}
}
}
//Clean up
//delete[] v_subgraph;
//delete[] min_scope;
//delete[] max_scope;
//delete[] scope_place;
delete[] start_vertices;
//delete[] scope_place_point;
for (int i = 0; i != G_N+1; ++i) {
delete[] scope_place_loc[i];
}
//delete[] scope_place_loc;
// Done with sampling / enumeration: stop the clock, return the time
return double (clock() - start_time) / CLOCKS_PER_SEC;
}
/********************* RANDOMIZATION **********************************/
double randomize_graph(maingraph & maing, short random_type,
int num_exchanges, int num_tries,
const bool vertex_color_matters, const bool edge_color_matters,
EdgeContainer & EC, long & total_tries, long & total_success,
randlib::rand &rand) {
clock_t start_time(clock()); // Start the time measurement
long rule4 = 0, rule5 = 0;
switch (random_type) {
case NO_REGARD :
for (int exchanges = 0; exchanges != num_exchanges; ++exchanges) {
EC.resetIteration();
while (EC.hasNextBag()) {
EC.goNextBag();
bagID bag_id = EC.getCurrentBag();
bag_index bag_size = EC.getBagSize(bag_id);
while (EC.hasNextElement()) {
EC.goNextElement();
bag_index current_index = EC.getCurrentIndex();
//Begin switch attempt
for (int tries = 0; tries != num_tries; tries++) {
++total_tries;
if (bag_size == 1)
{ total_tries+= (num_tries-1); break;}
//choose random exchange partner
bag_index random_index = (++rand) % bag_size;
while (random_index == current_index) {
random_index = (++rand) % bag_size;
}
//Check if switch can be performed
//Uses invariant that only unidirectional edges
//may be present in the EdgeContainer
//May be noncanonical edgecode!! (Direction
//is implicitly stored here)
const edge e = EC.getCurrentElement();
const edge f = EC.getIndexedElement(random_index);
const vertex eu = edge_get_u(e);
const vertex ev = edge_get_v(e);
const vertex fu = edge_get_u(f);
const vertex fv = edge_get_v(f);
if (eu != fu && eu != fv && ev != fu && ev != fv) { //check vertices
const edge e_code = edge_code(eu,ev);
const edge f_code = edge_code(fu,fv);
const edge eufv_code = edge_code(eu,fv);
const edge fuev_code = edge_code(fu,ev);
const edgetype et = maing.edges[e_code],
ft = maing.edges[f_code];
const short color_euev = (eu < ev) ? get_color_u_v(et) : get_color_v_u(et);
const short color_fufv = (fu < fv) ? get_color_u_v(ft) : get_color_v_u(ft);
const bool evfu_exists = maing.edges.find(fuev_code) != maing.edges.end();
const bool fveu_exists = maing.edges.find(eufv_code) != maing.edges.end();
bool can_switch = true;
// if (rm_colors(et) == UNDIR_U_V || rm_colors(ft) == UNDIR_U_V)
// can_switch = false;
if (fveu_exists) {
const edgetype n_et = maing.edges[eufv_code];
if (eu < fv && ((n_et & DIR_U_T_V) == DIR_U_T_V))
can_switch = false;
else if (eu > fv && ((n_et & DIR_V_T_U) == DIR_V_T_U))
can_switch = false;
}
if (evfu_exists) {
const edgetype n_ft = maing.edges[fuev_code];
if (fu < ev && ((n_ft & DIR_U_T_V) == DIR_U_T_V))
can_switch = false;
else if (fu > ev && ((n_ft & DIR_V_T_U) == DIR_V_T_U))
can_switch = false;
}
if (can_switch) {
//Perform
//perform on EC (bagID remains here)
EC.setIndexedElement(current_index,new_edge(eu,fv));
EC.setIndexedElement(random_index,new_edge(fu,ev));
///*
//4 Things to consider when manipulating HashMap:
// 1. del euev: may be bidirected
const edgetype net = (eu < ev) ? get_vu(et) : get_uv(et);
if (net == 0) {
maing.edges.erase(e_code);
} else {
maing.edges[e_code] = net;
}
// 2. del fufv: may be bidirected
const edgetype nft = (fu < fv) ? get_vu(ft) : get_uv(ft);
if (nft == 0) {
maing.edges.erase(f_code);
} else {
maing.edges[f_code] = nft;
}
// 3. set eu->fv: eu<-fv may already exist
edgetype type_eufv = (eu < fv) ? (colored_u_v(DIR_U_T_V,color_euev))
: (colored_v_u(DIR_V_T_U,color_euev));
if (fveu_exists) {
maing.edges[eufv_code] |= type_eufv;
} else {
maing.edges[eufv_code] = type_eufv;
}
// 4. set fu->ev: fu<-ev may already exist
edgetype type_fuev = (fu < ev) ? (colored_u_v(DIR_U_T_V,color_fufv))
: (colored_v_u(DIR_V_T_U,color_fufv));
if (evfu_exists) {
maing.edges[fuev_code] |= type_fuev;
} else {
maing.edges[fuev_code] = type_fuev;
}
//*/
++total_success;
break;//Break try-loop
}//end exchange
}//end check vertices
}//end exchange attempt
}
}
}
//Update Maingraph
build_graph(maing);
break; // break case NO_REGARD
case GLOBAL_CONST :
for (int exchanges = 0; exchanges != num_exchanges; ++exchanges) {
EC.resetIteration();
while (EC.hasNextBag()) {
EC.goNextBag();
bagID bag_id = EC.getCurrentBag();
bag_index bag_size = EC.getBagSize(bag_id);
while (EC.hasNextElement()) {
EC.goNextElement();
bag_index current_index = EC.getCurrentIndex();
//Begin switch attempt
for (int tries = 0; tries != num_tries; tries++) {
++total_tries;
const edge e = EC.getCurrentElement();
vertex eu = edge_get_u(e);
vertex ev = edge_get_v(e);
const edgetype et = maing.edges[e];
short color_euev = get_color_u_v(et);
short color_eveu = get_color_v_u(et);
//-------------------------------------------------
if (rm_colors(et) == UNDIR_U_V) {
//---------------------Start new global BIDIR rules
//Try to apply rule 5
vertex cand_w = NILLVERTEX, cand_x = NILLVERTEX, cand_y = NILLVERTEX;
short cand_color_wx = 0, cand_color_xy = 0;
double num_rule5_candiates = 0.0;
//Determine the direction in which we are searching
if (rand.trueWithProb(0.5)) {
const vertex tmp = eu;
eu = ev;
ev = tmp;
const short tmpcolor = color_euev;
color_euev = color_eveu;
color_eveu = tmpcolor;
}
//find appropriate edge in bag
//retrieve bagID
short colr_u = vertex_color_matters ? maing.vertex_colors[eu] : 1;
short colr_v = vertex_color_matters ? maing.vertex_colors[ev] : 1;
short colr_euev = edge_color_matters ? color_euev : 1;
// short colr_eveu = edge_color_matters ? color_eveu : 1;
const bagID cand_ebag = getBagID(colr_euev,0,colr_u,colr_v);
const bag_index ebag_size = EC.getBagSize(cand_ebag);
if (ebag_size > 0) {
const edge edge_wx = EC.getElement( (++rand)%ebag_size ,cand_ebag);
const edgetype etype_wx = maing.edges[edge_wx];
const vertex vert_w = (rm_colors(etype_wx) == DIR_U_T_V) ? edge_get_u(edge_wx) : edge_get_v(edge_wx);
const vertex vert_x = (rm_colors(etype_wx) == DIR_U_T_V) ? edge_get_v(edge_wx) : edge_get_u(edge_wx);
const short color_wx = (vert_w<vert_x) ? get_color_u_v(etype_wx) : get_color_v_u(etype_wx);
//go over neighbours and find candidates
if (vert_w != eu && vert_w != ev && vert_x != eu && vert_x != ev
&& maing.edges.find(edge_code(eu,vert_x)) == maing.edges.end()
&& maing.edges.find(edge_code(ev,vert_w)) == maing.edges.end()) { //vertices ok?
for (unsigned long i = 0; i != maing.num_neighbours[vert_x]; ++i) {
vertex y = maing.neighbours[vert_x][i];
if (y!= eu && y != ev && maing.edges.find(edge_code(ev,y)) == maing.edges.end()) {
const edgetype et_xy = maing.edges[edge_code(vert_x,y)];
const bool direction_xy_ok = (vert_x<y) ? (rm_colors(et_xy) == DIR_U_T_V) : (rm_colors(et_xy) == DIR_V_T_U);
const short color_xy = (vert_x<y) ? get_color_u_v(et_xy) : get_color_v_u(et_xy);
const bool xy_vcolor_ok = vertex_color_matters ? (maing.vertex_colors[eu] == maing.vertex_colors[y]) : true;
const bool xy_ecolor_ok = edge_color_matters ? (color_eveu == color_xy) : true;
if (direction_xy_ok && xy_vcolor_ok && xy_ecolor_ok) {
if (rand.trueWithProb(1.0 / (++num_rule5_candiates))) {
cand_w = vert_w;
cand_x = vert_x;
cand_y = y;
cand_color_xy = color_xy;
cand_color_wx = color_wx;
}
}
}
}
}//end vertices ok check
//if candidates were found, perform the switch
if (cand_w != NILLVERTEX && cand_x != NILLVERTEX && cand_y != NILLVERTEX) {
/* wxMessageBox(wxString("ex: ") << eu << " " << ev << " " << cand_w
<< " " << cand_x << " " << cand_y
<< " " << color_euev << " " << cand_color_wx
<< " " << color_eveu << " " << cand_color_xy, "Error reading file", wxICON_ERROR | wxOK);
*/
++rule5;
const edge e_ux = edge_code(eu,cand_x),
e_wx = edge_code(cand_w,cand_x),
e_wv = edge_code(cand_w,ev),
e_xy = edge_code(cand_x,cand_y),
e_vy = edge_code(ev,cand_y);
//adjust edges in maingraph
const edgetype et_ux = colored_edge(eu,cand_x,color_euev,color_eveu),
et_wv = colored_edge(ev,cand_w,0,cand_color_wx),
et_vy = colored_edge(ev,cand_y,cand_color_xy,0);
maing.edges[e_ux] = et_ux;
maing.edges[e_wv] = et_wv;
maing.edges[e_vy] = et_vy;
maing.edges.erase(e);
maing.edges.erase(e_wx);
maing.edges.erase(e_xy);
//adjust neighbors in maingraph
replace_neighbour(maing,eu,ev,cand_x);
replace_neighbour(maing,ev,eu,cand_y);
add_neighbour(maing,ev,cand_w);
replace_neighbour(maing,cand_w,cand_x,ev);
replace_neighbour(maing,cand_x,cand_y,eu);
del_neighbour(maing,cand_x,cand_w);
replace_neighbour(maing,cand_y,cand_x,ev);
//adjust edgecontainer
EC.loggedReplaceElement(e,e_ux);
EC.loggedReplaceElement(e_wx,e_wv);
EC.loggedReplaceElement(e_xy,e_vy);
break;
}
}//end if ebagsize > 0
//Try to apply rule 4
//find candidates, choose random one
//Perform if success and break edge exchange try
vertex candidate_w = NILLVERTEX, candidate_x = NILLVERTEX;
short candidate_ecolor_tow = 0, candidate_ecolor_wx = 0;
double number_of_candidates = 0.0;
//iterate over v neighbors
for (unsigned long i = 0; i != maing.num_neighbours[ev]; ++i) {
vertex w = maing.neighbours[ev][i];
const edgetype et_vw = maing.edges[edge_code(ev,w)];
const bool direction_vw_ok = (ev<w) ? (rm_colors(et_vw) == DIR_U_T_V) : (rm_colors(et_vw) == DIR_V_T_U);
const short color_vw = (ev<w) ? get_color_u_v(et_vw) : get_color_v_u(et_vw);
const bool uw_vcolor_ok = vertex_color_matters ? (maing.vertex_colors[eu] == maing.vertex_colors[w]) : true;
const bool uw_ecolor_ok = edge_color_matters ? (color_eveu == color_vw) : true;
if ( direction_vw_ok //check if single edge
&& uw_vcolor_ok //vertex colors ok?
&& uw_ecolor_ok ) {
//neighbor w would be ok
for (unsigned long j = 0; j != maing.num_neighbours[w]; ++j) {
vertex x = maing.neighbours[w][j];
const edgetype et_wx = maing.edges[edge_code(w,x)];
const bool direction_wx_ok = (w<x) ? (rm_colors(et_wx) == DIR_U_T_V) : (rm_colors(et_wx) == DIR_V_T_U);
const short color_wx = (w<x) ? get_color_u_v(et_wx) : get_color_v_u(et_wx);
const bool wx_vcolor = vertex_color_matters ? (maing.vertex_colors[ev] == maing.vertex_colors[x]) : true;
const bool wx_ecolor = edge_color_matters ? (color_euev == color_wx) : true;
if (direction_wx_ok && x!=eu && x!=ev
&& wx_vcolor //vertex colors ok?
&& wx_ecolor
&& (maing.edges.find(edge_code(eu,x)) == maing.edges.end() ) ) {
//We have a candidate
//This candidate is set with a certain probability
if (rand.trueWithProb(1.0 / (++number_of_candidates))) {
candidate_w = w;
candidate_x = x;
candidate_ecolor_tow = color_vw;
candidate_ecolor_wx = color_wx;
}
}
}
}
}
bool w_adjacent_v = true;
//iterate over u neighbors
for (unsigned long i = 0; i != maing.num_neighbours[eu]; ++i) {
vertex w = maing.neighbours[eu][i];
const edgetype et_uw = maing.edges[edge_code(eu,w)];
const bool direction_uw_ok = (eu<w) ? (rm_colors(et_uw) == DIR_U_T_V) : (rm_colors(et_uw) == DIR_V_T_U);
const short color_uw = (eu<w) ? get_color_u_v(et_uw) : get_color_v_u(et_uw);
const bool vw_vcolor = vertex_color_matters ? (maing.vertex_colors[ev] == maing.vertex_colors[w]) : true;
const bool vw_ecolor = edge_color_matters ? (color_euev == color_uw) : true;