, . , , , .
API Mutable Graph: http://www.boost.org/doc/libs/1_55_0/libs/graph/doc/MutableGraph.html
:
template<typename Vertex, typename Graph>
void merge_vertices(Vertex src, Vertex tgt, Graph &g)
{
for (auto it = boost::out_edges(src, g);
it.first != it.second; ++it.first)
{
Vertex u = target(*it.first,g);
add_edge(u,tgt,g);
}
clear_vertex(src,g);
remove_vertex(src,g);
}
, , .
:
typedef boost::adjacency_list<boost::listS,
boost::listS, boost::undirectedS > graph_t;
typedef boost::graph_traits<graph_t>::vertex_descriptor vertex_t;
int main(int, char**)
{
typedef std::map<vertex_t, size_t> IndexMap;
IndexMap mapIndex;
graph_t g;
vertex_t v0 = add_vertex(g);
mapIndex[v0]=0;
vertex_t v1 = add_vertex(g);
mapIndex[v1]=1;
vertex_t v2 = add_vertex(g);
mapIndex[v2]=2;
vertex_t v3 = add_vertex(g);
mapIndex[v3]=3;
add_edge(v0,v2,g);
add_edge(v1,v3,g);
add_edge(v1,v0,g);
std::cout << "Before merging " << std::endl;
boost::print_graph(g, boost::make_assoc_property_map(mapIndex));
merge_vertices(v1,v2,g);
std::cout << "After merging "<< std::endl;
boost::print_graph(g, boost::make_assoc_property_map(mapIndex));;
}
:
Before merging
0 <--> 2 1
1 <--> 3 0
2 <--> 0
3 <--> 1
After merging
0 <--> 2 2
2 <--> 0 3 0
3 <--> 2