-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimportantSubtrees.c
More file actions
112 lines (101 loc) · 3.14 KB
/
importantSubtrees.c
File metadata and controls
112 lines (101 loc) · 3.14 KB
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
#include <stddef.h>
#include <stdlib.h>
#include <assert.h>
#include "graph.h"
#include "connectedComponents.h"
#include "iterativeSubtreeIsomorphism.h"
#include "importantSubtrees.h"
/**
* Return a list of graphs corresponding to the connected components of g (or NULL, if g is empty).
*/
struct Graph* graph2Components(struct Graph* g, struct GraphPool* gp) {
// init visited to 0
for (int v=0; v<g->n; ++v) {
g->vertices[v]->lowPoint = -1;
}
// mark vertices of connected components in g
int nComponents = 0;
for (int v=0; v<g->n; ++v) {
if (g->vertices[v]->lowPoint == -1) {
markComp(g->vertices[v], nComponents);
++nComponents;
}
}
struct Graph* result = NULL;
if (nComponents > 0) {
// create graph struct for each found component
struct Graph** components = malloc(nComponents * sizeof(struct Graph*));
for (int i=0; i<nComponents; ++i) {
components[i] = getGraph(gp);
}
// make list from array.
for (int i=0; i<nComponents-1; ++i) {
components[i]->next = components[i+1];
}
// count number of vertices in each component
for (int v=0; v<g->n; ++v) {
int c = g->vertices[v]->lowPoint;
++(components[c]->n);
}
// create vertex arrays for graphs
for (int i=0; i<nComponents; ++i) {
setVertexNumber(components[i], components[i]->n);
}
// store vertices of g in the corresponding component graphs
for (int v=0; v<g->n; ++v) {
int c = g->vertices[v]->lowPoint;
int newpos = components[c]->number;
components[c]->vertices[newpos] = g->vertices[v];
g->vertices[v]->number = newpos;
components[c]->number++;
}
// TODO set components->m, components->number, ...
result = components[0];
free(components);
}
return result;
}
void graph2ComponentCleanup(struct Graph* g, struct Graph* components, struct GraphPool* gp) {
for (struct Graph* c=components; c!=NULL; c=c->next) {
for (int v=0; v<c->n; ++v) {
c->vertices[v] = NULL;
}
c->n = 0;
}
dumpGraphList(gp, components);
for (int v=0; v<g->n; ++v) {
g->vertices[v]->number = v;
}
}
int importanceCount(struct Graph* g, struct Graph* h, struct GraphPool* gp) {
struct Graph* components = graph2Components(g, gp);
int freq = 0;
for (struct Graph* component=components; component!=NULL; component=component->next) {
if (isSubtree(component, h, gp)) {
++freq;
}
}
graph2ComponentCleanup(g, components, gp);
return freq;
}
double importanceRelative(struct Graph* g, struct Graph* h, struct GraphPool* gp) {
struct Graph* components = graph2Components(g, gp);
int freq = 0;
int count = 0;
for (struct Graph* component=components; component!=NULL; component=component->next) {
if (isSubtree(component, h, gp)) {
++freq;
}
++count;
}
graph2ComponentCleanup(g, components, gp);
return ((double)freq) / ((double)count);
}
char isImportantSubtreeAbsolute(struct Graph* g, struct Graph* h, int absoluteThreshold, struct GraphPool* gp) {
int importance = importanceCount(g, h, gp);
return importance >= absoluteThreshold;
}
char isImportantSubtreeRelative(struct Graph* g, struct Graph* h, double relativeThreshold, struct GraphPool* gp) {
double importance = importanceRelative(g, h, gp);
return importance >= relativeThreshold;
}