【问题标题】:Copy Boost Labeled Graph复制 Boost 标记图
【发布时间】:2021-03-03 03:29:24
【问题描述】:

我希望能够将带标签的图形适配器与底层邻接列表一起复制。

以下代码 sn-p 与此答案中的代码基本相同: https://stackoverflow.com/a/35640105/9033613,除了邻接列表使用 setS 而不是 vecS 的事实。我还修改了示例,以便在复制后删除原始图形。

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

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

typedef boost::adjacency_list<boost::setS, boost::setS, boost::bidirectionalS, NodeInfo1, EdgeInfo1> AList;
typedef boost::labeled_graph<AList, std::string> Graph;

void print(Graph& g) {
  auto pmap = boost::get(&NodeInfo1::i, g);
  for(auto v : boost::make_iterator_range(boost::vertices(g))) {
    std::cout << pmap[v] << " --> ";
    for (const auto &e : boost::make_iterator_range(boost::out_edges(v, g))) {
      const auto &v_t = boost::target(e, g);
      std::cout << pmap[v_t] << " ";
    }
    std::cout << std::endl;
  }
}

auto TestCopyGraph(const Graph& grid)
{
  Graph g1 = grid; // just copy-construct
  return g1;
}

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

  auto copied = TestCopyGraph(*grid);
  print(copied);


  grid.reset();
  // 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(copied);
}

现在,如果我使用 vecS 作为顶点描述符,示例按预期工作,但如果我使用 setS 而不是 vecS,代码会出现分段错误,因为复制操作实际上并未复制邻接表的底层结构. 我该如何解决这个问题,或者一般来说如何复制上面代码中定义的带标签的图形适配器?

【问题讨论】:

  • 你能创建一个更小的例子来演示这个问题吗?尝试尽可能简单地开始 - 创建一个 setS 或 vecS,复制它们并尝试重现崩溃。如果没有,那么您能否仅使用 AList 重现崩溃(不作为 Graph 的一部分)。等等......它在代码中的什么地方崩溃了?
  • @joeking 64 LoC 对于 Boost Graph 启动器来说非常简单

标签: c++ boost boost-graph


【解决方案1】:

遗憾的是,这是labeled_graph 适配器的另一个限制¹。

真正的问题是适配器将原始顶点描述符存储在其内部属性中(map)。在vecS vertex-container-selector 的情况下,顶点描述符是一个从0 开始的整数[0, num_vertices(g)),它很高兴地转移到图的相同副本。

但是,对于基于节点的顶点容器选择器(listSsetS、...),顶点描述符是一个不透明的句柄(基本上是一个指针类型双关语到 void*)。显然,这些不会转移到图的副本。更糟糕的是,在副本上使用顶点描述符时,您会得到Undefined Behaviour。如果副本的源仍在附近,您最终可能会从错误的图表访问顶点存储。如果图表已经被破坏,你会得到任何其他行为,如果你幸运的是崩溃。

如何解决

恐怕你不能。

我们可以在底层labeed_graph.graph() 上使用boost::copy_graph。我们需要提供一个外部 vertex_index 属性映射。

但是,这给我们留下了实际复制标签的无法解决的任务。这是无法解决的,因为 没有办法 来迭代标签,或者从顶点描述符中提取它们。而且我们无法添加功能——甚至不能通过子类化——因为map_type _map;private

让我们无所事事

当然,如果我们可以改变boost/graph/labeled_graph.hpp 来声明friend

template <typename G, typename L, typename S>
friend labeled_graph<G, L, S> clone_labeled_graph(labeled_graph<G, L, S> const& src);

我们可能会实现类外的功能:

template <typename G, typename L, typename S>
labeled_graph<G, L, S> clone_labeled_graph(labeled_graph<G, L, S> const& src) {
    labeled_graph<G, L, S> dst;
    graph_detail::clone_labeled_graph(src.graph(), src._map, dst.graph(), dst._map);
    return dst;
}

一个微妙的选择是我们将参数拆分到底层图形及其标签映射中,这样我们就不必指定过多的朋友模板。为什么?

这是因为我们需要根据标签映射的类型进行调度(如前所述,这将根据顶点索引的性质采取各种形式):

template <typename Graph, typename Map>
static inline void clone_labeled_graph(Graph const& g, Map const& srcmap, Graph& dst, Map& dstmap) {
    clone_labeled_graph(g, srcmap, dst, dstmap, container_category(srcmap));
}

有了这些,我们可以实现各种重载:

