-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathRankFHDecomp.cpp
executable file
·316 lines (250 loc) · 8.01 KB
/
RankFHDecomp.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
#include "RankFHDecomp.h"
#include "VertexSeparator.h"
#include "BaseSeparator.h"
RankFHDecomp::RankFHDecomp(const HypergraphSharedPtr &HGraph, double k) : MyHg { HGraph }, MyK{k}, MyFecCalculator{HGraph}
{
MyRank = MyHg->arity();
}
RankFHDecomp::~RankFHDecomp()
{
}
CompCache & RankFHDecomp::getSepParts(VertexSeparatorSharedPtr & sep) const
{
for (auto t : MyTriedSeps)
if (t.first == sep) {
sep = t.first;
break;
}
return MyTriedSeps[sep];
}
size_t RankFHDecomp::separate(VertexSeparatorSharedPtr bag, const HyperedgeVector &edges, vector<DecompComponent> &partitions) const
{
int label = 0;
unordered_map<HyperedgeSharedPtr, int, NamedEntityHash> eLabels;
unordered_map<VertexSharedPtr, int, NamedEntityHash> vLabels;
partitions.clear();
//First set the label for all seperating vertices
for (auto &v : bag->allVertices())
vLabels[v] = -1;
for (auto &he : edges)
if (eLabels[he] == 0) {
//don't add he to the component if it is covered by the bag
if (!he->isCoveredBy(bag->vertices())) {
DecompComponent comp(static_pointer_cast<BaseSeparator>(bag));
label++;
comp.add(he);
eLabels[he] = label;
//find all edges reachable from the current component
for (int i = 0; i < comp.size(); i++) {
for (auto &v : comp[i]->allVertices()) {
if (vLabels[v] == 0) {
vLabels[v] = label;
for (auto &reach_he : MyHg->allVertexNeighbors(v))
if (eLabels[reach_he] == 0) {
if (!reach_he->isCoveredBy(bag->vertices())) {
eLabels[reach_he] = label;
comp.add(reach_he);
}
else
eLabels[reach_he] = -1;
}
}
}
}
partitions.push_back(comp);
}
else
eLabels[he] = -1;
}
return partitions.size();
}
HypertreeSharedPtr RankFHDecomp::getCutNode(int label, const DecompComponent & comp, const VertexSet & Chi) const
{
HypertreeSharedPtr htree = getHTNode(comp.component(), Chi);
htree->setCut();
htree->setLabel(label);
return htree;
}
HypertreeSharedPtr RankFHDecomp::getHTNode(const HyperedgeVector &lambda, const VertexSet &Chi, const list<HypertreeSharedPtr> &Subtrees) const
{
HypertreeSharedPtr HTree = make_shared<Hypertree>(MyHg);
// Insert hyperedges and nodes into the hypertree-node
for (auto &e : lambda) {
HTree->insLambda(e);
}
// Insert additional chi-labels to guarantee connectedness
for (auto &v : Chi)
HTree->insChi(v);
// Insert children into the hypertree-node
for (auto &subtree : Subtrees) {
//cout << "Calling insChild" << endl;
HTree->insChild(subtree);
}
return HTree;
}
HypertreeSharedPtr RankFHDecomp::decomp(const HyperedgeVector & HEdges, const VertexSet & Connector, int RecLevel) const
{
//Possible set of vertices
VertexSet vertices;
//Bag of current node
VertexSeparatorSharedPtr bag;
//Output Hypertree
HypertreeSharedPtr htree{ nullptr };
if (Connector.size()+1 > MyK*MyRank)
return nullptr;
//Initialize set of possible vertices
for (auto e : HEdges)
for (auto v : e->allVertices())
if (Connector.find(v) == Connector.end())
vertices.insert(v);
// Main Loop: Try to add a vertex to the bag
for (auto it = vertices.begin(); htree == nullptr && it != vertices.end(); it++) {
VertexSharedPtr v = (*it);
bag = make_shared<VertexSeparator>(Connector);
bag->insert(v);
/*
for (int i = 0; i <= RecLevel; i++)
cout << "+";
cout << " " << bag << endl;
*/
// Can we cover the new bag with MyK
double width;
unique_ptr<FractionalEdgeCover> fec = make_unique<FractionalEdgeCover>(MyFecCalculator.computeFEC(bag->vertices(), width));
if (width > MyK) {
bag->erase(v);
continue;
}
vector<DecompComponent> partitions;
vector<bool> cut_parts;
//vector<double> cut_parts_fw;
list<HypertreeSharedPtr> subtrees;
bool fail_sep;
auto &reused = getSepParts(bag);
int nbr_of_parts = separate(bag, HEdges, partitions);
// end if no partitions are left
if (nbr_of_parts == 0) {
htree = getHTNode(fec->getEdges(), bag->vertices(), subtrees);
htree->setFec(fec);
return htree;
}
// Create auxiliary array
cut_parts.clear();
//cut_parts_fw.clear();
cut_parts.resize(nbr_of_parts);
//cut_parts_fw.resize(nbr_of_parts);
// Check partitions for decomposibility and undecomposibility
fail_sep = false;
for (int i = 0; i < partitions.size(); i++) {
if (partitions[i].size() > HEdges.size()) {
//writeErrorMsg("Monotonicity violated.", "DetKDecomp::decomp");
fail_sep = true;
reused.failed.push_back(partitions[i].first());
break;
}
// Check for undecomposability
if (partitions[i].containsOneOf(reused.failed)) {
fail_sep = true;
break;
}
// Check for decomposibility
if (HyperedgeSharedPtr help = partitions[i].containsOneOf(reused.succ)) {
cut_parts[i] = true;
//cut_parts_fw[i] = reused.succFW[help];
}
else
cut_parts[i] = false;
}
if (!fail_sep) {
// Decompose partitions into hypertrees
for (int i = 0; i < partitions.size(); i++) {
double currentChildFW = -1;
if (cut_parts[i]) {
// Prune subtree
//currentChildFW = cut_parts_fw[i];
htree = getCutNode(RecLevel + 1, partitions[i], bag->vertices());
}
else {
// Decompose component recursively
htree = decomp(partitions[i], RecLevel + 1);
if (htree == nullptr)
reused.failed.push_back(partitions[i][0]);
else {
reused.succ.push_back(partitions[i][0]);
reused.succFW[partitions[i][0]] = currentChildFW;
}
}
if (htree != nullptr)
subtrees.push_back(htree);
else break;
}
//Either all components decomposed or some component failed
if (htree != nullptr) {
// Create a new hypertree node
// Build a separator (edges that are covered by the bag)
htree = getHTNode(fec->getEdges(), bag->vertices(), subtrees);
htree->setFec(fec);
}
}
}
return htree;
}
/*
***Description***
The method builds a hypertree decomposition of a given hypergraph.
INPUT: HGraph: Hypergraph that has to be decomposed
iK: Maximum separator size
OUTPUT: return: Hypertree decomposition of HGraph
*/
HypertreeSharedPtr RankFHDecomp::buildHypertree()
{
HypertreeSharedPtr HTree;
HyperedgeVector HEdges;
// Order hyperedges heuristically
HEdges = MyHg->getMCSOrder();
//cout << HEdges << endl;
// Store initial heuristic order as weight
//for(int i=0; i < HEdges.size(); i++)
// HEdges[i]->setWeight(i);
// Build hypertree decomposition
HTree = decomp(HEdges);
// Expand pruned hypertree nodes
if ((HTree != nullptr) && (HTree->getCutNode() != nullptr)) {
cout << "Expanding hypertree ..." << endl;
expandHTree(HTree);
}
return HTree;
}
/*
***Description***
The method expands pruned hypertree nodes, i.e., subgraphs which were not decomposed but are
known to be decomposable are decomposed.
INPUT: HTree: Hypertree that has to be expanded
OUTPUT: HTree: Expanded hypertree
*/
void RankFHDecomp::expandHTree(HypertreeSharedPtr &HTree) const
{
HypertreeSharedPtr cut_node, subtree;
while ((cut_node = HTree->getCutNode()) != nullptr) {
// Store subgraph in an array
auto &lambda = cut_node->getLambda();
HyperedgeVector edges(lambda.begin(), lambda.end());
// Reconstruct connector vertices
VertexSet comp;
auto &chi = cut_node->getChi();
VertexSet connector;
for (auto &e : edges)
for (auto &v : e->allVertices())
comp.insert(v);
for (auto &v : chi)
if (chi.find(v) != chi.end())
connector.insert(v);
// Decompose subgraph
subtree = decomp(edges, connector, cut_node->getLabel());
if (subtree == nullptr)
writeErrorMsg("Illegal decomposition pruning.", "H_DetKDecomp::expandHTree");
// Replace the pruned node by the corresponding subtree
auto parent = cut_node->getParent().lock();
parent->insChild(subtree);
parent->remChild(cut_node);
}
}