-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathGraphTree.cpp
More file actions
272 lines (233 loc) · 6.43 KB
/
GraphTree.cpp
File metadata and controls
272 lines (233 loc) · 6.43 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
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
/* -------------------------------------------------
_ _ ___
__ _| |_ _ _(_)___/ __| __ __ _ _ _ _ _ ___ _ _
/ _` | _| '_| / -_)__ \/ _/ _` | ' \| ' \/ -_) '_|
\__, |\__|_| |_\___|___/\__\__,_|_||_|_||_\___|_|
|___/
gtrieScanner: quick discovery of network motifs
Released under Artistic License 2.0
(see README and LICENSE)
Pedro Ribeiro - CRACS & INESC-TEC, DCC/FCUP
----------------------------------------------------
Graph (0-1) Tree Implementation
Last Update: 11/02/2012
---------------------------------------------------- */
#include "GraphTree.h"
#include "Isomorphism.h"
GraphTreeNode::GraphTreeNode() {
frequency = 0;
zero = one = NULL;
}
GraphTreeNode::~GraphTreeNode() {
if (zero != NULL) delete zero;
if ( one != NULL) delete one;
}
void GraphTreeNode::zeroFrequency() {
frequency = 0;
if (zero != NULL) zero->zeroFrequency();
if (one != NULL) one->zeroFrequency();
}
void GraphTreeNode::incrementString(int pos, char *s) {
if (s[pos]==0) frequency++;
else {
if (s[pos]=='1') {
if (one==NULL) one = new GraphTreeNode();
one->incrementString(pos+1, s);
} else if (s[pos]=='0') {
if (zero==NULL) zero = new GraphTreeNode();
zero->incrementString(pos+1, s);
}
}
}
void GraphTreeNode::setString(int pos, char *s, int f) {
if (s[pos]==0) frequency=f;
else {
if (s[pos]=='1') {
if (one==NULL) one = new GraphTreeNode();
one->setString(pos+1, s, f);
} else if (s[pos]=='0') {
if (zero==NULL) zero = new GraphTreeNode();
zero->setString(pos+1, s, f);
}
}
}
void GraphTreeNode::addString(int pos, char *s, int f) {
if (s[pos]==0) frequency+=f;
else {
if (s[pos]=='1') {
if (one==NULL) one = new GraphTreeNode();
one->addString(pos+1, s, f);
} else if (s[pos]=='0') {
if (zero==NULL) zero = new GraphTreeNode();
zero->addString(pos+1, s, f);
}
}
}
void GraphTreeNode::showFrequency(int pos, char *s) {
if (zero == NULL && one == NULL) {
s[pos]=0;
printf("%s: %d\n", s, frequency);
} else {
if (zero != NULL) {
s[pos]='0';
zero->showFrequency(pos+1, s);
}
if (one != NULL) {
s[pos]='1';
one->showFrequency(pos+1, s);
}
}
}
bool GraphTreeNode::equal(GraphTreeNode *gt, int pos, char *s) {
if (zero == NULL && gt->zero!=NULL) return false;
if (zero != NULL && gt->zero==NULL) return false;
if (one == NULL && gt->one !=NULL) return false;
if (one != NULL && gt->one ==NULL) return false;
if (zero == NULL && one == NULL) {
s[pos]=0;
if (frequency != gt->frequency)
printf("NOT EQUAL TREE %s: %d != %d\n", s, frequency, gt->frequency);
return (frequency == gt->frequency);
} else {
bool fzero, fone;
fzero = fone = true;
if (zero != NULL) {
s[pos]='0';
fzero = zero->equal(gt->zero, pos+1, s);
}
if (one != NULL) {
s[pos]='1';
fone = one->equal(gt->one, pos+1, s);
}
return fzero && fone;
}
}
bool GraphTreeNode::equal(GTrie *gt, int size, int pos, char *s) {
if (zero == NULL && one == NULL) {
s[pos]=0;
int aux = gt->frequencyGraphString(size, s);
if (frequency != aux)
printf("NOT EQUAL GTRIE %s: %d != %d\n", s, frequency, aux);
return (frequency == aux);
} else {
bool fzero, fone;
fzero = fone = true;
if (zero != NULL) {
s[pos]='0';
fzero = zero->equal(gt, size, pos+1, s);
}
if (one != NULL) {
s[pos]='1';
fone = one->equal(gt, size, pos+1, s);
}
return fzero && fone;
}
}
void GraphTreeNode::populateGTrie(GTrie *gt, int size, int pos, char *s) {
if (zero == NULL && one == NULL) {
s[pos]=0;
gt->insertGraphString(size, s);
} else {
if (zero != NULL) {
s[pos]='0';
zero->populateGTrie(gt, size, pos+1, s);
}
if (one != NULL) {
s[pos]='1';
one->populateGTrie(gt, size, pos+1, s);
}
}
}
void GraphTreeNode::populateMap(mapStringInt *m, int size, int pos, char *s) {
if (zero == NULL && one == NULL) {
s[pos]=0;
char s2[size*size+1];
Isomorphism::canonicalBasedNauty(s, s2, size);
(*m)[s2] = frequency;
} else {
if (zero != NULL) {
s[pos]='0';
zero->populateMap(m, size, pos+1, s);
}
if (one != NULL) {
s[pos]='1';
one->populateMap(m, size, pos+1, s);
}
}
}
void GraphTreeNode::populateGTrieNauty(GTrie *gt, int size, int pos, char *s, bool dir) {
if (frequency>0) {
s[pos]=0;
gt->insertGraphNautyString(size, s, dir, 1);
}
if (zero != NULL) {
s[pos]='0';
zero->populateGTrieNauty(gt, size, pos+1, s, dir);
}
if (one != NULL) {
s[pos]='1';
one->populateGTrieNauty(gt, size, pos+1, s, dir);
}
}
double GraphTreeNode::countOccurrences() {
double aux = frequency;
if (zero != NULL) aux += zero->countOccurrences();
if (one != NULL) aux += one->countOccurrences();
return aux;
}
int GraphTreeNode::countGraphs() {
int aux = 0;
if (frequency>0) aux++;
if (zero != NULL) aux += zero->countGraphs();
if (one != NULL) aux += one->countGraphs();
return aux;
}
// -----------------------------------
GraphTree::GraphTree() {
root = new GraphTreeNode();
}
GraphTree::~GraphTree() {
if (root) delete root;
}
void GraphTree::zeroFrequency() {
root->zeroFrequency();
}
void GraphTree::incrementString(char *s) {
root->incrementString(0, s);
}
void GraphTree::setString(char *s, int f) {
root->setString(0, s, f);
}
void GraphTree::addString(char *s, int f) {
root->addString(0, s, f);
}
void GraphTree::showFrequency(int maxsize) {
char s[maxsize*maxsize+1];
root->showFrequency(0, s);
}
bool GraphTree::equal(GraphTree *gt, int maxsize) {
char s[maxsize*maxsize+1];
return root->equal(gt->root, 0, s);
}
bool GraphTree::equal(GTrie *gt, int maxsize) {
char s[maxsize*maxsize+1];
return root->equal(gt, maxsize, 0, s);
}
void GraphTree::populateGTrie(GTrie *gt, int maxsize) {
char s[maxsize*maxsize+1];
root->populateGTrie(gt, maxsize, 0, s);
}
void GraphTree::populateMap(mapStringInt *m, int maxsize) {
char s[maxsize*maxsize+1];
root->populateMap(m, maxsize, 0, s);
}
void GraphTree::populateGTrieNauty(GTrie *gt, int maxsize, bool dir) {
char s[maxsize*maxsize+1];
root->populateGTrieNauty(gt, maxsize, 0, s, dir);
}
double GraphTree::countOccurrences() {
return root->countOccurrences();
}
int GraphTree::countGraphs() {
return root->countGraphs();
}