复制带有绑定属性的邻接表图

3

以下是一个完整的代码片段,可以复制捆绑属性的图表,但会导致一堆编译器错误。需要做什么来解决这些问题?

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);
}
1个回答

2

这有点歪曲了问题。你实际上并没有复制邻接表,而是复制了 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.
 */

以下是复制邻接表的方法:

¹

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


感谢@Sehe。有没有办法删除一条边?在一对顶点之间可能会有多条边。这就是使用/*auto e = */的原因,以获得唯一的句柄。但是,在复制的图上,该句柄可能不再有效。 - Tims
当然,在复制的图上,该句柄将无效。找到边缘并从那里删除它。http://paste.ubuntu.com/15202442/ - sehe
谢谢,看起来不错,但是两个顶点之间有多条边,我想要删除其中一条而不是其他的。 - Tims
你需要一段标识信息(建议在边缘捆绑包中使用属性)/或者/你需要手动复制并保持原始边和复制边之间的映射(不建议这样做)。 - sehe

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接