forked from TUfischl/newdetkdecomp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHypergraph.cpp
356 lines (283 loc) · 8.15 KB
/
Hypergraph.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
#include <algorithm>
#include <cmath>
#include <memory>
#include "Globals.h"
#include "Hypergraph.h"
#include "CombinationIterator.h"
#include "Parser.h"
void Hypergraph::labelReachEdges(const HyperedgeSharedPtr &edge, int label) const
{
edge->setLabel(label);
for (auto e : EdgeNeighbors.at(edge))
if (e->getLabel() == 0)
labelReachEdges(e, label);
}
void Hypergraph::buildHypergraph(Parser &P)
{
VertexVector vertices;
HyperedgeSharedPtr edge;
for (int i = 0; i < P.getNbrOfVars(); i++)
vertices.push_back(std::make_shared<Vertex>(P.getVariable(i)));
for (int i = 0; i < P.getNbrOfAtoms(); i++) {
edge = std::make_shared<Hyperedge>(P.getAtom(i));
for (int j = 0; j < P.getNbrOfVars(i); j++)
edge->add(vertices[P.getNextAtomVar()]);
this->insertEdge(edge);
}
}
bool Hypergraph::hasAllEdges(const HyperedgeVector &edges) const
{
for (auto he : edges)
if (!hasEdge(he))
return false;
return true;
}
bool Hypergraph::hasAllEdges(const HyperedgeSet &edges) const
{
for (auto he : edges)
if (!hasEdge(he))
return false;
return true;
}
HyperedgeSharedPtr Hypergraph::getEdgeByID(int id) const
{
for (auto he : Edges)
if (he->getId() == id)
return he;
return nullptr;
}
VertexSharedPtr Hypergraph::getVertexByID(int id) const
{
for (auto v : Vertices)
if (v->getId() == id)
return v;
return nullptr;
}
void Hypergraph::setVertexLabels(int value) const
{
for (auto v : Vertices)
v->setLabel(value);
}
void Hypergraph::setEdgeLabels(int value) const
{
for (auto he : Edges)
he->setLabel(value);
}
void Hypergraph::setAllLabels(int value) const
{
setVertexLabels(value);
setEdgeLabels(value);
}
/*
void Hypergraph::resetEdgeLabels(int val)
{
for (auto he : Edges)
he->setLabel(val);
}
void Hypergraph::resetVertexLabels(int val)
{
for (auto v : Vertices)
v->setLabel(val);
}
*/
void Hypergraph::insertEdge(const HyperedgeSharedPtr &edge)
{
if (getEdgeByID(edge->getId()) != nullptr )
writeErrorMsg("This hypergraph already contains a Hyperedge with id " + to_string(edge->getId()), "Hypergraph::insertEdge");
if (dynamic_pointer_cast<Superedge>(edge) != nullptr)
CntSuperedges++;
Edges.insert(edge);
for (auto v : edge->allVertices()) {
if (find(Vertices.begin(), Vertices.end(), v) == Vertices.end())
Vertices.insert(v);
EdgeNeighbors[edge].insert(VertexNeighbors[v].cbegin(), VertexNeighbors[v].cend());
for (auto e : VertexNeighbors[v])
EdgeNeighbors[e].insert(edge);
VertexNeighbors[v].insert(edge);
}
}
bool Hypergraph::isConnected() const
{
setAllLabels();
if (getNbrOfEdges() > 0)
labelReachEdges(*Edges.begin());
for (auto e : Edges)
if (e->getLabel() == 0)
return false;
return true;
}
void Hypergraph::makeDual(Hypergraph & hg) const
{
unordered_map<uint, VertexSharedPtr> newVertices;
//Every edge becomes a vertex
for (auto e : Edges)
newVertices[e->getId()] = make_shared<Vertex>(e->getId(), e->getName());
//Every vertex becomes an edge
for (auto v : Vertices) {
HyperedgeSharedPtr e = make_shared<Hyperedge>(v->getId(), v->getName());
for (auto n : allVertexNeighbors(v))
e->add(newVertices[n->getId()]);
hg.insertEdge(e);
}
}
HyperedgeVector Hypergraph::getMCSOrder()
{
HyperedgeVector order;
HyperedgeVector candidates;
HyperedgeSet::iterator he_iter;
uint max_card{ 0 };
uint tmp_card{ 0 };
HyperedgeSharedPtr he;
//Reset edge labels (used to determine which hyperedges have been removed)
setEdgeLabels();
//Select randomly an initial hyperedge
auto it = Edges.begin();
for (int i = 0; i < random_range(0, Edges.size() - 1); i++, it++);
he = *it;
he->setLabel(1);
order.push_back(he);
//Remove nodes with highest connectivity iteratively
do {
he = nullptr;
he_iter = Edges.begin();
max_card = 0 ;
//find first vertex that has not been removed yet
for (; he_iter != Edges.end() && (*he_iter)->getLabel() != 0; he_iter++);
if (he_iter != Edges.end()) {
//count its connectivity
he = *he_iter;
for (auto n : allEdgeNeighbors(he))
max_card += n->getLabel();
candidates.push_back(he);
// Search for the node with highest connectivity,
// i.e., with highest number of neighbours in the set of nodes already removed
for (; he_iter != Edges.end(); he_iter++) {
he = *he_iter;
if (he->getLabel() == 0) {
tmp_card = 0;
for (auto n : allEdgeNeighbors(he))
tmp_card += n->getLabel();
if (tmp_card >= max_card) {
if (tmp_card > max_card) {
candidates.clear();
max_card = tmp_card;
}
candidates.push_back(he);
}
}
}
//randomly select the next node with highest connectivity
he = candidates[random_range(0, candidates.size() - 1)];
candidates.clear();
he->setLabel(1);
order.push_back(he);
}
} while (he != nullptr);
return order;
}
int Hypergraph::degree() const
{
int maxDegree = 0;
for (const VertexSharedPtr v : Vertices) {
int degree;
try {
degree = VertexNeighbors.at(v).size();
}
catch (const std::out_of_range& oor) {
degree = 0;
}
if (degree > maxDegree) maxDegree = degree;
}
return maxDegree;
}
int Hypergraph::bip(int k) const
{
int maxBip = 0;
HyperedgeVector edges(Edges.begin(), Edges.end());
if (k <= Edges.size()) {
CombinationIterator comb(Edges.size(), k);
comb.setStage(k);
int *indices;
while ((indices = comb.next()) != nullptr) {
VertexVector vertices;
for (auto v : edges[indices[0]]->allVertices()) {
bool found = true;
for (int i = 1; indices[i] != -1 && found; i++)
if (find(edges[indices[i]]->allVertices().begin(), edges[indices[i]]->allVertices().end(), v) == edges[indices[i]]->allVertices().end())
found = false;
if (found)
vertices.push_back(v);
}
if (vertices.size() > maxBip)
maxBip = vertices.size();
}
}
else {
VertexVector vertices;
for (auto v : edges[0]->allVertices()) {
bool found = true;
for (int i = 1; i < edges.size() && found; i++)
if (find(edges[i]->allVertices().begin(), edges[i]->allVertices().end(), v) == edges[i]->allVertices().end())
found = false;
if (found)
vertices.push_back(v);
}
return vertices.size();
}
return maxBip;
}
int Hypergraph::vcDimension() const
{
int maxVC = (int)floor(log(Edges.size()) / log(2));
int i;
VertexVector vertices(Vertices.begin(), Vertices.end());
//Find the maximum cardinality of a shattered subset of V
for (i = 1; i <= maxVC; i++) {
bool shattered = false;
//For each subset X of size vc check if it is shattered, if X is shattered then vc is at least i
int *indices;
CombinationIterator cit(vertices.size(), i);
cit.setStage(i);
while ((indices = cit.next()) != nullptr && !shattered) {
bool check_x = true;
set_type set_x;
for (int j = 0; indices[j] != -1; j++)
set_x.insert(vertices[indices[j]]);
//Collection<String> setX = cit.next();
//PowerSetIterator<String> itPSetX = new PowerSetIterator<String>(setX);
powerset_type pow_set_x = powerset(set_x);
//For each A \subseteq X check if there is an edge s.t. A = X \cap e
//if there is a subset such that this check fails (checkX = false), then X is not shattered.
for (powerset_type::iterator iter = pow_set_x.begin();
iter != pow_set_x.end() && check_x;
++iter) {
bool edge_found = false;
for (auto e_it = Edges.begin(); e_it != Edges.end() && !edge_found; e_it++) {
set_type help_x;
for (auto v : set_x) {
if (find((*e_it)->allVertices().begin(), (*e_it)->allVertices().end(), v) != (*e_it)->allVertices().end())
help_x.insert(v);
}
if (help_x == *iter)
edge_found = true;
}
if (!edge_found)
check_x = false;
}
if (check_x)
shattered = true;
}
if (!shattered)
return i - 1;
}
return i - 1;
}
int Hypergraph::arity() const
{
int ari = 0;
int help = 0;
for (auto e : Edges)
if ((help = e->getNbrOfVertices()) > ari)
ari = help;
return ari;
}