Combining graphs using a force graph

How to add a border between two different graphs using the Boost Graph library . I saw code for merging / compressing two vertices, but I don't want to do this. I want to connect the end vertex of one graph with the start vertex of another graph and make it one single graph. Both graphs are of the same type.

Please, help...

+4
source share
1 answer

First of all, we must recognize that the resulting graph is a SINGLE object. I assume that you want it to be the same Graph type as your original two graphs g1 and g2. This means that you have the choice to do one of the following: Graph g = g1 + g2 or g1 + = g2 (both options, of course, in pseudo-code).

In any case, the result will contain a copy of the second graph, and not the "original" object of the graph g2.

BGL actually provides you with a function for this, namely the copy_graph function

You can do something like the following

#include <boost/graph/adjacency_list.hpp> #include <boost/graph/copy.hpp> typedef boost::adjacency_list<> Graph; typedef Graph::vertex_descriptor vertex_t; void merging(Graph & g1, vertex_t v_in_g1, const Graph & g2, vertex_t u_in_g2) { typedef boost::property_map<Graph, 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; //for more generic graphs, one can try //typedef std::map<vertex_t, vertex_t> IsoMap; IsoMap mapV; boost::copy_graph( g2, g1, boost::orig_to_copy(mapV) ); //means g1 += g2 vertex_t u_in_g1 = mapV[u_in_g2]; boost::add_edge(v_in_g1, u_in_g1, g1); } 

See also copy graph (adjacency_list) to another

+4
source

Source: https://habr.com/ru/post/1496314/


All Articles