【发布时间】:2017-04-18 16:01:45
【问题描述】:
我正在使用无向 DFS (Depth First Search) 算法 implemented in boost::graph。
该算法需要顶点和边上的颜色值来跟踪已解析的值。 在the provided example 中,代码将这些颜色值存储为图形的内部属性:
typedef adjacency_list<
vecS,
vecS,
undirectedS,
no_property, // vertex properties
property<edge_color_t, default_color_type> // edge colors
> graph_t;
并且调用是使用“命名属性”版本完成的:
undirected_dfs(g, root_vertex(vertex_t(0)).visitor(vis)
.edge_color_map(get(edge_color, g)));
我的问题是我有顶点和边的自定义值。我使用似乎是the preferred way of doing,它被称为 “捆绑属性”:
struct my_vertex { int a1; float a2; }
struct my_edge { int b1; float b2; }
typedef adjacency_list<
vecS,
vecS,
undirectedS,
my_vertex, // vertex properties
my_edge // edge properties
> graph_t;
当然,前面的 DFS 函数调用不适用于这种图形类型定义。 对于顶点,手册声明提供了默认值,并且它确实构建得很好,只有特定的顶点类型和边类型,如上所示。 但是如果我想要一个特定的边缘类型,我得出的结论是我需要单独提供算法所需的颜色,因为我不能使用示例代码中显示的属性。所以我认为这可以通过将它们提供为“外部属性”来完成。
我的问题是:我该怎么做?
UTIL: edge_color_map(EdgeColorMap edge_color) 算法使用它来跟踪已访问过哪些边。 EdgeColorMap 类型必须是 Read/Write Property Map 及其键的模型 type 必须是图的边缘描述符类型和颜色的值类型 地图必须对 ColorValue 建模。
我不清楚,我试图阅读关于属性映射的部分,但我就是不明白。
在this answer 的帮助下,我在下面尝试了这个,但是失败了: (使用“未命名参数”版本)
std::vector<int> edge_color( boost::num_edges(g), 0);
std::vector<int> vertex_color( boost::num_vertices(g), 0 );
boost::undirected_dfs(
g,
boost::visitor( boost::default_dfs_visitor() ),
boost::vertex_color_map( vertex_color ),
boost::edge_color_map( edge_color ),
boost::root_vertex( vertex_t(0) )
);
如果有人能指出我正确的方向......
【问题讨论】:
标签: c++ boost depth-first-search boost-graph