Boost图形库:防止DFS访问未连接的节点

3
我有一个双向图,其中一些顶点未连接。我使用 boost::depth_first_search 遍历这些顶点,并提供起始源节点。我发现在处理完已连接的节点后,也会处理未连接的节点。如何避免访问这些节点?事实上,如何告诉 DFS 只访问从源节点可达的节点,而不访问其他任何节点?
以下是我的代码:
/// 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个回答

3

您需要使用 depth_first_visit,而不是 depth_first_search


我看到depth_first_visit需要一些ColorMap属性。如何提供(默认)一个? - user4979733
https://dev59.com/smbWa4cB1Zd3GeqPX46- - MSN

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接