copy_graph - 具有捆绑属性的 adjacency_list

copy_graph - adjacency_list with bundled properties(copy_graph - 具有捆绑属性的 adjacency_list)

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

问题描述

这是一个完整的片段,用于复制具有捆绑属性的图形,但会导致大量编译器错误.解决问题需要什么?

struct NodeInfo1 {};结构 EdgeInfo1 {};typedef boost::labeled_graph,std::string>图1;typedef std::pair<boost::graph_traits<Graph>::edge_descriptor, bool>边缘;void TestCases::TestCopyGraph(){图1网格,g1;EdgeInfo1 ei;Edge e = add_edge_by_label("A", "B", ei, grid);复制图(网格,g1);}

解决方案

这有点歪曲了问题.您不是实际上复制邻接列表,而是复制了labeled_graph适配器,这恰好不满足copy_graph要求的概念:

<块引用>

/** @name 标记的可变图* 标记的可变图隐藏了 add_ 和 remove_ 顶点函数* 可变图概念.请注意,remove_vertex 是隐藏的,因为* 删除没有键的顶点可能会留下悬空引用* 地图.*/

这是复制 adjacency_list:¹

typedef boost::adjacency_list一个列表;typedef boost::labeled_graph图形;void TestCopyGraph(){std::string names[3] = { "A", "B", "C" };图形网格(3,名称);EdgeInfo1 ei;/*auto e =*/add_edge_by_label("C", "B", ei, grid);AList g1;复制图(网格,g1);}

复制带标签的适配器

要容易得多.不需要 copy_graph,只需复制构造对象:

#include #include #include #include struct NodeInfo1 { int i;};struct EdgeInfo1 { int j;};typedef boost::adjacency_list一个列表;typedef boost::labeled_graph图形;自动 TestCopyGraph(){std::string names[3] = { "A", "B", "C" };NodeInfo1 props[3] = { {11}, {22}, {33} };图网格(3,名称,道具);/*auto e =*/add_edge_by_label("C", "B", EdgeInfo1{17}, grid);图 g1 = 网格;//只是复制构造返回 g1;}int main() {自动复制 = TestCopyGraph();打印图(复制);//检查属性是否被复制:顶点 B 有 NodeInfo1 22{auto pmap = boost::get(&NodeInfo1::i, 复制);std::cout <<复制后的顶点B NodeInfo1.i:" <<pmap[copyed.vertex("B")] <<"
";}//边属性也是:for (auto e : boost::make_iterator_range(edges(copyed)))std::cout <<Edge 具有属性 EdgeInfo1" <<复制的[e].j <<"
";std::cout <<"已删除 A:
";copy.remove_vertex("A");打印图(复制);}

印刷品

0 <-->1 <-->22 <-->1复制后的顶点 B NodeInfo1.i:22边缘具有属性 EdgeInfo1 17删除 A:0 <-->11 <-->0

¹ 请注意,由于labeled_graph 中的错误,您需要此补丁:https://github.com/boostorg/graph/pull/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")] << "
";
    }

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

    std::cout << "Removed A:
";
    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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:copy_graph - 具有捆绑属性的 adjacency_list

基础教程推荐