1 #ifndef BALL_DATATYPE_GRAPH_GRAPHALGORITHMS_H
2 #define BALL_DATATYPE_GRAPH_GRAPHALGORITHMS_H
4 #ifndef BALL_COMMON_GLOBAL_H
8 #ifndef BALL_COMMON_EXCEPTION_H
12 #ifndef BALL_DATATYPE_STRING_H
16 #include <boost/graph/properties.hpp>
17 #include <boost/graph/graph_traits.hpp>
18 #include <boost/graph/adjacency_list.hpp>
19 #include <boost/graph/tree_traits.hpp>
20 #include <boost/graph/iteration_macros.hpp>
21 #include <boost/graph/copy.hpp>
22 #include <boost/shared_ptr.hpp>
73 template <
class Graph>
77 typedef typename boost::graph_traits<Graph>::vertex_descriptor
VertexType;
80 typedef typename boost::graph_traits<Graph>::edge_descriptor
EdgeType;
85 typedef boost::adjacency_list<boost::listS, boost::listS, boost::undirectedS,
87 boost::property<boost::vertex_index_t, int> >,
92 template <
typename Graph1,
typename Graph2>
99 template <
typename Edge1,
typename Edge2>
105 mutable typename boost::property_map<Graph2, boost::edge_orig_ptr_t>::type
edge_orig_map;
108 template <
typename Graph1,
typename Graph2>
115 template <
typename Graph1,
typename Graph2>
124 template <
typename Vertex1,
typename Vertex2>
128 boost::put(boost::vertex_index,
g2, v2, boost::get(boost::vertex_index,
g1, v1));
131 mutable typename boost::property_map<Graph2, boost::vertex_orig_ptr_t>::type
vertex_orig_map;
136 template <
typename Graph1,
typename Graph2>
148 template <
class UndirectedGraph>
153 for (tie(ai, ai_end) = adjacent_vertices(vertex, graph); ai != ai_end; ++ai)
156 for (; bi != ai_end; ++bi)
157 if (*bi != *ai && !boost::edge(*ai, *bi, graph).second)
158 boost::add_edge(*ai, *bi, graph);
161 boost::clear_vertex(vertex, graph);
162 boost::remove_vertex(vertex, graph);
173 template <
class UndirectedGraph>
175 UndirectedGraph& graph)
180 for (tie(ai, ai_end) = adjacent_vertices(vertex, graph); ai != ai_end; ++ai)
182 result.
getNeighbours().push_back(boost::get(boost::vertex_index, graph, *ai));
184 typedef typename boost::property_traits<typename boost::property_map<UndirectedGraph, boost::edge_all_t>::type>::value_type EdgeProperties;
186 result.
getEdgeProperties().push_back(boost::get(boost::edge_all_t(), graph, boost::edge(vertex, *ai, graph).first));
189 for (; bi != ai_end; ++bi)
191 if (!boost::edge(*ai, *bi, graph).second)
193 boost::add_edge(*ai, *bi, graph);
194 result.
getEdges().push_back(std::make_pair(boost::get(boost::vertex_index, graph, *ai),
195 boost::get(boost::vertex_index, graph, *bi)));
200 boost::clear_vertex(vertex, graph);
201 boost::remove_vertex(vertex, graph);
215 template <
class UndirectedGraph>
216 class UndoEliminateOperation
219 typedef typename boost::graph_traits<UndirectedGraph>::vertex_descriptor
VertexType;
220 typedef typename boost::graph_traits<UndirectedGraph>::edge_descriptor
EdgeType;
223 typedef typename boost::property_traits<
typename boost::property_map<UndirectedGraph,
225 typedef typename boost::property_traits<
typename boost::property_map<UndirectedGraph,
253 std::vector<std::pair<int, int> >
edges_;
259 template <
class UndirectedGraph>
270 template <
class UndirectedGraph>
277 template <
class UndirectedGraph>
287 VertexType v = boost::add_vertex(vertex_properties_, *graph);
289 std::map<int, VertexType> index_to_vertex;
290 BGL_FORALL_VERTICES_T(v, *graph, UndirectedGraph)
292 index_to_vertex[boost::get(boost::vertex_index, *graph, v)] = v;
295 for (
Position i=0; i<neighbours_.size(); ++i)
297 boost::add_edge(v, index_to_vertex[neighbours_[i]], edge_properties_[i], *graph);
300 for (
Position i=0; i<edges_.size(); ++i)
302 boost::remove_edge(index_to_vertex[edges_[i].first], index_to_vertex[edges_[i].second], *graph);
308 template <
class UndirectedGraph>
309 void deepCopy(
const UndirectedGraph& src, UndirectedGraph& target)
311 typedef std::map<typename boost::graph_traits<UndirectedGraph>::vertex_descriptor,
int> VertexIndexMap;
312 typedef boost::associative_property_map<VertexIndexMap> VertexIndexPropertyMap;
314 VertexIndexMap vertex_map;
315 VertexIndexPropertyMap vertex_property_map(vertex_map);
317 typename boost::graph_traits<UndirectedGraph>::vertices_size_type count = 0;
319 typename boost::graph_traits<UndirectedGraph>::vertex_iterator vi, vend;
320 for (boost::tie(vi, vend) = boost::vertices(src); vi != vend; ++vi)
321 vertex_map[*vi] = count++;
323 boost::copy_graph(src, target, vertex_index_map(vertex_property_map));
326 template <
class Tree,
class From,
class To,
class Functor>
335 :
return_stack_(boost::shared_ptr<std::vector<To> >(new std::vector<To>())),
339 boost::traverse_tree(root(*
tree_), *
tree_, *
this);
347 template <
class Node>
352 template <
class Node>
357 template <
class Node>
361 boost::tie(c_i, c_end) = children(n, t);
363 bool is_leaf = (c_i == c_end);
370 for (; c_i != c_end; ++c_i)
376 To value = (*f_)(n, begin_arg, end_arg);
378 if (begin_arg != end_arg)
396 #endif // BALL_DATATYPE_GRAPH_GRAPHALGORITHMS_H