gpt4 book ai didi

c++ - boost 图形库 : Prevent DFS from visiting unconnected nodes

转载 作者:太空狗 更新时间:2023-10-29 21:15:31 24 4
gpt4 key购买 nike

我有一个双向图。一些顶点未连接。我使用 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

关于c++ - boost 图形库 : Prevent DFS from visiting unconnected nodes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37557241/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com