簡體   English   中英

Boost Graph Library:阻止DFS訪問未連接的節點

[英]Boost Graph Library: Prevent DFS from visiting unconnected nodes

我有一個雙向圖。 一些頂點是未連接的。 我使用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;
}

您想使用depth_first_visit ,而不是depth_first_search

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM