-
Notifications
You must be signed in to change notification settings - Fork 0
/
lwm_initAndCollect.c
1057 lines (882 loc) · 34.5 KB
/
lwm_initAndCollect.c
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
/*
* lwm_initAndCollect.c
*
* Created on: Mar 27, 2018
* Author: pascal
*/
#include <stdlib.h>
#include <string.h>
#include "graph.h"
#include "loading.h"
#include "outerplanar.h"
#include "searchTree.h"
#include "cs_Parsing.h"
#include "cs_Tree.h"
#include "sampleSubtrees.h"
#include "subtreeIsoUtils.h"
#include "localEasySubtreeIsomorphism.h"
#include "lwm_initAndCollect.h"
/**
* to avoid memory leaks, vertex and edge labels need to be explicitly copied before underlying graph or shallow graphs are dumped
*/
static void hardCopyGraphLabels(struct Graph* g) {
for (int vi=0; vi<g->n; ++vi) {
struct Vertex* v = g->vertices[vi];
if (v->isStringMaster == 0) {
v->label = copyString(v->label);
v->isStringMaster = 1;
}
for (struct VertexList* e=v->neighborhood; e!=NULL; e=e->next) {
if (e->isStringMaster == 0) {
e->label = copyString(e->label);
e->isStringMaster = 1;
}
}
}
}
int getDB(struct Graph*** db) {
struct Graph* g = NULL;
int dbSize = 0;
int i = 0;
while ((g = iterateFile())) {
/* make space for storing graphs in array */
if (dbSize <= i) {
dbSize = dbSize == 0 ? 128 : dbSize * 2;
*db = realloc(*db, dbSize * sizeof (struct Graph*));
}
/* store graph */
(*db)[i] = g;
++i;
}
return i;
}
int getSpanningTreeSamplesOfDB(struct Graph*** db, int k, struct GraphPool* gp, struct ShallowGraphPool* sgp) {
struct Graph* g = NULL;
int dbSize = 0;
int i = 0;
while ((g = iterateFile())) {
struct Graph* h = NULL;
if (g->n < 2) {
h = cloneGraph(g, gp);
} else {
// sample k spanning trees, canonicalize them and add them in a search tree (to avoid duplicates, i.e. isomorphic spanning trees)
struct ShallowGraph* sample = runForEachConnectedComponent(&xsampleSpanningTreesUsingWilson, g, k, k, 1, gp, sgp);
for (struct ShallowGraph* tree=sample; tree!=NULL; tree=tree->next) {
if (tree->m != 0) {
struct Graph* tmp = shallowGraphToGraph(tree, gp);
tmp->next = h;
h = tmp;
}
}
dumpShallowGraphCycle(sgp, sample);
}
h = mergeGraphs(h, gp);
h->number = g->number;
h->activity = g->activity;
// avoid memory leaks, access to freed memory, and free unused stuff
hardCopyGraphLabels(h);
dumpGraph(gp, g);
/* make space for storing graphs in array */
if (dbSize <= i) {
dbSize = dbSize == 0 ? 128 : dbSize * 2;
*db = realloc(*db, dbSize * sizeof (struct Graph*));
}
/* store graph */
(*db)[i] = h;
++i;
}
return i;
}
static int getAllSpanningTreesOfDB(struct Graph*** db, int k, struct GraphPool* gp, struct ShallowGraphPool* sgp) {
struct Graph* g = NULL;
int dbSize = 0;
int i = 0;
while ((g = iterateFile())) {
struct Graph* h = NULL;
if (g->n < 2) {
h = cloneGraph(g, gp);
} else {
// sample k spanning trees, canonicalize them and add them in a search tree (to avoid duplicates, i.e. isomorphic spanning trees)
struct ShallowGraph* sample = runForEachConnectedComponent(&xlistSpanningTrees, g, k, k, 1, gp, sgp);
for (struct ShallowGraph* tree=sample; tree!=NULL; tree=tree->next) {
if (tree->m != 0) {
struct Graph* tmp = shallowGraphToGraph(tree, gp);
tmp->next = h;
h = tmp;
}
}
dumpShallowGraphCycle(sgp, sample);
}
h = mergeGraphs(h, gp);
h->number = g->number;
h->activity = g->activity;
// avoid memory leaks, access to freed memory, and free unused stuff
hardCopyGraphLabels(h);
dumpGraph(gp, g);
/* make space for storing graphs in array */
if (dbSize <= i) {
dbSize = dbSize == 0 ? 128 : dbSize * 2;
*db = realloc(*db, dbSize * sizeof (struct Graph*));
}
/* store graph */
(*db)[i] = h;
++i;
}
return i;
}
static int getNonisomorphicSpanningTreeSamplesOfDB(struct Graph*** db, int k, struct GraphPool* gp, struct ShallowGraphPool* sgp) {
struct Graph* g = NULL;
int dbSize = 0;
int i = 0;
while ((g = iterateFile())) {
struct Vertex* searchTree = getVertex(gp->vertexPool);
struct ShallowGraph* sample = NULL;
if (g->n < 2) {
// a singleton is a tree. we add it / its unique spanning tree to the search tree
addToSearchTree(searchTree, canonicalStringOfTree(g, sgp), gp, sgp);
} else {
// sample k spanning trees, canonicalize them and add them in a search tree (to avoid duplicates, i.e. isomorphic spanning trees)
sample = runForEachConnectedComponent(&xsampleSpanningTreesUsingWilson, g, k, k, 1, gp, sgp);
for (struct ShallowGraph* tree=sample; tree!=NULL; tree=tree->next) {
if (tree->m != 0) {
struct Graph* tmp = shallowGraphToGraph(tree, gp);
addToSearchTree(searchTree, canonicalStringOfTree(tmp, sgp), gp, sgp);
/* garbage collection */
dumpGraph(gp, tmp);
}
}
}
// create a forest h of disjoint copies of the spanning trees
struct Graph* h = NULL;
struct ShallowGraph* strings = listStringsInSearchTree(searchTree, sgp);
for (struct ShallowGraph* string=strings; string!=NULL; string=string->next) {
struct Graph* tmp;
tmp = treeCanonicalString2Graph(string, gp);
tmp->next = h;
h = tmp;
}
h = mergeGraphs(h, gp);
h->number = g->number;
h->activity = g->activity;
// avoid memory leaks, access to freed memory, and free unused stuff
hardCopyGraphLabels(h);
dumpShallowGraphCycle(sgp, sample);
dumpShallowGraphCycle(sgp, strings);
dumpSearchTree(gp, searchTree);
dumpGraph(gp, g);
/* make space for storing graphs in array */
if (dbSize <= i) {
dbSize = dbSize == 0 ? 128 : dbSize * 2;
*db = realloc(*db, dbSize * sizeof (struct Graph*));
}
/* store graph */
(*db)[i] = h;
++i;
}
return i;
}
int getDBfromCanonicalStrings(struct Graph*** db, FILE* stream, size_t bufferSize, struct GraphPool* gp, struct ShallowGraphPool* sgp) {
int dbSize = 0;
int i = 0;
int graphId = -1;
int graphCount = -1;
char* buffer = malloc(bufferSize * sizeof(char));
while (fscanf(stream, "%i\t%i\t", &graphCount, &graphId) == 2) {
struct ShallowGraph* g = parseCString(stream, buffer, sgp);
/* make space for storing graphs in array */
if (dbSize <= i) {
dbSize = dbSize == 0 ? 128 : dbSize * 2;
*db = realloc(*db, dbSize * sizeof (struct Graph*));
}
/* store graph */
(*db)[i] = treeCanonicalString2Graph(g, gp);
(*db)[i]->number = graphId;
(*db)[i]->activity = graphCount;
hardCopyGraphLabels((*db)[i]);
dumpShallowGraph(sgp, g);
++i;
}
free(buffer);
return i;
}
/**
Find the frequent vertices in a graph db given by an array of graphs.
The frequent vertices are stored in the search tree, the return value of this function is the size of the
temporary data structure for merging search trees.
*/
int getFrequentVertices(struct Graph** db, int dbSize, struct Vertex* frequentVertices, struct GraphPool* gp) {
int i = 0;
struct compInfo* results = NULL;
int resultSize = 0;
/* iterate over all graphs in the database */
for (i=0; i<dbSize; ++i) {
struct Graph* g = db[i];
int v;
/* the vertices contained in g can be obtained from a single spanning tree, as all spanning trees contain
the same vertex set. However, to omit multiplicity, we again resort to a temporary searchTree */
struct Vertex* containedVertices = getVertex(gp->vertexPool);
/* init temporary result storage if necessary */
int neededResultSize = g->n;
int resultPos = 0;
if (neededResultSize > resultSize) {
if (results) {
free(results);
}
results = getResultVector(neededResultSize);
resultSize = neededResultSize;
}
for (v=0; v<g->n; ++v) {
/* See commented out how it would look if done by the book.
However, this has to be fast and canonicalStringOfTree has
too much overhead!
struct ShallowGraph* cString;
auxiliary->vertices[0]->label = patternGraph->vertices[v]->label;
cString = canonicalStringOfTree(auxiliary, sgp);
addToSearchTree(containedVertices, cString, gp, sgp); */
struct VertexList* cString = getVertexList(gp->listPool);
cString->label = g->vertices[v]->label;
containedVertices->d += addStringToSearchTree(containedVertices, cString, gp);
containedVertices->number += 1;
}
/* set multiplicity of patterns to 1 and add to global vertex pattern set, print to file */
resetToUnique(containedVertices);
mergeSearchTrees(frequentVertices, containedVertices, 1, results, &resultPos, frequentVertices, 0, gp);
dumpSearchTree(gp, containedVertices);
}
if (results != NULL) {
free(results);
}
return resultSize;
}
static void getFrequentEdges(struct Graph** db, int dbSize, int initialResultSetSize, struct Vertex* frequentEdges, struct GraphPool* gp) {
int i = 0;
struct compInfo* results = NULL;
int resultSize = 0;
if (initialResultSetSize > 0) {
results = getResultVector(initialResultSetSize);
resultSize = initialResultSetSize;
}
/* iterate over all graphs in the database */
for (i=0; i<dbSize; ++i) {
struct Graph* g = db[i];
int v;
/* frequency of an edge increases by one if there exists a pattern for the current graph (a spanning tree)
that contains the edge. Thus we need to find all edges contained in any spanning tree and then add them
to frequentEdges once omitting multiplicity */
struct Vertex* containedEdges = getVertex(gp->vertexPool);
/* init temporary result storage if necessary */
int neededResultSize = g->m;
int resultPos = 0;
if (neededResultSize > resultSize) {
if (results) {
free(results);
}
results = getResultVector(neededResultSize);
resultSize = neededResultSize;
}
for (v=0; v<g->n; ++v) {
struct VertexList* e;
for (e=g->vertices[v]->neighborhood; e!=NULL; e=e->next) {
int w = e->endPoint->number;
/* edges occur twice in patternGraph. just add them once to the search tree */
if (w > v) {
/* as for vertices, I use specialized code to generate
the canonical string of a single edge */
struct VertexList* cString;
if (strcmp(e->startPoint->label, e->endPoint->label) < 0) {
/* cString = v e (w) */
struct VertexList* tmp = getVertexList(gp->listPool);
tmp->label = e->endPoint->label;
cString = getTerminatorEdge(gp->listPool);
tmp->next = cString;
cString = getVertexList(gp->listPool);
cString->label = e->label;
cString->next = tmp;
tmp = getInitialisatorEdge(gp->listPool);
tmp->next = cString;
cString = getVertexList(gp->listPool);
cString->label = e->startPoint->label;
cString->next = tmp;
} else {
/* cString = w e (v) */
struct VertexList* tmp = getVertexList(gp->listPool);
tmp->label = e->startPoint->label;
cString = getTerminatorEdge(gp->listPool);
tmp->next = cString;
cString = getVertexList(gp->listPool);
cString->label = e->label;
cString->next = tmp;
tmp = getInitialisatorEdge(gp->listPool);
tmp->next = cString;
cString = getVertexList(gp->listPool);
cString->label = e->endPoint->label;
cString->next = tmp;
}
/* add the string to the search tree */
containedEdges->d += addStringToSearchTree(containedEdges, cString, gp);
containedEdges->number += 1;
}
}
}
/* set multiplicity of patterns to 1 and add to global edge pattern set */
resetToUnique(containedEdges);
mergeSearchTrees(frequentEdges, containedEdges, 1, results, &resultPos, frequentEdges, 0, gp);
dumpSearchTree(gp, containedEdges);
}
if (results) {
free(results);
}
}
static struct SupportSet* getSupportSetOfVertex(struct Graph** db, int** postoderDB, size_t nGraphs, struct Graph* h, int patternId) {
struct SupportSet* actualSupport = getSupportSet();
h->number = patternId;
for (size_t i=0; i<nGraphs; ++i) {
struct SubtreeIsoDataStore base = {0};
base.g = db[i];
base.postorder = postoderDB[i];
struct SubtreeIsoDataStore data = initIterativeSubtreeCheckForSingleton(base, h);
if (data.foundIso) {
appendSupportSetData(actualSupport, data);
} else {
dumpNewCube(data.S, data.g->n);
}
}
return actualSupport;
}
/**
* For a graph g and a singleton graph h (consisting of a single vertex)
* check, whether h is subgraph isomorphic to g. Aka. check if the vertex label of h
* occurs among the vertex labels of g
*/
static char singletonSubgraphCheck(struct Graph* g, struct Graph* h) {
char* vertexLabel = h->vertices[0]->label;
for (int v=0; v<g->n; ++v) {
if (labelCmp(g->vertices[v]->label, vertexLabel) == 0) {
return 1;
}
}
return 0;
}
static struct SupportSet* initLocalEasyForVertices(struct SpanningtreeTree* spanningTreesDB, size_t nGraphs, struct Graph* h, int patternId) {
struct SupportSet* actualSupport = getSupportSet();
h->number = patternId;
for (size_t i=0; i<nGraphs; ++i) {
struct SubtreeIsoDataStore data = {0};
data.g = spanningTreesDB[i].g;
data.h = h;
data.postorder = (int*)&(spanningTreesDB[i]); // we store a spanningtreetree pointer instead of a int
data.foundIso = singletonSubgraphCheck(data.g, data.h);
if (data.foundIso) {
appendSupportSetData(actualSupport, data);
}
}
return actualSupport;
}
static int** getPostorders(struct Graph** db, int nGraphs) {
int** postorderDB = malloc(nGraphs * sizeof(int*));
for (int g=0; g<nGraphs; ++g) {
postorderDB[g] = getPostorder(db[g], 0);
}
return postorderDB;
}
static void getFrequentVerticesAndEdges(struct Graph** db, int nGraphs, size_t threshold, struct Vertex** frequentVertices, struct Vertex** frequentEdges, FILE* logStream, struct GraphPool* gp) {
*frequentVertices = getVertex(gp->vertexPool);
*frequentEdges = getVertex(gp->vertexPool);
/* get frequent vertices */
int tmpResultSetSize = getFrequentVertices(db, nGraphs, *frequentVertices, gp);
filterSearchTree(*frequentVertices, threshold, *frequentVertices, gp);
fprintf(logStream, "Number of frequent vertices: %i\n", (*frequentVertices)->d); fflush(logStream);
/* get frequent edges: first edge id is given by number of frequent vertices */
offsetSearchTreeIds(*frequentEdges, (*frequentVertices)->lowPoint);
getFrequentEdges(db, nGraphs, tmpResultSetSize, *frequentEdges, gp);
filterSearchTree(*frequentEdges, threshold, *frequentEdges, gp);
fprintf(logStream, "Number of frequent edges: %i\n", (*frequentEdges)->d); fflush(logStream);
}
/**
* create data structures for levelwise mining for subtree and iterative subtree embedding operator
* for all frequent vertices in the db.
* the ids of the frequent vertices in the search tree might be altered to ensure a sorted list of support sets.
* To avoid leaks, the initial frequentVertices search tree must not be dumped until the end of all times.
*/
static struct SupportSet* createSingletonPatternSupportSetsForForestDB(struct Graph** db, int** postorders, int nGraphs, struct Vertex* frequentVertices, struct GraphPool* gp, struct ShallowGraphPool* sgp) {
(void)sgp; // unused
// init levelwise search data structures for patterns with one vertex
// data structures for iterative levelwise search
struct SupportSet* vertexSupportSets = NULL;
struct SupportSet* vertexSupportSetsTail = NULL;
int id = 1;
for (struct VertexList* e=frequentVertices->neighborhood; e!=NULL; e=e->next) {
struct Graph* candidate = createGraph(1, gp);
candidate->vertices[0]->label = e->label;
e->endPoint->lowPoint = id;
struct SupportSet* vertexSupport = getSupportSetOfVertex(db, postorders, nGraphs, candidate, id);
if (vertexSupportSetsTail != NULL) {
vertexSupportSetsTail->next = vertexSupport;
vertexSupportSetsTail = vertexSupport;
} else {
vertexSupportSets = vertexSupport;
vertexSupportSetsTail = vertexSupport;
}
++id;
}
return vertexSupportSets;
}
/**
* create data structures for levelwise mining for subtree and iterative subtree embedding operator
* for all frequent vertices in the db.
* the ids of the frequent vertices in the search tree might be altered to ensure a sorted list of support sets.
* To avoid leaks, the initial frequentVertices search tree must not be dumped until the end of all times.
*/
static struct SupportSet* createSingletonPatternSupportSetsForLocalEasyDB(struct SpanningtreeTree* sptTrees, int nGraphs, struct Vertex* frequentVertices, struct GraphPool* gp, struct ShallowGraphPool* sgp) {
(void)sgp; // unused
// init levelwise search data structures for patterns with one vertex
// data structures for iterative levelwise search
struct SupportSet* vertexSupportSets = NULL;
struct SupportSet* vertexSupportSetsTail = NULL;
int id = 1;
for (struct VertexList* e=frequentVertices->neighborhood; e!=NULL; e=e->next) {
struct Graph* candidate = createGraph(1, gp);
candidate->vertices[0]->label = e->label;
e->endPoint->lowPoint = id;
struct SupportSet* vertexSupport = initLocalEasyForVertices(sptTrees, nGraphs, candidate, id);
if (vertexSupportSetsTail != NULL) {
vertexSupportSetsTail->next = vertexSupport;
vertexSupportSetsTail = vertexSupport;
} else {
vertexSupportSets = vertexSupport;
vertexSupportSetsTail = vertexSupport;
}
++id;
}
return vertexSupportSets;
}
/**
Create a list of single-edge ShallowGraphs from a search tree containing single edge canonical strings.
Note that this method creates a hardcopy of the edges, strings and vertices.
Hence, it requires a pointer to a struct Graph* newVertices variable where it stores a newly created graph that holds all the new vertices.
To avoid memory leaks, this graph needs to be dumped together with the struct ShallowGraph* result of this method.
*/
struct ShallowGraph* edgeSearchTree2ShallowGraph(struct Vertex* frequentEdges, struct Graph** newVertices, struct GraphPool* gp, struct ShallowGraphPool* sgp) {
struct ShallowGraph* result = getShallowGraph(sgp);
struct VertexList* e;
struct Vertex* createdVertices = NULL;
int nVertices = 0;
int i;
struct Vertex* tmp;
for (e=frequentEdges->neighborhood; e!=NULL; e=e->next) {
struct Vertex* v = getVertex(gp->vertexPool);
struct VertexList* f;
v->label = e->label;
/* store newly created vertex in a list */
v->next = createdVertices;
createdVertices = v;
++nVertices;
for (f=e->endPoint->neighborhood->endPoint->neighborhood; f!=NULL; f=f->next) {
struct VertexList* g;
for (g=f->endPoint->neighborhood; g!=NULL; g=g->next) {
struct VertexList* new = getVertexList(sgp->listPool);
new->startPoint = v;
new->label = f->label;
new->endPoint = getVertex(gp->vertexPool);
new->endPoint->label = g->label;
pushEdge(result, new);
/* if the edge has not identical vertex labels at both vertices,
add the reverse edge to the output */
if (strcmp(v->label, new->endPoint->label) != 0) {
pushEdge(result, inverseEdge(new, sgp->listPool));
}
/* store newly created vertex in a list */
new->endPoint->next = createdVertices;
createdVertices = new->endPoint;
++nVertices;
}
}
}
/* to avoid memory leaks, make hardcopies of the labels */
for (e=result->edges; e!=NULL; e=e->next) {
if (!e->isStringMaster) {
e->isStringMaster = 1;
e->label = copyString(e->label);
}
if (!e->startPoint->isStringMaster) {
e->startPoint->isStringMaster = 1;
e->startPoint->label = copyString(e->startPoint->label);
}
if (!e->endPoint->isStringMaster) {
e->endPoint->isStringMaster = 1;
e->endPoint->label = copyString(e->endPoint->label);
}
}
/* to avoid memory leaks, make a graph containing all newly created vertices and return it*/
*newVertices = getGraph(gp);
setVertexNumber(*newVertices, nVertices);
for (i=0, tmp=createdVertices; i<nVertices; ++i, tmp=tmp->next) {
(*newVertices)->vertices[i] = tmp;
}
return result;
}
size_t initFrequentTreeMiningForForestDB(// input
size_t threshold,
double importance,
// output
struct Vertex** initialFrequentPatterns,
struct SupportSet** supportSets,
struct ShallowGraph** extensionEdgeList,
void** dataStructures,
// printing
FILE* featureStream,
FILE* patternStream,
FILE* logStream,
// pools
struct GraphPool* gp,
struct ShallowGraphPool* sgp) {
(void)importance; // unused
struct Graph** db = NULL;
int nGraphs = getDB(&db);
int** postorders = getPostorders(db, nGraphs);
struct Vertex* frequentVertices;
struct Vertex* frequentEdges;
getFrequentVerticesAndEdges(db, nGraphs, threshold, &frequentVertices, &frequentEdges, logStream, gp);
/* convert frequentEdges to ShallowGraph of extension edges */
struct Graph* extensionEdgesVertexStore = NULL;
struct ShallowGraph* extensionEdges = edgeSearchTree2ShallowGraph(frequentEdges, &extensionEdgesVertexStore, gp, sgp);
dumpSearchTree(gp, frequentEdges);
// levelwise search for patterns with one vertex:
struct SupportSet* frequentVerticesSupportSets = createSingletonPatternSupportSetsForForestDB(db, postorders, nGraphs, frequentVertices, gp, sgp);
printStringsInSearchTree(frequentVertices, patternStream, sgp);
printSupportSetsSparse(frequentVerticesSupportSets, featureStream);
// store pointers for final garbage collection
struct IterativeBfsForForestsDataStructures* x = malloc(sizeof(struct IterativeBfsForForestsDataStructures));
x->db = db;
x->postorders = postorders;
x->nGraphs = nGraphs;
x->extensionEdges = extensionEdges;
x->extensionEdgesVertexStore = extensionEdgesVertexStore;
x->initialFrequentPatterns = frequentVertices;
// 'return'
*initialFrequentPatterns = frequentVertices;
*supportSets = frequentVerticesSupportSets;
*extensionEdgeList = extensionEdges;
*dataStructures = x;
return 1; // returned patterns have 1 vertex
}
/**
* Very inefficient variant of exact frequent subtree mining by representing a
* graph by the set of all of its spanning trees
*/
size_t initGlobalTreeEnumerationForGraphDB(// input
size_t threshold,
double importance,
// output
struct Vertex** initialFrequentPatterns,
struct SupportSet** supportSets,
struct ShallowGraph** extensionEdgeList,
void** dataStructures,
// printing
FILE* featureStream,
FILE* patternStream,
FILE* logStream,
// pools
struct GraphPool* gp,
struct ShallowGraphPool* sgp) {
struct Graph** db = NULL;
int nGraphs = getAllSpanningTreesOfDB(&db, (int)importance, gp, sgp);
int** postorders = getPostorders(db, nGraphs);
struct Vertex* frequentVertices;
struct Vertex* frequentEdges;
getFrequentVerticesAndEdges(db, nGraphs, threshold, &frequentVertices, &frequentEdges, logStream, gp);
/* convert frequentEdges to ShallowGraph of extension edges */
struct Graph* extensionEdgesVertexStore = NULL;
struct ShallowGraph* extensionEdges = edgeSearchTree2ShallowGraph(frequentEdges, &extensionEdgesVertexStore, gp, sgp);
dumpSearchTree(gp, frequentEdges);
// levelwise search for patterns with one vertex:
struct SupportSet* frequentVerticesSupportSets = createSingletonPatternSupportSetsForForestDB(db, postorders, nGraphs, frequentVertices, gp, sgp);
printStringsInSearchTree(frequentVertices, patternStream, sgp);
printSupportSetsSparse(frequentVerticesSupportSets, featureStream);
// store pointers for final garbage collection
struct IterativeBfsForForestsDataStructures* x = malloc(sizeof(struct IterativeBfsForForestsDataStructures));
x->db = db;
x->postorders = postorders;
x->nGraphs = nGraphs;
x->extensionEdges = extensionEdges;
x->extensionEdgesVertexStore = extensionEdgesVertexStore;
x->initialFrequentPatterns = frequentVertices;
// 'return'
*initialFrequentPatterns = frequentVertices;
*supportSets = frequentVerticesSupportSets;
*extensionEdgeList = extensionEdges;
*dataStructures = x;
return 1; // returned patterns have 1 vertex
}
size_t initProbabilisticTreeMiningForGraphDB(// input
size_t threshold,
double importance,
// output
struct Vertex** initialFrequentPatterns,
struct SupportSet** supportSets,
struct ShallowGraph** extensionEdgeList,
void** dataStructures,
// printing
FILE* featureStream,
FILE* patternStream,
FILE* logStream,
// pools
struct GraphPool* gp,
struct ShallowGraphPool* sgp) {
struct Graph** db = NULL;
int nGraphs = getNonisomorphicSpanningTreeSamplesOfDB(&db, (int)importance, gp, sgp);
int** postorders = getPostorders(db, nGraphs);
struct Vertex* frequentVertices;
struct Vertex* frequentEdges;
getFrequentVerticesAndEdges(db, nGraphs, threshold, &frequentVertices, &frequentEdges, logStream, gp);
/* convert frequentEdges to ShallowGraph of extension edges */
struct Graph* extensionEdgesVertexStore = NULL;
struct ShallowGraph* extensionEdges = edgeSearchTree2ShallowGraph(frequentEdges, &extensionEdgesVertexStore, gp, sgp);
dumpSearchTree(gp, frequentEdges);
// levelwise search for patterns with one vertex:
struct SupportSet* frequentVerticesSupportSets = createSingletonPatternSupportSetsForForestDB(db, postorders, nGraphs, frequentVertices, gp, sgp);
printStringsInSearchTree(frequentVertices, patternStream, sgp);
printSupportSetsSparse(frequentVerticesSupportSets, featureStream);
// store pointers for final garbage collection
struct IterativeBfsForForestsDataStructures* x = malloc(sizeof(struct IterativeBfsForForestsDataStructures));
x->db = db;
x->postorders = postorders;
x->nGraphs = nGraphs;
x->extensionEdges = extensionEdges;
x->extensionEdgesVertexStore = extensionEdgesVertexStore;
x->initialFrequentPatterns = frequentVertices;
// 'return'
*initialFrequentPatterns = frequentVertices;
*supportSets = frequentVerticesSupportSets;
*extensionEdgeList = extensionEdges;
*dataStructures = x;
return 1; // returned patterns have 1 vertex
}
struct IterativeBfsForLocalEasyDataStructures {
struct SpanningtreeTree* sptTrees;
struct Vertex* initialFrequentPatterns;
struct ShallowGraph* extensionEdges;
struct Graph* extensionEdgesVertexStore;
int nGraphs;
};
size_t initExactLocalEasyForGraphDB(// input
size_t threshold,
double importance,
// output
struct Vertex** initialFrequentPatterns,
struct SupportSet** supportSets,
struct ShallowGraph** extensionEdgeList,
void** dataStructures,
// printing
FILE* featureStream,
FILE* patternStream,
FILE* logStream,
// pools
struct GraphPool* gp,
struct ShallowGraphPool* sgp) {
(void)importance; // unused
struct Graph** db = NULL;
size_t nGraphs = getDB(&db);
struct SpanningtreeTree* sptTrees = malloc(nGraphs * sizeof(struct SpanningtreeTree));
for (size_t i=0; i<nGraphs; ++i) {
struct BlockTree blockTree = getBlockTreeT(db[i], sgp);
sptTrees[i] = getFullSpanningtreeTree(blockTree, gp, sgp);
}
struct Vertex* frequentVertices;
struct Vertex* frequentEdges;
getFrequentVerticesAndEdges(db, nGraphs, threshold, &frequentVertices, &frequentEdges, logStream, gp);
/* convert frequentEdges to ShallowGraph of extension edges */
struct Graph* extensionEdgesVertexStore = NULL;
struct ShallowGraph* extensionEdges = edgeSearchTree2ShallowGraph(frequentEdges, &extensionEdgesVertexStore, gp, sgp);
dumpSearchTree(gp, frequentEdges);
free(db);
// levelwise search for patterns with one vertex:
struct SupportSet* frequentVerticesSupportSets = createSingletonPatternSupportSetsForLocalEasyDB(sptTrees, nGraphs, frequentVertices, gp, sgp);
printStringsInSearchTree(frequentVertices, patternStream, sgp);
printSupportSetsSparse(frequentVerticesSupportSets, featureStream);
// store pointers for final garbage collection
struct IterativeBfsForLocalEasyDataStructures* x = malloc(sizeof(struct IterativeBfsForLocalEasyDataStructures));
x->nGraphs = nGraphs;
x->extensionEdges = extensionEdges;
x->extensionEdgesVertexStore = extensionEdgesVertexStore;
x->initialFrequentPatterns = frequentVertices;
x->sptTrees = sptTrees;
// 'return'
*initialFrequentPatterns = frequentVertices;
*supportSets = frequentVerticesSupportSets;
*extensionEdgeList = extensionEdges;
*dataStructures = x;
return 1; // returned patterns have 1 vertex
}
/**
* In this method, we remove duplicate sampled local spanning trees.
*/
size_t initSampledLocalEasyForGraphDB(// input
size_t threshold,
double importance,
// output
struct Vertex** initialFrequentPatterns,
struct SupportSet** supportSets,
struct ShallowGraph** extensionEdgeList,
void** dataStructures,
// printing
FILE* featureStream,
FILE* patternStream,
FILE* logStream,
// pools
struct GraphPool* gp,
struct ShallowGraphPool* sgp) {
struct Graph** db = NULL;
size_t nGraphs = getDB(&db);
struct SpanningtreeTree* sptTrees = malloc(nGraphs * sizeof(struct SpanningtreeTree));
for (size_t i=0; i<nGraphs; ++i) {
struct BlockTree blockTree = getBlockTreeT(db[i], sgp);
sptTrees[i] = getSampledSpanningtreeTree(blockTree, (int)importance, 1, gp, sgp);
}
struct Vertex* frequentVertices;
struct Vertex* frequentEdges;
getFrequentVerticesAndEdges(db, nGraphs, threshold, &frequentVertices, &frequentEdges, logStream, gp);
/* convert frequentEdges to ShallowGraph of extension edges */
struct Graph* extensionEdgesVertexStore = NULL;
struct ShallowGraph* extensionEdges = edgeSearchTree2ShallowGraph(frequentEdges, &extensionEdgesVertexStore, gp, sgp);
dumpSearchTree(gp, frequentEdges);
free(db);
// levelwise search for patterns with one vertex:
struct SupportSet* frequentVerticesSupportSets = createSingletonPatternSupportSetsForLocalEasyDB(sptTrees, nGraphs, frequentVertices, gp, sgp);
printStringsInSearchTree(frequentVertices, patternStream, sgp);
printSupportSetsSparse(frequentVerticesSupportSets, featureStream);
// store pointers for final garbage collection
struct IterativeBfsForLocalEasyDataStructures* x = malloc(sizeof(struct IterativeBfsForLocalEasyDataStructures));
x->nGraphs = nGraphs;
x->extensionEdges = extensionEdges;
x->extensionEdgesVertexStore = extensionEdgesVertexStore;
x->initialFrequentPatterns = frequentVertices;
x->sptTrees = sptTrees;
// 'return'
*initialFrequentPatterns = frequentVertices;
*supportSets = frequentVerticesSupportSets;
*extensionEdgeList = extensionEdges;
*dataStructures = x;
return 1; // returned patterns have 1 vertex
}
void garbageCollectFrequentTreeMiningForForestDB(void** y, struct GraphPool* gp, struct ShallowGraphPool* sgp) {
struct IterativeBfsForForestsDataStructures* dataStructures = (struct IterativeBfsForForestsDataStructures*)y;
dumpSearchTree(gp, dataStructures->initialFrequentPatterns);
dumpShallowGraphCycle(sgp, dataStructures->extensionEdges);
dumpGraph(gp, dataStructures->extensionEdgesVertexStore);
for (int i=0; i<dataStructures->nGraphs; ++i) {
dumpGraph(gp, dataStructures->db[i]);
free(dataStructures->postorders[i]);
}
free(dataStructures->db);
free(dataStructures->postorders);
free(dataStructures);
}
void garbageCollectLocalEasyForGraphDB(void** y, struct GraphPool* gp, struct ShallowGraphPool* sgp) {
struct IterativeBfsForLocalEasyDataStructures* dataStructures = (struct IterativeBfsForLocalEasyDataStructures*)y;
dumpSearchTree(gp, dataStructures->initialFrequentPatterns);
dumpShallowGraphCycle(sgp, dataStructures->extensionEdges);
dumpGraph(gp, dataStructures->extensionEdgesVertexStore);
for (int i=0; i<dataStructures->nGraphs; ++i) {
dumpGraph(gp, dataStructures->sptTrees[i].g);
dumpSpanningtreeTree(dataStructures->sptTrees[i], gp);
}
free(dataStructures->sptTrees);
free(dataStructures);
}
static struct SupportSet* initPatternEnumerationForVertices(struct Graph** spanningTreesDB, size_t nGraphs, struct Graph* h, int patternId) {
struct SupportSet* actualSupport = getSupportSet();
h->number = patternId;
for (size_t i=0; i<nGraphs; ++i) {
struct SubtreeIsoDataStore data = {0};
data.g = spanningTreesDB[i];
data.h = h;
data.postorder = NULL;
data.foundIso = 1;
appendSupportSetData(actualSupport, data);
}
return actualSupport;
}
/**
* create data structures for levelwise mining for subtree and iterative subtree embedding operator
* for all frequent vertices in the db.
* the ids of the frequent vertices in the search tree might be altered to ensure a sorted list of support sets.
* To avoid leaks, the initial frequentVertices search tree must not be dumped until the end of all times.
*/
struct SupportSet* getSupportSetsOfVerticesForPatternEnumeration(struct Graph** db, int nGraphs, struct Vertex* frequentVertices, struct GraphPool* gp, struct ShallowGraphPool* sgp) {
(void)sgp; // unused
// init levelwise search data structures for patterns with one vertex
// data structures for iterative levelwise search
struct SupportSet* vertexSupportSets = NULL;
struct SupportSet* vertexSupportSetsTail = NULL;
int id = 1;
for (struct VertexList* e=frequentVertices->neighborhood; e!=NULL; e=e->next) {
struct Graph* candidate = createGraph(1, gp);
candidate->vertices[0]->label = e->label;
e->endPoint->lowPoint = id;
struct SupportSet* vertexSupport = initPatternEnumerationForVertices(db, nGraphs, candidate, id);
if (vertexSupportSetsTail != NULL) {
vertexSupportSetsTail->next = vertexSupport;
vertexSupportSetsTail = vertexSupport;
} else {
vertexSupportSets = vertexSupport;
vertexSupportSetsTail = vertexSupport;
}
++id;
}
return vertexSupportSets;
}
size_t initPatternEnumeration(// input
size_t threshold,
double importance,
// output
struct Vertex** initialFrequentPatterns,
struct SupportSet** supportSets,
struct ShallowGraph** extensionEdgeList,
void** dataStructures,
// printing
FILE* featureStream,