-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathFracImproveDecomp.cpp
executable file
·348 lines (269 loc) · 9.54 KB
/
FracImproveDecomp.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
// FracImproveDecomp.cpp: implementation of the FracImproveDecomp class.
//
//////////////////////////////////////////////////////////////////////
#include <cstdio>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <list>
#include <vector>
using namespace std;
#include "FracImproveDecomp.h"
#include "Hypertree.h"
#include "Hypergraph.h"
#include "Hyperedge.h"
#include "Vertex.h"
#include "Globals.h"
#include "FecCalculator.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
FracImproveDecomp::FracImproveDecomp(const HypergraphSharedPtr &HGraph, int k) : DetKDecomp(HGraph,k), MyFecCalculator(HGraph)
{
}
FracImproveDecomp::~FracImproveDecomp()
{
}
//////////////////////////////////////////////////////////////////////
// Class methods
//////////////////////////////////////////////////////////////////////
/*
***Description***
The method decomposes the hyperedges in a subhypergraph as described in Gottlob and
Samer: A Backtracking-Based Algorithm for Computing Hypertree-Decompositions.
INPUT: HEdges: Hyperedges in the subgraph
Connector: Connector nodes that must be covered
iRecLevel: Recursion level
OUTPUT: return: Hypertree decomposition of HEdges
*/
bool FracImproveDecomp::verifyFracHypertreeWidth(HypertreeSharedPtr & htree, double &outFW) const
{
double currentChildFW = -1;
double bestFW = -1;
unique_ptr<FractionalEdgeCover> fec = make_unique<FractionalEdgeCover>(MyFecCalculator.computeFEC(htree->getChi(), bestFW));
double weight = fec->computeWeight();
if (weight > threshold)
return false;
for (auto child : htree->allChildren()) {
bool value = verifyFracHypertreeWidth(child, currentChildFW);
if (value) {
if (bestFW < currentChildFW)
bestFW = currentChildFW;
}
else
return false;
}
outFW = bestFW;
htree->setFec(fec);
return true;
}
HypertreeSharedPtr FracImproveDecomp::decomp(const HyperedgeVector &HEdges, double &outFW, const VertexSet &Connector, int RecLevel) const
{
int i, j;
list<Hypertree *>::iterator TreeIter;
SeparatorSharedPtr separator{ nullptr };
vector<int> cov_sep_set, cov_weights;
vector<bool> in_comp;
size_t cnt_edges{ HEdges.size() };
int comp_end, nbr_sel_cov, i_add, sep_size;
size_t nbr_of_parts;
bool add_edge, fail_sep;
HypertreeSharedPtr htree{ nullptr };
double bestFWidth = globalBestFW;
HyperedgeVector inner_edges, bound_edges, add_edges;
/*
for (int k = 0; k <= RecLevel; k++)
cout << "+";
cout << " " << *HEdges << endl;
*/
if ((htree = decompTrivial(HEdges, Connector)) != nullptr) {
if (verifyFracHypertreeWidth(htree, outFW))
return htree;
else
htree = nullptr;
}
// Divide hyperedges into inner hyperedges and hyperedges containing some connecting nodes
comp_end = (int)divideCompEdges(HEdges, Connector, inner_edges, bound_edges);
in_comp.resize(bound_edges.size());
cov_weights.resize(bound_edges.size(), 0);
cov_sep_set.resize(MyK + 1);
for (i = 0; i < bound_edges.size(); i++)
i < comp_end ? in_comp[i] = true : in_comp[i] = false;
// Select initial hyperedges to cover the connecting nodes
nbr_sel_cov = setInitSubset(Connector, bound_edges, cov_sep_set, in_comp, cov_weights);
// Initialize AddEdges array
for (i = 0; i < bound_edges.size(); i++)
if (in_comp[i])
if (!bound_edges[i]->isHeavy())
add_edges.push_back(bound_edges[i]);
for (i = 0; i < inner_edges.size(); i++)
if (!inner_edges[i]->isHeavy())
add_edges.push_back(inner_edges[i]);
if (add_edges.size() <= 0)
writeErrorMsg("Illegal number of hyperedges.", "DetKDecomp::decomp");
if (nbr_sel_cov >= 0)
do {
// Check whether a covering hyperedge within the component was selected
add_edge = true;
for (i = 0; i < nbr_sel_cov; i++)
if (in_comp[cov_sep_set[i]]) {
add_edge = false;
break;
}
// Stop if no inner hyperedge can be in the separator
if (!add_edge || (MyK - nbr_sel_cov > 0)) {
i_add = 0;
add_edge ? sep_size = nbr_sel_cov + 1 : sep_size = nbr_sel_cov;
do {
// Output the search progress
// cout << "(" << RecLevel << ")" << endl;
// Create a separator
separator = make_shared<Separator>();
for (i = 0; i < nbr_sel_cov; i++)
separator->insert(bound_edges[cov_sep_set[i]]);
if (add_edge)
separator->insert(add_edges[i_add]);
// Check if selected hyperedges were already used before as separator
auto &reused = getSepParts(separator);
//Debugging output
/*
cout << "+++ Separator: ";
for (int i = 0; i < separator->size(); i++) {
cout << (*separator)[i]->getName();
if (i < separator->size()-1)
cout << ",";
}
cout << endl;
*/
// Before seperating check for low fhw
VertexSet chi = computeChi(HEdges, separator, Connector);
double chiFWidth;
unique_ptr<FractionalEdgeCover> fec = make_unique<FractionalEdgeCover>(MyFecCalculator.computeFEC(chi, chiFWidth));
if (chiFWidth > threshold)
continue;
bestFWidth = chiFWidth;
vector<DecompComponent> partitions;
vector<bool> cut_parts;
vector<double> cut_parts_fw;
list<HypertreeSharedPtr> Subtrees;
// Separate hyperedges into partitions with corresponding connector nodes
nbr_of_parts = separate(separator, HEdges, partitions);
// 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 (i = 0; i < partitions.size(); i++) {
if (partitions[i].size() >= cnt_edges) {
//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 (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]);
}
else {
// Decompose component recursively
htree = decomp(partitions[i], currentChildFW, 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 (currentChildFW > bestFWidth) {
bestFWidth = 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
for (i = 0; i < nbr_sel_cov; i++) {
j = cov_sep_set[i];
in_comp[j] ? bound_edges[j]->setLabel(-1) : bound_edges[j]->setLabel(0);
}
if (add_edge)
add_edges[i_add]->setLabel(-1);
htree = getHTNode(HEdges, separator, Connector, Subtrees);
htree->setFec(fec);
}
}
} while (add_edge && (htree == nullptr) && (++i_add < add_edges.size()));
}
} while ((htree == nullptr) && ((nbr_sel_cov = setNextSubset(Connector, bound_edges, cov_sep_set, in_comp, cov_weights)) > 0));
outFW = bestFWidth;
return htree;
}
/*
***Description***
The method builds a hypertree decomposition of a given hypergraph as described in Gottlob
and Samer: A Backtracking-Based Algorithm for Computing Hypertree-Decompositions.
INPUT: HGraph: Hypergraph that has to be decomposed
iK: Maximum separator size
OUTPUT: return: Hypertree decomposition of HGraph
*/
HypertreeSharedPtr FracImproveDecomp::buildHypertree(double minImprovement, double &fw)
{
HypertreeSharedPtr HTree;
HyperedgeVector HEdges;
globalBestFW = MyK + 1;
threshold = MyK - minImprovement;
// 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, fw);
// Expand pruned hypertree nodes
if ((HTree != nullptr) && (HTree->getCutNode() != nullptr)) {
cout << "Expanding hypertree ..." << endl;
expandHTree(HTree);
}
return HTree;
}
VertexSet FracImproveDecomp::computeChi(const HyperedgeVector &comp, const shared_ptr<Separator> &Sep, const VertexSet &Connector) const
{
VertexSet vcomp;
VertexSet chi;
for (auto &e : comp)
for (auto &v : e->allVertices())
vcomp.insert(v);
// Insert hyperedges and nodes into the hypertree-node
for (auto &e : (*Sep)) {
for (auto &v : e->allVertices())
if (vcomp.find(v) != vcomp.end())
chi.insert(v);
}
// Insert additional chi-labels to guarantee connectedness
for (auto &v : Connector)
chi.insert(v);
return chi;
}