| 1 |
// -*- Mode: C++; tab-width: 2; -*- |
| 2 |
// vi: set ts=2: |
| 3 |
// |
| 4 |
|
| 5 |
namespace BALL |
| 6 |
{ |
| 7 |
template <class UndirectedGraph> |
| 8 |
TreeWidth<UndirectedGraph>::TreeWidth(UndirectedGraph const& input) |
| 9 |
: input_(&input) |
| 10 |
{ |
| 11 |
// first, compute the connected components of the graph |
| 12 |
|
| 13 |
// since we do not know what kind of graph we are working with, we first |
| 14 |
// create a suitable index map |
| 15 |
typedef typename GRAPH::GraphTraits<UndirectedGraph>::VertexType OriginalVertexType; |
| 16 |
typedef std::map<OriginalVertexType, int> OriginalVertexIndexMap; |
| 17 |
typedef boost::associative_property_map<OriginalVertexIndexMap> OriginalVertexPropertyIndexMap; |
| 18 |
|
| 19 |
OriginalVertexIndexMap vi, vc; |
| 20 |
OriginalVertexPropertyIndexMap vertex_indices(vi), connected_components(vc); |
| 21 |
|
| 22 |
int num_vertex = 0; |
| 23 |
BGL_FORALL_VERTICES_T(vertex, input, UndirectedGraph) |
| 24 |
{ |
| 25 |
vi[vertex] = num_vertex++; |
| 26 |
} |
| 27 |
|
| 28 |
int num_components = boost::connected_components(input, connected_components, vertex_index_map(vertex_indices)); |
| 29 |
|
| 30 |
// now, copy each copmonent into an editable graph |
| 31 |
components_.resize(num_components); |
| 32 |
|
| 33 |
// and copy them into individual graphs |
| 34 |
for (int i=0; i<num_components; ++i) |
| 35 |
{ |
| 36 |
components_[i] = boost::shared_ptr<EditableGraph>(new EditableGraph()); |
| 37 |
|
| 38 |
ComponentFilter_<OriginalVertexPropertyIndexMap> filter(connected_components, i); |
| 39 |
boost::filtered_graph<UndirectedGraph, boost::keep_all, |
| 40 |
ComponentFilter_<OriginalVertexPropertyIndexMap> > fg(input, boost::keep_all(), filter); |
| 41 |
|
| 42 |
boost::copy_graph(input, *(components_[i]), |
| 43 |
vertex_copy(GRAPH::makeEditableVertexCopier(input, *(components_[i]))). |
| 44 |
edge_copy(GRAPH::makeEditableEdgeCopier(input, *(components_[i])))); |
| 45 |
} |
| 46 |
|
| 47 |
// now, generate the nice tree decompositions |
| 48 |
typedef typename TreeWidthImplementation<EditableGraph>::MinorMinWidth MinorMinWidth; |
| 49 |
typedef typename TreeWidthImplementation<EditableGraph>::GreedyFillIn GreedyFillIn; |
| 50 |
|
| 51 |
nice_tree_decompositions_.resize(num_components); |
| 52 |
|
| 53 |
for (int i=0; i<num_components; ++i) |
| 54 |
{ |
| 55 |
// Quick-Bound-And-Branch algorithm to compute a perfect elimination order |
| 56 |
typename TreeWidthImplementation<EditableGraph>::template QuickBB<MinorMinWidth, GreedyFillIn> decomposer(*components_[i]); |
| 57 |
|
| 58 |
// use the computed elimination order to build an optimal tree decomposition |
| 59 |
typename TreeWidthImplementation<EditableGraph>::EliminationOrder permutation = decomposer.compute(); |
| 60 |
|
| 61 |
typename TreeWidthImplementation<EditableGraph>::template TreeDecompositionBuilder<UndirectedGraph> dbuilder; |
| 62 |
boost::shared_ptr<TreeDecomposition> tree_decomposition_b = dbuilder(*components_[i], permutation); |
| 63 |
|
| 64 |
// transform this tree decomposition into a nice tree decomposition |
| 65 |
nice_tree_decomposition_graphs_.push_back(boost::shared_ptr<TreeDecompositionGraph>(new TreeDecompositionGraph())); |
| 66 |
nice_tree_decompositions_[i] = dbuilder.makeNice(nice_tree_decomposition_graphs_[i]); |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
template <class UndirectedGraph> |
| 71 |
Size TreeWidth<UndirectedGraph>::computeTreeWidth(TreeDecomposition const& td) |
| 72 |
{ |
| 73 |
Size result = 1; |
| 74 |
|
| 75 |
BGL_FORALL_VERTICES_T(current_vertex, td._g, UndirectedGraph) |
| 76 |
{ |
| 77 |
result = std::max((Size)(boost::get(boost::vertex_bag_content, td, current_vertex).size()), result); |
| 78 |
} |
| 79 |
|
| 80 |
return result - 1; |
| 81 |
} |
| 82 |
|
| 83 |
template <class UndirectedGraph> |
| 84 |
void TreeWidth<UndirectedGraph>::writeGraphvizFile(std::ostream& out, TreeDecomposition const& td) |
| 85 |
{ |
| 86 |
boost::write_graphviz(out, td._g, BagContentWriter(&td, input_)); |
| 87 |
} |
| 88 |
|
| 89 |
template <> |
| 90 |
void TreeWidth<MolecularGraph>::BagContentWriter::operator() (std::ostream& out, const TreeDecompositionBag& v) const; |
| 91 |
|
| 92 |
template <class UndirectedGraph> |
| 93 |
void TreeWidth<UndirectedGraph>::BagContentWriter::operator() (std::ostream& out, const TreeDecompositionBag& v) const |
| 94 |
{ |
| 95 |
out << "[label=\"" << boost::get(boost::vertex_index, *td_, v) << "\"]"; |
| 96 |
} |
| 97 |
|
| 98 |
// ***************************************************************************************** |
| 99 |
// * TreeWidthImplementation * |
| 100 |
// ***************************************************************************************** |
| 101 |
|
| 102 |
// ***************************************************************************************** |
| 103 |
// * GeneralLowerBoundAlgorithm * |
| 104 |
// ***************************************************************************************** |
| 105 |
template <class UndirectedGraph> |
| 106 |
template <class Criterion, class Reducer> |
| 107 |
Size TreeWidthImplementation<UndirectedGraph>::GeneralLowerBoundAlgorithm<Criterion, Reducer>::operator() ( |
| 108 |
UndirectedGraph const& original_graph) |
| 109 |
{ |
| 110 |
UndirectedGraph graph_copy; |
| 111 |
GRAPH::deepCopy(original_graph, graph_copy); |
| 112 |
|
| 113 |
Size maximum = boost::num_vertices(graph_copy); |
| 114 |
Size lower_bound = 0; |
| 115 |
|
| 116 |
while (boost::num_vertices(graph_copy) > 1) |
| 117 |
{ |
| 118 |
Size minimum = maximum; |
| 119 |
|
| 120 |
Criterion criterion(graph_copy); |
| 121 |
Reducer reducer(graph_copy); |
| 122 |
|
| 123 |
VertexType best_vertex = *(boost::vertices(graph_copy).first); |
| 124 |
BGL_FORALL_VERTICES_T(current_vertex, graph_copy, UndirectedGraph) |
| 125 |
{ |
| 126 |
// find vertex which fits the criterium |
| 127 |
Size criterion_value = criterion(current_vertex); |
| 128 |
|
| 129 |
if (criterion_value < minimum) |
| 130 |
{ |
| 131 |
best_vertex = current_vertex; |
| 132 |
minimum = criterion_value; |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
lower_bound = std::max(lower_bound, minimum); |
| 137 |
|
| 138 |
// now reduce graph by the found vertex |
| 139 |
reducer(best_vertex); |
| 140 |
} |
| 141 |
|
| 142 |
return lower_bound; |
| 143 |
} |
| 144 |
|
| 145 |
// ***************************************************************************************** |
| 146 |
// * MinorMinWidthReducer * |
| 147 |
// ***************************************************************************************** |
| 148 |
|
| 149 |
template <class UndirectedGraph> |
| 150 |
TreeWidthImplementation<UndirectedGraph>::MinorMinWidthReducer::MinorMinWidthReducer(UndirectedGraph& graph) |
| 151 |
: graph_(graph) |
| 152 |
{ |
| 153 |
} |
| 154 |
|
| 155 |
template <class UndirectedGraph> |
| 156 |
void TreeWidthImplementation<UndirectedGraph>::MinorMinWidthReducer::operator () (VertexType& vertex) |
| 157 |
{ |
| 158 |
if (boost::out_degree(vertex, graph_) == 0) |
| 159 |
{ |
| 160 |
throw GRAPH::UnconnectedGraphException(__FILE__, __LINE__, String("MinorMinWidth")); |
| 161 |
} |
| 162 |
|
| 163 |
// iterate over the neighbourhood of the given vertex |
| 164 |
VertexType min_vertex = *(boost::adjacent_vertices(vertex, graph_).first); |
| 165 |
|
| 166 |
Size min_degree = boost::out_degree(min_vertex, graph_); |
| 167 |
|
| 168 |
BGL_FORALL_ADJ_T(vertex, neighbour, graph_, UndirectedGraph) |
| 169 |
{ |
| 170 |
Size current_degree = boost::out_degree(neighbour, graph_); |
| 171 |
|
| 172 |
if (current_degree < min_degree) |
| 173 |
{ |
| 174 |
min_vertex = neighbour; |
| 175 |
min_degree = current_degree; |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
if (min_degree < boost::out_degree(vertex, graph_)) |
| 180 |
{ |
| 181 |
contractEdge(vertex, min_vertex); |
| 182 |
} |
| 183 |
else |
| 184 |
{ |
| 185 |
contractEdge(min_vertex, vertex); |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
template <class UndirectedGraph> |
| 190 |
void TreeWidthImplementation<UndirectedGraph>::MinorMinWidthReducer::contractEdge(VertexType& u, VertexType& v) |
| 191 |
{ |
| 192 |
std::vector<std::pair<VertexType, VertexType> > new_edges; |
| 193 |
BGL_FORALL_ADJ_T(v, neighbour, graph_, UndirectedGraph) |
| 194 |
{ |
| 195 |
// connect the neighbour to u |
| 196 |
if (neighbour != u && !boost::edge(u, neighbour, graph_).second) |
| 197 |
{ |
| 198 |
new_edges.push_back(std::make_pair(u, neighbour)); |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
for (Position i=0; i<new_edges.size(); ++i) |
| 203 |
boost::add_edge(new_edges[i].first, new_edges[i].second, graph_); |
| 204 |
|
| 205 |
// remove v from the graph |
| 206 |
boost::clear_vertex(v, graph_); |
| 207 |
boost::remove_vertex(v, graph_); |
| 208 |
} |
| 209 |
|
| 210 |
// ***************************************************************************************** |
| 211 |
// * MinorMinWidthCriterion * |
| 212 |
// ***************************************************************************************** |
| 213 |
|
| 214 |
template <class UndirectedGraph> |
| 215 |
TreeWidthImplementation<UndirectedGraph>::MinorMinWidthCriterion::MinorMinWidthCriterion(UndirectedGraph const& graph) |
| 216 |
: graph_(graph) |
| 217 |
{ |
| 218 |
} |
| 219 |
|
| 220 |
template <class UndirectedGraph> |
| 221 |
Size TreeWidthImplementation<UndirectedGraph>::MinorMinWidthCriterion::operator () (VertexType& a) const |
| 222 |
{ |
| 223 |
return boost::out_degree(a, graph_); |
| 224 |
} |
| 225 |
|
| 226 |
// ***************************************************************************************** |
| 227 |
// * GreedyX * |
| 228 |
// ***************************************************************************************** |
| 229 |
|
| 230 |
template <class UndirectedGraph> template <class Criterion> |
| 231 |
typename TreeWidthImplementation<UndirectedGraph>::EliminationOrder |
| 232 |
TreeWidthImplementation<UndirectedGraph>::GreedyX<Criterion>::operator() (UndirectedGraph& original_graph) |
| 233 |
{ |
| 234 |
UndirectedGraph graph; |
| 235 |
GRAPH::deepCopy(original_graph, graph); |
| 236 |
|
| 237 |
Criterion criterion; |
| 238 |
|
| 239 |
Size length = boost::num_vertices(graph); |
| 240 |
std::vector<Size> permutation(length); // UNDEFINED_VERTEX_ID); |
| 241 |
|
| 242 |
if (length == 0) |
| 243 |
return EliminationOrder(permutation, length); |
| 244 |
|
| 245 |
Size upper_bound = 0; |
| 246 |
for (Size index = 0; index < length; ++index) |
| 247 |
{ |
| 248 |
VertexType& vertex = criterion(graph); |
| 249 |
|
| 250 |
Size degree = boost::out_degree(vertex, graph); |
| 251 |
if (degree == 0 && index < length-1) |
| 252 |
{ |
| 253 |
throw GRAPH::UnconnectedGraphException(__FILE__, __LINE__, "GreedyX"); |
| 254 |
} |
| 255 |
|
| 256 |
permutation[index] = boost::get(boost::vertex_index, graph, vertex); |
| 257 |
|
| 258 |
upper_bound = std::max(upper_bound, degree); |
| 259 |
|
| 260 |
GRAPH::eliminateVertex(vertex, graph); |
| 261 |
} |
| 262 |
|
| 263 |
return EliminationOrder(permutation, upper_bound); |
| 264 |
} |
| 265 |
|
| 266 |
// ***************************************************************************************** |
| 267 |
// * FillInHeuristic * |
| 268 |
// ***************************************************************************************** |
| 269 |
|
| 270 |
template <class UndirectedGraph> |
| 271 |
typename TreeWidthImplementation<UndirectedGraph>::VertexType& |
| 272 |
TreeWidthImplementation<UndirectedGraph>::FillInHeuristic::operator() (UndirectedGraph& graph) |
| 273 |
{ |
| 274 |
VertexIterator v_i, v_j, v_end; |
| 275 |
boost::tie(v_i, v_end) = boost::vertices(graph); |
| 276 |
|
| 277 |
VertexIterator v_min = v_i; |
| 278 |
Size min_value = edgeIncreaseByEliminating(v_min, graph); |
| 279 |
|
| 280 |
if (min_value > 0) |
| 281 |
{ |
| 282 |
for (; v_i != v_end; ++v_i) |
| 283 |
{ |
| 284 |
Size value = edgeIncreaseByEliminating(v_i, graph); |
| 285 |
|
| 286 |
if (value < min_value) |
| 287 |
{ |
| 288 |
v_min = v_i; |
| 289 |
min_value = value; |
| 290 |
|
| 291 |
if (min_value == 0) |
| 292 |
{ |
| 293 |
return *v_min; |
| 294 |
} |
| 295 |
} |
| 296 |
} |
| 297 |
} |
| 298 |
|
| 299 |
return *v_min; |
| 300 |
} |
| 301 |
|
| 302 |
template <class UndirectedGraph> |
| 303 |
Size TreeWidthImplementation<UndirectedGraph>::FillInHeuristic::edgeIncreaseByEliminating(VertexIterator vertex, |
| 304 |
UndirectedGraph& graph) |
| 305 |
{ |
| 306 |
Size result = 0; |
| 307 |
|
| 308 |
typename boost::graph_traits<UndirectedGraph>::adjacency_iterator a_i, a_j, ai_end; |
| 309 |
|
| 310 |
for (boost::tie(a_i, ai_end) = boost::adjacent_vertices(*vertex, graph); a_i != ai_end; ++a_i) |
| 311 |
{ |
| 312 |
a_j = a_i; ++a_j; |
| 313 |
for (; a_j != ai_end; ++a_j) |
| 314 |
{ |
| 315 |
VertexType i = *a_i; |
| 316 |
VertexType j = *a_j; |
| 317 |
|
| 318 |
if (!boost::edge(i, j, graph).second) |
| 319 |
{ |
| 320 |
++result; |
| 321 |
} |
| 322 |
} |
| 323 |
} |
| 324 |
|
| 325 |
return result; |
| 326 |
} |
| 327 |
|
| 328 |
// ***************************************************************************************** |
| 329 |
// * QuickBB * |
| 330 |
// ***************************************************************************************** |
| 331 |
|
| 332 |
template <class UndirectedGraph> |
| 333 |
template <class L, class U> |
| 334 |
TreeWidthImplementation<UndirectedGraph>::QuickBB<L, U>::QuickBB(UndirectedGraph const& graph) |
| 335 |
: graph_(graph), |
| 336 |
state(), |
| 337 |
greedy_solution(), |
| 338 |
own_solution(), |
| 339 |
visitedSubgraphs(), |
| 340 |
upper_bound(0) |
| 341 |
{ |
| 342 |
} |
| 343 |
|
| 344 |
template <class UndirectedGraph> |
| 345 |
template <class L, class U> |
| 346 |
typename TreeWidthImplementation<UndirectedGraph>::template QuickBB<L, U>::SIMPLICIAL_TYPE |
| 347 |
TreeWidthImplementation<UndirectedGraph>::QuickBB<L, U>::isSimplicial(VertexType& vertex) const |
| 348 |
{ |
| 349 |
if (boost::out_degree(vertex, graph_) <= 1) |
| 350 |
return IS_SIMPLICIAL; |
| 351 |
|
| 352 |
VertexType n1 = VertexType(); |
| 353 |
VertexType n2 = VertexType(); |
| 354 |
bool found_unconnected = false; |
| 355 |
|
| 356 |
typename boost::graph_traits<UndirectedGraph>::adjacency_iterator a_i, a_j, ai_end; |
| 357 |
|
| 358 |
for (boost::tie(a_i, ai_end) = boost::adjacent_vertices(vertex, graph_); a_i != ai_end; ++a_i) |
| 359 |
{ |
| 360 |
a_j = a_i; ++a_j; |
| 361 |
for (; a_j != ai_end; ++a_j) |
| 362 |
{ |
| 363 |
VertexType i = *a_i; |
| 364 |
VertexType j = *a_j; |
| 365 |
|
| 366 |
if (!boost::edge(i, j, graph_).second) |
| 367 |
{ |
| 368 |
if (!found_unconnected) |
| 369 |
{ |
| 370 |
n1 = i; |
| 371 |
n2 = j; |
| 372 |
found_unconnected = true; |
| 373 |
} |
| 374 |
else if (i == n1 || i == n2) |
| 375 |
{ |
| 376 |
n1 = i; |
| 377 |
n2 = i; |
| 378 |
} |
| 379 |
else if (j == n1 || j == n2) |
| 380 |
{ |
| 381 |
n1 = j; |
| 382 |
n2 = j; |
| 383 |
} |
| 384 |
else |
| 385 |
{ |
| 386 |
return NOT_SIMPLICIAL; |
| 387 |
} |
| 388 |
} |
| 389 |
} |
| 390 |
} |
| 391 |
|
| 392 |
if (!n1) |
| 393 |
{ |
| 394 |
return IS_SIMPLICIAL; |
| 395 |
} |
| 396 |
else |
| 397 |
{ |
| 398 |
return ALMOST_SIMPLICIAL; |
| 399 |
} |
| 400 |
} |
| 401 |
|
| 402 |
template <class UndirectedGraph> |
| 403 |
template <class L, class U> |
| 404 |
void TreeWidthImplementation<UndirectedGraph>::QuickBB<L, U>::prune(QuickBBState& cstate) |
| 405 |
{ |
| 406 |
Size nxt = cstate.permutation.size(); |
| 407 |
|
| 408 |
BGL_FORALL_VERTICES_T(current_vertex, graph_, UndirectedGraph) |
| 409 |
{ |
| 410 |
Size index = boost::get(boost::vertex_index, graph_, current_vertex); |
| 411 |
index_to_vertex_[index] = current_vertex; |
| 412 |
SIMPLICIAL_TYPE simplicial(isSimplicial(current_vertex)); |
| 413 |
if (simplicial == IS_SIMPLICIAL || simplicial == ALMOST_SIMPLICIAL) |
| 414 |
{ |
| 415 |
cstate.permutation.push_back(index); |
| 416 |
} |
| 417 |
} |
| 418 |
|
| 419 |
for (typename std::vector<Size>::const_iterator st_iter = cstate.permutation.begin() + nxt; |
| 420 |
st_iter != cstate.permutation.end(); |
| 421 |
++st_iter) |
| 422 |
{ |
| 423 |
VertexType vertex = index_to_vertex_[*st_iter]; |
| 424 |
cstate.g = std::max(cstate.g, (Size)boost::out_degree(vertex, graph_)); |
| 425 |
cstate.f = std::max(cstate.f, cstate.g); |
| 426 |
GRAPH::eliminateVertex(vertex, graph_); |
| 427 |
} |
| 428 |
} |
| 429 |
|
| 430 |
template <class UndirectedGraph> |
| 431 |
template <class L, class U> |
| 432 |
typename TreeWidthImplementation<UndirectedGraph>::template QuickBB<L, U>::BitSet |
| 433 |
TreeWidthImplementation<UndirectedGraph>::QuickBB<L, U>::buildBitset() const |
| 434 |
{ |
| 435 |
BitSet set; |
| 436 |
|
| 437 |
BGL_FORALL_VERTICES_T(v, graph_, UndirectedGraph) |
| 438 |
{ |
| 439 |
set[v] = true; |
| 440 |
} |
| 441 |
|
| 442 |
return set; |
| 443 |
} |
| 444 |
|
| 445 |
template <class UndirectedGraph> |
| 446 |
template <class L, class U> |
| 447 |
typename TreeWidthImplementation<UndirectedGraph>::EliminationOrder |
| 448 |
TreeWidthImplementation<UndirectedGraph>::QuickBB<L, U>::compute() |
| 449 |
{ |
| 450 |
Size lower_bound = L()(graph_); |
| 451 |
greedy_solution = U()(graph_); |
| 452 |
|
| 453 |
state.f = lower_bound; |
| 454 |
state.h = lower_bound; |
| 455 |
state.g = 0; |
| 456 |
|
| 457 |
upper_bound = greedy_solution.second; |
| 458 |
if (lower_bound < upper_bound) |
| 459 |
{ |
| 460 |
prune(state); |
| 461 |
branchAndBound(state); |
| 462 |
|
| 463 |
own_solution = EliminationOrder(state.permutation, upper_bound); |
| 464 |
} |
| 465 |
else |
| 466 |
{ |
| 467 |
own_solution = greedy_solution; |
| 468 |
} |
| 469 |
|
| 470 |
return own_solution; |
| 471 |
} |
| 472 |
|
| 473 |
template <class UndirectedGraph> |
| 474 |
template <class L, class U> |
| 475 |
void TreeWidthImplementation<UndirectedGraph>::QuickBB<L, U>::branchAndBound(QuickBBState& nstate) |
| 476 |
{ |
| 477 |
Size length = boost::num_vertices(graph_); |
| 478 |
|
| 479 |
if (length == 0) |
| 480 |
{ |
| 481 |
upper_bound = nstate.f; |
| 482 |
} |
| 483 |
else if (length == 1) |
| 484 |
{ |
| 485 |
if (upper_bound >= nstate.f) |
| 486 |
{ |
| 487 |
upper_bound = nstate.f; |
| 488 |
state.permutation = nstate.permutation; |
| 489 |
BGL_FORALL_VERTICES_T(v, graph_, UndirectedGraph) // yes, it is only one, but still... |
| 490 |
{ |
| 491 |
state.permutation.push_back(boost::get(boost::vertex_index, graph_, v)); |
| 492 |
} |
| 493 |
} |
| 494 |
} |
| 495 |
else |
| 496 |
{ |
| 497 |
index_to_vertex_.clear(); |
| 498 |
BGL_FORALL_VERTICES_T(v, graph_, UndirectedGraph) |
| 499 |
{ |
| 500 |
index_to_vertex_[boost::get(boost::vertex_index, graph_, v)] = v; |
| 501 |
} |
| 502 |
|
| 503 |
for (typename std::vector<Size>::iterator viter = greedy_solution.first.begin(); |
| 504 |
viter != greedy_solution.first.end(); ++viter) |
| 505 |
{ |
| 506 |
if (index_to_vertex_.find(*viter) != index_to_vertex_.end()) |
| 507 |
{ |
| 508 |
VertexType& vertex = index_to_vertex_[*viter]; |
| 509 |
Size degree = boost::out_degree(vertex, graph_); |
| 510 |
|
| 511 |
GRAPH::UndoEliminateOperation<UndirectedGraph> elimination = GRAPH::eliminateVertexUndoable(vertex, graph_); |
| 512 |
|
| 513 |
QuickBBState xstate(nstate); |
| 514 |
xstate.g = std::max(nstate.g, degree); |
| 515 |
xstate.permutation.push_back(*viter); |
| 516 |
|
| 517 |
BitSet bitset(buildBitset()); |
| 518 |
|
| 519 |
Size lower_bound = L()(graph_); |
| 520 |
xstate.h = lower_bound; |
| 521 |
xstate.f = std::max(xstate.g, lower_bound); |
| 522 |
|
| 523 |
if (xstate.f >= upper_bound) |
| 524 |
{ |
| 525 |
VertexType vertex_replace = elimination.undo(); |
| 526 |
index_to_vertex_[*viter] = vertex_replace; |
| 527 |
continue; |
| 528 |
} |
| 529 |
|
| 530 |
MapPos entry(visitedSubgraphs.insert(MapEntry(bitset, xstate.f))); |
| 531 |
|
| 532 |
if (entry.second) |
| 533 |
{ |
| 534 |
branchAndBound(xstate); |
| 535 |
} |
| 536 |
else if ((entry.first)->second > xstate.f) |
| 537 |
{ |
| 538 |
entry.first->second = xstate.f; |
| 539 |
branchAndBound(xstate); |
| 540 |
} |
| 541 |
|
| 542 |
VertexType vertex_replace = elimination.undo(); |
| 543 |
index_to_vertex_[*viter] = vertex_replace; |
| 544 |
} |
| 545 |
} |
| 546 |
} |
| 547 |
} |
| 548 |
|
| 549 |
// ***************************************************************************************** |
| 550 |
// * TreeDecompositionBuilder * |
| 551 |
// ***************************************************************************************** |
| 552 |
|
| 553 |
template <class UndirectedGraph> |
| 554 |
template <class OriginalGraphType> |
| 555 |
boost::shared_ptr<typename TreeWidthImplementation<UndirectedGraph>:: |
| 556 |
template TreeDecompositionBuilder<OriginalGraphType>::TreeDecomposition> |
| 557 |
TreeWidthImplementation<UndirectedGraph>::TreeDecompositionBuilder<OriginalGraphType>::operator() ( |
| 558 |
UndirectedGraph const& original_graph, |
| 559 |
EliminationOrder const& permutation) |
| 560 |
{ |
| 561 |
if (num_vertices(original_graph) == 0) |
| 562 |
{ |
| 563 |
return boost::shared_ptr<TreeDecomposition>(); |
| 564 |
} |
| 565 |
|
| 566 |
UndirectedGraph graph; |
| 567 |
GRAPH::deepCopy(original_graph, graph); |
| 568 |
|
| 569 |
std::map<int, VertexType> original_vertices; |
| 570 |
std::map<int, VertexType> new_vertices; |
| 571 |
|
| 572 |
VertexIterator v_i, v_end; |
| 573 |
for (boost::tie(v_i, v_end) = boost::vertices(original_graph); v_i != v_end; ++v_i) |
| 574 |
{ |
| 575 |
original_vertices[boost::get(boost::vertex_index, original_graph, *v_i)] = *v_i; |
| 576 |
} |
| 577 |
|
| 578 |
for (boost::tie(v_i, v_end) = boost::vertices(graph); v_i != v_end; ++v_i) |
| 579 |
{ |
| 580 |
new_vertices[boost::get(boost::vertex_index, graph, *v_i)] = *v_i; |
| 581 |
} |
| 582 |
|
| 583 |
std::vector<int> permutation_order(boost::num_vertices(original_graph)); |
| 584 |
|
| 585 |
Size last_vertex_id = permutation.first.size() - 1; |
| 586 |
|
| 587 |
tree_graph_ = boost::shared_ptr<TreeDecompositionGraph>(new TreeDecompositionGraph()); |
| 588 |
|
| 589 |
std::vector<TreeDecompositionBag> bags(last_vertex_id+1); |
| 590 |
std::vector<TreeDecompositionBag> parent_map(last_vertex_id+1); |
| 591 |
|
| 592 |
std::vector<GRAPH::UndoEliminateOperation<UndirectedGraph> > eliminations; |
| 593 |
eliminations.reserve(last_vertex_id); |
| 594 |
|
| 595 |
// first: eliminate each vertex and remember its permutation order |
| 596 |
for (Size i = 0; i <= last_vertex_id; ++i) |
| 597 |
{ |
| 598 |
size_t index = permutation.first[i]; |
| 599 |
VertexType new_vertex = new_vertices[index]; |
| 600 |
|
| 601 |
permutation_order[index] = i; |
| 602 |
|
| 603 |
if (i < last_vertex_id) |
| 604 |
{ |
| 605 |
eliminations.push_back(GRAPH::eliminateVertexUndoable(new_vertex, graph)); |
| 606 |
} |
| 607 |
} |
| 608 |
|
| 609 |
std::set<typename TreeWidth<OriginalGraphType>::OriginalVertexType> current_content; |
| 610 |
|
| 611 |
// last vertex is root of tree decomposition |
| 612 |
size_t first_index = permutation.first[last_vertex_id]; |
| 613 |
current_content.insert(boost::get(boost::vertex_orig_ptr, graph, new_vertices[first_index])); |
| 614 |
|
| 615 |
for (Position i=0; i<=last_vertex_id; ++i) |
| 616 |
{ |
| 617 |
bags[i] = boost::add_vertex(*tree_graph_); |
| 618 |
} |
| 619 |
|
| 620 |
TreeDecompositionBag& root = bags[last_vertex_id]; |
| 621 |
boost::put(boost::vertex_bag_content, *tree_graph_, root, current_content ); |
| 622 |
boost::put(boost::vertex_bag_type, *tree_graph_, root, TreeWidth<OriginalGraphType>::ROOT_BAG); |
| 623 |
|
| 624 |
// next: de-eliminate each vertex and add it and its neighbours to the tree decomposition |
| 625 |
for (Index i = last_vertex_id - 1; i >= 0; --i) |
| 626 |
{ |
| 627 |
current_content.clear(); |
| 628 |
|
| 629 |
VertexType ro_replace = eliminations[i].undo(); |
| 630 |
new_vertices[boost::get(boost::vertex_index, graph, ro_replace)] = ro_replace; |
| 631 |
|
| 632 |
size_t index = permutation.first[i]; |
| 633 |
VertexType new_vertex = new_vertices[index]; |
| 634 |
|
| 635 |
TreeDecompositionBag bag = bags[i]; |
| 636 |
|
| 637 |
// add it to bag |
| 638 |
current_content.insert(boost::get(boost::vertex_orig_ptr, original_graph, original_vertices[index])); |
| 639 |
|
| 640 |
// add its neighbours to bag and search the neighbour with minimal permutation order |
| 641 |
NeighbourIterator neighbour; |
| 642 |
Size min_permutation_order = last_vertex_id; |
| 643 |
BGL_FORALL_ADJ_T(new_vertex, neighbour, graph, UndirectedGraph) |
| 644 |
{ |
| 645 |
size_t n_index = boost::get(boost::vertex_index, original_graph, neighbour); |
| 646 |
current_content.insert(boost::get(boost::vertex_orig_ptr, original_graph, original_vertices[n_index])); |
| 647 |
|
| 648 |
min_permutation_order = std::min(min_permutation_order, (Size)permutation_order[n_index]); |
| 649 |
|
| 650 |
// the bag with the neighour with minimal permutation order becomes parent of the |
| 651 |
// new created bag |
| 652 |
} |
| 653 |
|
| 654 |
boost::put(boost::vertex_bag_content, *tree_graph_, bag, current_content); |
| 655 |
boost::add_edge(bags[min_permutation_order], bag, *tree_graph_); |
| 656 |
} |
| 657 |
|
| 658 |
tree_ = boost::shared_ptr<TreeDecomposition>(new TreeDecomposition(*tree_graph_, root, |
| 659 |
boost::make_iterator_property_map(parent_map.begin(), |
| 660 |
boost::get(boost::vertex_index, *tree_graph_)))); |
| 661 |
|
| 662 |
return tree_; |
| 663 |
} |
| 664 |
|
| 665 |
template <class UndirectedGraph> |
| 666 |
template <class OriginalGraphType> |
| 667 |
typename TreeWidth<OriginalGraphType>::TreeDecompositionBag |
| 668 |
TreeWidthImplementation<UndirectedGraph>::TreeDecompositionBuilder<OriginalGraphType>::operator() (TreeDecompositionBag n, |
| 669 |
typename std::vector<TreeDecompositionBag>::iterator c_i, |
| 670 |
typename std::vector<TreeDecompositionBag>::iterator c_end) |
| 671 |
{ |
| 672 |
bool is_leaf = (c_i == c_end); |
| 673 |
bool is_root = (n == root(*tree_)); |
| 674 |
|
| 675 |
if (is_root) |
| 676 |
{ |
| 677 |
int node_type = TreeWidth<OriginalGraphType>::ROOT_BAG; |
| 678 |
|
| 679 |
if (is_leaf) // we only have one bag in the original decomposition |
| 680 |
{ |
| 681 |
return buildRoot_(buildLeaf_(n)); |
| 682 |
} |
| 683 |
else |
| 684 |
{ |
| 685 |
return buildSingle_(n, node_type, branch_(n, node_type, c_i, c_end)); |
| 686 |
} |
| 687 |
} |
| 688 |
else if (is_leaf) |
| 689 |
{ |
| 690 |
return buildLeaf_(n); |
| 691 |
} |
| 692 |
else |
| 693 |
{ |
| 694 |
int node_type = TreeWidth<OriginalGraphType>::INNER_BAG; |
| 695 |
|
| 696 |
return buildSingle_(n, node_type, branch_(n, node_type, c_i, c_end)); |
| 697 |
} |
| 698 |
} |
| 699 |
|
| 700 |
template <class UndirectedGraph> |
| 701 |
template <class OriginalGraphType> |
| 702 |
boost::shared_ptr<typename TreeWidthImplementation<UndirectedGraph>:: |
| 703 |
template TreeDecompositionBuilder<OriginalGraphType>::TreeDecomposition> |
| 704 |
TreeWidthImplementation<UndirectedGraph>::TreeDecompositionBuilder<OriginalGraphType>::makeNice( |
| 705 |
boost::shared_ptr<TreeDecompositionGraph>& nice_tree) |
| 706 |
{ |
| 707 |
nice_tree_ = nice_tree; |
| 708 |
|
| 709 |
GRAPH::PostOrderFolding<TreeDecomposition, TreeDecompositionBag, TreeDecompositionBag, |
| 710 |
TreeDecompositionBuilder<OriginalGraphType> > pof(*tree_, *this); |
| 711 |
|
| 712 |
std::vector<TreeDecompositionBag> parent_map(boost::num_vertices(*nice_tree_)); |
| 713 |
|
| 714 |
return boost::shared_ptr<TreeDecomposition>(new TreeDecomposition(*nice_tree_, root_, |
| 715 |
boost::make_iterator_property_map(parent_map.begin(), |
| 716 |
boost::get(boost::vertex_index, *nice_tree_)))); |
| 717 |
} |
| 718 |
|
| 719 |
template <class UndirectedGraph> |
| 720 |
template <class OriginalGraphType> |
| 721 |
typename TreeWidth<OriginalGraphType>::TreeDecompositionBag |
| 722 |
TreeWidthImplementation<UndirectedGraph>::TreeDecompositionBuilder<OriginalGraphType>::buildRoot_(TreeDecompositionBag child) |
| 723 |
{ |
| 724 |
TreeDecompositionBag last_vertex = TreeDecompositionBag(); |
| 725 |
|
| 726 |
TreeDecompositionContent content = boost::get(boost::vertex_bag_content, *nice_tree_, child); |
| 727 |
|
| 728 |
if (content.size() > 1) |
| 729 |
{ |
| 730 |
last_vertex = *content.begin(); |
| 731 |
content.erase(content.begin()); |
| 732 |
|
| 733 |
child = linkWithForgetNodes_(content, child); |
| 734 |
} |
| 735 |
else if (content.size() == 1) |
| 736 |
{ |
| 737 |
last_vertex = *content.begin(); |
| 738 |
} |
| 739 |
|
| 740 |
root_ = boost::add_vertex(*nice_tree_); |
| 741 |
|
| 742 |
boost::put(boost::vertex_bag_type, *nice_tree_, root_, TreeWidth<OriginalGraphType>::ROOT_BAG); |
| 743 |
|
| 744 |
if (content.size() > 0) |
| 745 |
boost::put(boost::vertex_bag_special, *nice_tree_, root_, last_vertex); |
| 746 |
|
| 747 |
boost::add_edge(root_, child, *nice_tree_); |
| 748 |
|
| 749 |
return root_; |
| 750 |
} |
| 751 |
|
| 752 |
template <class UndirectedGraph> |
| 753 |
template <class OriginalGraphType> |
| 754 |
typename TreeWidth<OriginalGraphType>::TreeDecompositionBag |
| 755 |
TreeWidthImplementation<UndirectedGraph>::TreeDecompositionBuilder<OriginalGraphType>::buildLeaf_(TreeDecompositionBag node) |
| 756 |
{ |
| 757 |
TreeDecompositionContent content = boost::get(boost::vertex_bag_content, *tree_, node); |
| 758 |
OriginalVertexType leaf_inner_vertex = *content.begin(); |
| 759 |
content.erase(content.begin()); |
| 760 |
|
| 761 |
TreeDecompositionBag new_vertex = boost::add_vertex(*nice_tree_); |
| 762 |
|
| 763 |
TreeDecompositionContent new_content; |
| 764 |
new_content.insert(leaf_inner_vertex); |
| 765 |
|
| 766 |
boost::put(boost::vertex_bag_content, *nice_tree_, new_vertex, new_content ); |
| 767 |
boost::put(boost::vertex_bag_special, *nice_tree_, new_vertex, leaf_inner_vertex ); |
| 768 |
boost::put(boost::vertex_bag_type, *nice_tree_, new_vertex, TreeWidth<OriginalGraphType>::LEAF_BAG); |
| 769 |
|
| 770 |
return linkWithIntroduceNodes_(content, new_vertex); |
| 771 |
} |
| 772 |
|
| 773 |
template <class UndirectedGraph> |
| 774 |
template <class OriginalGraphType> |
| 775 |
typename TreeWidth<OriginalGraphType>::TreeDecompositionBag |
| 776 |
TreeWidthImplementation<UndirectedGraph>::TreeDecompositionBuilder<OriginalGraphType>::buildJoin_( |
| 777 |
TreeDecompositionBag node, TreeDecompositionBag left, TreeDecompositionBag right, bool do_forget) |
| 778 |
{ |
| 779 |
// first: both children should have identical inner vertex sets |
| 780 |
TreeDecompositionContent parent_set = boost::get(boost::vertex_bag_content, *tree_, node); |
| 781 |
|
| 782 |
if (do_forget) |
| 783 |
{ |
| 784 |
left = linkWithForgetNodes_(parent_set, left); |
| 785 |
right = linkWithForgetNodes_(parent_set, right); |
| 786 |
} |
| 787 |
left = linkWithIntroduceNodes_(boost::get(boost::vertex_bag_content, *nice_tree_, right), left); |
| 788 |
right = linkWithIntroduceNodes_(boost::get(boost::vertex_bag_content, *nice_tree_, left), right); |
| 789 |
|
| 790 |
// now both children should be equal |
| 791 |
TreeDecompositionBag join = boost::add_vertex(*nice_tree_); |
| 792 |
|
| 793 |
boost::put(boost::vertex_bag_content, *nice_tree_, join, boost::get(boost::vertex_bag_content, *nice_tree_, left)); |
| 794 |
boost::put(boost::vertex_bag_type, *nice_tree_, join, TreeWidth<OriginalGraphType>::JOIN_BAG ); |
| 795 |
|
| 796 |
boost::add_edge(join, left, *nice_tree_); |
| 797 |
boost::add_edge(join, right, *nice_tree_); |
| 798 |
|
| 799 |
return join; |
| 800 |
} |
| 801 |
|
| 802 |
template <class UndirectedGraph> |
| 803 |
template <class OriginalGraphType> |
| 804 |
typename TreeWidth<OriginalGraphType>::TreeDecompositionBag |
| 805 |
TreeWidthImplementation<UndirectedGraph>::TreeDecompositionBuilder<OriginalGraphType>::buildSingle_( |
| 806 |
TreeDecompositionBag node, int node_type, TreeDecompositionBag child) |
| 807 |
{ |
| 808 |
if (node_type == TreeWidth<OriginalGraphType>::ROOT_BAG) |
| 809 |
{ |
| 810 |
return buildRoot_(buildLinkage_(node, child)); |
| 811 |
} |
| 812 |
else if (node_type == TreeWidth<OriginalGraphType>::INNER_BAG) |
| 813 |
{ |
| 814 |
return buildLinkage_(node, child); |
| 815 |
} |
| 816 |
else |
| 817 |
// this should not happen... |
| 818 |
return TreeDecompositionBag(); |
| 819 |
} |
| 820 |
|
| 821 |
template <class UndirectedGraph> |
| 822 |
template <class OriginalGraphType> |
| 823 |
typename TreeWidth<OriginalGraphType>::TreeDecompositionBag |
| 824 |
TreeWidthImplementation<UndirectedGraph>::TreeDecompositionBuilder<OriginalGraphType>::buildLinkage_( |
| 825 |
TreeDecompositionBag node, TreeDecompositionBag child) |
| 826 |
{ |
| 827 |
return linkWithIntroduceNodes_(boost::get(boost::vertex_bag_content, *tree_, node), |
| 828 |
linkWithForgetNodes_(boost::get(boost::vertex_bag_content, *tree_, node), child)); |
| 829 |
} |
| 830 |
|
| 831 |
template <class UndirectedGraph> |
| 832 |
template <class OriginalGraphType> |
| 833 |
typename TreeWidth<OriginalGraphType>::TreeDecompositionBag |
| 834 |
TreeWidthImplementation<UndirectedGraph>::TreeDecompositionBuilder<OriginalGraphType>::linkWithIntroduceNodes_( |
| 835 |
TreeDecompositionContent parent_set, TreeDecompositionBag child) |
| 836 |
{ |
| 837 |
TreeDecompositionContent child_set = boost::get(boost::vertex_bag_content, *nice_tree_, child); |
| 838 |
TreeDecompositionBag current_parent = child; |
| 839 |
|
| 840 |
// for each vertex, which exists in parent and doesn't exist in child |
| 841 |
for (typename TreeDecompositionContent::iterator iter = parent_set.begin(); iter != parent_set.end(); ++iter) |
| 842 |
{ |
| 843 |
OriginalVertexType vertex = *iter; |
| 844 |
|
| 845 |
if (std::find(child_set.begin(), child_set.end(), vertex) == child_set.end()) |
| 846 |
{ |
| 847 |
// insert introduce node |
| 848 |
TreeDecompositionBag new_parent = boost::add_vertex(*nice_tree_); |
| 849 |
|
| 850 |
TreeDecompositionContent current_parent_set = boost::get(boost::vertex_bag_content, *nice_tree_, current_parent); |
| 851 |
current_parent_set.insert(vertex); |
| 852 |
|
| 853 |
boost::put(boost::vertex_bag_content, *nice_tree_, new_parent, current_parent_set ); |
| 854 |
boost::put(boost::vertex_bag_type, *nice_tree_, new_parent, TreeWidth<OriginalGraphType>::INTRODUCE_BAG); |
| 855 |
boost::put(boost::vertex_bag_special, *nice_tree_, new_parent, vertex ); |
| 856 |
|
| 857 |
boost::add_edge(new_parent, current_parent, *nice_tree_); |
| 858 |
|
| 859 |
current_parent = new_parent; |
| 860 |
} |
| 861 |
} |
| 862 |
|
| 863 |
return current_parent; |
| 864 |
} |
| 865 |
|
| 866 |
template <class UndirectedGraph> |
| 867 |
template <class OriginalGraphType> |
| 868 |
typename TreeWidth<OriginalGraphType>::TreeDecompositionBag |
| 869 |
TreeWidthImplementation<UndirectedGraph>::TreeDecompositionBuilder<OriginalGraphType>::linkWithForgetNodes_( |
| 870 |
TreeDecompositionContent parent_set, TreeDecompositionBag child) |
| 871 |
{ |
| 872 |
TreeDecompositionContent child_set = boost::get(boost::vertex_bag_content, *nice_tree_, child); |
| 873 |
|
| 874 |
TreeDecompositionBag current_parent = child; |
| 875 |
|
| 876 |
// for each vertex, which exists in child and doesn't exist in parent |
| 877 |
for (typename TreeDecompositionContent::iterator iter = child_set.begin(); iter != child_set.end(); ++iter) |
| 878 |
{ |
| 879 |
OriginalVertexType vertex = *iter; |
| 880 |
if (std::find(parent_set.begin(), parent_set.end(), vertex) == parent_set.end()) |
| 881 |
{ |
| 882 |
// insert forget node |
| 883 |
TreeDecompositionBag new_parent = boost::add_vertex(*nice_tree_); |
| 884 |
|
| 885 |
TreeDecompositionContent new_content = boost::get(boost::vertex_bag_content, *nice_tree_, current_parent); |
| 886 |
typename TreeDecompositionContent::iterator vertex_it = std::find(new_content.begin(), new_content.end(), vertex); |
| 887 |
new_content.erase(vertex_it); |
| 888 |
|
| 889 |
boost::put(boost::vertex_bag_content, *nice_tree_, new_parent, new_content ); |
| 890 |
boost::put(boost::vertex_bag_type, *nice_tree_, new_parent, TreeWidth<OriginalGraphType>::FORGET_BAG); |
| 891 |
boost::put(boost::vertex_bag_special, *nice_tree_, new_parent, vertex ); |
| 892 |
|
| 893 |
boost::add_edge(new_parent, current_parent, *nice_tree_); |
| 894 |
|
| 895 |
current_parent = new_parent; |
| 896 |
} |
| 897 |
} |
| 898 |
|
| 899 |
return current_parent; |
| 900 |
} |
| 901 |
|
| 902 |
template <class UndirectedGraph> |
| 903 |
template <class OriginalGraphType> |
| 904 |
typename TreeWidth<OriginalGraphType>::TreeDecompositionBag |
| 905 |
TreeWidthImplementation<UndirectedGraph>::TreeDecompositionBuilder<OriginalGraphType>::branch_(TreeDecompositionBag node, |
| 906 |
int node_type, |
| 907 |
typename std::vector<TreeDecompositionBag>::iterator begin, |
| 908 |
typename std::vector<TreeDecompositionBag>::iterator end) |
| 909 |
{ |
| 910 |
size_t length = distance(begin, end); |
| 911 |
|
| 912 |
if (length == 1) |
| 913 |
{ |
| 914 |
return *begin; |
| 915 |
} |
| 916 |
else if (length == 2) |
| 917 |
{ |
| 918 |
typename std::vector<TreeDecompositionBag>::iterator now = begin; |
| 919 |
++begin; |
| 920 |
return buildJoin_(node, *now, *begin, true); |
| 921 |
} |
| 922 |
else |
| 923 |
{ |
| 924 |
typename std::vector<TreeDecompositionBag>::iterator middle = begin + length/2; |
| 925 |
return buildJoin_(node, branch_(node, node_type, begin, middle), |
| 926 |
branch_(node, node_type, middle, end), length < 4); |
| 927 |
} |
| 928 |
|
| 929 |
return TreeDecompositionBag(); |
| 930 |
} |
| 931 |
|
| 932 |
} |