初步问题:Boost Graph Library: Prevent DFS from visiting unconnected nodes
我正在尝试使用 boost :: depth_first_visit ,但不知道如何提供ColorMap属性。我尝试了这里示例中给出的方法:http://www.boost.org/doc/libs/1_58_0/libs/graph/example/loops_dfs.cpp
我的(相关)代码:
/// Define vertex properties.
struct NodeProperty
{
unsigned id; /// Id.
unsigned kind; /// Kind.
unsigned depth; /// Depth.
unsigned layer_color; /// Layer color.
unsigned signal_color; /// Signal color.
unsigned sch_color; /// Sch color.
CBoundingBox bounds; /// Bounds.
NodeProperty()
: id(0), kind(0), depth(0), layer_color(0), signal_color(0), sch_color(0), bounds(0,0,0,0)
{
;
}
};
/// Define net topology graph.
typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::bidirectionalS, NodeProperty> Graph;
/// Define Vertex + iterator.
typedef boost::graph_traits<Graph>::vertex_descriptor Vertex;
typedef boost::graph_traits<Graph>::vertex_iterator VertexItr;
/// Define Edge + iterator.
typedef boost::graph_traits<Graph>::edge_descriptor Edge;
typedef boost::graph_traits<Graph>::edge_iterator EdgeItr;
class receiver_visitor : public boost::default_dfs_visitor
{
public:
receiver_visitor(std::vector<Vertex>& r)
: recv(r)
{
;
}
void discover_vertex(Vertex v, Graph const& g) const
{
std::cout << "Visit: " << v << std::endl;
if (g[v].sch_color) {
recv.push_back(g[v].sch_color);
}
}
std::vector<Vertex>& recv;
};
std::vector<std::size_t>
NetTopology::getReceivers(std::size_t src) const
{
std::vector<Vertex> recv;
receiver_visitor vis(recv);
std::vector<boost::default_color_type> color_map(boost::num_vertices(data_->g));
//boost::depth_first_search(data_->g, boost::visitor(vis).root_vertex(src));
boost::depth_first_visit(data_->g,
src,
boost::visitor(vis),
boost::make_iterator_property_map(color_map.begin(), boost::get(boost::vertex_index, data_->g), color_map[0]));
return recv;
}
我收到编译错误,我不知道如何修复。有什么想法吗?
/p/dt/cad/em64t_SLES11/boost/1.58.0_gcc472_64/include/boost/graph/depth_first_search.hpp: In instantiation of 'void boost::depth_first_visit(const IncidenceGraph&, typen
ame boost::graph_traits<Graph>::vertex_descriptor, DFSVisitor, ColorMap) [with IncidenceGraph = boost::adjacency_list<boost::vecS, boost::vecS, boost::bidirectionalS, tr
on::les::NodeProperty>; DFSVisitor = boost::bgl_named_params<tron::les::receiver_visitor, boost::graph_visitor_t, boost::no_property>; ColorMap = boost::iterator_propert
y_map<__gnu_cxx::__normal_iterator<boost::default_color_type*, std::vector<boost::default_color_type> >, boost::vec_adj_list_vertex_id_map<tron::les::NodeProperty, long
unsigned int>, boost::default_color_type, boost::default_color_type&>; typename boost::graph_traits<Graph>::vertex_descriptor = long unsigned int]':
NetTopology.cpp:353:139: required from here
/p/dt/cad/em64t_SLES11/boost/1.58.0_gcc472_64/include/boost/graph/depth_first_search.hpp:341:5: error: 'struct boost::bgl_named_params<tron::les::receiver_visitor, boost
::graph_visitor_t, boost::no_property>' has no member named 'start_vertex'
vis.start_vertex(u, g);
答案 0 :(得分:3)
您最初使用的depth_first_search
的重载是:
template <class Graph, class class P, class T, class R>
void depth_first_search(Graph& G,
const bgl_named_params<P, T, R>& params);
,您要用于depth_first_visit
的是:
template <class IncidenceGraph, class DFSVisitor, class ColorMap>
void depth_first_visit(IncidenceGraph& g,
typename graph_traits<IncidenceGraph>::vertex_descriptor s,
DFSVisitor& vis, ColorMap color);
第一个使用Named Parameters
,因此如果您想使用默认的起始顶点,则需要使用boost::visitor(vis).root_vertex(src)
(或boost::root_vertex(src).visitor(vis)
或boost::visitor(vis)
。由于depth_first_visit
不使用它们,您需要移除对boost::visitor(...)
的调用并直接传递vis
:
boost::depth_first_visit(data_->g,
src,
vis,
boost::make_iterator_property_map(color_map.begin(), boost::get(boost::vertex_index, data_->g), color_map[0]));