如何从boost图形库中的边迭代器构造adjacency_list?

How to construct adjacency_list from edge iterators in boost graph library?

我有一个关于 boost 图形库的问题。

boost::adajacency_list 有一个来自一系列边迭代器和顶点数的构造函数。

template <class EdgeIterator>
adjacency_list(EdgeIterator first, EdgeIterator last,
               vertices_size_type n, 
               edges_size_type m = 0, 
               const GraphProperty& p = GraphProperty())

我尝试通过以下代码使用此构造函数,

#include <boost/graph/adjacency_list.hpp>

using namespace boost;
using Graph = adjacency_list<>;

int main()
{
  using edge_iterator = graph_traits<Graph>::edge_iterator;

  Graph g;
  auto const vd0 = add_vertex(g);
  auto const vd1 = add_vertex(g);
  add_edge(vd0, vd1, g);

  edge_iterator ei, ej;
  tie(ei, ej) = edges(g);

  Graph h(ei, ej, 2);
  return 0;
}

代码编译失败,出现以下错误,

/usr/include/boost/graph/detail/adjacency_list.hpp:2121:29: error: ‘boost::detail::adj_list_edge_iterator<boost::range_detail::integer_iterator<long unsigned int>, boost::detail::out_edge_iter<__gnu_cxx::__normal_iterator<boost::detail::stored_edge_property<long unsigned int, boost::no_property>*, std::vector<boost::detail::stored_edge_property<long unsigned int, boost::no_property>, std::allocator<boost::detail::stored_edge_property<long unsigned int, boost::no_property> > > >, long unsigned int, boost::detail::edge_desc_impl<boost::directed_tag, long unsigned int>, long int>, boost::adjacency_list<> >::value_type {aka class boost::detail::edge_desc_impl<boost::directed_tag, long unsigned int>}’ has no member named ‘first’
           add_edge((*first).first, (*first).second,
                    ~~~~~~~~~^~~~~
/usr/include/boost/graph/detail/adjacency_list.hpp:2121:45: error: ‘boost::detail::adj_list_edge_iterator<boost::range_detail::integer_iterator<long unsigned int>, boost::detail::out_edge_iter<__gnu_cxx::__normal_iterator<boost::detail::stored_edge_property<long unsigned int, boost::no_property>*, std::vector<boost::detail::stored_edge_property<long unsigned int, boost::no_property>, std::allocator<boost::detail::stored_edge_property<long unsigned int, boost::no_property> > > >, long unsigned int, boost::detail::edge_desc_impl<boost::directed_tag, long unsigned int>, long int>, boost::adjacency_list<> >::value_type {aka class boost::detail::edge_desc_impl<boost::directed_tag, long unsigned int>}’ has no member named ‘second’
           add_edge((*first).first, (*first).second,

参考https://www.boost.org/doc/libs/1_37_0/libs/graph/doc/adjacency_list.html

如果您只是想要副本,就像您在示例中那样:

Graph h = g;

或者如果 g 是不同的类型:copy_graph¹

你的方法:

std::vector<std::pair<Graph::vertex_descriptor, Graph::vertex_descriptor> > pairlist;
for (auto ed : boost::make_iterator_range(edges(g))) {
    pairlist.emplace_back(source(ed, g), target(ed, g));
}

Graph h(pairlist.begin(), pairlist.end(), 2);

¹ 另见 copy a graph (adjacency_list) to another one

现场演示

显示所有提到的方法:

Live On Coliru

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

using namespace boost;
using Graph = adjacency_list<>;

int main() {
    Graph g;
    add_edge(add_vertex(g), add_vertex(g), g);

    std::vector<std::pair<Graph::vertex_descriptor, Graph::vertex_descriptor> > pairlist;
    for (auto ed : boost::make_iterator_range(edges(g))) {
        pairlist.emplace_back(source(ed, g), target(ed, g));
    }

    Graph h(pairlist.begin(), pairlist.end(), num_vertices(g));
    Graph i = h;
    Graph j;
    boost::copy_graph(h, j);
}