template <typename Graph, typename Map>
static inline void clone_labeled_graph(Graph const& src, Map const& srcmap, Graph& dst, Map& dstmap, random_access_container_tag) {
    copy_graph(src, dst);

    dstmap.resize(srcmap.size());
    for (size_t v = 0; v < srcmap.size(); ++v) {
        put_vertex_label(dstmap, dst, srcmap[0], v);
    }
}

template <typename Graph, typename Map>
static inline void clone_labeled_graph(Graph const& src, Map const& srcmap, Graph& dst, Map& dstmap, unique_associative_container_tag) {
    using V = graph_traits<Graph>::vertex_descriptor;
    std::map<V, size_t> vidx;
    for (auto v : boost::make_iterator_range(vertices(src)))
        vidx.emplace(v, vidx.size());
    auto idmap = boost::make_assoc_property_map(vidx);

    std::map<V, V> corresponding;
    auto o2cmap = make_assoc_property_map(corresponding);

    copy_graph(src, dst, vertex_index_map(idmap).orig_to_copy(o2cmap));

    for (auto const& [label, v] : srcmap) {
        put_vertex_label(dstmap, dst, label, corresponding.at(v));
    }
}

template <typename Graph, typename Map>
static inline void clone_labeled_graph(Graph const& src, Map const& srcmap, Graph& dst, Map& dstmap, multiple_associative_container_tag) {
    clone_labeled_graph(src, srcmap, dstmap, unique_associative_container_tag{});
}

注意这个

  • 注意通用(它应该与vecSlistS 等一起使用)
  • 不注重效率
  • 不支持 C++03 编译

现场演示

