[英]Boost graph: How to copy the nodes and edges of a graph without copying properties?
我正在使用帶有捆綁屬性的boost圖。 在我構建第一個參考樹之后。 我想有幾個其他樹具有相同的結構和層次結構,但具有不同的頂點和邊緣屬性。 我發現有一個copy_graph方法,但不知道如何使用它實現我的目的。 例如,我首先創建一個參考樹, VertexProperty1
和EdgeProperty1
是捆綁屬性
typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::bidirectionalS, VertexProperty1, EdgeProperty1> Graph;
Graph g1;
經過一些處理后,g1包含一些頂點和邊。 然后我想要一個具有不同捆綁屬性的復制樹。
typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::bidirectionalS, VertexProperty2, EdgeProperty2> Graph2;
copy_graph(g1, g2, ???);
在此先感謝任何幫助。 示例代碼將是首選。
如果查看文檔 ,可以看到參數vertex_copy
和edge_copy
是實際復制屬性的參數。 這些參數的默認值復制每個頂點/邊的所有屬性,你需要“無所事事”的東西:
struct do_nothing
{
template <typename VertexOrEdge1, typename VertexOrEdge2>
void operator()(const VertexOrEdge1& , VertexOrEdge2& ) const
{
}
};
然后像這樣調用copy_graph
:
copy_graph(g1,g2,boost::vertex_copy(do_nothing()).edge_copy(do_nothing()));
#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);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.