我正在构建 boost 图。对于图中的每个连接组件,我想为该连接组件中的每个顶点传播一个唯一的 ID 值。我想知道是否有办法使用 Boost
的 BFSVisitor
概念来做到这一点?
我猜这可以使用 examine_edge
函数 ( http://www.boost.org/doc/libs/1_55_0/libs/graph/doc/BFSVisitor.html ) 来完成,但我很难弄清楚如何实现这样的类。任何见解/示例链接都会有很大帮助!
最佳答案
听起来您只想实现自定义 BFS 访问者。这已得到解答here .
特别是在您的 discover_vertex 方法中。你可以只使用:
void discover_vertex(const graph_t::vertex_descriptor &s, const graph_t &g) const {
std::cout << "Discover: " << g[s] << std::endl;
g[s].component_id = myNewId;
}
或者您可以使用属性映射。
put(component_id,s,myNewId);
前提是您将其作为顶点属性。您还可以向 BFS 访问者构造函数添加一个变量,这是您希望顶点拥有的新 ID。
Here是将变量传递给构造函数的自定义 DFS 示例。与BFS原理相同。
关于c++ - 使用 BFSVisitor 在连接的组件中传播值? ( boost ,C++)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25091588/