0

I want to copy edges with their vertices and properties from dataG.front(), and add it to testg , I tried what I found in "Accessing bundled properties" section http://www.boost.org/doc/libs/1_57_0/libs/graph/doc/bundles.html but it isn't working for me. PS: dataG is a vector of graphs.

typedef std::pair<edge_iter, edge_iter> edge_pair;
Graph testg;
if (!dataG.empty()) 
{
    auto const& gr = dataG.front();         
    for (edge_pair ep = edges(gr); ep.first != ep.second; ++ep.first) //ep edge number 
    {
        auto ep = edges(gr).first;  // ep edge number

        vertex_t from = source(*ep.first, gr);
        vertex_t to   = target(*ep.first, gr);

        boost::add_vertex(gr[from], testg);
        boost::add_vertex(gr[to], testg);

        boost::add_edge(from, to, gr[*ep.first], testg);

    }
}

edges properties works but there is a problem in source and target. (vertex_t and add_vertex part), How to add directly the vertices properties to the added one because there is a duplication here.

PS: for more information here is the full code http://pastebin.com/2iztGAa6

TylerH
  • 20,799
  • 66
  • 75
  • 101
  • Possible duplicate of [copying edges with adjacent vertices and their properties using BOOST](http://stackoverflow.com/questions/28827006/copying-edges-with-adjacent-vertices-and-their-properties-using-boost) – TylerH Feb 07 '17 at 02:47

1 Answers1

0

As you noticed, the vertices might be duplicated, and this is especially true if you "merge" multiple of the source graphs into one graph.

If you don't mind to re-write the vertex properties (and keeping the value last assigned in case the values aren't identical all the time) you can just use a property map:

boost::property_map<Graph, boost::vertex_bundle_t>::type vpmap = boost::get(boost::vertex_bundle, testg);

//so:
vpmap[from] = gr[from];
vpmap[to]   = gr[to];

Then again, there's also the equivalent access like so:

testg[from] = gr[from];
testg[to]   = gr[to];

You can even addres individual bundled members:

boost::property_map<Graph, int VertexProperties::*>::type idmap    = boost::get(&VertexProperties::id, testg);
boost::property_map<Graph, int VertexProperties::*>::type labelmap = boost::get(&VertexProperties::label, testg);
idmap[from]    = gr[from].id;
labelmap[from] = gr[from].label;

All the samples based on this documentation page

sehe
  • 374,641
  • 47
  • 450
  • 633
  • this is correct ? `vertex_t from = source(*ep, gr); vertex_t to = target(*ep, gr); boost::property_map::type idmap = boost::get(&VertexProperties::id, testg); boost::property_map::type labelmap = boost::get(&VertexProperties::label, testg); idmap[from] = gr[from].id; labelmap[from] = gr[from].label; boost::add_edge(from, to, EdgeProperties(idmap[from], labelmap[from]), testg);` – KHALDOUN Mohsen Mar 02 '15 at 14:53
  • Why do you keep posting unreadable messes in the comments after the question was already answered. I'm sure that you can test whether it is correct. – sehe Mar 02 '15 at 14:54