-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEmbeddingUtils.cpp
More file actions
268 lines (227 loc) · 7.67 KB
/
EmbeddingUtils.cpp
File metadata and controls
268 lines (227 loc) · 7.67 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
#include "EmbeddingUtils.h"
#include <algorithm>
//int EmbeddingUtils::time;
const int EmbeddingUtils::NIL;
//std::vector<int> EmbeddingUtils::parent;
//std::vector<int> EmbeddingUtils::disc;
//std::vector<int> EmbeddingUtils::low;
//std::vector<int> EmbeddingUtils::order;
//std::vector<bool> EmbeddingUtils::visited;
//std::vector<bool> EmbeddingUtils::ap;
EmbeddingUtils::EmbeddingUtils() {};
void EmbeddingUtils::articulationRec(int u, BasicEmbedding &embedding, std::vector<std::vector<int>> &adj, int *time, std::vector<int> &parent, std::vector<int>& disc, std::vector<int> &low, std::vector<bool> &visited, std::vector<bool> &ap) {
// number of children in the DFS
int children = 0;
// visiting new node
visited[u] = true;
// discovery time and low value
disc[u] = ++(*time);
low[u] = *time;
std::vector<int> words = embedding.getWords();
int k=0;
int v=adj[u][k];
while (v != -1) {
if (!visited[v]) {
children++;
parent[v] = u;
articulationRec(v, embedding, adj, time, parent, disc, low, visited, ap);
// Check if the subtree rooted with v has a connection to
// one of the ancestors of u
low[u] = std::min(low[u], low[v]);
// u is an articulation point in following cases
// (1) u is root of DFS tree and has two or more chilren.
if (parent[u] == NIL && children > 1)
ap[u] = true;
// (2) If u is not root and low value of one of its child
// is more than discovery value of u.
if (parent[u] != NIL && low[v] >= disc[u])
ap[u] = true;
}
// Update low value of u for parent function calls.
else if (v != parent[u])
low[u] = std::min(low[u], disc[v]);
k++;
v=adj[u][k];
}
}
//void EmbeddingUtils::dfsRec(int u, BasicEmbedding &embedding, std::vector<std::vector<int>> &adj, std::vector<bool> &visited, std::vector<int> &order) {
void EmbeddingUtils::dfsRec(int u, BasicEmbedding &embedding, EmbMatrix adj, std::vector<bool> &visited, std::vector<int> &order) {
// number of children in the DFS
// visiting new node
visited[u] = true;
//embedding.print();
//std::cout << "visiting node " << u << std::endl;
std::vector<int> words = embedding.getWords();
order.push_back(words[u]);
int k=0;
int v=adj[u][k];
while (v != -1) {
if (!visited[v]) {
dfsRec(v, embedding, adj, visited, order);
}
k++;
v=adj[u][k];
}
}
std::vector<bool> EmbeddingUtils::articulation(BasicEmbedding &embedding) {
int s = embedding.getNumWords();
std::vector<int> words = embedding.getWords();
int time = 0;
std::vector<int> parent(s,NIL);
std::vector<int> disc(s,NIL);
std::vector<int> low(s,NIL);
std::vector<bool> visited(s,false);
std::vector<bool> ap(s,false);
std::vector<int> order;
//create embedding adj matrix.
std::vector<std::vector<int>> adj( s, std::vector<int>(s+1,-1));
int adjsize[s]={0};
for (int u = 0; u < s; ++u) {
for (int v = u; v < s; ++v) {
// here we verify whether each vertice is adjacent to each vertice
// In practive (embeddings << graph) this is better than accessing
// directly the whole graph adjacency (the only one available)
if (!embedding.areWordsNeighbours(words[u], words[v]))
continue;
adj[u][adjsize[u]]=v;
adj[v][adjsize[v]]=u;
adjsize[u]++;
adjsize[v]++;
}
}
for (int u = 0; u < embedding.getNumWords(); ++u) {
//System.out.println("Start from edge: " + u);
if (visited[u] == false)
articulationRec(u, embedding, adj, &time, parent, disc, low, visited, ap);
}
return ap;
}
std::vector<bool> EmbeddingUtils::articulation(BasicEmbedding &embedding, int wordId) {
int s = embedding.getNumWords() + 1;
std::vector<int> words = embedding.getWords();
int time = 0;
std::vector<int> parent(s,NIL);
std::vector<int> disc(s,NIL);
std::vector<int> low(s,NIL);
std::vector<bool> visited(s,false);
std::vector<bool> ap(s,false);
std::vector<int> order;
//create embedding adj matrix.
std::vector<std::vector<int>> adj( s, std::vector<int>(s+1,-1));
int adjsize[s]={0};
for (int u = 0; u < s-1; ++u) {
for (int v = u; v < s-1; ++v) {
// here we verify whether each vertice is adjacent to each vertice
// In practive (embeddings << graph) this is better than accessing
// directly the whole graph adjacency (the only one available)
if (!embedding.areWordsNeighbours(words[u], words[v]))
continue;
adj[u][adjsize[u]]=v;
adj[v][adjsize[v]]=u;
adjsize[u]++;
adjsize[v]++;
}
//for the wordId
if (embedding.areWordsNeighbours(words[u], wordId)) {
adj[u][adjsize[u]]=s-1;
adj[s-1][adjsize[s-1]]=u;
adjsize[u]++;
adjsize[s-1]++;
}
}
for (int u = 0; u < embedding.getNumWords(); ++u) {
//System.out.println("Start from edge: " + u);
if (visited[u] == false)
articulationRec(u, embedding, adj, &time, parent, disc, low, visited, ap);
}
return ap;
}
std::vector<int> EmbeddingUtils::dfs(BasicEmbedding &embedding) {
int s = embedding.getNumWords();
std::vector<int> words = embedding.getWords();
std::vector<bool> visited(s,false);
std::vector<int> order;
//create embedding adj matrix.
//std::vector<std::vector<int>> adj(s, std::vector<int>(s+1, -1));
EmbMatrix adj;
int adjsize[s]={0};
for (int u = 0; u < s; ++u) {
for (int v = u; v < s; ++v) {
// here we verify whether each vertice is adjacent to each vertice
// In practive (embeddings << graph) this is better than accessing
// directly the whole graph adjacency (the only one available)
if (!embedding.areWordsNeighbours(words[u], words[v]))
continue;
adj[u][adjsize[u]]=v;
adj[v][adjsize[v]]=u;
adjsize[u]++;
adjsize[v]++;
}
adj[u][adjsize[u]]=-1;
}
for (int u = 0; u < embedding.getNumWords(); ++u) {
//std::cout << "Start from word: " << u << std::endl;
if (visited[u] == false)
dfsRec(u, embedding, adj, visited, order);
}
return order;
}
bool EmbeddingUtils::isConnected(BasicEmbedding &embedding) {
int s = embedding.getNumWords();
std::vector<int> words = embedding.getWords();
std::vector<bool> visited(s,false);
std::vector<int> order;
//std::cout << "create embedding adj matrix for embedding " << embedding << std::endl;
//std::vector<std::vector<int>> adj(s, std::vector<int>(s+1,-1));
EmbMatrix adj;
int adjsize[s]={0};
for (int u = 0; u < s; ++u) {
for (int v = u; v < s; ++v) {
// here we verify whether each vertice is adjacent to each vertice
// In practive (embeddings << graph) this is better than accessing
// directly the whole graph adjacency (the only one available)
if (v == u || !embedding.areWordsNeighbours(words[u], words[v]))
continue;
adj[u][adjsize[u]]=v;
adj[v][adjsize[v]]=u;
adjsize[u]++;
adjsize[v]++;
}
adj[u][adjsize[u]]=-1;
}
/*for (int u = 0; u < 10; ++u) {
std::cout << "node " << u << " ";
for (int v = u; v < 10; ++v) {
std::cout << " " << adj[u][v];
}
std::cout << std::endl;
}*/
dfsRec(0, embedding, adj, visited, order);
for (int u = 0; u < embedding.getNumWords(); ++u)
if (visited[u]==false) return false;
return true;
}
bool EmbeddingUtils::isQuasiClique(BasicEmbedding &embedding, double a) {
std::vector<int> words = embedding.getWords();
double factor = a*(double)(embedding.getNumWords() - 1.);
for (int u = 0; u < embedding.getNumWords(); ++u) {
int d = embedding.getWordDegree(words[u]);
//std::cout << "factor : " << factor << " degree node: " << d << std::endl;
if (factor > d) return false;
}
return true;
}
std::vector<size_t> EmbeddingUtils::getSubsetWordsHash(BasicEmbedding &embedding) {
std::vector<int> words(embedding.getWords());
std::sort(words.begin(), words.end());
std::vector<size_t> sub;
for (int i = 0; i < (int) words.size(); i++) {
size_t seed = 0;
for (int j = 0; j < (int) words.size(); j++) {
if (i != j)
boost::hash_combine(seed, words[j] * 2654435761);
}
sub.push_back(seed);
}
return sub;
}