Live On Wandbox

  • 文件main.cpp

     #define BOOST_ALLOW_DEPRECATED_HEADERS
     #include <boost/graph/adjacency_list.hpp>
     #include <boost/graph/copy.hpp>
     #include <iostream>
     #include "labeled_graph.hpp" // adds the friend declaration ONLY
     #include "clone_labeled_graph.hpp" // our out-of-class friend implementation
    
     template <typename Graph>
     void print(Graph const& g) {
         std::cout << "====\n";
         for(auto v : boost::make_iterator_range(boost::vertices(g))) {
             std::cout << g.graph()[v].i << " --> ";
             for (const auto &e : boost::make_iterator_range(boost::out_edges(v, g))) {
                 const auto &v_t = boost::target(e, g);
                 std::cout << g.graph()[v_t].i << " ";
             }
             std::cout << std::endl;
         }
     }
    
     template <typename VertexSelector>
     void test_clone_labeled_graph() {
         std::cout << __PRETTY_FUNCTION__ << "\n";
         struct NodeInfo1 { int i; };
         struct EdgeInfo1 { int j; };
    
         typedef boost::adjacency_list<boost::setS, VertexSelector, boost::bidirectionalS, NodeInfo1, EdgeInfo1> AList;
         typedef boost::labeled_graph<AList, std::string> Graph;
    
         std::string names[] = { "A", "B", "C" };
         NodeInfo1 props[] = { {11}, {22}, {33} };
         std::unique_ptr<Graph> grid(new Graph(3, names, props));
    
         /*auto e =*/ add_edge_by_label("C", "B", EdgeInfo1{17}, *grid);
    
         auto copied = clone_labeled_graph(*grid);
         grid.reset();
    
         print(copied);
    
         auto& g = copied.graph();
         // check that properties were copied: vertex B has NodeInfo1 22
         {
             std::cout << "Vertex B NodeInfo1.i after copy: " << g[copied.vertex("B")].i << "\n";
         }
    
         // edge properties too:
         for (auto e : boost::make_iterator_range(boost::edges(copied))) {
             std::cout << "Edge has property EdgeInfo1 " << g[e].j << "\n";
         }
    
         std::cout << "Removed A:\n";
         copied.remove_vertex("A");
         print(copied);
     }
    
     int main() {
         test_clone_labeled_graph<boost::vecS>();
         test_clone_labeled_graph<boost::listS>();
         test_clone_labeled_graph<boost::setS>();
     }
    
  • 文件labeled_graph.hpp

     // ...
    
     namespace boost
     {
     // ...
    
     template < typename Graph, typename Label, typename Selector = defaultS >
     class labeled_graph : protected labeled_graph_types< Graph, Label, Selector >
     {
         // ...
    
     private:
         graph_type _graph;
         map_type _map;
    
         template <typename G, typename L, typename S>
         friend labeled_graph<G, L, S> clone_labeled_graph(labeled_graph<G, L, S> const& src);
     };
    
     // ...
    
  • 文件clone_labeled_graph.hpp

     #include <boost/graph/labeled_graph.hpp>
     #include <boost/graph/copy.hpp>
    
     namespace boost {
    
     namespace graph_detail {
         template <typename Graph, typename Map>
         static inline void clone_labeled_graph(Graph const& src, Map const& srcmap, Graph& dst, Map& dstmap, random_access_container_tag) {
             copy_graph(src, dst);
    
             dstmap.resize(srcmap.size());
             for (size_t v = 0; v < srcmap.size(); ++v) {
                 put_vertex_label(dstmap, dst, srcmap[0], v);
             }
         }
    
         template <typename Graph, typename Map>
         static inline void clone_labeled_graph(Graph const& src, Map const& srcmap, Graph& dst, Map& dstmap, unique_associative_container_tag) {
             using V = graph_traits<Graph>::vertex_descriptor;
             std::map<V, size_t> vidx;
             for (auto v : boost::make_iterator_range(vertices(src)))
                 vidx.emplace(v, vidx.size());
             auto idmap = boost::make_assoc_property_map(vidx);
    
             std::map<V, V> corresponding;
             auto o2cmap = make_assoc_property_map(corresponding);
    
             copy_graph(src, dst, vertex_index_map(idmap).orig_to_copy(o2cmap));
    
             for (auto const& [label, v] : srcmap) {
                 put_vertex_label(dstmap, dst, label, corresponding.at(v));
             }
         }
    
         template <typename Graph, typename Map>
         static inline void clone_labeled_graph(Graph const& src, Map const& srcmap, Graph& dst, Map& dstmap, multiple_associative_container_tag) {
             clone_labeled_graph(src, srcmap, dstmap, unique_associative_container_tag{});
         }
    
         template <typename Graph, typename Map>
         static inline void clone_labeled_graph(Graph const& g, Map const& srcmap, Graph& dst, Map& dstmap) {
             clone_labeled_graph(g, srcmap, dst, dstmap, container_category(srcmap));
         }
     } // namespace graph_detail
    
     template <typename G, typename L, typename S>
     labeled_graph<G, L, S> clone_labeled_graph(labeled_graph<G, L, S> const& src) {
         labeled_graph<G, L, S> dst;
         graph_detail::clone_labeled_graph(src.graph(), src._map, dst.graph(), dst._map);
         return dst;
     }
    
     } // namespace boost
    

打印

void test_clone_labeled_graph() [with VertexSelector = boost::vecS]
====
11 -->
22 -->
33 --> 22
Vertex B NodeInfo1.i after copy: 22
Edge has property EdgeInfo1 17
Removed A:
====
22 -->
33 --> 22
void test_clone_labeled_graph() [with VertexSelector = boost::listS]
====
11 -->
22 -->
33 --> 22
Vertex B NodeInfo1.i after copy: 22
Edge has property EdgeInfo1 17
Removed A:
====
22 -->
33 --> 22
void test_clone_labeled_graph() [with VertexSelector = boost::setS]
====
11 -->
22 -->
33 --> 22
Vertex B NodeInfo1.i after copy: 22
Edge has property EdgeInfo1 17
Removed A:
====
22 -->
33 --> 22

¹ 参见previous 导致barriers 的剧集

【讨论】:

  • 做一些覆盖测试我发现你需要更改为无符号标签以及使用vecS来打random_access_container_tag案例:wandbox.org/permlink/rrwasb11Futodxbw -但现在我也测试了:)
  • 感谢@sehe 的精彩解释!现在,我觉得创建一个新图并复制所有顶点/边并防止在子类上复制图对我来说是一个更好的选择,即使它的成本更高。我可以考虑使用与标记图不同的图,我只需要能够通过沿途修改的图中的某种固定标签直接访问顶点信息。
  • 是的,我倾向于使用捆绑属性或外部属性映射来管理标签。参见例如the gist of this comment
  • 如果你想要一个属性来查找顶点,也许 Bim​​ap 最有用stackoverflow.com/questions/26697477/…
  • 我一定会检查的,谢谢。同时,我只需要能够清除现有的图形结构。我错过了删除顶点时底层地图对象未更新的部分,如果我从您关于不同问题的其他帖子中理解,则无法从底层地图中删除单个项目(这对我来说仍然很奇怪)。那么有没有办法更新底层地图,或者在重新初始化期间完全删除地图?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-11-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多