| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
#ifndef BALL_DATATYPE_GRAPH_TREEWIDTH_H |
| 6 |
#define BALL_DATATYPE_GRAPH_TREEWIDTH_H |
| 7 |
|
| 8 |
#ifndef BALL_COMMON_GLOBAL_H |
| 9 |
# include <BALL/COMMON/global.h> |
| 10 |
#endif |
| 11 |
|
| 12 |
#ifndef BALL_COMMON_EXCEPTION_H |
| 13 |
# include <BALL/COMMON/exception.h> |
| 14 |
#endif |
| 15 |
|
| 16 |
#ifndef BALL_CONCEPT_BASEFUNCTOR_H |
| 17 |
# include <BALL/CONCEPT/baseFunctor.h> |
| 18 |
#endif |
| 19 |
|
| 20 |
#ifndef BALL_DATATYPE_GRAPH_GRAPHALGORITHMS_H |
| 21 |
# include <BALL/DATATYPE/GRAPH/graphAlgorithms.h> |
| 22 |
#endif |
| 23 |
|
| 24 |
#ifndef BALL_DATATYPE_GRAPH_MOLECULARGRAPH_H |
| 25 |
# include <BALL/DATATYPE/GRAPH/molecularGraph.h> |
| 26 |
#endif |
| 27 |
|
| 28 |
#include <algorithm> |
| 29 |
#include <functional> |
| 30 |
#include <map> |
| 31 |
#include <set> |
| 32 |
#include <vector> |
| 33 |
#include <iostream> |
| 34 |
|
| 35 |
#include <boost/graph/connected_components.hpp> |
| 36 |
#include <boost/graph/filtered_graph.hpp> |
| 37 |
#include <boost/graph/graph_as_tree.hpp> |
| 38 |
#include <boost/graph/graphviz.hpp> |
| 39 |
#include <boost/graph/copy.hpp> |
| 40 |
|
| 41 |
namespace boost |
| 42 |
{ |
| 43 |
enum vertex_bag_content_t { vertex_bag_content }; |
| 44 |
enum vertex_bag_special_t { vertex_bag_special }; |
| 45 |
enum vertex_bag_type_t { vertex_bag_type }; |
| 46 |
|
| 47 |
BOOST_INSTALL_PROPERTY(vertex, bag_content); |
| 48 |
BOOST_INSTALL_PROPERTY(vertex, bag_special); |
| 49 |
BOOST_INSTALL_PROPERTY(vertex, bag_type); |
| 50 |
} |
| 51 |
|
| 52 |
namespace BALL |
| 53 |
{ |
| 54 |
template <class EditableGraph> class TreeWidthImplementation; |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
template <class UndirectedGraph> |
| 59 |
class TreeWidth |
| 60 |
{ |
| 61 |
public: |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
enum BagType { |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
INTRODUCE_BAG, |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
LEAF_BAG, |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
FORGET_BAG, |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
ROOT_BAG, |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
JOIN_BAG, |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
INNER_BAG, |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
END_BAG |
| 94 |
}; |
| 95 |
|
| 96 |
typedef typename GRAPH::GraphTraits<UndirectedGraph>::EditableGraph EditableGraph; |
| 97 |
typedef typename boost::graph_traits<UndirectedGraph>::vertex_descriptor OriginalVertexType; |
| 98 |
|
| 99 |
typedef std::set<OriginalVertexType> TreeDecompositionContent; |
| 100 |
|
| 101 |
typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::directedS, |
| 102 |
boost::property<boost::vertex_bag_content_t, std::set<OriginalVertexType>, |
| 103 |
boost::property<boost::vertex_bag_special_t, OriginalVertexType, |
| 104 |
boost::property<boost::vertex_bag_type_t, int> > >, |
| 105 |
boost::no_property> TreeDecompositionGraph; |
| 106 |
|
| 107 |
typedef typename boost::graph_traits<TreeDecompositionGraph>::vertex_descriptor TreeDecompositionBag; |
| 108 |
|
| 109 |
typedef boost::iterator_property_map<typename std::vector<TreeDecompositionBag>::iterator, |
| 110 |
typename boost::property_map<TreeDecompositionGraph, boost::vertex_index_t>::type> |
| 111 |
TreeDecompositionParentMap; |
| 112 |
typedef boost::graph_as_tree<TreeDecompositionGraph, TreeDecompositionParentMap> TreeDecomposition; |
| 113 |
|
| 114 |
TreeWidth(UndirectedGraph const& input); |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
static Size computeTreeWidth(TreeDecomposition const& td); |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
void writeGraphvizFile(std::ostream& out, TreeDecomposition const& td); |
| 125 |
|
| 126 |
std::vector<boost::shared_ptr<EditableGraph> >& getComponents() { return components_; } |
| 127 |
std::vector<boost::shared_ptr<TreeDecomposition> >& getNiceTreeDecompositions() { return nice_tree_decompositions_; } |
| 128 |
|
| 129 |
protected: |
| 130 |
template <typename ComponentMap> |
| 131 |
class ComponentFilter_ |
| 132 |
{ |
| 133 |
public: |
| 134 |
ComponentFilter_(ComponentMap cm, Position i) |
| 135 |
: cm_(cm), |
| 136 |
component_(i) |
| 137 |
{ } |
| 138 |
|
| 139 |
template <typename Vertex> |
| 140 |
bool operator() (const Vertex& e) const |
| 141 |
{ |
| 142 |
return ((cm_)[e] == component_); |
| 143 |
} |
| 144 |
|
| 145 |
protected: |
| 146 |
ComponentMap cm_; |
| 147 |
Position component_; |
| 148 |
}; |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
class BagContentWriter |
| 153 |
{ |
| 154 |
public: |
| 155 |
BagContentWriter(TreeDecomposition const* td, UndirectedGraph const* original_graph) |
| 156 |
: td_(td), |
| 157 |
original_graph_(original_graph) |
| 158 |
{ } |
| 159 |
|
| 160 |
void operator() (std::ostream& out, const TreeDecompositionBag& v) const; |
| 161 |
|
| 162 |
protected: |
| 163 |
TreeDecomposition const* td_; |
| 164 |
UndirectedGraph const* original_graph_; |
| 165 |
}; |
| 166 |
|
| 167 |
|
| 168 |
MolecularGraph const* input_; |
| 169 |
|
| 170 |
std::vector<boost::shared_ptr<EditableGraph> > components_; |
| 171 |
|
| 172 |
std::vector<boost::shared_ptr<TreeDecomposition> > nice_tree_decompositions_; |
| 173 |
std::vector<boost::shared_ptr<TreeDecompositionGraph> > nice_tree_decomposition_graphs_; |
| 174 |
}; |
| 175 |
|
| 176 |
template <class UndirectedGraph> |
| 177 |
class TreeWidthImplementation |
| 178 |
{ |
| 179 |
public: |
| 180 |
|
| 181 |
typedef typename boost::graph_traits<UndirectedGraph>::vertex_descriptor VertexType; |
| 182 |
typedef typename boost::graph_traits<UndirectedGraph>::adjacency_iterator NeighbourIterator; |
| 183 |
typedef typename boost::graph_traits<UndirectedGraph>::vertex_iterator VertexIterator; |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
typedef std::pair<std::vector<Size>, Size> EliminationOrder; |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
template<class Criterion, class Reducer> |
| 204 |
class GeneralLowerBoundAlgorithm |
| 205 |
: public UnaryFunctor<UndirectedGraph, Size> |
| 206 |
{ |
| 207 |
public: |
| 208 |
GeneralLowerBoundAlgorithm() {} |
| 209 |
|
| 210 |
virtual Size operator() (UndirectedGraph const& originalGraph); |
| 211 |
}; |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
class MinorMinWidthReducer |
| 220 |
{ |
| 221 |
public: |
| 222 |
MinorMinWidthReducer(UndirectedGraph& graph); |
| 223 |
|
| 224 |
void operator() (VertexType& vertex); |
| 225 |
void contractEdge(VertexType& u, VertexType& v); |
| 226 |
|
| 227 |
protected: |
| 228 |
UndirectedGraph& graph_; |
| 229 |
}; |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
class MinorMinWidthCriterion |
| 235 |
{ |
| 236 |
public: |
| 237 |
MinorMinWidthCriterion(UndirectedGraph const& graph); |
| 238 |
|
| 239 |
Size operator() (VertexType& vertex) const; |
| 240 |
|
| 241 |
protected: |
| 242 |
UndirectedGraph const& graph_; |
| 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 |
typedef GeneralLowerBoundAlgorithm<MinorMinWidthCriterion, MinorMinWidthReducer> MinorMinWidth; |
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
template<class Criterion> |
| 287 |
class GreedyX |
| 288 |
: public UnaryFunctor<UndirectedGraph, typename std::pair< |
| 289 |
std::vector<boost::graph_traits<typename UndirectedGraph::vertex_descriptor> >, Size> > |
| 290 |
{ |
| 291 |
public: |
| 292 |
EliminationOrder operator() (UndirectedGraph& original_graph); |
| 293 |
}; |
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
struct FillInHeuristic |
| 300 |
{ |
| 301 |
VertexType& operator() (UndirectedGraph& graph); |
| 302 |
|
| 303 |
Size edgeIncreaseByEliminating(VertexIterator vertex, UndirectedGraph& graph); |
| 304 |
}; |
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
template <class Lowerbound=MinorMinWidth, class Upperbound=GreedyX<FillInHeuristic> > |
| 316 |
class QuickBB |
| 317 |
{ |
| 318 |
public: |
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
enum SIMPLICIAL_TYPE |
| 325 |
{ |
| 326 |
NOT_SIMPLICIAL, |
| 327 |
ALMOST_SIMPLICIAL, |
| 328 |
IS_SIMPLICIAL |
| 329 |
}; |
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
QuickBB(UndirectedGraph const& graph); |
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
EliminationOrder compute(); |
| 340 |
|
| 341 |
SIMPLICIAL_TYPE isSimplicial(VertexType& vertex) const; |
| 342 |
|
| 343 |
protected: |
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
struct QuickBBState |
| 348 |
{ |
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
unsigned int g; |
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
unsigned int h; |
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
unsigned int f; |
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
std::vector<Size> permutation; |
| 368 |
}; |
| 369 |
|
| 370 |
typedef std::map<VertexType, bool> BitSet; |
| 371 |
typedef std::map<BitSet, Size> GraphMap; |
| 372 |
typedef std::pair<typename GraphMap::iterator, bool> MapPos; |
| 373 |
typedef std::pair<BitSet, Size> MapEntry; |
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
UndirectedGraph graph_; |
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
QuickBBState state; |
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
EliminationOrder greedy_solution; |
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
EliminationOrder own_solution; |
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
GraphMap visitedSubgraphs; |
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
Size upper_bound; |
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
void branchAndBound(QuickBBState& nstate); |
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
void prune(QuickBBState&); |
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
BitSet buildBitset() const; |
| 431 |
|
| 432 |
protected: |
| 433 |
std::map<int, VertexType> index_to_vertex_; |
| 434 |
}; |
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
typedef GreedyX<FillInHeuristic> GreedyFillIn; |
| 446 |
|
| 447 |
template <class OriginalGraphType> |
| 448 |
class TreeDecompositionBuilder |
| 449 |
{ |
| 450 |
public: |
| 451 |
typedef typename TreeWidth<OriginalGraphType>::TreeDecomposition TreeDecomposition; |
| 452 |
typedef typename TreeWidth<OriginalGraphType>::TreeDecompositionBag TreeDecompositionBag; |
| 453 |
typedef typename TreeWidth<OriginalGraphType>::TreeDecompositionGraph TreeDecompositionGraph; |
| 454 |
|
| 455 |
typedef typename TreeWidth<OriginalGraphType>::OriginalVertexType OriginalVertexType; |
| 456 |
|
| 457 |
typedef std::set<OriginalVertexType> TreeDecompositionContent; |
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
boost::shared_ptr<TreeDecomposition> operator() (UndirectedGraph const& graph, EliminationOrder const& permutation); |
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
boost::shared_ptr<TreeDecomposition> makeNice(boost::shared_ptr<TreeDecompositionGraph>& nice_tree); |
| 477 |
|
| 478 |
TreeDecompositionBag operator() (TreeDecompositionBag n, |
| 479 |
typename std::vector<TreeDecompositionBag>::iterator c_i, typename std::vector<TreeDecompositionBag>::iterator c_end); |
| 480 |
|
| 481 |
protected: |
| 482 |
TreeDecompositionBag buildRoot_(TreeDecompositionBag child); |
| 483 |
TreeDecompositionBag buildLeaf_(TreeDecompositionBag child); |
| 484 |
TreeDecompositionBag buildJoin_(TreeDecompositionBag node, TreeDecompositionBag left, |
| 485 |
TreeDecompositionBag right, bool do_forget); |
| 486 |
|
| 487 |
TreeDecompositionBag buildSingle_(TreeDecompositionBag node, int node_type, |
| 488 |
TreeDecompositionBag child); |
| 489 |
|
| 490 |
TreeDecompositionBag buildLinkage_(TreeDecompositionBag node, TreeDecompositionBag child); |
| 491 |
|
| 492 |
TreeDecompositionBag linkWithIntroduceNodes_(TreeDecompositionContent parent_set, TreeDecompositionBag child); |
| 493 |
TreeDecompositionBag linkWithForgetNodes_ (TreeDecompositionContent parent_set, TreeDecompositionBag child); |
| 494 |
|
| 495 |
TreeDecompositionBag branch_(TreeDecompositionBag node, int node_type, |
| 496 |
typename std::vector<TreeDecompositionBag>::iterator begin, |
| 497 |
typename std::vector<TreeDecompositionBag>::iterator end); |
| 498 |
|
| 499 |
boost::shared_ptr<TreeDecomposition> tree_; |
| 500 |
boost::shared_ptr<TreeDecompositionGraph> tree_graph_; |
| 501 |
boost::shared_ptr<TreeDecompositionGraph> nice_tree_; |
| 502 |
|
| 503 |
TreeDecompositionBag root_; |
| 504 |
}; |
| 505 |
|
| 506 |
}; |
| 507 |
} |
| 508 |
|
| 509 |
#include <BALL/DATATYPE/GRAPH/treeWidth.iC> |
| 510 |
|
| 511 |
#endif |