4

The boost function boost::graph::copy_graph

 template <class VertexListGraph, class MutableGraph>  void
 copy_graph(const VertexListGraph& G, MutableGraph& G_copy,
     const bgl_named_params<P, T, R>& params = all defaults)

lists in its parameter description UTIL/OUT: orig_to_copy(Orig2CopyMap c) which is a mapping from vertices in copy to vertices in original. I need this mapping!

(scroll to bottom on http://www.boost.org/doc/libs/1_55_0/libs/graph/doc/copy_graph.html)

How can I access/provide this last parameter orig_to_copy? Can you give a code example, ie complete this code for me?

void doSomething(graph_t& g){
  graph_t g_copy;
  copy_graph(g, g_copy, [...???...]);
  // here I would like to access the Orig2CopyMap
}
hooch
  • 1,135
  • 1
  • 16
  • 31

2 Answers2

7

found this solution here: http://d.hatena.ne.jp/gununu/20111006/1317880754

void doSomething(graph_t& g){
    typedef graph_t::vertex_descriptor vertex_t;
    typedef std::map<vertex_t, vertex_t> vertex_map_t;
    vertex_map_t vertexMap;
    // this boost type is needed around the map
    associative_property_map<vertex_map_t> vertexMapWrapper(vertexMap);
    graph_t g_copy;
    copy_graph(g, g_copy, boost::orig_to_copy(vertexMapWrapper));
    std::cout << "mapping from copy to original: " << std::endl;
    for(auto& iter : vertexMap){
        std::cout << iter.first << " -> " << iter.second << std::endl;
    }
}
hooch
  • 1,135
  • 1
  • 16
  • 31
  • Rather than having `vertexMapWrapper`, you can just write `boost::orig_to_copy(boost::make_assoc_property_map(vertexMap))` – Justin Jul 13 '18 at 20:43
4

Something like this:

typedef boost::graph_traits<graph_t>::vertex_descriptor vertex_t;
typedef boost::property_map<graph_t, boost::vertex_index_t>::type index_map_t;

//for simple adjacency_list<> this type would be more efficient:
typedef boost::iterator_property_map<typename std::vector<vertex_t>::iterator,
    index_map_t,vertex_t,vertex_t&> IsoMap;

//maps vertices of g to vertices of g_copy
std::vector<vertex_t> isoValues( num_vertices(g));    
IsoMap mapV( isoValues.begin());

boost::copy_graph( g, g_copy, boost::orig_to_copy(mapV) ); //means g_copy += g
Michael Simbirsky
  • 3,045
  • 1
  • 12
  • 24
  • 2
    I get a segfault here. I think the issue is that something needs to be inside the IsoMap, like a simple std::map that backs the property map. I found a working solution here: http://d.hatena.ne.jp/gununu/20111006/1317880754 – hooch Jun 20 '14 at 18:12
  • Thank you, I missed the container (isoValues) behind the property map. Now the code works properly. – Michael Simbirsky Jun 21 '14 at 02:19