简体   繁体   中英

Boost Graph Library: BFS on a Vertex-filtered graph?

I'm trying to write a version of Breadth-first search which only searches a subgraph of my given graph. To do this, I'm trying to use the filtered_graph class from Boost BGL.

I'm getting huge, ugly template type-errors when trying to do this.

I'm wondering:

  1. How do I convert v , my start vertex in the original graph, into a vertex in the filtered graph?
  2. What should the type of my visitor be for the BFS? Is it MyVisitor<Vertex, SubGraph> like I have, or do I need a different vertex type specific to the filtered graph? (Here SubGraph is a typedef for the filtered graph type).

Below is a snippet of my code that compiles when the BFS is commented out, but gives an error when it isn't.

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

It gives the following ugly template error:

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.

What the error is telling you at the very beginning, is that VertexSetFilter must have a default constructor. Also, the documentation on boost::filtered_graph reads:

Also, the predicate must be Default Constructible [1].
[1] The reason for requiring Default Constructible in the EdgePredicate and VertexPredicate types is that these predicates are stored by-value (for performance reasons) in the filter iterator adaptor, and iterators are required to be Default Constructible by the C++ Standard.

So, you just have to add a default constructor for VertexSetFilter , eg

VertexSetFilter() = default;

Which is also done in the example accessible by the above link.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM