c++ - copy_graph - 具有捆绑属性的 adjacency_list

标签 c++ boost graph properties boost-graph

这是复制具有捆绑属性的图形的完整代码段,但会导致一堆编译器错误。解决这些问题需要什么?

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

最佳答案

这有点歪曲了问题。您不是实际上复制邻接表,而是复制 labeled_graph 适配器,它恰好不满足 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.
 */

这里是复制 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);
}

复制标签适配器

简单多了。不需要copy_graph,只需复制构造对象:

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

打印

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

¹ 请注意,由于 labeled_graph 中的错误,您需要此补丁:https://github.com/boostorg/graph/pull/58

关于c++ - copy_graph - 具有捆绑属性的 adjacency_list,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35616136/

相关文章:

C++ Weird Diamond继承问题

c++ - 第三方API中的编译错误-Visual Studio

c++ - 使 C++ Eigen LU 更快(我的测试显示比 GSL 慢 2 倍)

c++ - 为什么 boost::property_tree::write_json() 将整数值转换为字符串?这是不正确的。

c++ - 数据模型、图形库、C++

c++ - vector 的 vector 会是连续的吗?

c++ - 如何使用 boost 异常将信息添加到 std::exception

c++ - BOOST_STRONG_TYPEDEF并 move 语义

algorithm - 维护没有循环或菱形的图形

graph - 将多个顺序 HBase 查询的结果传递给 Mapreduce 作业