Debian Codegraph @

include/BALL/DATATYPE/GRAPH/treeWidth.h

raw symbols SCIP
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
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 /** This class computes a minimal tree decomposition for the given input graph.
56 *
57 */
58 template <class UndirectedGraph>
59 class TreeWidth
60 {
61 public:
62 /**
63 * The type of this bag
64 */
65 enum BagType {
66 /**
67 * Introduce bags differs from their childs in exactly one new vertex
68 */
69 INTRODUCE_BAG,
70 /**
71 * Leaf bags contains just one vertex and have no childs
72 */
73 LEAF_BAG,
74 /**
75 * Forget bags contain one vertex less than their children
76 */
77 FORGET_BAG,
78 /**
79 * Root bags have an empty vertex set
80 */
81 ROOT_BAG,
82 /**
83 * Join bags have two children, which have both the same inner vertices as their parent
84 */
85 JOIN_BAG,
86 /**
87 * Inner bags are any kind of inner node in the tree, i.e., JOIN, INTRODUCE, or FORGET nodes
88 */
89 INNER_BAG,
90 /**
91 * End bags aren't defined, so you can use them as null-value or as placeholder
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 /** Compute the tree width of a given tree decomposition.
117 * This function iterates over all nodes in the graph to determine the tree width,
118 * i.e., the (maximum number of vertices over all bags) - 1
119 */
120 static Size computeTreeWidth(TreeDecomposition const& td);
121
122 /** Write a tree decomposition in graphviz format.
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 /** PropertyWriter for graphviz output.
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 // TODO: would UndirectedGraph suffice here?
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 * An EliminationOrder is a permutation of vertices of a graph which can be used to build
187 * a Fill-In-Graph. The TreeDecomposition of such a graph is a minimal Tree-Decomposition of the source graph,
188 * if the elimination order is perfect.
189 * first is the permutation of vertices, second is the tree width of the Three-Decomposition of such a Fill-In graph
190 */
191 typedef std::pair<std::vector<Size>, Size> EliminationOrder;
192
193 /**
194 * @brief Generic lower bound algorithm on graphs.
195 *
196 * This class defines a base that can be extended to many different lower bound algorithms that
197 * have the same procedure: Find the best vertex by a special criterion, reduce
198 * the graph size by one, and repeat these two steps until the graph has only one vertex.
199 * @tparam UndirectedGraph the graph type
200 * @tparam Criterion the criterium which chooses the next vertex
201 * @tparam Reducer the reducer which removes a vertex from the graph to reduce it's size
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 * Contract the edge of the vertex which fits the MinorMinWidthCriterion with
215 * a neighbour with minimum degree
216 * @tparam UndirectedGraph the graph type
217 * @throw BALL::GRAPH::UnconnectedGraphException if called on unconnected graphs
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 * search for a vertex with minimum degree
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 * Minor-Min-Width is a lowerbound algorithm for computing the
247 * treewidth.
248 * It contracts in each step a vertex u, which has minimum degree in graph,
249 * with a vertex v, which has a minimum degree in u's neighbourhood. The
250 * maximum of the minimum degrees is the lowerbound for the treewidth.
251 *
252 * The basic idea behind this algorithm is:
253 * - the treewidth of a graph is never lower than the treewidth of its minor
254 * - the treewidth of a graph is never lower than the minimum degree of its vertices
255 * (because you can always find an optimal tree decomposition which contains a leaf,
256 * which has at least one vertex v, which isn't contained in the parents vertex set.
257 * For each edge of this vertex v, there must be a vertex in the leaf. So degree(v)
258 * is a minimal treewidth of this tree decomposition!)
259 * @throw BALL::GRAPH::UnconnectedGraphException if called on unconnected graphs
260 */
261 /*template <class UndirectedGraph>
262 class MinorMinWidth
263 : public GeneralLowerBoundAlgorithm<UndirectedGraph, MinorMinWidthCriterion<UndirectedGraph>,
264 MinorMinWidthReducer<UndirectedGraph> >
265 {
266 };
267 */
268 typedef GeneralLowerBoundAlgorithm<MinorMinWidthCriterion, MinorMinWidthReducer> MinorMinWidth;
269
270
271 /**
272 * Algorithm which can be extended to different upperbound heuristics which follow the
273 * same procedure: Finding a vertex v by a special criterium, insert this vertex
274 * in the elimination order and then eliminate it in the graph.
275 *
276 * The basic idea is to build a Fill-In-Graph. Such a Fill-In-Graph can be produced by
277 * eliminating the vertices of a graph and adding the edges, which appear by eliminating, into
278 * the Fill-In-Graph. If the vertices were eliminated in the correct order, the tree decomposition of the
279 * Fill-In-Graph is also the optimal tree decomposition of the source graph. Otherwise, it's a tree decomposition
280 * with higher treewidth which can be used as upperbound. The order of eliminating is called EliminationOrder.
281 * Each GreedyX algorithm returns the EliminationOrder with the vertex permuation and the treewidth. This
282 * vertex permutation can be used to build a tree decomposition of the source graph.
283 * @tparam Criterion the criterium which chooses the next vertex to eliminate
284 * @throw BALL::GRAPH::UnconnectedGraphException if called on unconnected graphs
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 * A criterium for GreedyFillIn which search for a vertex with
297 * minimum number of additional edges after eliminating
298 */
299 struct FillInHeuristic
300 {
301 VertexType& operator() (UndirectedGraph& graph);
302
303 Size edgeIncreaseByEliminating(VertexIterator vertex, UndirectedGraph& graph);
304 };
305
306 /**
307 * This algorithm computes a perfect elimination order in a branch and bound approach.
308 * First it computes a greedy solution. Then it tries each vertex permutation and uses
309 * a lower bound algorithm to check, if this branch can be better than either the best found solution
310 * or a found permutation of the same vertices but in a different order.
311 * If not, the branch is bounded and the algorithm tries another permutation.
312 * @tparam Lowerbound the lowerbound algorithm which should be used by this branch and bound algorithm
313 * @tparam Upperbound the greedy algorithm which is used to compute a initial solution
314 */
315 template <class Lowerbound=MinorMinWidth, class Upperbound=GreedyX<FillInHeuristic> >
316 class QuickBB
317 {
318 public:
319 /**
320 * A vertex IS simplicial, if its neighbourhood induces a clique.
321 * A vertex is ALMOST simplicial, it all but one of its neighbours
322 * induces a clique.
323 */
324 enum SIMPLICIAL_TYPE
325 {
326 NOT_SIMPLICIAL,
327 ALMOST_SIMPLICIAL,
328 IS_SIMPLICIAL
329 };
330
331 /**
332 * Builds a new QuickBB algorithm for the given graph
333 */
334 QuickBB(UndirectedGraph const& graph);
335
336 /**
337 * computes the best elimination order
338 */
339 EliminationOrder compute();
340
341 SIMPLICIAL_TYPE isSimplicial(VertexType& vertex) const;
342
343 protected:
344 /**
345 * a single vertex in the search-tree
346 */
347 struct QuickBBState
348 {
349 /**
350 * width of the permutation along the path from the root
351 */
352 unsigned int g;
353
354 /**
355 * lowerbound of g
356 */
357 unsigned int h;
358
359 /**
360 * best lowerbound for this state
361 */
362 unsigned int f;
363
364 /**
365 * current elimination order
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 * The graph for which the tree decomposition is built
377 */
378 UndirectedGraph graph_;
379
380 /**
381 * the current vertex in the search-tree
382 */
383 QuickBBState state;
384
385 /**
386 * An initial permutation which is computed by a greedy algorithm
387 */
388 EliminationOrder greedy_solution;
389
390 /**
391 * The permutation which is found by this algorithm
392 */
393 EliminationOrder own_solution;
394
395 /**
396 * Remembers the eliminated vertices of found branches during the algorithm. A branch is
397 * bound if it eliminates the same vertices but with a worse penalty.
398 */
399 GraphMap visitedSubgraphs;
400
401 /**
402 * the current upper bound. A branch is bound if it has a worse penalty than the upper bound.
403 * Each found solution gives a new upper bound. The greedy solution is the initial upper bound.
404 * The algorithm terminates if it's upper bound is equal to the lowerbound.
405 */
406 Size upper_bound;
407
408 /**
409 * Each vertex in the search tree is an elimination order. The root is an elimination order of length 0. It's children
410 * are elimination order of length 1 and so on. The leafs are elimination order of length n and define a
411 * tree decomposition. This algorithm searches the best solution (= permutation with minimal tree width) in this
412 * search tree. It computes the lowerbound for each vertex to get the mimimal tree width of the subtree, which is
413 * rooted in this vertex. Branches are bounded, if their lowerbound is higher than the tree width of the best
414 * found solution, or if there is another branch with a better tree width which eliminates the same vertices but
415 * in a different order. To speed up the computation, the algorithm uses a greedy solution as "template".
416 */
417 void branchAndBound(QuickBBState& nstate);
418
419 /**
420 * Vertices which are simplicial or almost simplicial can be eliminated instantly. So this function is called
421 * at the begin of the algorithm to reduce the number of vertices and the length of the searched permutation.
422 * You could call this function in each branch&bound step, but testing the simpliciality is expensive. So this is
423 * done only one time in the algorithm.
424 */
425 void prune(QuickBBState&);
426
427 /**
428 * The bitset remembers the eliminated vertices without an ordering.
429 */
430 BitSet buildBitset() const;
431
432 protected:
433 std::map<int, VertexType> index_to_vertex_;
434 };
435
436 /**
437 * An upperbound heuristic which computes an EliminationOrder,
438 * which can build a tree decomposition, and a treewidth of a given
439 * graph. The basic idea is to add as few as possible edges into the
440 * FillInGraph, because this should reduce the treewidth of the FillInGraph.
441 * Nevertheless, it's just a heuristic, so there is no guarantee, that
442 * the treewidth is minimal.
443 * @throw BALL::GRAPH::UnconnectedGraphException if called on unconnected graphs
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 * Builds a tree decomposition by the given elimination order
461 * @param graph The source graph for which the tree decomposition is built
462 * @param permutation the elimination order which is used to build the tree
463 * @return tree_decomposition an empty TreeNodeList which is filled with instances of TreeDecompositionBag
464 */
465 boost::shared_ptr<TreeDecomposition> operator() (UndirectedGraph const& graph, EliminationOrder const& permutation);
466
467 /**
468 * Converts the TreeDecomposition into a NiceTreeDecomposition
469 * A nice tree decomposition is a binary tree with five vertex types:
470 * - Introduce-nodes, which have one child and one more inner vertex than their child
471 * - Forget-nodes, which have one child and one inner vertex less than their child
472 * - Join-nodes, which have two childs and the same inner vertices as their childs
473 * - Leaf-nodes, which have no childs and exactly one inner vertex
474 * - Root-nodes, which have one child and no inner vertices
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 // BALL_DATATYPE_GRAPH_TREEWIDTH_H