首页 文章

Boost Graph Library:顶点过滤图上的BFS?

提问于
浏览
2

我'm trying to write a version of Breadth-first search which only searches a subgraph of my given graph. To do this, I' m试图使用Boost BGL中的 filtered_graph 类 .

尝试这样做时,我遇到了巨大的,丑陋的模板类型错误 .

我在想:

  • 如何将原始图形中的起始顶点 v 转换为滤波图形中的顶点?

  • 我的访客类型应该是什么样的BFS?它是 MyVisitor<Vertex, SubGraph> 像我一样,还是我需要一个特定于过滤图的不同顶点类型? (这里 SubGraph 是过滤图表类型的typedef) .

下面是我的代码片段,它在BFS被注释掉时编译,但是当它没有注释时会给出错误 .

#include <cstdlib>
#include <iostream>
#include <set>
#include <climits>
#include <algorithm>

#include <boost/property_map/property_map.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/breadth_first_search.hpp>
#include <boost/graph/filtered_graph.hpp>

#include <vector>

//Adapted from http://stackoverflow.com/questions/14470566/how-to-traverse-graph-in-boost-use-bfs
typedef boost::adjacency_list < boost::vecS, boost::vecS, boost::undirectedS > Graph;
typedef typename boost::graph_traits<Graph>::edge_descriptor Edge;
typedef typename boost::graph_traits<Graph>::vertex_descriptor Vertex;

/**
  Functor to filter vertices in a graph which are not in the given set
  */
class VertexSetFilter
{
public:
    std::set<Vertex> S;

    VertexSetFilter(std::set<Vertex> inputSet)
    {
        S = inputSet;
    }

    bool operator()(const Vertex& v) const
    {
        return S.find(v) != S.end(); // keep all vertx_descriptors greater than 3
    }
};


template < typename TVertex, typename TGraph >
class MyVisitor : public boost::default_bfs_visitor
{
private:
    int numFound;

public:

    void discover_vertex(TVertex u, const TGraph & g) const
    {
        std::cout << u << std::endl;
    }
    int getNumFound()
    {
        return numFound;
    }
};

int restrictedBFS(std::set<Vertex> S, Vertex v, Graph G)
{
    VertexSetFilter myFilter(S);
    typedef boost::filtered_graph<Graph, boost::keep_all, VertexSetFilter > SubGraph;
    SubGraph filteredG(G, boost::keep_all(), myFilter);

    MyVisitor<Vertex, SubGraph> vis;
    //won't compile when this line is uncommented
    boost::breadth_first_search(filteredG, v, boost::visitor(vis));
    return -1;
}


int main()
{
    std::cout << "Hello";
}

它给出了以下丑陋的模板错误:

In file included from bfs.cc:10:
In file included from /usr/include/boost/graph/filtered_graph.hpp:17:
/usr/include/boost/iterator/filter_iterator.hpp:54:7: error: constructor for 'boost::filter_iterator<VertexSetFilter,
  boost::range_detail::integer_iterator<unsigned long> >' must explicitly initialize the member 'm_predicate'
  which does not have a default constructor
  filter_iterator() { }
  ^
/usr/include/boost/graph/breadth_first_search.hpp:119:68: note: in instantiation of member function
  'boost::filter_iterator<VertexSetFilter, boost::range_detail::integer_iterator<unsigned long>
  >::filter_iterator' requested here
typename boost::graph_traits<VertexListGraph>::vertex_iterator i, i_end;
                                  ^
/usr/include/boost/graph/breadth_first_search.hpp:135:5: note: in instantiation of function template specialization
  'boost::breadth_first_search<boost::filtered_graph<boost::adjacency_list<boost::vecS, boost::vecS,
  boost::undirectedS, boost::no_property, boost::no_property, boost::no_property, boost::listS>, boost::keep_all,
  VertexSetFilter>, unsigned long *, boost::queue<unsigned long, std::deque<unsigned long,
  std::allocator<unsigned long> > >, MyVisitor<unsigned long,
  boost::filtered_graph<boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS, boost::no_property,
  boost::no_property, boost::no_property, boost::listS>, boost::keep_all, VertexSetFilter> >,
  boost::two_bit_color_map<boost::vec_adj_list_vertex_id_map<boost::no_property, unsigned long> > >' requested
  here
