使用增强图合并图形

时间:2013-08-10 13:05:05

标签: boost graph boost-graph

如何使用Boost Graph Library 在两个不同的图表之间添加边缘。我见过合并/收缩两个顶点的代码,但我不想那样做。 我想将一个图形的结束顶点链接到另一个图形的起始顶点,并将其作为单个图形。两个图形都是相同的类型。

请帮助......

1 个答案:

答案 0 :(得分:4)

首先,我们必须承认结果图是一个SINGLE对象。我假设您希望它与原始的两个图形g1和g2具有相同类型的Graph。这意味着您可以选择执行以下操作之一:   图g = g1 + g2或g1 + = g2(两种选择当然都是伪代码)。

在任何情况下,结果都将包含第二个图形的副本,而不是图形g2的“原始”对象。

BGL实际上为您提供了一个功能,即功能“ copy_graph

您可以执行以下操作

#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);
}

另见copy a graph (adjacency_list) to another one

相关问题