1
votes

it is my first post on Stackoverflow. So please don't be to hard on me if I violate any posting rules by accident.

I am fighting with the BGL. For a project at my university I need some graph related stuff. Therefore one important function that I need is to merge two undirected graphs.

I have already searched for related questions and found these topics:

Merging graphs using boost graph

copy a graph (adjacency_list) to another one

The first one doesn't work for me since I am always getting an "out of range" error within the copy_graph function. I am sure that the code is not the problem by my attempt to adopt it to my program.

The second one did merge the two graphs (related to the vertices) but the edge information of the new graph wasn't correct.

This code shows how I define my graph. It's an undirected graph with vertex properties (no edge properties):

struct m_vertex_properties {
Element* element;
Strip* strip;
};

typedef adjacency_list<vecS, vecS, undirectedS, m_vertex_properties,no_property> MyGraph;
typedef MyGraph::vertex_descriptor NodeID;

Based on the second link I posted I used the following function:

*static*/ void Fracture::merging(MyGraph & g1, NodeID v_in_g1, MyGraph & g2, NodeID    u_in_g2) {       // EDGES ARE NOT CREATED CORRECTLY!!!

typedef map<NodeID, size_t> IndexMap;
IndexMap mapIndex;
associative_property_map<IndexMap> propmapIndex(mapIndex);

int i=0;
BGL_FORALL_VERTICES(v, g2, MyGraph)
{
    put(propmapIndex, v, i++);
}

copy_graph( g2, g1, vertex_index_map( propmapIndex ) ); //means g1 += g2
}

As I mentioned: The resulting graph (g1) does contain all vertices (in my test scenario 2+3) but for some reason only 3 edges.

I hope that for someone being much more advanced than I am the mistake is easy to find. Honestly I don't even understand everything the function does. Thank you guys!

1
A SSCCE might help.Mike M

1 Answers

-1
votes

If vertices are copied from g2 into g1, it sounds very weird that edges were not copied. Probably somehow the edges were not there.

You can try to print the source graphs and the resulting graph using the following code:

#include <boost/graph/graph_utility.hpp>
std::cout << std::endl << "graph g2" <<std::endl;
boost::print_graph(g2);

(and similarly for g1 both before and after the function call).