breadth_first_search(g, sources, sources + 1, Q, vis, color);
^
/usr/include/boost/graph/breadth_first_search.hpp:257:7: note: in instantiation of function template specialization
  'boost::breadth_first_search<boost::filtered_graph<boost::adjacency_list<boost::vecS, boost::vecS,
  boost::undirectedS, boost::no_property, boost::no_property, boost::no_property, boost::listS>, boost::keep_all,
  VertexSetFilter>, boost::queue<unsigned long, std::deque<unsigned long, std::allocator<unsigned long> > >,
  MyVisitor<unsigned long, boost::filtered_graph<boost::adjacency_list<boost::vecS, boost::vecS,
  boost::undirectedS, boost::no_property, boost::no_property, boost::no_property, boost::listS>, boost::keep_all,
  VertexSetFilter> >, boost::two_bit_color_map<boost::vec_adj_list_vertex_id_map<boost::no_property, unsigned
  long> > >' requested here
  breadth_first_search
  ^
/usr/include/boost/graph/breadth_first_search.hpp:312:9: note: in instantiation of function template specialization
  'boost::detail::bfs_helper<boost::filtered_graph<boost::adjacency_list<boost::vecS, boost::vecS,
  boost::undirectedS, boost::no_property, boost::no_property, boost::no_property, boost::listS>, boost::keep_all,
  VertexSetFilter>, boost::two_bit_color_map<boost::vec_adj_list_vertex_id_map<boost::no_property, unsigned long>
  >, MyVisitor<unsigned long, boost::filtered_graph<boost::adjacency_list<boost::vecS, boost::vecS,
  boost::undirectedS, boost::no_property, boost::no_property, boost::no_property, boost::listS>, boost::keep_all,
  VertexSetFilter> >, MyVisitor<unsigned long, boost::filtered_graph<boost::adjacency_list<boost::vecS,
  boost::vecS, boost::undirectedS, boost::no_property, boost::no_property, boost::no_property, boost::listS>,
  boost::keep_all, VertexSetFilter> >, boost::graph_visitor_t, boost::no_property>' requested here
    bfs_helper
    ^
/usr/include/boost/graph/breadth_first_search.hpp:344:30: note: in instantiation of function template specialization
  'boost::detail::bfs_dispatch<boost::param_not_found>::apply<boost::filtered_graph<boost::adjacency_list<boost::vecS,
  boost::vecS, boost::undirectedS, boost::no_property, boost::no_property, boost::no_property, boost::listS>,
  boost::keep_all, VertexSetFilter>, MyVisitor<unsigned long,
  boost::filtered_graph<boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS, boost::no_property,
  boost::no_property, boost::no_property, boost::listS>, boost::keep_all, VertexSetFilter> >,
  boost::graph_visitor_t, boost::no_property>' requested here
