copy_graph - 的adjacency_list捆绑性质 [英] copy_graph - adjacency_list with bundled properties

查看:158
本文介绍了copy_graph - 的adjacency_list捆绑性质的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是一个完整的片段,以图复制捆绑性质,但在一堆编译器错误的结果。现在需要解决的问题?

 结构NodeInfo1 {};
结构EdgeInfo1 {};TYPEDEF提振:: labeled_graph<提高::的adjacency_list<
    促进血管内皮细胞::,促进血管内皮细胞::,提振:: undirectedS,NodeInfo1,EdgeInfo1>中
    标准::字符串> Graph1;的typedef的std ::对<提高:: graph_traits<&图表GT; :: edge_descriptor的,布尔>边缘;
无效的TestCase :: TestCopyGraph()
{
    Graph1格,G1;
    EdgeInfo1 EI;    边e = add_edge_by_label(A,B,EI,网格);
    copy_graph(网格,G1);
}


解决方案

这是稍微misre presenting的问题。你不是的真正的复制邻接表,你复制labeled_graph适配器,这恰好不是满足 copy_graph 所需的概念:


  / ** @name标记可变图
 *标记的可变图隐藏add_和remove_顶点功能
 *易变的图形概念。请注意,remove_vertex被隐藏,因为
 *无需移除其关键可以离开在晃来晃去的参考顶点
 * 地图。
 * /


下面是复制的adjacency_list:¹

 的typedef的boost ::的adjacency_list<促进血管内皮细胞::,促进血管内皮细胞::,提振:: undirectedS,NodeInfo1,EdgeInfo1>一个列表;
TYPEDEF提振:: labeled_graph< ALIST,标准::字符串>图形;无效TestCopyGraph()
{
    性病::字符串名称[3] = {A,B,C};
    图格(3名);
    EdgeInfo1 EI;    / *自动E = * / add_edge_by_label(C,B,EI,网格);    ALIST G1;
    copy_graph(网格,G1);
}

复制标记的适配器

要容易得多。否 copy_graph 要求,只需复制构造对象:

 的#include<升压/图/ adjacency_list.hpp>
#包括LT&;升压/图/ copy.hpp>
#包括LT&;升压/图/ labeled_graph.hpp>
#包括LT&;升压/图/ graph_utility.hpp>结构NodeInfo1 {INT I; };
结构EdgeInfo1 {诠释J; };TYPEDEF提振::的adjacency_list<的boost ::血管内皮细胞,促进血管内皮细胞::,提振:: undirectedS,NodeInfo1,EdgeInfo1>一个列表;
TYPEDEF提振:: labeled_graph< ALIST,标准::字符串>图形;自动TestCopyGraph()
{
    性病::字符串名称[3] = {A,B,C};
      NodeInfo1道具[3] = {{11},{22},{33}};
    图格(3,名称,道具);
    / *自动E = * / add_edge_by_label(C,B,EdgeInfo1 {17},网格);    图G1 =网格; //只是复制,构建
    返回G1;
}诠释主(){
    自动复制= TestCopyGraph();    print_graph(复制);    //检查的属性被复制:顶点B有NodeInfo1 22
    {
        汽车PMAP =的boost ::得到(安培; NodeInfo1 ::我复制);
        性病::法院LT&;< 复制后顶点乙NodeInfo1.i:&LT< PMAP [copied.vertex(B)]下;&下; \\ n;
    }    //边缘性质太:
    为(自动E:提高:: make_iterator_range(边(复制)))
        性病::法院LT&;< 边有产权EdgeInfo1<<复制[E] .J<< \\ n;    性病::法院LT&;< 删除了:\\ n;
    copied.remove_vertex(A);
    print_graph(复制);
}

打印

  0℃;  - >
1下 - →; 2
2' - →; 1
复制后的顶点乙NodeInfo1.i:22
边上有物业EdgeInfo1 17
删除了:
0℃ - →; 1
1下 - →; 0

¹请注意,您需要因为labeled_graph虫子这个补丁: https://github.com / boostorg /图形/拉/ 58

Here is a complete snippet to copy a graph with bundled properties, but results in bunch of compiler errors. What is needed to fix the problems?

struct NodeInfo1    {};
struct EdgeInfo1 {};

typedef boost::labeled_graph< boost::adjacency_list<
    boost::vecS, boost::vecS, boost::undirectedS, NodeInfo1, EdgeInfo1>,
    std::string> Graph1;

typedef std::pair<boost::graph_traits<Graph>::edge_descriptor, bool> Edge;


void TestCases::TestCopyGraph()
{
    Graph1 grid, g1;
    EdgeInfo1 ei;

    Edge e = add_edge_by_label("A", "B", ei, grid);
    copy_graph(grid, g1);
}

解决方案

That's slightly misrepresenting the question. You're not actually copying the adjacency list, you're copying the labeled_graph adaptor, which happens to not satisfy the concepts required by copy_graph:

/** @name Labeled Mutable Graph
 * The labeled mutable graph hides the add_ and remove_ vertex functions from
 * the mutable graph concept. Note that the remove_vertex is hidden because
 * removing the vertex without its key could leave a dangling reference in
 * the map.
 */

Here's copying the adjacency_list: ¹

typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS, NodeInfo1, EdgeInfo1> AList;
typedef boost::labeled_graph<AList, std::string> Graph;

void TestCopyGraph()
{
    std::string names[3] = { "A", "B", "C" };
    Graph grid(3, names);
    EdgeInfo1 ei;

    /*auto e =*/ add_edge_by_label("C", "B", ei, grid);

    AList g1;
    copy_graph(grid, g1);
}

Copying the Labeled adaptor

Is much easier. No copy_graph required, just copy-construct the object:

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

struct NodeInfo1 { int i; };
struct EdgeInfo1 { int j; };

typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS, NodeInfo1, EdgeInfo1> AList;
typedef boost::labeled_graph<AList, std::string> Graph;

auto TestCopyGraph()
{
    std::string names[3] = { "A", "B", "C" };
      NodeInfo1 props[3] = { {11}, {22}, {33} };
    Graph grid(3, names, props);
    /*auto e =*/ add_edge_by_label("C", "B", EdgeInfo1{17}, grid);

    Graph g1 = grid; // just copy-construct
    return g1;
}

int main() {
    auto copied = TestCopyGraph();

    print_graph(copied);

    // check that properties were copied: vertex B has NodeInfo1 22
    {
        auto pmap = boost::get(&NodeInfo1::i, copied);
        std::cout << "Vertex B NodeInfo1.i after copy: " << pmap[copied.vertex("B")] << "\n";
    }

    // edge properties too:
    for (auto e : boost::make_iterator_range(edges(copied)))
        std::cout << "Edge has property EdgeInfo1 " << copied[e].j << "\n";

    std::cout << "Removed A:\n";
    copied.remove_vertex("A");
    print_graph(copied);
}

Prints

0 <--> 
1 <--> 2 
2 <--> 1 
Vertex B NodeInfo1.i after copy: 22
Edge has property EdgeInfo1 17
Removed A:
0 <--> 1 
1 <--> 0 

¹ Note that you need this patch because of bugs in labeled_graph: https://github.com/boostorg/graph/pull/58

这篇关于copy_graph - 的adjacency_list捆绑性质的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