问题
I have a bidirectional graph. Some of the vertices are unconnected. I use boost::depth_first_search to traverse the vertices. I also supply the starting source node. I see that the unconnected vertices are also processed after the connected nodes are done. How can I prevent visiting such nodes? In fact, how can I tell DFS to only visit those nodes reachable from the source node and don't visit anything else?
I have the following code:
/// 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 of polygon.
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;
class receiver_visitor : public boost::default_dfs_visitor
{
public:
receiver_visitor(std::vector<Vertex>& r)
: res(r)
{
;
}
void discover_vertex(Vertex v, Graph const& g) const
{
std::cout << "Visit: " << v << std::endl;
if (g[v].sch_color) {
res.push_back(g[v].sch_color);
}
}
std::vector<Vertex>& res;
};
std::vector<std::size_t>
NetTopology::getReceivers(std::size_t src) const
{
std::vector<Vertex> r;
receiver_visitor vis(r);
boost::depth_first_search(data_->g, boost::visitor(vis).root_vertex(src));
return r;
}
回答1:
You want to use depth_first_visit
, not depth_first_search
.
来源:https://stackoverflow.com/questions/37557241/boost-graph-library-prevent-dfs-from-visiting-unconnected-nodes