detail::bfs_dispatch<C>::apply(ng, s, params,
            ^
bfs.cc:65:12: note: in instantiation of function template specialization
  'boost::breadth_first_search<boost::filtered_graph<boost::adjacency_list<boost::vecS, boost::vecS,
  boost::undirectedS, boost::no_property, boost::no_property, boost::no_property, boost::listS>, boost::keep_all,
  VertexSetFilter>, MyVisitor<unsigned long, boost::filtered_graph<boost::adjacency_list<boost::vecS,
  boost::vecS, boost::undirectedS, boost::no_property, boost::no_property, boost::no_property, boost::listS>,
  boost::keep_all, VertexSetFilter> >, boost::graph_visitor_t, boost::no_property>' requested here
boost::breadth_first_search(filteredG, v, boost::visitor(vis));
      ^
/usr/include/boost/iterator/filter_iterator.hpp:106:17: note: member is declared here
  Predicate m_predicate;
        ^
bfs.cc:22:7: note: 'VertexSetFilter' declared here
class VertexSetFilter
  ^
In file included from bfs.cc:10:
/usr/include/boost/graph/filtered_graph.hpp:69:7: error: constructor for
  'boost::detail::out_edge_predicate<boost::keep_all, VertexSetFilter,
  boost::filtered_graph<boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS, boost::no_property,
  boost::no_property, boost::no_property, boost::listS>, boost::keep_all, VertexSetFilter> >' must explicitly
  initialize the member 'm_vertex_pred' which does not have a default constructor
  out_edge_predicate() { }
  ^
/usr/include/boost/iterator/filter_iterator.hpp:54:7: note: in instantiation of member function
  'boost::detail::out_edge_predicate<boost::keep_all, VertexSetFilter,
  boost::filtered_graph<boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS, boost::no_property,
  boost::no_property, boost::no_property, boost::listS>, boost::keep_all, VertexSetFilter> >::out_edge_predicate'
  requested here
  filter_iterator() { }
  ^
/usr/include/boost/graph/breadth_first_search.hpp:71:41: note: in instantiation of member function
  'boost::filter_iterator<boost::detail::out_edge_predicate<boost::keep_all, VertexSetFilter,
  boost::filtered_graph<boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS, boost::no_property,
  boost::no_property, boost::no_property, boost::listS>, boost::keep_all, VertexSetFilter> >,
  boost::detail::out_edge_iter<__gnu_cxx::__normal_iterator<boost::detail::stored_edge_iter<unsigned long,
  std::_List_iterator<boost::list_edge<unsigned long, boost::no_property> >, boost::no_property> *,
  std::vector<boost::detail::stored_edge_iter<unsigned long, std::_List_iterator<boost::list_edge<unsigned long,
  boost::no_property> >, boost::no_property>, std::allocator<boost::detail::stored_edge_iter<unsigned long,
  std::_List_iterator<boost::list_edge<unsigned long, boost::no_property> >, boost::no_property> > > >, unsigned
  long, boost::detail::edge_desc_impl<boost::undirected_tag, unsigned long>, long> >::filter_iterator' requested
  here
typename GTraits::out_edge_iterator ei, ei_end;
                    ^
/usr/include/boost/graph/breadth_first_search.hpp:124:5: note: in instantiation of function template specialization
  'boost::breadth_first_visit<boost::filtered_graph<boost::adjacency_list<boost::vecS, boost::vecS,
  boost::undirectedS, boost::no_property, boost::no_property, boost::no_property, boost::listS>, boost::keep_all,
  VertexSetFilter>, boost::queue<unsigned long, std::deque<unsigned long, std::allocator<unsigned long> > >,
  MyVisitor<unsigned long, boost::filtered_graph<boost::adjacency_list<boost::vecS, boost::vecS,
  boost::undirectedS, boost::no_property, boost::no_property, boost::no_property, boost::listS>, boost::keep_all,
  VertexSetFilter> >, boost::two_bit_color_map<boost::vec_adj_list_vertex_id_map<boost::no_property, unsigned
  long> >, unsigned long *>' requested here
breadth_first_visit(g, sources_begin, sources_end, Q, vis, color);
^
/usr/include/boost/graph/breadth_first_search.hpp:135:5: note: in instantiation of function template specialization
  'boost::breadth_first_search<boost::filtered_graph<boost::adjacency_list<boost::vecS, boost::vecS,
  boost::undirectedS, boost::no_property, boost::no_property, boost::no_property, boost::listS>, boost::keep_all,
  VertexSetFilter>, unsigned long *, boost::queue<unsigned long, std::deque<unsigned long,
  std::allocator<unsigned long> > >, MyVisitor<unsigned long,
  boost::filtered_graph<boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS, boost::no_property,
  boost::no_property, boost::no_property, boost::listS>, boost::keep_all, VertexSetFilter> >,
  boost::two_bit_color_map<boost::vec_adj_list_vertex_id_map<boost::no_property, unsigned long> > >' requested
  here
breadth_first_search(g, sources, sources + 1, Q, vis, color);
^
/usr/include/boost/graph/breadth_first_search.hpp:257:7: note: in instantiation of function template specialization
  'boost::breadth_first_search<boost::filtered_graph<boost::adjacency_list<boost::vecS, boost::vecS,
  boost::undirectedS, boost::no_property, boost::no_property, boost::no_property, boost::listS>, boost::keep_all,
  VertexSetFilter>, boost::queue<unsigned long, std::deque<unsigned long, std::allocator<unsigned long> > >,
  MyVisitor<unsigned long, boost::filtered_graph<boost::adjacency_list<boost::vecS, boost::vecS,
  boost::undirectedS, boost::no_property, boost::no_property, boost::no_property, boost::listS>, boost::keep_all,
  VertexSetFilter> >, boost::two_bit_color_map<boost::vec_adj_list_vertex_id_map<boost::no_property, unsigned
  long> > >' requested here
  breadth_first_search
  ^
/usr/include/boost/graph/breadth_first_search.hpp:312:9: note: in instantiation of function template specialization
  'boost::detail::bfs_helper<boost::filtered_graph<boost::adjacency_list<boost::vecS, boost::vecS,
  boost::undirectedS, boost::no_property, boost::no_property, boost::no_property, boost::listS>, boost::keep_all,
  VertexSetFilter>, boost::two_bit_color_map<boost::vec_adj_list_vertex_id_map<boost::no_property, unsigned long>
  >, MyVisitor<unsigned long, boost::filtered_graph<boost::adjacency_list<boost::vecS, boost::vecS,
  boost::undirectedS, boost::no_property, boost::no_property, boost::no_property, boost::listS>, boost::keep_all,
  VertexSetFilter> >, MyVisitor<unsigned long, boost::filtered_graph<boost::adjacency_list<boost::vecS,
  boost::vecS, boost::undirectedS, boost::no_property, boost::no_property, boost::no_property, boost::listS>,
  boost::keep_all, VertexSetFilter> >, boost::graph_visitor_t, boost::no_property>' requested here
    bfs_helper
    ^
/usr/include/boost/graph/breadth_first_search.hpp:344:30: note: in instantiation of function template specialization
  'boost::detail::bfs_dispatch<boost::param_not_found>::apply<boost::filtered_graph<boost::adjacency_list<boost::vecS,
  boost::vecS, boost::undirectedS, boost::no_property, boost::no_property, boost::no_property, boost::listS>,
  boost::keep_all, VertexSetFilter>, MyVisitor<unsigned long,
  boost::filtered_graph<boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS, boost::no_property,
  boost::no_property, boost::no_property, boost::listS>, boost::keep_all, VertexSetFilter> >,
  boost::graph_visitor_t, boost::no_property>' requested here
detail::bfs_dispatch<C>::apply(ng, s, params,
            ^
bfs.cc:65:12: note: in instantiation of function template specialization
  'boost::breadth_first_search<boost::filtered_graph<boost::adjacency_list<boost::vecS, boost::vecS,
  boost::undirectedS, boost::no_property, boost::no_property, boost::no_property, boost::listS>, boost::keep_all,
  VertexSetFilter>, MyVisitor<unsigned long, boost::filtered_graph<boost::adjacency_list<boost::vecS,
  boost::vecS, boost::undirectedS, boost::no_property, boost::no_property, boost::no_property, boost::listS>,
  boost::keep_all, VertexSetFilter> >, boost::graph_visitor_t, boost::no_property>' requested here
boost::breadth_first_search(filteredG, v, boost::visitor(vis));
      ^
/usr/include/boost/graph/filtered_graph.hpp:79:23: note: member is declared here
  VertexPredicate m_vertex_pred;
          ^
bfs.cc:22:7: note: 'VertexSetFilter' declared here
class VertexSetFilter
  ^
2 errors generated.

1 回答

  • 5

    错误在一开始就告诉你的是 VertexSetFilter 必须有一个默认的构造函数 . 此外,documentation on boost::filtered_graph读取:

    此外,谓词必须是Default Constructible [1] . [1]的原因需要缺省构造在EdgePredicate和VertexPredicate类型是这些谓词存储由值(出于性能原因)在过滤器迭代器适配器,和迭代器需要是由C标准缺省构造 .

    因此,您只需为 VertexSetFilter 添加默认构造函数,例如

    VertexSetFilter() = default;
    

    在上述链接可访问的示例中也可以完成此操作 .

相关问题