BALL  1.4.79
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
treeWidth.h
Go to the documentation of this file.
1 // -*- Mode: C++; tab-width: 2; -*-
2 // vi: set ts=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
18 #endif
19 
20 #ifndef BALL_DATATYPE_GRAPH_GRAPHALGORITHMS_H
22 #endif
23 
24 #ifndef 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 {
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;
58  template <class UndirectedGraph>
59  class TreeWidth
60  {
61  public:
65  enum BagType {
94  };
95 
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>,
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>
112  typedef boost::graph_as_tree<TreeDecompositionGraph, TreeDecompositionParentMap> TreeDecomposition;
113 
114  TreeWidth(UndirectedGraph const& input);
115 
120  static Size computeTreeWidth(TreeDecomposition const& td);
121 
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>
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_;
148  };
149 
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:
164  UndirectedGraph const* original_graph_;
165  };
166 
167  // TODO: would UndirectedGraph suffice here?
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>
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 
191  typedef std::pair<std::vector<Size>, Size> EliminationOrder;
192 
203  template<class Criterion, class Reducer>
205  : public UnaryFunctor<UndirectedGraph, Size>
206  {
207  public:
209 
210  virtual Size operator() (UndirectedGraph const& originalGraph);
211  };
212 
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 
235  {
236  public:
237  MinorMinWidthCriterion(UndirectedGraph const& graph);
238 
239  Size operator() (VertexType& vertex) const;
240 
241  protected:
242  UndirectedGraph const& graph_;
243  };
244 
261  /*template <class UndirectedGraph>
262  class MinorMinWidth
263  : public GeneralLowerBoundAlgorithm<UndirectedGraph, MinorMinWidthCriterion<UndirectedGraph>,
264  MinorMinWidthReducer<UndirectedGraph> >
265  {
266  };
267  */
269 
270 
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 
300  {
301  VertexType& operator() (UndirectedGraph& graph);
302 
303  Size edgeIncreaseByEliminating(VertexIterator vertex, UndirectedGraph& graph);
304  };
305 
315  template <class Lowerbound=MinorMinWidth, class Upperbound=GreedyX<FillInHeuristic> >
316  class QuickBB
317  {
318  public:
325  {
329  };
330 
334  QuickBB(UndirectedGraph const& graph);
335 
340 
341  SIMPLICIAL_TYPE isSimplicial(VertexType& vertex) const;
342 
343  protected:
348  {
352  unsigned int g;
353 
357  unsigned int h;
358 
362  unsigned int f;
363 
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 
378  UndirectedGraph graph_;
379 
384 
389 
394 
400 
407 
417  void branchAndBound(QuickBBState& nstate);
418 
425  void prune(QuickBBState&);
426 
430  BitSet buildBitset() const;
431 
432  protected:
433  std::map<int, VertexType> index_to_vertex_;
434  };
435 
446 
447  template <class OriginalGraphType>
449  {
450  public:
454 
456 
457  typedef std::set<OriginalVertexType> TreeDecompositionContent;
458 
465  boost::shared_ptr<TreeDecomposition> operator() (UndirectedGraph const& graph, EliminationOrder const& permutation);
466 
476  boost::shared_ptr<TreeDecomposition> makeNice(boost::shared_ptr<TreeDecompositionGraph>& nice_tree);
477 
479  typename std::vector<TreeDecompositionBag>::iterator c_i, typename std::vector<TreeDecompositionBag>::iterator c_end);
480 
481  protected:
485  TreeDecompositionBag right, bool do_forget);
486 
488  TreeDecompositionBag child);
489 
491 
494 
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 
504  };
505 
506  };
507 }
508 
509 #include <BALL/DATATYPE/GRAPH/treeWidth.iC>
510 
511 #endif // BALL_DATATYPE_GRAPH_TREEWIDTH_H
TreeDecompositionBag buildRoot_(TreeDecompositionBag child)
Generic lower bound algorithm on graphs.
Definition: treeWidth.h:204
std::pair< BitSet, Size > MapEntry
Definition: treeWidth.h:373
UndirectedGraph const * original_graph_
Definition: treeWidth.h:164
std::vector< boost::shared_ptr< TreeDecomposition > > nice_tree_decompositions_
Definition: treeWidth.h:172
TreeWidth< OriginalGraphType >::OriginalVertexType OriginalVertexType
Definition: treeWidth.h:455
TreeDecompositionBag linkWithIntroduceNodes_(TreeDecompositionContent parent_set, TreeDecompositionBag child)
TreeDecompositionBag buildSingle_(TreeDecompositionBag node, int node_type, TreeDecompositionBag child)
std::map< VertexType, bool > BitSet
Definition: treeWidth.h:370
boost::shared_ptr< TreeDecompositionGraph > tree_graph_
Definition: treeWidth.h:500
SIMPLICIAL_TYPE isSimplicial(VertexType &vertex) const
boost::shared_ptr< TreeDecomposition > makeNice(boost::shared_ptr< TreeDecompositionGraph > &nice_tree)
Size edgeIncreaseByEliminating(VertexIterator vertex, UndirectedGraph &graph)
std::set< OriginalVertexType > TreeDecompositionContent
Definition: treeWidth.h:99
TreeWidth< OriginalGraphType >::TreeDecompositionGraph TreeDecompositionGraph
Definition: treeWidth.h:453
boost::graph_traits< UndirectedGraph >::vertex_descriptor VertexType
Definition: treeWidth.h:181
vertex_bag_content_t
Definition: treeWidth.h:43
std::set< OriginalVertexType > TreeDecompositionContent
Definition: treeWidth.h:457
VertexType & operator()(UndirectedGraph &graph)
boost::iterator_property_map< typename std::vector< TreeDecompositionBag >::iterator, typename boost::property_map< TreeDecompositionGraph, boost::vertex_index_t >::type > TreeDecompositionParentMap
Definition: treeWidth.h:111
std::vector< boost::shared_ptr< TreeDecompositionGraph > > nice_tree_decomposition_graphs_
Definition: treeWidth.h:173
boost::graph_traits< UndirectedGraph >::vertex_iterator VertexIterator
Definition: treeWidth.h:183
TreeWidth(UndirectedGraph const &input)
std::pair< typename GraphMap::iterator, bool > MapPos
Definition: treeWidth.h:372
std::pair< std::vector< Size >, Size > EliminationOrder
Definition: treeWidth.h:191
vertex_bag_type_t
Definition: treeWidth.h:45
boost::graph_as_tree< TreeDecompositionGraph, TreeDecompositionParentMap > TreeDecomposition
Definition: treeWidth.h:112
std::vector< boost::shared_ptr< TreeDecomposition > > & getNiceTreeDecompositions()
Definition: treeWidth.h:127
BagContentWriter(TreeDecomposition const *td, UndirectedGraph const *original_graph)
Definition: treeWidth.h:155
virtual Size operator()(UndirectedGraph const &originalGraph)
void contractEdge(VertexType &u, VertexType &v)
boost::shared_ptr< TreeDecomposition > tree_
Definition: treeWidth.h:499
ComponentFilter_(ComponentMap cm, Position i)
Definition: treeWidth.h:134
MinorMinWidthCriterion(UndirectedGraph const &graph)
boost::graph_traits< UndirectedGraph >::vertex_descriptor OriginalVertexType
Definition: treeWidth.h:97
boost::shared_ptr< TreeDecomposition > operator()(UndirectedGraph const &graph, EliminationOrder const &permutation)
TreeDecompositionBag buildJoin_(TreeDecompositionBag node, TreeDecompositionBag left, TreeDecompositionBag right, bool do_forget)
boost::shared_ptr< TreeDecompositionGraph > nice_tree_
Definition: treeWidth.h:501
std::vector< boost::shared_ptr< EditableGraph > > components_
Definition: treeWidth.h:170
TreeWidth< OriginalGraphType >::TreeDecomposition TreeDecomposition
Definition: treeWidth.h:451
void branchAndBound(QuickBBState &nstate)
TreeDecompositionBag linkWithForgetNodes_(TreeDecompositionContent parent_set, TreeDecompositionBag child)
TreeDecompositionBag buildLeaf_(TreeDecompositionBag child)
boost::graph_traits< TreeDecompositionGraph >::vertex_descriptor TreeDecompositionBag
Definition: treeWidth.h:107
TreeWidth< OriginalGraphType >::TreeDecompositionBag TreeDecompositionBag
Definition: treeWidth.h:452
static Size computeTreeWidth(TreeDecomposition const &td)
GRAPH::GraphTraits< UndirectedGraph >::EditableGraph EditableGraph
Definition: treeWidth.h:96
std::map< BitSet, Size > GraphMap
Definition: treeWidth.h:371
vertex_bag_special_t
Definition: treeWidth.h:44
std::map< int, VertexType > index_to_vertex_
Definition: treeWidth.h:433
boost::adjacency_list< boost::vecS, boost::vecS, boost::directedS, boost::property< boost::vertex_bag_content_t, std::set< OriginalVertexType >, boost::property< boost::vertex_bag_special_t, OriginalVertexType, boost::property< boost::vertex_bag_type_t, int > > >, boost::no_property > TreeDecompositionGraph
Definition: treeWidth.h:105
bool operator()(const Vertex &e) const
Definition: treeWidth.h:140
MolecularGraph const * input_
Definition: treeWidth.h:168
GeneralLowerBoundAlgorithm< MinorMinWidthCriterion, MinorMinWidthReducer > MinorMinWidth
Definition: treeWidth.h:268
void writeGraphvizFile(std::ostream &out, TreeDecomposition const &td)
EliminationOrder operator()(UndirectedGraph &original_graph)
std::vector< boost::shared_ptr< EditableGraph > > & getComponents()
Definition: treeWidth.h:126
TreeDecomposition const * td_
Definition: treeWidth.h:163
BOOST_INSTALL_PROPERTY(vertex, atom_ptr)
TreeDecompositionBag branch_(TreeDecompositionBag node, int node_type, typename std::vector< TreeDecompositionBag >::iterator begin, typename std::vector< TreeDecompositionBag >::iterator end)
GreedyX< FillInHeuristic > GreedyFillIn
Definition: treeWidth.h:445
TreeDecompositionBag buildLinkage_(TreeDecompositionBag node, TreeDecompositionBag child)
boost::graph_traits< UndirectedGraph >::adjacency_iterator NeighbourIterator
Definition: treeWidth.h:182
QuickBB(UndirectedGraph const &graph)
boost::adjacency_list< boost::listS, boost::listS, boost::undirectedS, boost::property< boost::vertex_orig_ptr_t, VertexType, boost::property< boost::vertex_index_t, int > >, boost::property< boost::edge_orig_ptr_t, EdgeType > > EditableGraph
void operator()(std::ostream &out, const TreeDecompositionBag &v) const