Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Boost graph: How to copy the nodes and edges of a graph without copying properties?

Tags:

c++

graph

boost

I am using boost graph with bundled properties. After I build the first reference tree. I would like to have several other trees with the same structure and hierarchy but with different vertex and edge property. I find there is a copy_graph method but don't know how to achieve my purpose using this. For example, I first create a reference tree, VertexProperty1 and EdgeProperty1 are bundled properties

typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::bidirectionalS, VertexProperty1, EdgeProperty1> Graph;
Graph g1;

After some processing, g1 contains some vertices and edges. Then I would want to have a copied tree with different bundled properties.

typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::bidirectionalS, VertexProperty2, EdgeProperty2> Graph2;
copy_graph(g1, g2, ???);

Thanks in advance for any kind help. Example code would be preferred.

like image 371
youround Avatar asked Jan 26 '16 13:01

youround


1 Answers

If you look at the documentation you can see that the parameters vertex_copy and edge_copy are the ones that actually copy the properties. The default value for those parameters copy all the properties in every vertex/edge, you need something that "does nothing" instead:

struct do_nothing
{
    template <typename VertexOrEdge1, typename VertexOrEdge2>
    void operator()(const VertexOrEdge1& , VertexOrEdge2& ) const 
    {
    }
};

And then invoke copy_graph like this:

copy_graph(g1,g2,boost::vertex_copy(do_nothing()).edge_copy(do_nothing()));

Running on Coliru

#include <iostream>
#include <string>

#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/copy.hpp>
#include <boost/graph/graph_utility.hpp> 

struct VertexProp1
{
    int color;
};

struct VertexProp2
{
    std::string name;
};

struct EdgeProp1
{
    double weight;
};

struct EdgeProp2
{
    std::string name;
};

typedef boost::adjacency_list<boost::vecS,boost::vecS,boost::bidirectionalS,VertexProp1,EdgeProp1> Graph1;
typedef boost::graph_traits<Graph1>::vertex_descriptor VertexDesc;

typedef boost::adjacency_list<boost::vecS,boost::vecS,boost::bidirectionalS,VertexProp2,EdgeProp2> Graph2;

struct do_nothing
{
    template <typename VertexOrEdge1, typename VertexOrEdge2>
    void operator()(const VertexOrEdge1& , VertexOrEdge2& ) const 
    {
    }
};

void build_graph(Graph1& g)
{
    VertexDesc v0=add_vertex(VertexProp1{1},g);
    VertexDesc v1=add_vertex(VertexProp1{2},g);
    VertexDesc v2=add_vertex(VertexProp1{3},g);
    add_edge(v0,v1,EdgeProp1{1.0},g);
    add_edge(v1,v2,EdgeProp1{2.0},g);
    add_edge(v2,v0,EdgeProp1{3.0},g);

}


int main()
{
    Graph1 g1;
    build_graph(g1);

    std::cout << "Graph1" << std::endl;
    print_graph(g1);

    Graph2 g2;

    copy_graph(g1,g2,boost::vertex_copy(do_nothing()).edge_copy(do_nothing()));

    std::cout << "Graph2" << std::endl;
    print_graph(g2);

}
like image 160
4 revs Avatar answered Nov 15 '22 05:11

4 revs